1
0

Taboo.scala 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 Score(kind: String, points: Int, card: Card, username: String = "")
  67. case object NextCard
  68. case object PrepRound
  69. case object StartRound
  70. case class EndRound(points: Int, card: Option[Card])
  71. class TabooGame(val chatActor: ActorRef) extends Actor {
  72. var ready = false
  73. var round: Option[Round] = None
  74. var roundActor: ActorRef = null
  75. val teamA: Team = new Team
  76. val teamB: Team = new Team
  77. var currentTeam: Team = teamB
  78. var opposingTeam: Team = teamA
  79. def receive = {
  80. case Join(username) =>
  81. if(teamA.size < 2 || teamA.size <= teamB.size) {
  82. teamA.join(username)
  83. }
  84. else {
  85. teamB.join(username)
  86. }
  87. announceStatus("join", username)
  88. if(round.isEmpty) {
  89. self ! PrepRound
  90. }
  91. case Quit(username) =>
  92. teamA.leave(username)
  93. teamB.leave(username)
  94. val lackingMembers = teamA.size < 2 || currentTeam == teamB && teamB.size < 2
  95. if(ready && lackingMembers) {
  96. ready = false
  97. }
  98. announceStatus("quit", username)
  99. if(!round.isEmpty && lackingMembers) {
  100. roundActor ! End
  101. }
  102. case Talk(username, "/status") => announceStatus()
  103. case Talk(username, text) => round match {
  104. case Some(round) =>
  105. if(username == round.team.player) text match {
  106. case "/pass" | "/p" => roundActor ! Pass
  107. case "/correct" | "/c" => roundActor ! Correct(username)
  108. case text => roundActor ! Information(text)
  109. }
  110. else if(round.team.guessers(username)) {
  111. roundActor ! Guess(username, text)
  112. }
  113. else if(round.monitors(username)) text match {
  114. case "/taboo" | "/t" => roundActor ! Taboo(username)
  115. case "/correct" | "/c" => roundActor ! Correct(username)
  116. }
  117. case None =>
  118. if(username == currentTeam.player) text match {
  119. case "/start" | "/s" => self ! StartRound
  120. }
  121. }
  122. case Score(kind, points, card, user) =>
  123. chatActor ! Announce(Json.obj(
  124. "kind" -> "point",
  125. "action" -> kind,
  126. "points" -> points,
  127. "card" -> card,
  128. "user" -> user
  129. ))
  130. self ! NextCard
  131. case NextCard =>
  132. val card = Card.getRandom()
  133. roundActor ! card
  134. val message = Json.obj(
  135. "kind" -> "card",
  136. "card" -> card
  137. )
  138. (round.get.monitors + player).foreach { user =>
  139. chatActor ! Tell(user, message)
  140. }
  141. case PrepRound =>
  142. if(!ready && teamA.size >= 2) {
  143. ready = true
  144. if(teamB.size < 2) {
  145. currentTeam = teamA
  146. opposingTeam = teamB
  147. }
  148. else {
  149. val temp = currentTeam
  150. currentTeam = opposingTeam
  151. opposingTeam = temp
  152. }
  153. currentTeam.nextPlayer()
  154. chatActor ! Announce(Json.obj(
  155. "kind" -> "roundReady",
  156. "player" -> currentTeam.player
  157. ))
  158. }
  159. case StartRound =>
  160. round = Some(Round(currentTeam, opposingTeam.members.toSet))
  161. roundActor = context.actorOf(Props[TabooRound])
  162. Akka.system.scheduler.scheduleOnce(1 minute, roundActor, End)
  163. chatActor ! Announce(Json.obj(
  164. "kind" -> "roundStart",
  165. "round" -> round
  166. ))
  167. self ! NextCard
  168. case EndRound(points, card) =>
  169. chatActor ! Announce(Json.obj(
  170. "kind" -> "roundEnd",
  171. "points" -> points,
  172. "card" -> card
  173. ))
  174. round = None
  175. ready = false
  176. self ! PrepRound
  177. }
  178. def player = round.get.team.player
  179. def announceStatus(kind: String = "status", user: String = "*GM") {
  180. chatActor ! Announce(Json.obj(
  181. "kind" -> kind,
  182. "user" -> user,
  183. "pendingPlayer" -> ifOpt(ready)(currentTeam.player),
  184. "round" -> round,
  185. "teamA" -> teamA,
  186. "teamB" -> teamB
  187. ))
  188. }
  189. self ! PrepRound
  190. }
  191. class TabooRound extends Actor {
  192. var card: Option[Card] = None
  193. var points = 0
  194. def receive = {
  195. case newCard: Card =>
  196. card = Some(newCard)
  197. case Guess(username, text) => card.map { card =>
  198. if(card.isCorrect(text)) {
  199. points += 1
  200. sender ! Score("correct", points, card, username)
  201. this.card = None
  202. }
  203. }
  204. case Information(text) => card.map { card =>
  205. if(card.isTaboo(text)) {
  206. points -= 1
  207. sender ! Score("invalid", points, card)
  208. this.card = None
  209. }
  210. }
  211. case Pass => card.map { card =>
  212. points -= 1
  213. sender ! Score("pass", points, card)
  214. this.card = None
  215. }
  216. case Correct(username) => card.map { card =>
  217. points += 1
  218. sender ! Score("correctp", points, card, username)
  219. this.card = None
  220. }
  221. case Taboo(username) => card.map { card =>
  222. points -= 1
  223. sender ! Score("taboo", points, card)
  224. this.card = None
  225. }
  226. case End =>
  227. sender ! EndRound(points, card)
  228. context.stop(self)
  229. }
  230. }