Implement voting for the "New Game" button
authorCarl Worth <cworth@cworth.org>
Sat, 27 Jun 2020 23:32:05 +0000 (16:32 -0700)
committerCarl Worth <cworth@cworth.org>
Sat, 27 Jun 2020 23:32:05 +0000 (16:32 -0700)
Making this phase advance much like the other phases, and dependent on
a majority of the players rather than just letting one player stomp
all over things.

Beyond this, we still do want to allow players to inspect the state of
past rounds, but the UI for that that will have to come later.

empathy/empathy.jsx

index c216e4921b9b5d68d8731defddc353125a010d68..2c70bc387f6d23cfbed01144fea26f3a142df1c3 100644 (file)
@@ -89,6 +89,8 @@ events.addEventListener("game-state", event => {
 
   window.game.set_scores(state.scores);
 
+  window.game.set_new_game_votes(state.new_game_votes);
+
   window.game.state_ready();
 });
 
@@ -176,6 +178,18 @@ events.addEventListener("scores", event => {
   window.game.set_scores(scores);
 });
 
+events.addEventListener("vote-new-game", event => {
+  const player = JSON.parse(event.data);
+
+  window.game.set_player_vote_new_game(player);
+});
+
+events.addEventListener("unvote-new-game", event => {
+  const player = JSON.parse(event.data);
+
+  window.game.set_player_unvote_new_game(player);
+});
+
 /*********************************************************
  * Game and supporting classes                           *
  *********************************************************/
@@ -807,6 +821,7 @@ class Game extends React.PureComponent {
       judging_idle: false,
       end_judging_votes: new Set(),
       scores: null,
+      new_game_votes: new Set(),
       ready: false
     };
   }
@@ -856,6 +871,7 @@ class Game extends React.PureComponent {
       judging_idle: false,
       end_judging_votes: new Set(),
       scores: null,
+      new_game_votes: new Set(),
       ready: false
     });
   }
@@ -1015,6 +1031,24 @@ class Game extends React.PureComponent {
     });
   }
 
+  set_new_game_votes(players) {
+    this.setState({
+      new_game_votes: new Set(players)
+    });
+  }
+
+  set_player_vote_new_game(player) {
+    this.setState({
+      new_game_votes: new Set([...this.state.new_game_votes, player])
+    });
+  }
+
+  set_player_unvote_new_game(player) {
+    this.setState({
+      new_game_votes: new Set([...this.state.new_game_votes].filter(p => p !== player))
+    });
+  }
+
   state_ready() {
     this.setState({
       ready: true
@@ -1049,10 +1083,22 @@ class Game extends React.PureComponent {
             })}
           </ul>
           <button
-            className="new-game"
-            onClick={() => fetch_post_json('reset') }
+            className="vote-button"
+            onClick={() => fetch_post_json(`new-game/${state.active_prompt.id}`) }
           >
             New Game
+            <div className="vote-choices">
+              {[...state.new_game_votes].map(v => {
+                return (
+                  <div
+                    key={v}
+                    className="vote-choice"
+                  >
+                    {v}
+                  </div>
+                );
+              })}
+            </div>
           </button>
         </div>
       );