Taboo.scala 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package models
  2. import util._
  3. import akka.actor._
  4. import scala.concurrent.duration._
  5. import play.api.libs.concurrent._
  6. import play.api.libs.json._
  7. import play.api.Play.current
  8. import play.api.libs.concurrent.Execution.Implicits._
  9. object Team {
  10. implicit val writes = new Writes[Team] {
  11. def writes(o: Team) = Json.obj(
  12. "members" -> o.members,
  13. "player" -> o.player,
  14. "guessers" -> o.guessers
  15. )
  16. }
  17. }
  18. class Team {
  19. var members = List.empty[String]
  20. var player = ""
  21. var guessers = Set.empty[String]
  22. def hasPlayer(user: String) = members.indexOf(user) >= 0
  23. def nextPlayer() = {
  24. val index = (members.indexOf(player) + 1) % members.size
  25. player = members(index)
  26. guessers = members.filterNot(_ == player).toSet
  27. player
  28. }
  29. def join(user: String) = {
  30. if(!hasPlayer(user)) {
  31. members = members :+ user
  32. }
  33. }
  34. def leave(user: String) = {
  35. members = members.filterNot(_ == user)
  36. }
  37. def isEmpty = members.isEmpty
  38. def size = members.size
  39. }
  40. object Round {
  41. val DURATION = 60
  42. implicit val writes = new Writes[Round] {
  43. def writes(o: Round) = Json.obj(
  44. "team" -> o.team,
  45. "monitors" -> o.monitors,
  46. "remainingTime" -> o.remainingTime
  47. )
  48. }
  49. }
  50. case class Round(
  51. team: Team,
  52. monitors: Set[String],
  53. startTime: Long = System.currentTimeMillis
  54. ) {
  55. def remainingTime = {
  56. val sinceStart = (System.currentTimeMillis - startTime) / 1000
  57. Math.max(0, Round.DURATION - sinceStart)
  58. }
  59. }
  60. case class Information(text: String)
  61. case class Guess(username: String, text: String)
  62. case object Pass
  63. case class Correct(username: String)
  64. case class Taboo(username: String)
  65. case object End
  66. case class Abuse(kind: String, username: String)
  67. case class Score(kind: String, points: Int, card: Card, username: String = "")
  68. case object NextCard
  69. case object PrepRound
  70. case object StartRound
  71. case class EndRound(points: Int, card: Option[Card])
  72. class TabooGame(val chatActor: ActorRef) extends Actor {
  73. var ready = false
  74. var round: Option[Round] = None
  75. var roundActor: ActorRef = null
  76. val teamA: Team = new Team
  77. val teamB: Team = new Team
  78. var currentTeam: Team = teamB
  79. var opposingTeam: Team = teamA
  80. def receive = {
  81. case Join(username) =>
  82. if(teamA.size < 2 || teamA.size <= teamB.size) {
  83. teamA.join(username)
  84. }
  85. else {
  86. teamB.join(username)
  87. }
  88. announceStatus("join", username)
  89. if(round.isEmpty) {
  90. self ! PrepRound
  91. }
  92. case Quit(username) =>
  93. teamA.leave(username)
  94. teamB.leave(username)
  95. val lackingMembers = teamA.size < 2 || currentTeam == teamB && teamB.size < 2
  96. if(ready && lackingMembers) {
  97. ready = false
  98. }
  99. announceStatus("quit", username)
  100. if(!round.isEmpty && lackingMembers) {
  101. roundActor ! End
  102. }
  103. case Talk(username, "/status") => announceStatus()
  104. case Talk(username, text) => round match {
  105. case Some(round) =>
  106. if(username == round.team.player) text match {
  107. case "/pass" | "/p" => roundActor ! Pass
  108. case "/correct" | "/c" => roundActor ! Correct(username)
  109. case text => roundActor ! Information(text)
  110. }
  111. else if(round.team.guessers(username)) {
  112. roundActor ! Guess(username, text)
  113. }
  114. else if(round.monitors(username)) text match {
  115. case "/taboo" | "/t" => roundActor ! Taboo(username)
  116. case "/correct" | "/c" => roundActor ! Correct(username)
  117. }
  118. case None =>
  119. if(username == currentTeam.player) text match {
  120. case "/start" | "/s" => self ! StartRound
  121. }
  122. }
  123. case Score(kind, points, card, user) =>
  124. chatActor ! Announce(Json.obj(
  125. "kind" -> "point",
  126. "action" -> kind,
  127. "points" -> points,
  128. "card" -> card,
  129. "user" -> user
  130. ))
  131. self ! NextCard
  132. case Abuse(kind, user) =>
  133. chatActor ! Announce(Json.obj(
  134. "kind" -> "abuse",
  135. "action" -> kind,
  136. "user" -> user
  137. ))
  138. case NextCard =>
  139. val card = Card.getRandom()
  140. roundActor ! card
  141. val message = Json.obj(
  142. "kind" -> "card",
  143. "card" -> card
  144. )
  145. (round.get.monitors + player).foreach { user =>
  146. chatActor ! Tell(user, message)
  147. }
  148. case PrepRound =>
  149. if(!ready && teamA.size >= 2) {
  150. ready = true
  151. if(teamB.size < 2) {
  152. currentTeam = teamA
  153. opposingTeam = teamB
  154. }
  155. else {
  156. val temp = currentTeam
  157. currentTeam = opposingTeam
  158. opposingTeam = temp
  159. }
  160. currentTeam.nextPlayer()
  161. chatActor ! Announce(Json.obj(
  162. "kind" -> "roundReady",
  163. "player" -> currentTeam.player
  164. ))
  165. }
  166. case StartRound =>
  167. round = Some(Round(currentTeam, opposingTeam.members.toSet))
  168. roundActor = context.actorOf(Props[TabooRound])
  169. Akka.system.scheduler.scheduleOnce(1 minute, roundActor, End)
  170. chatActor ! Announce(Json.obj(
  171. "kind" -> "roundStart",
  172. "round" -> round
  173. ))
  174. self ! NextCard
  175. case EndRound(points, card) =>
  176. chatActor ! Announce(Json.obj(
  177. "kind" -> "roundEnd",
  178. "points" -> points,
  179. "card" -> card
  180. ))
  181. round = None
  182. ready = false
  183. self ! PrepRound
  184. }
  185. def player = round.get.team.player
  186. def announceStatus(kind: String = "status", user: String = "*GM") {
  187. chatActor ! Announce(Json.obj(
  188. "kind" -> kind,
  189. "user" -> user,
  190. "pendingPlayer" -> ifOpt(ready)(currentTeam.player),
  191. "round" -> round,
  192. "teamA" -> teamA,
  193. "teamB" -> teamB
  194. ))
  195. }
  196. self ! PrepRound
  197. }
  198. class TabooRound extends Actor {
  199. var card: Option[Card] = None
  200. var points = 0
  201. var guesses = 0
  202. var infos = 0
  203. def receive = {
  204. case newCard: Card =>
  205. card = Some(newCard)
  206. guesses = 0
  207. infos = 0
  208. case End =>
  209. sender ! EndRound(points, card)
  210. context.stop(self)
  211. case other => card.map { card =>
  212. other match {
  213. case Guess(username, text) =>
  214. guesses += 1
  215. if(card.isCorrect(text)) {
  216. point(1, "correct", username)
  217. }
  218. case Information(text) =>
  219. infos += 1
  220. if(card.isTaboo(text)) {
  221. point(-1, "invalid")
  222. }
  223. case Pass => point(-1, "pass")
  224. case Correct(username) =>
  225. if(guesses == 0) {
  226. abuse("correctp", username)
  227. }
  228. else {
  229. point(1, "correctp", username)
  230. }
  231. case Taboo(username) =>
  232. if(infos == 0) {
  233. abuse("taboo", username)
  234. }
  235. else {
  236. point(-1, "taboo", username)
  237. }
  238. }
  239. }
  240. }
  241. def abuse(kind: String, username: String) = {
  242. sender ! Abuse(kind, username)
  243. }
  244. def point(pointDiff: Int, kind: String, username: String = "") = card.map { card =>
  245. points += pointDiff
  246. sender ! Score(kind, points, card, username)
  247. this.card = None
  248. }
  249. }