Taboo.scala 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 = 120
  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. case _ => Unit
  118. }
  119. case None =>
  120. if(username == currentTeam.player) text match {
  121. case "/start" | "/s" => self ! StartRound
  122. case _ => Unit
  123. }
  124. }
  125. case Score(kind, points, card, user) =>
  126. chatActor ! Announce(Json.obj(
  127. "kind" -> "point",
  128. "action" -> kind,
  129. "points" -> points,
  130. "card" -> card,
  131. "user" -> user
  132. ))
  133. self ! NextCard
  134. case Abuse(kind, user) =>
  135. chatActor ! Announce(Json.obj(
  136. "kind" -> "abuse",
  137. "action" -> kind,
  138. "user" -> user
  139. ))
  140. case NextCard =>
  141. val card = Card.getRandom()
  142. roundActor ! card
  143. val message = Json.obj(
  144. "kind" -> "card",
  145. "card" -> card
  146. )
  147. (round.get.monitors + player).foreach { user =>
  148. chatActor ! Tell(user, message)
  149. }
  150. case PrepRound =>
  151. if(!ready && teamA.size >= 2) {
  152. ready = true
  153. if(teamB.size < 2) {
  154. currentTeam = teamA
  155. opposingTeam = teamB
  156. }
  157. else {
  158. val temp = currentTeam
  159. currentTeam = opposingTeam
  160. opposingTeam = temp
  161. }
  162. currentTeam.nextPlayer()
  163. chatActor ! Announce(Json.obj(
  164. "kind" -> "roundReady",
  165. "player" -> currentTeam.player
  166. ))
  167. }
  168. case StartRound =>
  169. round = Some(Round(currentTeam, opposingTeam.members.toSet))
  170. roundActor = context.actorOf(Props[TabooRound])
  171. Akka.system.scheduler.scheduleOnce(1 minute, roundActor, End)
  172. chatActor ! Announce(Json.obj(
  173. "kind" -> "roundStart",
  174. "round" -> round
  175. ))
  176. self ! NextCard
  177. case EndRound(points, card) =>
  178. chatActor ! Announce(Json.obj(
  179. "kind" -> "roundEnd",
  180. "points" -> points,
  181. "card" -> card
  182. ))
  183. round = None
  184. ready = false
  185. self ! PrepRound
  186. }
  187. def player = round.get.team.player
  188. def announceStatus(kind: String = "status", user: String = "*GM") {
  189. chatActor ! Announce(Json.obj(
  190. "kind" -> kind,
  191. "user" -> user,
  192. "pendingPlayer" -> ifOpt(ready)(currentTeam.player),
  193. "round" -> round,
  194. "teamA" -> teamA,
  195. "teamB" -> teamB
  196. ))
  197. }
  198. self ! PrepRound
  199. }
  200. class TabooRound extends Actor {
  201. var card: Option[Card] = None
  202. var points = 0
  203. var guesses = 0
  204. var infos = 0
  205. def receive = {
  206. case newCard: Card =>
  207. card = Some(newCard)
  208. guesses = 0
  209. infos = 0
  210. case End =>
  211. sender ! EndRound(points, card)
  212. context.stop(self)
  213. case other => card.map { card =>
  214. other match {
  215. case Guess(username, text) =>
  216. guesses += 1
  217. if(card.isCorrect(text)) {
  218. point(1, "correct", username)
  219. }
  220. case Information(text) =>
  221. infos += 1
  222. if(card.isTaboo(text)) {
  223. point(-1, "invalid")
  224. }
  225. case Pass => point(-1, "pass")
  226. case Correct(username) =>
  227. if(guesses == 0) {
  228. abuse("correctp", username)
  229. }
  230. else {
  231. point(1, "correctp", username)
  232. }
  233. case Taboo(username) =>
  234. if(infos == 0) {
  235. abuse("taboo", username)
  236. }
  237. else {
  238. point(-1, "taboo", username)
  239. }
  240. }
  241. }
  242. }
  243. def abuse(kind: String, username: String) = {
  244. sender ! Abuse(kind, username)
  245. }
  246. def point(pointDiff: Int, kind: String, username: String = "") = card.map { card =>
  247. points += pointDiff
  248. sender ! Score(kind, points, card, username)
  249. this.card = None
  250. }
  251. }