1
0
Эх сурвалжийг харах

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 жил өмнө
parent
commit
1f431ca4b4

+ 15 - 2
app/models/Taboo.scala

@@ -47,15 +47,27 @@ class Team {
 }
 
 object Round {
+  val DURATION = 60
+
   implicit val writes = new Writes[Round] {
     def writes(o: Round) = Json.obj(
       "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 Guess(username: String, text: String)
@@ -203,6 +215,7 @@ class TabooGame(val chatActor: ActorRef) extends Actor {
     chatActor ! Announce(Json.obj(
       "kind" -> kind,
       "user" -> user,
+      "round" -> round,
       "teamA" -> teamA,
       "teamB" -> teamB
     ))

+ 15 - 12
public/javascripts/tabooServices.js

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

+ 2 - 2
public/partials/chatRoom.html

@@ -30,10 +30,10 @@
     </div>
     <div class="span4" ng-controller="GameCtrl">
       <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>
       </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.isMonitor()" ng-click="game.taboo()">Uh-uh!</button>
       </div>