Use "answer" instead of "answers" when there is only one item in a category
[lmno.games] / empathy / empathy.jsx
index e67bbc87afbc91941c7460bb52aeb865aeac0d5b..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                           *
  *********************************************************/
@@ -205,18 +227,23 @@ const PlayerInfo = React.memo(props => {
   if (! props.player.id)
     return null;
 
+  const all_players = [props.player, ...props.other_players];
+
+  const sorted_players = all_players.sort((a,b) => {
+    return b.score - a.score;
+  });
+
+  const names_and_scores = sorted_players.map(player => {
+    if (player.score)
+      return `${player.name} (${player.score})`;
+    else
+      return player.name;
+  }).join(', ');
+
   return (
     <div className="player-info">
       <span className="players-header">Players: </span>
-      {props.player.name}
-      {props.player.score > 0 ? ` (${props.player.score})` : ""}
-      {props.other_players.map(other => (
-        <span key={other.id}>
-          {", "}
-          {other.name}
-          {other.score > 0 ? ` (${other.score})` : ""}
-        </span>
-      ))}
+      <span>{names_and_scores}</span>
     </div>
   );
 });
@@ -547,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>
@@ -584,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
@@ -709,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>
@@ -740,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}>
@@ -793,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
     };
   }
 
@@ -822,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: [],
@@ -835,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
     });
   }
 
@@ -965,7 +1004,6 @@ class Game extends React.PureComponent {
   }
 
   set_judging_idle(value) {
-    console.log("Setting judging idle to " + value);
     this.setState({
       judging_idle: value
     });
@@ -995,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;
@@ -1002,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>
               );
             })}
@@ -1017,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>
       );
@@ -1055,6 +1130,9 @@ class Game extends React.PureComponent {
              />;
     }
 
+    if (! state.ready)
+      return null;
+
     return [
       <GameInfo
         key="game-info"