Use "answer" instead of "answers" when there is only one item in a category
[lmno.games] / empathy / empathy.jsx
index 566ad105c5e3cdb3727ffe504d74ca52e9c0efc4..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);
 
@@ -67,6 +73,8 @@ events.addEventListener("game-state", event => {
 
   window.game.set_players_answering(state.players_answering);
 
+  window.game.set_answering_idle(state.answering_idle);
+
   window.game.set_end_answers(state.end_answers);
 
   window.game.set_ambiguities(state.ambiguities);
@@ -75,9 +83,15 @@ events.addEventListener("game-state", event => {
 
   window.game.set_players_judging(state.players_judging);
 
+  window.game.set_judging_idle(state.judging_idle);
+
   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 => {
@@ -104,6 +118,12 @@ events.addEventListener("player-answering", event => {
   window.game.set_player_answering(player);
 });
 
+events.addEventListener("answering-idle", event => {
+  const value = JSON.parse(event.data);
+
+  window.game.set_answering_idle(value);
+});
+
 events.addEventListener("vote-end-answers", event => {
   const player = JSON.parse(event.data);
 
@@ -134,6 +154,12 @@ events.addEventListener("player-judging", event => {
   window.game.set_player_judging(player);
 });
 
+events.addEventListener("judging-idle", event => {
+  const value = JSON.parse(event.data);
+
+  window.game.set_judging_idle(value);
+});
+
 events.addEventListener("vote-end-judging", event => {
   const player = JSON.parse(event.data);
 
@@ -152,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                           *
  *********************************************************/
@@ -189,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>
   );
 });
@@ -402,14 +445,19 @@ class Ambiguities extends React.PureComponent {
 
     this.state = {
       word_sets: word_sets,
-      submitted: false,
       selected: null
     };
 
+    this.submitted = false;
     this.judging_sent_recently = false;
   }
 
   async handle_submit() {
+
+    /* Don't submit a second time. */
+    if (this.submitted)
+      return;
+
     const response = await fetch_post_json(
       `judged/${this.props.prompt.id}`,{
         word_groups: this.state.word_sets.map(set => Array.from(set))
@@ -427,9 +475,7 @@ class Ambiguities extends React.PureComponent {
       return;
     }
 
-    this.setState({
-      submitted: true
-    });
+    this.submitted = true;
   }
 
   handle_click(word) {
@@ -498,12 +544,37 @@ class Ambiguities extends React.PureComponent {
   }
 
   render() {
-    if (this.state.submitted)
+    let move_on_button = null;
+
+    if (this.props.idle) {
+      move_on_button = (
+        <button
+          className="vote-button"
+          onClick={() => fetch_post_json(`end-judging/${this.props.prompt.id}`) }
+        >
+          Move On
+          <div className="vote-choices">
+            {[...this.props.votes].map(v => {
+              return (
+                <div
+                  key={v}
+                  className="vote-choice"
+                >
+                  {v}
+                  </div>
+              );
+            })}
+          </div>
+        </button>
+      );
+    }
+
+    if (this.props.players_judged.has(this.props.player.name)) {
       return (
         <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>
@@ -522,27 +593,11 @@ class Ambiguities extends React.PureComponent {
               );
             })}
           </ul>
-          <button
-            className="vote-button"
-            onClick={() => fetch_post_json(`end-judging/${this.props.prompt.id}`) }
-          >
-            Move On
-            <div className="vote-choices">
-              {[...this.props.votes].map(v => {
-                return (
-                  <div
-                    key={v}
-                    className="vote-choice"
-                  >
-                    {v}
-                  </div>
-                );
-              })}
-            </div>
-          </button>
+          {move_on_button}
 
         </div>
       );
+    }
 
     const btn_class = "ambiguity-button";
     const btn_selected_class = btn_class + " selected";
@@ -556,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
@@ -596,9 +652,7 @@ class ActivePrompt extends React.PureComponent {
     super(props);
     const items = props.prompt.items;
 
-    this.state = {
-      submitted: false
-    };
+    this.submitted = false;
 
     this.answers = [...Array(items)].map(() => React.createRef());
     this.answering_sent_recently = false;
@@ -630,6 +684,10 @@ class ActivePrompt extends React.PureComponent {
     /* Prevent the default page-changing form-submission behavior. */
     event.preventDefault();
 
+    /* And don't submit a second time. */
+    if (this.submitted)
+      return;
+
     const response = await fetch_post_json(`answer/${this.props.prompt.id}`, {
       answers: this.answers.map(r => r.current.value)
     });
@@ -646,18 +704,40 @@ class ActivePrompt extends React.PureComponent {
 
     /* Everything worked. Server is happy with our answers. */
     form.reset();
-    this.setState({
-        submitted: true
-    });
+    this.submitted = true;
   }
 
   render() {
-    if (this.state.submitted)
+    let move_on_button = null;
+    if (this.props.idle) {
+      move_on_button =(
+        <button
+          className="vote-button"
+          onClick={() => fetch_post_json(`end-answers/${this.props.prompt.id}`) }
+        >
+          Move On
+          <div className="vote-choices">
+            {[...this.props.votes].map(v => {
+              return (
+                <div
+                  key={v}
+                  className="vote-choice"
+                >
+                  {v}
+                </div>
+              );
+            })}
+          </div>
+        </button>
+      );
+    }
+
+    if (this.props.players_answered.has(this.props.player.name)) {
       return (
         <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>
@@ -676,27 +756,11 @@ class ActivePrompt extends React.PureComponent {
               );
             })}
           </ul>
-          <button
-            className="vote-button"
-            onClick={() => fetch_post_json(`end-answers/${this.props.prompt.id}`) }
-          >
-            Move On
-            <div className="vote-choices">
-              {[...this.props.votes].map(v => {
-                return (
-                  <div
-                    key={v}
-                    className="vote-choice"
-                  >
-                    {v}
-                  </div>
-                );
-              })}
-            </div>
-          </button>
+          {move_on_button}
 
         </div>
       );
+    }
 
     return (
       <div className="active-prompt">
@@ -704,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}>
@@ -750,12 +815,16 @@ class Game extends React.PureComponent {
       active_prompt: null,
       players_answered: new Set(),
       players_answering: {},
+      answering_idle: false,
       end_answers_votes: new Set(),
       ambiguities: null,
       players_judged: new Set(),
       players_judging: {},
+      judging_idle: false,
       end_judging_votes: new Set(),
-      scores: null
+      scores: null,
+      new_game_votes: new Set(),
+      ready: false
     };
   }
 
@@ -784,18 +853,28 @@ 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: [],
       active_prompt: null,
       players_answered: new Set(),
       players_answering: {},
+      answering_idle: false,
       end_answers_votes: new Set(),
       ambiguities: null,
       players_judged: new Set(),
       players_judging: {},
+      judging_idle: false,
       end_judging_votes: new Set(),
-      scores: null
+      scores: null,
+      new_game_votes: new Set(),
+      ready: false
     });
   }
 
@@ -859,6 +938,12 @@ class Game extends React.PureComponent {
     });
   }
 
+  set_answering_idle(value) {
+    this.setState({
+      answering_idle: value
+    });
+  }
+
   set_end_answers(players) {
     this.setState({
       end_answers_votes: new Set(players)
@@ -918,6 +1003,12 @@ class Game extends React.PureComponent {
     });
   }
 
+  set_judging_idle(value) {
+    this.setState({
+      judging_idle: value
+    });
+  }
+
   set_end_judging(players) {
     this.setState({
       end_judging_votes: new Set(players)
@@ -942,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;
@@ -949,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>
               );
             })}
@@ -964,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>
       );
@@ -983,8 +1111,10 @@ class Game extends React.PureComponent {
       return <Ambiguities
                prompt={state.active_prompt}
                words={state.ambiguities}
+               player={state.player_info}
                players_judged={state.players_judged}
                players_judging={state.players_judging}
+               idle={state.judging_idle}
                votes={state.end_judging_votes}
              />;
     }
@@ -992,12 +1122,17 @@ class Game extends React.PureComponent {
     if (state.active_prompt) {
       return <ActivePrompt
                prompt={state.active_prompt}
+               player={state.player_info}
                players_answered={state.players_answered}
                players_answering={state.players_answering}
+               idle={state.answering_idle}
                votes={state.end_answers_votes}
              />;
     }
 
+    if (! state.ready)
+      return null;
+
     return [
       <GameInfo
         key="game-info"