Add display of scores
authorCarl Worth <cworth@cworth.org>
Wed, 10 Jun 2020 04:29:45 +0000 (21:29 -0700)
committerCarl Worth <cworth@cworth.org>
Wed, 10 Jun 2020 04:29:45 +0000 (21:29 -0700)
And a not-quite-yet-fully-functional "New Game" button.

I don't know if "New Game" is broken on this side or on the server
side, (this was part of a 90-minute flurry of code).

empathy/empathy.jsx

index 2aec434654cf26d7bc5078298ab89f5d3739ce9d..b03d697ef1cfd2561ffc1b223d4939e3b616fa4a 100644 (file)
@@ -60,6 +60,8 @@ events.addEventListener("game-state", event => {
   }
 
   window.game.set_active_prompt(state.active_prompt);
+
+  window.game.set_scores(state.scores);
 });
 
 events.addEventListener("prompt", event => {
@@ -80,6 +82,12 @@ events.addEventListener("answered", event => {
   window.game.set_players_answered(players_answered);
 });
 
+events.addEventListener("scores", event => {
+  const scores = JSON.parse(event.data);
+
+  window.game.set_scores(scores);
+});
+
 /*********************************************************
  * Game and supporting classes                           *
  *********************************************************/
@@ -453,10 +461,54 @@ class Game extends React.PureComponent {
     });
   }
 
+  set_scores(scores) {
+    this.setState({
+      scores: scores
+    });
+  }
+
   render() {
     const state = this.state;
     const players_total = 1 + state.other_players.length;
 
+    if (state.scores) {
+      return (
+        <div className="scores">
+          <h2>Scores</h2>
+          <ul>
+            {state.scores.scores.map(score => {
+              return (
+                <li key={score.player}>
+                  {score.player}: {score.score}
+                </li>
+              );
+            })}
+          </ul>
+          <h2>Words submitted</h2>
+          <ul>
+            {state.scores.words.map(word => {
+              return (
+                <li key={word.word}>
+                  {word.word}:
+                  {word.players.map(p => {
+                    return (
+                      <span key={p}>{p}{" "}</span>
+                    );
+                  })}
+                </li>
+              );
+            })}
+          </ul>
+          <button
+            className="new-game"
+            onClick={() => fetch_post_json('reset') }
+          >
+            New Game
+          </button>
+        </div>
+      );
+    }
+
     if (state.active_prompt) {
       return <ActivePrompt
                prompt={state.active_prompt}