Track server change of grouping players with tied scores
[lmno.games] / empathy / empathy.jsx
1 const MAX_PROMPT_ITEMS = 20;
2
3 function undisplay(element) {
4   element.style.display="none";
5 }
6
7 function add_message(severity, message) {
8   message = `<div class="message ${severity}" onclick="undisplay(this)">
9 <span class="hide-button" onclick="undisplay(this.parentElement)">&times;</span>
10 ${message}
11 </div>`;
12   const message_area = document.getElementById('message-area');
13   message_area.insertAdjacentHTML('beforeend', message);
14 }
15
16 /*********************************************************
17  * Handling server-sent event stream                     *
18  *********************************************************/
19
20 const events = new EventSource("events");
21
22 events.onerror = function(event) {
23   if (event.target.readyState === EventSource.CLOSED) {
24     setTimeout(() => {
25       add_message("danger", "Connection to server lost.");
26     }, 1000);
27   }
28 };
29
30 events.addEventListener("game-info", event => {
31   const info = JSON.parse(event.data);
32
33   window.game.set_game_info(info);
34 });
35
36 events.addEventListener("player-info", event => {
37   const info = JSON.parse(event.data);
38
39   window.game.set_player_info(info);
40 });
41
42 events.addEventListener("player-enter", event => {
43   const info = JSON.parse(event.data);
44
45   window.game.set_other_player_info(info);
46 });
47
48 events.addEventListener("player-exit", event => {
49   const info = JSON.parse(event.data);
50
51   window.game.remove_player(info);
52 });
53
54 events.addEventListener("player-update", event => {
55   const info = JSON.parse(event.data);
56
57   if (info.id === window.game.state.player_info.id)
58     window.game.set_player_info(info);
59   else
60     window.game.set_other_player_info(info);
61 });
62
63 events.addEventListener("game-state", event => {
64   const state = JSON.parse(event.data);
65
66   window.game.reset_game_state();
67
68   window.game.set_prompts(state.prompts);
69
70   window.game.set_active_prompt(state.active_prompt);
71
72   window.game.set_players_answered(state.players_answered);
73
74   window.game.set_players_answering(state.players_answering);
75
76   window.game.set_answering_idle(state.answering_idle);
77
78   window.game.set_end_answers(state.end_answers);
79
80   window.game.set_ambiguities(state.ambiguities);
81
82   window.game.set_players_judged(state.players_judged);
83
84   window.game.set_players_judging(state.players_judging);
85
86   window.game.set_judging_idle(state.judging_idle);
87
88   window.game.set_end_judging(state.end_judging);
89
90   window.game.set_scores(state.scores);
91
92   window.game.set_new_game_votes(state.new_game_votes);
93
94   window.game.state_ready();
95 });
96
97 events.addEventListener("prompt", event => {
98   const prompt = JSON.parse(event.data);
99
100   window.game.add_or_update_prompt(prompt);
101 });
102
103 events.addEventListener("start", event => {
104   const prompt = JSON.parse(event.data);
105
106   window.game.set_active_prompt(prompt);
107 });
108
109 events.addEventListener("player-answered", event => {
110   const player = JSON.parse(event.data);
111
112   window.game.set_player_answered(player);
113 });
114
115 events.addEventListener("player-answering", event => {
116   const player = JSON.parse(event.data);
117
118   window.game.set_player_answering(player);
119 });
120
121 events.addEventListener("answering-idle", event => {
122   const value = JSON.parse(event.data);
123
124   window.game.set_answering_idle(value);
125 });
126
127 events.addEventListener("vote-end-answers", event => {
128   const player = JSON.parse(event.data);
129
130   window.game.set_player_vote_end_answers(player);
131 });
132
133 events.addEventListener("unvote-end-answers", event => {
134   const player = JSON.parse(event.data);
135
136   window.game.set_player_unvote_end_answers(player);
137 });
138
139 events.addEventListener("ambiguities", event => {
140   const ambiguities = JSON.parse(event.data);
141
142   window.game.set_ambiguities(ambiguities);
143 });
144
145 events.addEventListener("player-judged", event => {
146   const player = JSON.parse(event.data);
147
148   window.game.set_player_judged(player);
149 });
150
151 events.addEventListener("player-judging", event => {
152   const player = JSON.parse(event.data);
153
154   window.game.set_player_judging(player);
155 });
156
157 events.addEventListener("judging-idle", event => {
158   const value = JSON.parse(event.data);
159
160   window.game.set_judging_idle(value);
161 });
162
163 events.addEventListener("vote-end-judging", event => {
164   const player = JSON.parse(event.data);
165
166   window.game.set_player_vote_end_judging(player);
167 });
168
169 events.addEventListener("unvote-end-judging", event => {
170   const player = JSON.parse(event.data);
171
172   window.game.set_player_unvote_end_judging(player);
173 });
174
175 events.addEventListener("scores", event => {
176   const scores = JSON.parse(event.data);
177
178   window.game.set_scores(scores);
179 });
180
181 events.addEventListener("vote-new-game", event => {
182   const player = JSON.parse(event.data);
183
184   window.game.set_player_vote_new_game(player);
185 });
186
187 events.addEventListener("unvote-new-game", event => {
188   const player = JSON.parse(event.data);
189
190   window.game.set_player_unvote_new_game(player);
191 });
192
193 /*********************************************************
194  * Game and supporting classes                           *
195  *********************************************************/
196
197 function copy_to_clipboard(id)
198 {
199   const tmp = document.createElement("input");
200   tmp.setAttribute("value", document.getElementById(id).innerHTML);
201   document.body.appendChild(tmp);
202   tmp.select();
203   document.execCommand("copy");
204   document.body.removeChild(tmp);
205 }
206
207 const GameInfo = React.memo(props => {
208   if (! props.id)
209     return null;
210
211   return (
212     <div className="game-info">
213       <span className="game-id">{props.id}</span>
214       {" "}
215       Share this link to invite friends:{" "}
216       <span id="game-share-url">{props.url}</span>
217       {" "}
218       <button
219         className="inline"
220         onClick={() => copy_to_clipboard('game-share-url')}
221       >Copy Link</button>
222     </div>
223   );
224 });
225
226 const PlayerInfo = React.memo(props => {
227   if (! props.player.id)
228     return null;
229
230   const all_players = [props.player, ...props.other_players];
231
232   const sorted_players = all_players.sort((a,b) => {
233     return b.score - a.score;
234   });
235
236   const names_and_scores = sorted_players.map(player => {
237     if (player.score)
238       return `${player.name} (${player.score})`;
239     else
240       return player.name;
241   }).join(', ');
242
243   return (
244     <div className="player-info">
245       <span className="players-header">Players: </span>
246       <span>{names_and_scores}</span>
247     </div>
248   );
249 });
250
251 function fetch_method_json(method, api = '', data = {}) {
252   const response = fetch(api, {
253     method: method,
254     headers: {
255       'Content-Type': 'application/json'
256     },
257     body: JSON.stringify(data)
258   });
259   return response;
260 }
261
262 function fetch_post_json(api = '', data = {}) {
263   return fetch_method_json('POST', api, data);
264 }
265
266 async function fetch_put_json(api = '', data = {}) {
267   return fetch_method_json('PUT', api, data);
268 }
269
270 class CategoryRequest extends React.PureComponent {
271   constructor(props) {
272     super(props);
273     this.category = React.createRef();
274
275     this.handle_change = this.handle_change.bind(this);
276     this.handle_submit = this.handle_submit.bind(this);
277   }
278
279   handle_change(event) {
280     const category_input = this.category.current;
281     const category = category_input.value;
282
283     const match = category.match(/[0-9]+/);
284     if (match) {
285       const num_items = parseInt(match[0], 10);
286       if (num_items <= MAX_PROMPT_ITEMS)
287         category_input.setCustomValidity("");
288     }
289   }
290
291   async handle_submit(event) {
292     const form = event.currentTarget;
293     const category_input = this.category.current;
294     const category = category_input.value;
295
296     /* Prevent the default page-changing form-submission behavior. */
297     event.preventDefault();
298
299     const match = category.match(/[0-9]+/);
300     if (match === null) {
301       category_input.setCustomValidity("Category must include a number");
302       form.reportValidity();
303       return;
304     }
305
306     const num_items = parseInt(match[0], 10);
307
308     if (num_items > MAX_PROMPT_ITEMS) {
309       category_input.setCustomValidity(`Maximum number of items is ${MAX_PROMPT_ITEMS}`);
310       form.reportValidity();
311       return;
312     }
313
314     const response = await fetch_post_json("prompts", {
315       items: num_items,
316       prompt: category
317     });
318
319     if (response.status === 200) {
320       const result = await response.json();
321       if (! result.valid) {
322         add_message("danger", result.message);
323         return;
324       }
325     } else {
326       add_message("danger", "An error occurred submitting your category");
327     }
328
329     form.reset();
330   }
331
332   render() {
333     return (
334       <div className="category-request">
335         <h2>Submit a Category</h2>
336         <p>
337           Suggest a category to play. Don't forget to include the
338           number of items for each person to submit.
339         </p>
340
341         <form onSubmit={this.handle_submit} >
342           <div className="form-field large">
343             <input
344               type="text"
345               id="category"
346               placeholder="6 things at the beach"
347               required
348               autoComplete="off"
349               onChange={this.handle_change}
350               ref={this.category}
351             />
352           </div>
353
354           <div className="form-field large">
355             <button type="submit">
356               Send
357             </button>
358           </div>
359
360         </form>
361       </div>
362     );
363   }
364 }
365
366 const PromptOptions = React.memo(props => {
367
368   if (props.prompts.length === 0)
369     return null;
370
371   return (
372     <div className="prompt-options">
373       <h2>Vote on Categories</h2>
374       <p>
375         Select any categories below that you'd like to play.
376         You can choose as many as you'd like.
377       </p>
378       {props.prompts.map(p => {
379         return (
380           <button
381             className="vote-button"
382             key={p.id}
383             onClick={() => fetch_post_json(`vote/${p.id}`) }
384           >
385             {p.prompt}
386             <div className="vote-choices">
387               {p.votes.map(v => {
388                 return (
389                   <div
390                     key={v}
391                     className="vote-choice"
392                   >
393                     {v}
394                   </div>
395                 );
396               })}
397             </div>
398           </button>
399         );
400       })}
401     </div>
402   );
403 });
404
405 const LetsPlay = React.memo(props => {
406
407   const quorum = Math.round((props.num_players + 1) / 2);
408   const max_votes = props.prompts.reduce(
409     (max_so_far, v) => Math.max(max_so_far, v.votes.length), 0);
410
411   if (max_votes < quorum)
412     return null;
413
414   const candidates = props.prompts.filter(p => p.votes.length >= quorum);
415   const index = Math.floor(Math.random() * candidates.length);
416   const winner = candidates[index];
417
418   return (
419     <div className="lets-play">
420       <h2>Let's Play</h2>
421       <p>
422         That should be enough voting. If you're not waiting for any
423         other players to join, then let's start.
424       </p>
425       <button
426         className="lets-play"
427         onClick={() => fetch_post_json(`start/${winner.id}`) }
428       >
429         Start Game
430       </button>
431     </div>
432   );
433 });
434
435 class Ambiguities extends React.PureComponent {
436
437   constructor(props) {
438     super(props);
439
440     const word_sets = props.words.map(word => {
441       const set = new Set();
442       set.add(word);
443       return set;
444     });
445
446     this.state = {
447       word_sets: word_sets,
448       selected: null
449     };
450
451     this.submitted = false;
452     this.judging_sent_recently = false;
453   }
454
455   async handle_submit() {
456
457     /* Don't submit a second time. */
458     if (this.submitted)
459       return;
460
461     const response = await fetch_post_json(
462       `judged/${this.props.prompt.id}`,{
463         word_groups: this.state.word_sets.map(set => Array.from(set))
464       }
465     );
466
467     if (response.status === 200) {
468       const result = await response.json();
469       if (! result.valid) {
470         add_message("danger", result.message);
471         return;
472       }
473     } else {
474       add_message("danger", "An error occurred submitting the results of your judging");
475       return;
476     }
477
478     this.submitted = true;
479   }
480
481   handle_click(word) {
482
483     /* Let the server know we are doing some judging, (but rate limit
484      * this so we don't send a "judging" notification more frquently
485      * than necessary.
486      */
487     if (! this.judging_sent_recently) {
488       fetch_post_json(`judging/${this.props.prompt.id}`);
489       this.judging_sent_recently = true;
490       setTimeout(() => { this.judging_sent_recently = false; }, 1000);
491     }
492
493     if (this.state.selected == word) {
494       /* Second click on same word removes the word from the group. */
495       const idx = this.state.word_sets.findIndex(s => s.has(word));
496       const set = this.state.word_sets[idx];
497       if (set.size === 1) {
498         /* When the word is already alone, there's nothing to do but
499          * to un-select it. */
500         this.setState({
501           selected: null
502         });
503         return;
504       }
505
506       const new_set = new Set([...set].filter(w => w !== word));
507       this.setState({
508         selected: null,
509         word_sets: [...this.state.word_sets.slice(0, idx),
510                     new_set,
511                     new Set().add(word),
512                     ...this.state.word_sets.slice(idx+1)]
513       });
514     } else if (this.state.selected) {
515       /* Click of a second word groups the two together. */
516       const idx1 = this.state.word_sets.findIndex(s => s.has(this.state.selected));
517       const idx2 = this.state.word_sets.findIndex(s => s.has(word));
518       const set1 = this.state.word_sets[idx1];
519       const set2 = this.state.word_sets[idx2];
520       const new_set = new Set([...set2, ...set1]);
521       if (idx1 < idx2) {
522         this.setState({
523           selected: null,
524           word_sets: [...this.state.word_sets.slice(0, idx1),
525                       ...this.state.word_sets.slice(idx1 + 1, idx2),
526                       new_set,
527                       ...this.state.word_sets.slice(idx2 + 1)]
528         });
529       } else {
530         this.setState({
531           selected: null,
532           word_sets: [...this.state.word_sets.slice(0, idx2),
533                       new_set,
534                       ...this.state.word_sets.slice(idx2 + 1, idx1),
535                       ...this.state.word_sets.slice(idx1 + 1)]
536         });
537       }
538     } else {
539       /* First click of a word selects it. */
540       this.setState({
541         selected: word
542       });
543     }
544   }
545
546   render() {
547     let move_on_button = null;
548
549     if (this.props.idle) {
550       move_on_button = (
551         <button
552           className="vote-button"
553           onClick={() => fetch_post_json(`end-judging/${this.props.prompt.id}`) }
554         >
555           Move On
556           <div className="vote-choices">
557             {[...this.props.votes].map(v => {
558               return (
559                 <div
560                   key={v}
561                   className="vote-choice"
562                 >
563                   {v}
564                   </div>
565               );
566             })}
567           </div>
568         </button>
569       );
570     }
571
572     if (this.props.players_judged.has(this.props.player.name)) {
573       return (
574         <div className="please-wait">
575           <h2>Submission received</h2>
576           <p>
577             The following players have completed judging:
578             {[...this.props.players_judged].join(', ')}
579           </p>
580           <p>
581             Still waiting for the following players:
582           </p>
583           <ul>
584             {Object.keys(this.props.players_judging).map(player => {
585               return (
586                 <li
587                   key={player}
588                 >
589                   {player}
590                   {this.props.players_judging[player] ?
591                    <span className="typing"/> : null }
592                 </li>
593               );
594             })}
595           </ul>
596           {move_on_button}
597
598         </div>
599       );
600     }
601
602     const btn_class = "ambiguity-button";
603     const btn_selected_class = btn_class + " selected";
604
605     return (
606       <div className="ambiguities">
607         <h2>Judging Answers</h2>
608         <p>
609           Click on each pair of answers that should be scored as equivalent,
610           (and click any word twice to split it out from a group). Remember,
611           what goes around comes around, so it's best to be generous when
612           judging.
613         </p>
614         {this.state.word_sets.map(set => {
615           return (
616             <div
617               className="ambiguity-group"
618               key={Array.from(set)[0]}
619             >
620               {Array.from(set).map(word => {
621                 return (
622                   <button
623                     className={this.state.selected === word ?
624                                btn_selected_class : btn_class }
625                     key={word}
626                     onClick={() => this.handle_click(word)}
627                   >
628                     {word}
629                   </button>
630                 );
631               })}
632             </div>
633           );
634         })}
635         <p>
636           Click here when done judging:<br/>
637           <button
638             onClick={() => this.handle_submit()}
639           >
640             Send
641           </button>
642         </p>
643       </div>
644     );
645   }
646 }
647
648 class ActivePrompt extends React.PureComponent {
649
650   constructor(props) {
651     super(props);
652     const items = props.prompt.items;
653
654     this.submitted = false;
655
656     this.answers = [...Array(items)].map(() => React.createRef());
657     this.answering_sent_recently = false;
658
659     this.handle_submit = this.handle_submit.bind(this);
660     this.handle_change = this.handle_change.bind(this);
661   }
662
663   handle_change(event) {
664     /* We don't care (or even look) at what the player is typing at
665      * this point. We simply want to be informed that the player _is_
666      * typing so that we can tell the server (which will tell other
667      * players) that there is activity here.
668      */
669
670     /* Rate limit so that we don't send an "answering" notification
671      * more frequently than necessary.
672      */
673     if (! this.answering_sent_recently) {
674       fetch_post_json(`answering/${this.props.prompt.id}`);
675       this.answering_sent_recently = true;
676       setTimeout(() => { this.answering_sent_recently = false; }, 1000);
677     }
678   }
679
680   async handle_submit(event) {
681     const form = event.currentTarget;
682
683     /* Prevent the default page-changing form-submission behavior. */
684     event.preventDefault();
685
686     /* And don't submit a second time. */
687     if (this.submitted)
688       return;
689
690     const response = await fetch_post_json(`answer/${this.props.prompt.id}`, {
691       answers: this.answers.map(r => r.current.value)
692     });
693     if (response.status === 200) {
694       const result = await response.json();
695       if (! result.valid) {
696         add_message("danger", result.message);
697         return;
698       }
699     } else {
700       add_message("danger", "An error occurred submitting your answers");
701       return;
702     }
703
704     /* Everything worked. Server is happy with our answers. */
705     form.reset();
706     this.submitted = true;
707   }
708
709   render() {
710     let move_on_button = null;
711     if (this.props.idle) {
712       move_on_button =(
713         <button
714           className="vote-button"
715           onClick={() => fetch_post_json(`end-answers/${this.props.prompt.id}`) }
716         >
717           Move On
718           <div className="vote-choices">
719             {[...this.props.votes].map(v => {
720               return (
721                 <div
722                   key={v}
723                   className="vote-choice"
724                 >
725                   {v}
726                 </div>
727               );
728             })}
729           </div>
730         </button>
731       );
732     }
733
734     if (this.props.players_answered.has(this.props.player.name)) {
735       return (
736         <div className="please-wait">
737           <h2>Submission received</h2>
738           <p>
739             The following players have submitted their answers:
740             {[...this.props.players_answered].join(', ')}
741           </p>
742           <p>
743           Still waiting for the following players:
744           </p>
745           <ul>
746             {Object.keys(this.props.players_answering).map(player => {
747               return (
748                 <li
749                   key={player}
750                 >
751                   {player}
752                   {this.props.players_answering[player] ?
753                    <span className="typing"/> : null }
754                 </li>
755               );
756             })}
757           </ul>
758           {move_on_button}
759
760         </div>
761       );
762     }
763
764     return (
765       <div className="active-prompt">
766         <h2>The Game of Empathy</h2>
767         <p>
768           Remember, you're trying to match your answers with
769           what the other players submit.
770           Give {this.props.prompt.items} answers for the following prompt:
771         </p>
772         <h2>{this.props.prompt.prompt}</h2>
773         <form onSubmit={this.handle_submit}>
774           {[...Array(this.props.prompt.items)].map((whocares,i) => {
775             return (
776               <div
777                 key={i}
778                 className="form-field large">
779                 <input
780                   type="text"
781                   name={`answer_${i}`}
782                   required
783                   autoComplete="off"
784                   onChange={this.handle_change}
785                   ref={this.answers[i]}
786                 />
787               </div>
788             );
789           })}
790
791           <div
792             key="submit-button"
793             className="form-field large">
794             <button type="submit">
795               Send
796             </button>
797           </div>
798
799         </form>
800       </div>
801     );
802   }
803 }
804
805 class Game extends React.PureComponent {
806   constructor(props) {
807     super(props);
808     this.state = {
809       game_info: {},
810       player_info: {},
811       other_players: [],
812       prompts: [],
813       active_prompt: null,
814       players_answered: new Set(),
815       players_answering: {},
816       answering_idle: false,
817       end_answers_votes: new Set(),
818       ambiguities: null,
819       players_judged: new Set(),
820       players_judging: {},
821       judging_idle: false,
822       end_judging_votes: new Set(),
823       scores: null,
824       new_game_votes: new Set(),
825       ready: false
826     };
827   }
828
829   set_game_info(info) {
830     this.setState({
831       game_info: info
832     });
833   }
834
835   set_player_info(info) {
836     this.setState({
837       player_info: info
838     });
839   }
840
841   set_other_player_info(info) {
842     const other_players_copy = [...this.state.other_players];
843     const idx = other_players_copy.findIndex(o => o.id === info.id);
844     if (idx >= 0) {
845       other_players_copy[idx] = info;
846     } else {
847       other_players_copy.push(info);
848     }
849     this.setState({
850       other_players: other_players_copy
851     });
852   }
853
854   remove_player(info) {
855     this.setState({
856       other_players: this.state.other_players.filter(o => o.id !== info.id)
857     });
858   }
859
860   reset_game_state() {
861     this.setState({
862       prompts: [],
863       active_prompt: null,
864       players_answered: new Set(),
865       players_answering: {},
866       answering_idle: false,
867       end_answers_votes: new Set(),
868       ambiguities: null,
869       players_judged: new Set(),
870       players_judging: {},
871       judging_idle: false,
872       end_judging_votes: new Set(),
873       scores: null,
874       new_game_votes: new Set(),
875       ready: false
876     });
877   }
878
879   set_prompts(prompts) {
880     this.setState({
881       prompts: prompts
882     });
883   }
884
885   add_or_update_prompt(prompt) {
886     const prompts_copy = [...this.state.prompts];
887     const idx = prompts_copy.findIndex(p => p.id === prompt.id);
888     if (idx >= 0) {
889       prompts_copy[idx] = prompt;
890     } else {
891       prompts_copy.push(prompt);
892     }
893     this.setState({
894       prompts: prompts_copy
895     });
896   }
897
898   set_active_prompt(prompt) {
899     this.setState({
900       active_prompt: prompt
901     });
902   }
903
904   set_players_answered(players) {
905     this.setState({
906       players_answered: new Set(players)
907     });
908   }
909
910   set_player_answered(player) {
911     const new_players_answering = {...this.state.players_answering};
912     delete new_players_answering[player];
913
914     this.setState({
915       players_answered: new Set([...this.state.players_answered, player]),
916       players_answering: new_players_answering
917     });
918   }
919
920   set_players_answering(players) {
921     const players_answering = {};
922     for (let player of players) {
923       players_answering[player] = {active: false};
924     }
925     this.setState({
926       players_answering: players_answering
927     });
928   }
929
930   set_player_answering(player) {
931     this.setState({
932       players_answering: {
933         ...this.state.players_answering,
934         [player]: {active: true}
935       }
936     });
937   }
938
939   set_answering_idle(value) {
940     this.setState({
941       answering_idle: value
942     });
943   }
944
945   set_end_answers(players) {
946     this.setState({
947       end_answers_votes: new Set(players)
948     });
949   }
950
951   set_player_vote_end_answers(player) {
952     this.setState({
953       end_answers_votes: new Set([...this.state.end_answers_votes, player])
954     });
955   }
956
957   set_player_unvote_end_answers(player) {
958     this.setState({
959       end_answers_votes: new Set([...this.state.end_answers_votes].filter(p => p !== player))
960     });
961   }
962
963   set_ambiguities(ambiguities) {
964     this.setState({
965       ambiguities: ambiguities
966     });
967   }
968
969   set_players_judged(players) {
970     this.setState({
971       players_judged: new Set(players)
972     });
973   }
974
975   set_player_judged(player) {
976     const new_players_judging = {...this.state.players_judging};
977     delete new_players_judging[player];
978
979     this.setState({
980       players_judged: new Set([...this.state.players_judged, player]),
981       players_judging: new_players_judging
982     });
983   }
984
985   set_players_judging(players) {
986     const players_judging = {};
987     for (let player of players) {
988       players_judging[player] = {active: false};
989     }
990     this.setState({
991       players_judging: players_judging
992     });
993   }
994
995   set_player_judging(player) {
996     this.setState({
997       players_judging: {
998         ...this.state.players_judging,
999         [player]: {active: true}
1000       }
1001     });
1002   }
1003
1004   set_judging_idle(value) {
1005     this.setState({
1006       judging_idle: value
1007     });
1008   }
1009
1010   set_end_judging(players) {
1011     this.setState({
1012       end_judging_votes: new Set(players)
1013     });
1014   }
1015
1016   set_player_vote_end_judging(player) {
1017     this.setState({
1018       end_judging_votes: new Set([...this.state.end_judging_votes, player])
1019     });
1020   }
1021
1022   set_player_unvote_end_judging(player) {
1023     this.setState({
1024       end_judging_votes: new Set([...this.state.end_judging_votes].filter(p => p !== player))
1025     });
1026   }
1027
1028   set_scores(scores) {
1029     this.setState({
1030       scores: scores
1031     });
1032   }
1033
1034   set_new_game_votes(players) {
1035     this.setState({
1036       new_game_votes: new Set(players)
1037     });
1038   }
1039
1040   set_player_vote_new_game(player) {
1041     this.setState({
1042       new_game_votes: new Set([...this.state.new_game_votes, player])
1043     });
1044   }
1045
1046   set_player_unvote_new_game(player) {
1047     this.setState({
1048       new_game_votes: new Set([...this.state.new_game_votes].filter(p => p !== player))
1049     });
1050   }
1051
1052   state_ready() {
1053     this.setState({
1054       ready: true
1055     });
1056   }
1057
1058   render() {
1059     const state = this.state;
1060     const players_total = 1 + state.other_players.length;
1061
1062     if (state.scores) {
1063       return (
1064         <div className="scores">
1065           <h2>Scores</h2>
1066           <ul>
1067             {state.scores.scores.map(score => {
1068               return (
1069                 <li key={score.player}>
1070                   {score.players.join("/")}: {score.score}
1071                 </li>
1072               );
1073             })}
1074           </ul>
1075           <h2>Words submitted</h2>
1076           <ul>
1077             {state.scores.words.map(word => {
1078               return (
1079                 <li key={word.word}>
1080                   {`${word.word}: ${word.players.join(', ')}`}
1081                 </li>
1082               );
1083             })}
1084           </ul>
1085           <button
1086             className="vote-button"
1087             onClick={() => fetch_post_json(`new-game/${state.active_prompt.id}`) }
1088           >
1089             New Game
1090             <div className="vote-choices">
1091               {[...state.new_game_votes].map(v => {
1092                 return (
1093                   <div
1094                     key={v}
1095                     className="vote-choice"
1096                   >
1097                     {v}
1098                   </div>
1099                 );
1100               })}
1101             </div>
1102           </button>
1103         </div>
1104       );
1105     }
1106
1107     if (state.ambiguities){
1108       return <Ambiguities
1109                prompt={state.active_prompt}
1110                words={state.ambiguities}
1111                player={state.player_info}
1112                players_judged={state.players_judged}
1113                players_judging={state.players_judging}
1114                idle={state.judging_idle}
1115                votes={state.end_judging_votes}
1116              />;
1117     }
1118
1119     if (state.active_prompt) {
1120       return <ActivePrompt
1121                prompt={state.active_prompt}
1122                player={state.player_info}
1123                players_answered={state.players_answered}
1124                players_answering={state.players_answering}
1125                idle={state.answering_idle}
1126                votes={state.end_answers_votes}
1127              />;
1128     }
1129
1130     if (! state.ready)
1131       return null;
1132
1133     return [
1134       <GameInfo
1135         key="game-info"
1136         id={state.game_info.id}
1137         url={state.game_info.url}
1138       />,
1139       <PlayerInfo
1140         key="player-info"
1141         game={this}
1142         player={state.player_info}
1143         other_players={state.other_players}
1144       />,
1145       <p key="spacer"></p>,
1146       <CategoryRequest
1147         key="category-request"
1148       />,
1149       <PromptOptions
1150         key="prompts"
1151         prompts={state.prompts}
1152       />,
1153       <LetsPlay
1154         key="lets-play"
1155         num_players={1+state.other_players.length}
1156         prompts={state.prompts}
1157       />
1158     ];
1159   }
1160 }
1161
1162 ReactDOM.render(<Game
1163                   ref={(me) => window.game = me}
1164                 />, document.getElementById("empathy"));