]> git.cworth.org Git - empires-server/commitdiff
Accumulate running scores for a multi-round game
authorCarl Worth <cworth@cworth.org>
Wed, 10 Jun 2020 16:15:36 +0000 (09:15 -0700)
committerCarl Worth <cworth@cworth.org>
Wed, 10 Jun 2020 16:15:36 +0000 (09:15 -0700)
Obviously, if players stick around for more than one round, they are
going to want to see hwo their score accumulates.

empathy.js
game.js

index 60e1ecfd825b53de2db2132c0dd9987c15076229..f9bf7410d4da8e77f3208b6608005dc770dabbbd 100644 (file)
@@ -125,6 +125,16 @@ class Empathy extends Game {
         player: a.player.name,
         score: score
       });
+      /* Now that we've computed the score for a player for this
+       * round, also accumulate that score into the players runnning
+       * total. */
+      if (a.player.score)
+        a.player.score += score;
+      else
+        a.player.score = score;
+
+      /* And broadcast that new score out. */
+      this.broadcast_event('player-update', a.player.info_json());
     }
 
     scores.sort((a,b) => {
@@ -139,11 +149,14 @@ class Empathy extends Game {
       return b.players.length - a.players.length;
     });
 
+    /* Put this round's scores into the game state object so it will
+     * be sent to any new clients that join. */
     this.state.scores = {
       scores: scores,
       words: word_submitters_arr
     };
 
+    /* And broadcast the scores to all connected clients. */
     this.broadcast_event_object('scores', this.state.scores);
   }
 }
diff --git a/game.js b/game.js
index eb1ba807c3e1d9ec5a0e9eda65fceafaff052338..86a0e70b96f311dc2133898e679c1afa619406a7 100644 (file)
--- a/game.js
+++ b/game.js
@@ -36,7 +36,8 @@ class Player {
     return JSON.stringify({
       id: this.id,
       name: this.name,
-      team: this.team.name
+      team: this.team.name,
+      score: this.score
     });
   }
 }
@@ -184,7 +185,7 @@ class Game {
 
     /* After adding the player to the list, and if we are already past
      * the first move, assign this player to the first team that
-     * doesn't already have a player aissgned (if any). */
+     * doesn't already have a player assigned (if any). */
     if (! this.first_move) {
       const have_players = Array(this.teams.length).fill(false);
       this.players.forEach(p => {