Use "answer" instead of "answers" when there is only one item in a category
[lmno.games] / empathy / empathy.jsx
index 0c1fccfe0b60e1a399c2d2bf2c27082c46c1c5eb..6133463f35a7a9d6ba4d58cbe8e4188bf4af48a9 100644 (file)
@@ -45,6 +45,12 @@ events.addEventListener("player-enter", event => {
   window.game.set_other_player_info(info);
 });
 
+events.addEventListener("player-exit", event => {
+  const info = JSON.parse(event.data);
+
+  window.game.remove_player(info);
+});
+
 events.addEventListener("player-update", event => {
   const info = JSON.parse(event.data);
 
@@ -82,6 +88,10 @@ events.addEventListener("game-state", event => {
   window.game.set_end_judging(state.end_judging);
 
   window.game.set_scores(state.scores);
+
+  window.game.set_new_game_votes(state.new_game_votes);
+
+  window.game.state_ready();
 });
 
 events.addEventListener("prompt", event => {
@@ -168,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                           *
  *********************************************************/
@@ -552,7 +574,7 @@ class Ambiguities extends React.PureComponent {
         <div className="please-wait">
           <h2>Submission received</h2>
           <p>
-            The following players have completed judging:
+            The following players have completed judging:{' '}
             {[...this.props.players_judged].join(', ')}
           </p>
           <p>
@@ -589,6 +611,7 @@ class Ambiguities extends React.PureComponent {
           what goes around comes around, so it's best to be generous when
           judging.
         </p>
+        <h2>{this.props.prompt.prompt}</h2>
         {this.state.word_sets.map(set => {
           return (
             <div
@@ -714,7 +737,7 @@ class ActivePrompt extends React.PureComponent {
         <div className="please-wait">
           <h2>Submission received</h2>
           <p>
-            The following players have submitted their answers:
+            The following players have submitted their answers:{' '}
             {[...this.props.players_answered].join(', ')}
           </p>
           <p>
@@ -745,7 +768,8 @@ class ActivePrompt extends React.PureComponent {
         <p>
           Remember, you're trying to match your answers with
           what the other players submit.
-          Give {this.props.prompt.items} answers for the following prompt:
+          Give {this.props.prompt.items} answer
+          {this.props.prompt.items > 1 ? 's' : ''} for the following prompt:
         </p>
         <h2>{this.props.prompt.prompt}</h2>
         <form onSubmit={this.handle_submit}>
@@ -798,7 +822,9 @@ class Game extends React.PureComponent {
       players_judging: {},
       judging_idle: false,
       end_judging_votes: new Set(),
-      scores: null
+      scores: null,
+      new_game_votes: new Set(),
+      ready: false
     };
   }
 
@@ -827,6 +853,12 @@ class Game extends React.PureComponent {
     });
   }
 
+  remove_player(info) {
+    this.setState({
+      other_players: this.state.other_players.filter(o => o.id !== info.id)
+    });
+  }
+
   reset_game_state() {
     this.setState({
       prompts: [],
@@ -840,7 +872,9 @@ class Game extends React.PureComponent {
       players_judging: {},
       judging_idle: false,
       end_judging_votes: new Set(),
-      scores: null
+      scores: null,
+      new_game_votes: new Set(),
+      ready: false
     });
   }
 
@@ -970,7 +1004,6 @@ class Game extends React.PureComponent {
   }
 
   set_judging_idle(value) {
-    console.log("Setting judging idle to " + value);
     this.setState({
       judging_idle: value
     });
@@ -1000,6 +1033,30 @@ 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
+    });
+  }
+
   render() {
     const state = this.state;
     const players_total = 1 + state.other_players.length;
@@ -1007,12 +1064,13 @@ class Game extends React.PureComponent {
     if (state.scores) {
       return (
         <div className="scores">
+          <h2>{state.active_prompt.prompt}</h2>
           <h2>Scores</h2>
           <ul>
             {state.scores.scores.map(score => {
               return (
                 <li key={score.player}>
-                  {score.player}: {score.score}
+                  {score.players.join("/")}: {score.score}
                 </li>
               );
             })}
@@ -1022,16 +1080,28 @@ class Game extends React.PureComponent {
             {state.scores.words.map(word => {
               return (
                 <li key={word.word}>
-                  {`${word.word}: ${word.players.join(', ')}`}
+                  {word.word} ({word.players.length}): {word.players.join(', ')}
                 </li>
               );
             })}
           </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>
       );
@@ -1060,6 +1130,9 @@ class Game extends React.PureComponent {
              />;
     }
 
+    if (! state.ready)
+      return null;
+
     return [
       <GameInfo
         key="game-info"