From 02c84b85c58b60f48bb0baf37cacdd037e84d28d Mon Sep 17 00:00:00 2001
From: Carl Worth <cworth@cworth.org>
Date: Wed, 10 Jun 2020 09:15:36 -0700
Subject: [PATCH] Accumulate running scores for a multi-round game

Obviously, if players stick around for more than one round, they are
going to want to see hwo their score accumulates.
---
 empathy.js | 13 +++++++++++++
 game.js    |  5 +++--
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/empathy.js b/empathy.js
index 60e1ecf..f9bf741 100644
--- a/empathy.js
+++ b/empathy.js
@@ -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 eb1ba80..86a0e70 100644
--- 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 => {
-- 
2.45.2