Browse Source

Send round details via status

This allows us to have a player who joins in the middle of the game to
see the timer progressing. We also decouple the time remaining from the
client code and rely on the server to provide it instead.
Thomas Dy 11 years ago
parent
commit
1f431ca4b4
3 changed files with 32 additions and 16 deletions
  1. 15 2
      app/models/Taboo.scala
  2. 15 12
      public/javascripts/tabooServices.js
  3. 2 2
      public/partials/chatRoom.html

+ 15 - 2
app/models/Taboo.scala

@@ -47,15 +47,27 @@ class Team {
 }
 }
 
 
 object Round {
 object Round {
+  val DURATION = 60
+
   implicit val writes = new Writes[Round] {
   implicit val writes = new Writes[Round] {
     def writes(o: Round) = Json.obj(
     def writes(o: Round) = Json.obj(
       "team" -> o.team,
       "team" -> o.team,
-      "monitors" -> o.monitors
+      "monitors" -> o.monitors,
+      "remainingTime" -> o.remainingTime
     )
     )
   }
   }
 }
 }
 
 
-case class Round(team: Team, monitors: Set[String])
+case class Round(
+  team: Team,
+  monitors: Set[String],
+  startTime: Long = System.currentTimeMillis
+) {
+  def remainingTime = {
+    val sinceStart = (System.currentTimeMillis - startTime) / 1000
+    Math.max(0, Round.DURATION - sinceStart)
+  }
+}
 
 
 case class Information(text: String)
 case class Information(text: String)
 case class Guess(username: String, text: String)
 case class Guess(username: String, text: String)
@@ -203,6 +215,7 @@ class TabooGame(val chatActor: ActorRef) extends Actor {
     chatActor ! Announce(Json.obj(
     chatActor ! Announce(Json.obj(
       "kind" -> kind,
       "kind" -> kind,
       "user" -> user,
       "user" -> user,
+      "round" -> round,
       "teamA" -> teamA,
       "teamA" -> teamA,
       "teamB" -> teamB
       "teamB" -> teamB
     ))
     ))

+ 15 - 12
public/javascripts/tabooServices.js

@@ -34,24 +34,26 @@ angular.module('tabooServices', [])
   game.pass = function() {
   game.pass = function() {
     Chat.send('/pass');
     Chat.send('/pass');
   };
   };
-  game.roundStart = function() {
+  game.roundStart = function(round) {
     game.pendingRound = false;
     game.pendingRound = false;
-    game.roundStarted = true;
+    game.round = round;
     game.points = 0;
     game.points = 0;
-    game.timer.start(60);
+    Timer.start(game.round.remainingTime);
   };
   };
   game.roundEnd = function() {
   game.roundEnd = function() {
     game.pendingRound = false;
     game.pendingRound = false;
-    game.roundStarted = false;
+    game.round = null;
     game.card = null;
     game.card = null;
-    game.monitors = [];
   };
   };
   game.isMonitor = function() {
   game.isMonitor = function() {
-    return game.monitors.indexOf(Chat.username) >= 0;
+    return game.round.monitors.indexOf(Chat.username) >= 0;
   };
   };
   game.isPlayer = function() {
   game.isPlayer = function() {
-    return game.player == Chat.username;
+    return game.round.team.player == Chat.username;
   };
   };
+  game.roundRunning = function() {
+    return game.round != null;
+  }
 
 
   $rootScope.$on('ws:connected', init);
   $rootScope.$on('ws:connected', init);
   $rootScope.$on('ws:message', onmessage);
   $rootScope.$on('ws:message', onmessage);
@@ -63,8 +65,7 @@ angular.module('tabooServices', [])
     game.points = 0;
     game.points = 0;
     game.pendingRound = false;
     game.pendingRound = false;
     game.roundStarted = false;
     game.roundStarted = false;
-    game.player = '';
-    game.monitors = [];
+    game.round = null;
     game.timer = Timer;
     game.timer = Timer;
     game.status();
     game.status();
   }
   }
@@ -102,9 +103,7 @@ angular.module('tabooServices', [])
     }
     }
     else if(message.kind == "roundStart") {
     else if(message.kind == "roundStart") {
       gmMessage("Start game!");
       gmMessage("Start game!");
-      game.roundStart();
-      game.player = message.round.team.player;
-      game.monitors = message.round.monitors;
+      game.roundStart(message.round);
     }
     }
     else if(message.kind == "roundEnd") {
     else if(message.kind == "roundEnd") {
       if(message.card) {
       if(message.card) {
@@ -124,6 +123,10 @@ angular.module('tabooServices', [])
   function updateStatus(message) {
   function updateStatus(message) {
     game.teamA = message.teamA;
     game.teamA = message.teamA;
     game.teamB = message.teamB;
     game.teamB = message.teamB;
+    game.round = message.round;
+    if(game.round) {
+      Timer.start(game.round.remainingTime);
+    }
   }
   }
 
 
   function gmMessage(message) {
   function gmMessage(message) {

+ 2 - 2
public/partials/chatRoom.html

@@ -30,10 +30,10 @@
     </div>
     </div>
     <div class="span4" ng-controller="GameCtrl">
     <div class="span4" ng-controller="GameCtrl">
       <button class="btn" ng-show="game.pendingRound" ng-click="game.startRound()">Start</button>
       <button class="btn" ng-show="game.pendingRound" ng-click="game.startRound()">Start</button>
-      <div ng-show="game.roundStarted">
+      <div ng-if="game.roundRunning()">
         <h1>{{game.timer.count}}</h1>
         <h1>{{game.timer.count}}</h1>
       </div>
       </div>
-      <div ng-show="game.roundStarted">
+      <div ng-if="game.roundRunning()">
         <button class="btn" ng-show="game.isPlayer()" ng-click="game.pass()">Pass</button>
         <button class="btn" ng-show="game.isPlayer()" ng-click="game.pass()">Pass</button>
         <button class="btn" ng-show="game.isMonitor()" ng-click="game.taboo()">Uh-uh!</button>
         <button class="btn" ng-show="game.isMonitor()" ng-click="game.taboo()">Uh-uh!</button>
       </div>
       </div>