Taboo.scala 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. var cardPool: CardPool = CardPool.get()
  81. def receive = {
  82. case Join(username) =>
  83. if(teamA.size < 2 || teamA.size <= teamB.size) {
  84. teamA.join(username)
  85. }
  86. else {
  87. teamB.join(username)
  88. }
  89. announceStatus("join", username)
  90. if(round.isEmpty) {
  91. self ! PrepRound
  92. }
  93. case Quit(username) =>
  94. teamA.leave(username)
  95. teamB.leave(username)
  96. val lackingMembers = teamA.size < 2 || currentTeam == teamB && teamB.size < 2
  97. if(ready && lackingMembers) {
  98. ready = false
  99. }
  100. announceStatus("quit", username)
  101. if(!round.isEmpty && lackingMembers) {
  102. roundActor ! End
  103. }
  104. case Talk(username, "/status") => announceStatus()
  105. case Talk(username, text) => round match {
  106. case Some(round) =>
  107. if(username == round.team.player) text match {
  108. case "/pass" | "/p" => roundActor ! Pass
  109. case "/correct" | "/c" => roundActor ! Correct(username)
  110. case text => roundActor ! Information(text)
  111. }
  112. else if(round.team.guessers(username)) {
  113. roundActor ! Guess(username, text)
  114. }
  115. else if(round.monitors(username)) text match {
  116. case "/taboo" | "/t" => roundActor ! Taboo(username)
  117. case "/correct" | "/c" => roundActor ! Correct(username)
  118. case _ => Unit
  119. }
  120. case None =>
  121. if(username == currentTeam.player) text match {
  122. case "/start" | "/s" => self ! StartRound
  123. case _ => Unit
  124. }
  125. }
  126. case Score(kind, points, card, user) =>
  127. chatActor ! Announce(Json.obj(
  128. "kind" -> "point",
  129. "action" -> kind,
  130. "points" -> points,
  131. "card" -> card,
  132. "user" -> user
  133. ))
  134. self ! NextCard
  135. case Abuse(kind, user) =>
  136. chatActor ! Announce(Json.obj(
  137. "kind" -> "abuse",
  138. "action" -> kind,
  139. "user" -> user
  140. ))
  141. case NextCard =>
  142. if(!cardPool.hasNext) {
  143. cardPool = CardPool.get()
  144. chatActor ! Announce(Json.obj(
  145. "kind" -> "talk",
  146. "user" -> "*GM",
  147. "message" -> "And we're out of cards... Reshuffling the current set. You can help by contributing more cards though! *hint* *hint*"
  148. ))
  149. }
  150. val card = cardPool.next()
  151. roundActor ! card
  152. val message = Json.obj(
  153. "kind" -> "card",
  154. "card" -> card
  155. )
  156. (round.get.monitors + player).foreach { user =>
  157. chatActor ! Tell(user, message)
  158. }
  159. case PrepRound =>
  160. if(!ready && teamA.size >= 2) {
  161. ready = true
  162. if(teamB.size < 2) {
  163. currentTeam = teamA
  164. opposingTeam = teamB
  165. }
  166. else {
  167. val temp = currentTeam
  168. currentTeam = opposingTeam
  169. opposingTeam = temp
  170. }
  171. currentTeam.nextPlayer()
  172. chatActor ! Announce(Json.obj(
  173. "kind" -> "roundReady",
  174. "player" -> currentTeam.player
  175. ))
  176. }
  177. case StartRound =>
  178. round = Some(Round(currentTeam, opposingTeam.members.toSet))
  179. roundActor = context.actorOf(Props[TabooRound])
  180. Akka.system.scheduler.scheduleOnce(Round.DURATION seconds, roundActor, End)
  181. chatActor ! Announce(Json.obj(
  182. "kind" -> "roundStart",
  183. "round" -> round
  184. ))
  185. self ! NextCard
  186. case EndRound(points, card) =>
  187. chatActor ! Announce(Json.obj(
  188. "kind" -> "roundEnd",
  189. "points" -> points,
  190. "card" -> card
  191. ))
  192. round = None
  193. ready = false
  194. self ! PrepRound
  195. }
  196. def player = round.get.team.player
  197. def announceStatus(kind: String = "status", user: String = "*GM") {
  198. chatActor ! Announce(Json.obj(
  199. "kind" -> kind,
  200. "user" -> user,
  201. "pendingPlayer" -> ifOpt(ready)(currentTeam.player),
  202. "round" -> round,
  203. "teamA" -> teamA,
  204. "teamB" -> teamB
  205. ))
  206. }
  207. self ! PrepRound
  208. }
  209. class TabooRound extends Actor {
  210. var card: Option[Card] = None
  211. var points = 0
  212. var guesses = 0
  213. var infos = 0
  214. def receive = {
  215. case newCard: Card =>
  216. card = Some(newCard)
  217. guesses = 0
  218. infos = 0
  219. case End =>
  220. sender ! EndRound(points, card)
  221. context.stop(self)
  222. case other => card.map { card =>
  223. other match {
  224. case Guess(username, text) =>
  225. guesses += 1
  226. if(card.isCorrect(text)) {
  227. point(1, "correct", username)
  228. }
  229. case Information(text) =>
  230. infos += 1
  231. if(card.isTaboo(text)) {
  232. point(-1, "invalid")
  233. }
  234. case Pass => point(-1, "pass")
  235. case Correct(username) =>
  236. if(guesses == 0) {
  237. abuse("correctp", username)
  238. }
  239. else {
  240. point(1, "correctp", username)
  241. }
  242. case Taboo(username) =>
  243. if(infos == 0) {
  244. abuse("taboo", username)
  245. }
  246. else {
  247. point(-1, "taboo", username)
  248. }
  249. }
  250. }
  251. }
  252. def abuse(kind: String, username: String) = {
  253. sender ! Abuse(kind, username)
  254. }
  255. def point(pointDiff: Int, kind: String, username: String = "") = card.map { card =>
  256. points += pointDiff
  257. sender ! Score(kind, points, card, username)
  258. this.card = None
  259. }
  260. }