1 const express = require("express");
2 const Game = require("./game.js");
4 const MAX_PROMPT_ITEMS = 20;
6 /* This time parameter specifies a time period in which a phase will
7 * not be considered idle in any circumstance. That is, this should be
8 * a reasonable time in which any "active" player should have at least
9 * started interacting with the current phase.
11 * Specified in seconds
13 const PHASE_MINIMUM_TIME = 30;
15 /* This parameter gives the amount of time that the game will wait to
16 * see activity from pending players. If this amount of time passes
17 * with no activity from any of them, the server will emit an "idle"
18 * event which will let clients issue a vote to end the current phase.
20 * Specified in seconds
22 const PHASE_IDLE_TIMEOUT = 15;
24 class Empathy extends Game {
31 players_answering: new Set(),
32 answering_idle: false,
33 end_answers: new Set(),
36 players_judging: new Set(),
38 end_judging: new Set(),
40 new_game_votes: new Set()
43 this.answering_idle_timer = 0;
44 this.answering_start_time_ms = 0;
45 this.judging_idle_timer = 0;
46 this.judging_start_time_ms = 0;
47 this.next_prompt_id = 1;
48 this.equivalencies = {};
53 /* Before closing out the current round, we accumulate into each
54 * player's overall score the results from the current round.
56 * Note: Rather than applying the actual points from each round
57 * into the player's score, we instead accumulate up the number of
58 * players that they bested in each round. This ensures that each
59 * round receives an equal weight in the overall scoring. */
60 let bested = this.state.scores.scores.reduce(
61 (total, score) => total + score.players.length, 0);
62 for (let i = 0; i < this.state.scores.scores.length; i++) {
63 const score = this.state.scores.scores[i];
64 bested -= score.players.length;
65 for (let player_name of score.players) {
66 const player = this.players.find(p => p.name === player_name);
68 player.score += bested;
70 player.score = bested;
72 /* And broadcast that new score out. */
73 this.broadcast_event('player-update', player.info_json());
77 /* Now that we're done with the active prompt, we remove it from
78 * the list of prompts and also remove any prompts that received
79 * more negative votes than positive. This keeps the list of
82 const active_id = this.state.active_prompt.id;
84 this.state.prompts.filter(
85 p => p.id !== active_id && p.votes.length >= p.votes_against.length
88 this.state.active_prompt = null;
89 this.state.players_answered = [];
90 this.state.players_answering = new Set();
91 this.state.answering_idle = false;
92 this.state.end_answers = new Set();
93 this.state.ambiguities = null;
94 this.state.players_judged = [];
95 this.state.players_judging = new Set();
96 this.state.judging_idle = false;
97 this.state.end_judging = new Set();
98 this.state.scores = null;
99 this.state.new_game_votes = new Set();
102 if (this.answering_idle_timer) {
103 clearTimeout(this.answering_idle_timer);
105 this.answering_idle_timer = 0;
106 this.answering_start_time_ms = 0;
107 if (this.judging_idle_timer) {
108 clearTimeout(this.judging_idle_timer);
110 this.judging_idle_timer = 0;
111 this.judging_start_time_ms = 0;
112 this.equivalencies = {};
114 this.broadcast_event('game-state', this.game_state_json());
117 add_prompt(items, prompt_string) {
118 if (items > MAX_PROMPT_ITEMS) {
121 message: `Maximum number of items is ${MAX_PROMPT_ITEMS}`
128 message: "Category must require at least one item"
132 const prompt = new Prompt(this.next_prompt_id, items, prompt_string);
133 this.next_prompt_id++;
135 this.state.prompts.push(prompt);
137 this.broadcast_event_object('prompt', prompt);
145 /* Returns true if vote toggled, false for player or prompt not found */
146 toggle_vote(prompt_id, session_id) {
147 const player = this.players_by_session[session_id];
149 const prompt = this.state.prompts.find(p => p.id === prompt_id);
150 if (! prompt || ! player)
153 prompt.toggle_vote(player.name);
155 this.broadcast_event_object('prompt', prompt);
160 toggle_vote_against(prompt_id, session_id) {
161 const player = this.players_by_session[session_id];
163 const prompt = this.state.prompts.find(p => p.id === prompt_id);
164 if (! prompt || ! player)
167 prompt.toggle_vote_against(player.name);
169 this.broadcast_event_object('prompt', prompt);
174 /* Returns true on success, false for prompt not found. */
176 const prompt = this.state.prompts.find(p => p.id === prompt_id);
180 /* Ignore any start request that comes in while a prompt is
181 * already being played. */
182 if (this.state.active_prompt)
185 this.state.active_prompt = prompt;
187 this.broadcast_event_object('start', prompt);
192 receive_answer(prompt_id, session_id, answers) {
193 const player = this.players_by_session[session_id];
195 return { valid: false, message: "Player not found" };
197 const prompt = this.state.prompts.find(p => p.id === prompt_id);
199 return { valid: false, message: "Prompt not found" };
201 if (prompt !== this.state.active_prompt)
202 return { valid: false, message: "Prompt no longer active" };
204 /* Save the complete answers for our own use later. */
210 /* Update state (to be sent out to any future clients) */
211 this.state.players_answering.delete(player.name);
212 this.state.players_answered.push(player.name);
214 /* And notify all players that this player has answered. */
215 this.broadcast_event_object('player-answered', player.name);
217 /* If no players are left in the answering list then we don't need
218 * to wait for the answering_idle_timer to fire, because a person
219 * who isn't there obviously can't be typing. So we can broadcast
220 * the idle event right away. Note that we do this only if the
221 * answering phase has been going for a while to avoid the case of
222 * the first player being super fast and emptying the
223 * players_answering list before anyone else even got in.
225 if (this.state.players_answering.size === 0 &&
226 ((Date.now() - this.answering_start_time_ms) / 1000) > PHASE_MINIMUM_TIME)
228 this.broadcast_event_object('answering-idle', true);
231 return { valid: true };
234 receive_answering(prompt_id, session_id) {
235 const player = this.players_by_session[session_id];
237 return { valid: false, message: "Player not found" };
239 const prompt = this.state.prompts.find(p => p.id === prompt_id);
241 return { valid: false, message: "Prompt not found" };
243 if (prompt !== this.state.active_prompt)
244 return { valid: false, message: "Prompt no longer active" };
246 if (this.answering_idle_timer) {
247 clearTimeout(this.answering_idle_timer);
248 this.ansering_idle_timer = 0;
250 if (! this.state.answering_idle) {
251 this.answering_idle_timer = setTimeout(() => {
252 this.state.answering_idle = true;
253 this.broadcast_event_object('answering-idle', true);
254 }, PHASE_IDLE_TIMEOUT * 1000);
257 if (this.answering_start_time_ms === 0)
258 this.answering_start_time_ms = Date.now();
260 /* Notify all players that this player is actively answering. */
261 this.state.players_answering.add(player.name);
262 this.broadcast_event_object('player-answering', player.name);
264 return { valid: true };
267 /* Returns true if vote toggled, false for player or prompt not found */
268 toggle_end_answers(prompt_id, session_id) {
269 const player = this.players_by_session[session_id];
271 const prompt = this.state.prompts.find(p => p.id === prompt_id);
272 if (! prompt || ! player)
275 if (this.state.end_answers.has(player.name)) {
276 this.state.end_answers.delete(player.name);
277 this.broadcast_event_object('unvote-end-answers', player.name);
279 this.state.end_answers.add(player.name);
280 this.broadcast_event_object('vote-end-answers', player.name);
289 for (let a of this.answers) {
290 for (let word of a.answers) {
291 const key = this.canonize(word);
292 word_map[key] = word;
296 this.state.ambiguities = Object.values(word_map).sort((a,b) => {
297 return a.toLowerCase().localeCompare(b.toLowerCase());
300 if (this.judging_start_time_ms === 0) {
301 this.judging_start_time_ms = Date.now();
304 this.broadcast_event_object('ambiguities', this.state.ambiguities);
306 /* Notify all players of every player that is judging. */
307 for (let player_name of this.state.players_answered) {
308 this.state.players_judging.add(player_name);
309 this.broadcast_event_object('player-judging', player_name);
313 reset_judging_timeout() {
314 if (this.judging_idle_timer) {
315 clearTimeout(this.judging_idle_timer);
316 this.judging_idle_timer = 0;
318 if (! this.state.judging_idle) {
319 this.judging_idle_timer = setTimeout(() => {
320 this.state.judging_idle = true;
321 this.broadcast_event_object('judging-idle', true);
322 }, PHASE_IDLE_TIMEOUT * 1000);
326 receive_judged(prompt_id, session_id, word_groups) {
327 const player = this.players_by_session[session_id];
329 return { valid: false, message: "Player not found" };
331 const prompt = this.state.prompts.find(p => p.id === prompt_id);
333 return { valid: false, message: "Prompt not found" };
335 if (prompt !== this.state.active_prompt)
336 return { valid: false, message: "Prompt no longer active" };
338 this.reset_judging_timeout();
340 /* Each player submits some number of groups of answers that
341 * should be considered equivalent. The server expands that into
342 * the set of pair-wise equivalencies that are expressed. The
343 * reason for that is so that the server can determine which
344 * pair-wise equivalencies have majority support.
346 for (let group of word_groups) {
348 for (let i = 0; i < group.length - 1; i++) {
349 for (let j = i + 1; j < group.length; j++) {
350 let eq = [group[i], group[j]];
352 /* Put the two words into a reliable order so that we don't
353 * miss a pair of equivalent equivalencies just because they
354 * happen to be in the opposite order. */
355 if (eq[0].localeCompare(eq[1]) > 0) {
356 eq = [group[j], group[i]];
359 const key=`${this.canonize(eq[0])}:${this.canonize(eq[1])}`;
361 const exist = this.equivalencies[key];
365 this.equivalencies[key] = {
375 /* Update state (to be sent out to any future clients) */
376 this.state.players_judging.delete(player.name);
377 this.state.players_judged.push(player.name);
379 /* And notify all players that this player has judged. */
380 this.broadcast_event_object('player-judged', player.name);
382 /* If no players are left in the judging list then we don't need
383 * to wait for the judging_idle_timer to fire, because a person
384 * who isn't there obviously can't be judging. So we can broadcast
385 * the idle event right away. Note that we do this only if the
386 * judging phase has been going for a while to avoid the case of
387 * the first player being super fast and emptying the
388 * players_judging list before anyone else even got in.
390 if (this.state.players_judging.size === 0 &&
391 ((Date.now() - this.judging_start_time_ms) / 1000) > PHASE_MINIMUM_TIME)
393 this.broadcast_event_object('judging-idle', true);
396 return { valid: true };
399 receive_judging(prompt_id, session_id) {
401 const player = this.players_by_session[session_id];
403 return { valid: false, message: "Player not found" };
405 const prompt = this.state.prompts.find(p => p.id === prompt_id);
407 return { valid: false, message: "Prompt not found" };
409 if (prompt !== this.state.active_prompt)
410 return { valid: false, message: "Prompt no longer active" };
412 this.reset_judging_timeout();
414 /* Notify all players that this player is actively judging. */
415 this.state.players_judging.add(player.name);
416 this.broadcast_event_object('player-judging', player.name);
418 return { valid: true };
421 /* Returns true if vote toggled, false for player or prompt not found */
422 toggle_end_judging(prompt_id, session_id) {
423 const player = this.players_by_session[session_id];
425 const prompt = this.state.prompts.find(p => p.id === prompt_id);
426 if (! prompt || ! player)
429 if (this.state.end_judging.has(player.name)) {
430 this.state.end_judging.delete(player.name);
431 this.broadcast_event_object('unvote-end-judging', player.name);
433 this.state.end_judging.add(player.name);
434 this.broadcast_event_object('vote-end-judging', player.name);
440 /* Returns true if vote toggled, false for player or prompt not found */
441 toggle_new_game(prompt_id, session_id) {
442 const player = this.players_by_session[session_id];
444 const prompt = this.state.prompts.find(p => p.id === prompt_id);
445 if (! prompt || ! player)
448 if (this.state.new_game_votes.has(player.name)) {
449 this.state.new_game_votes.delete(player.name);
450 this.broadcast_event_object('unvote-new-game', player.name);
452 this.state.new_game_votes.add(player.name);
453 this.broadcast_event_object('vote-new-game', player.name);
460 return word.trim().toLowerCase();
464 const word_submitters = {};
466 /* Perform a (non-strict) majority ruling on equivalencies,
467 * dropping all that didn't get enough votes. */
468 const quorum = Math.floor((this.state.players_judged.length + 1)/2);
469 const agreed_equivalencies = Object.values(this.equivalencies).filter(
470 eq => eq.count >= quorum);
472 /* And with that agreed set of equivalencies, construct the full
474 const word_maps = {};
476 for (let e of agreed_equivalencies) {
477 const word0_canon = this.canonize(e.words[0]);
478 const word1_canon = this.canonize(e.words[1]);
479 let group = word_maps[word0_canon];
481 group = word_maps[word1_canon];
483 group = { words: [], players: new Set()};
485 if (! word_maps[word0_canon]) {
486 word_maps[word0_canon] = group;
487 group.words.push(e.words[0]);
490 if (! word_maps[word1_canon]) {
491 word_maps[word1_canon] = group;
492 group.words.push(e.words[1]);
496 /* Now go through answers from each player and add the player
497 * to the set corresponding to each word group. */
498 for (let a of this.answers) {
499 for (let word of a.answers) {
500 const word_canon = this.canonize(word);
501 /* If there's no group yet, this is a singleton word. */
502 if (word_maps[word_canon]) {
503 word_maps[word_canon].players.add(a.player);
505 const group = { words: [word], players: new Set() };
506 group.players.add(a.player);
507 word_maps[word_canon] = group;
512 /* Now that we've assigned the players to these word maps, we now
513 * want to collapse the groups down to a single array of
516 * The difference between "word_maps" and "word_groups" is that
517 * the former has a property for every word that maps to a group,
518 * (so if you iterate over the keys you will see the same group
519 * multiple times). In contrast, iterating over"word_groups" will
520 * have you visit each group only once. */
521 const word_groups = Object.entries(word_maps).filter(
522 entry => entry[0] === this.canonize(entry[1].words[0]))
523 .map(entry => entry[1]);
525 /* Now, go through each word group and assign the scores out to
526 * the corresponding players.
528 * Note: We do this by going through the word groups, (as opposed
529 * to the list of words from the players again), specifically to
530 * avoid giving a player points for a wrod group twice (in the
531 * case where a player submits two different words that the group
532 * ends up judging as equivalent).
534 this.players.forEach(p => p.round_score = 0);
535 for (let group of word_groups) {
536 group.players.forEach(p => p.round_score += group.players.size);
539 const scores = this.players.filter(p => p.active).map(p => {
546 scores.sort((a,b) => {
547 return b.score - a.score;
550 /* After sorting individual players by score, group players
551 * together who have the same score. */
552 const reducer = (list, next) => {
553 if (list.length && list[list.length-1].score == next.score)
554 list[list.length-1].players.push(next.player);
556 list.push({players: [next.player], score: next.score});
560 const grouped_scores = scores.reduce(reducer, []);
562 /* Put the word groups into a form the client can consume.
564 const words_submitted = word_groups.map(
567 word: group.words.join('/'),
568 players: Array.from(group.players).map(p => p.name)
573 words_submitted.sort((a,b) => {
574 return b.players.length - a.players.length;
577 /* Put this round's scores into the game state object so it will
578 * be sent to any new clients that join. */
579 this.state.scores = {
580 scores: grouped_scores,
581 words: words_submitted
584 /* And broadcast the scores to all connected clients. */
585 this.broadcast_event_object('scores', this.state.scores);
589 Empathy.router = express.Router();
590 const router = Empathy.router;
593 constructor(id, items, prompt) {
596 this.prompt = prompt;
598 this.votes_against = [];
601 toggle_vote(player_name) {
602 if (this.votes.find(v => v === player_name))
603 this.votes = this.votes.filter(v => v !== player_name);
605 this.votes.push(player_name);
608 toggle_vote_against(player_name) {
609 if (this.votes_against.find(v => v === player_name)) {
610 this.votes_against = this.votes_against.filter(v => v !== player_name);
612 this.votes_against.push(player_name);
613 /* When voting against, we also remove any vote _for_ the same
615 this.votes = this.votes.filter(v => v !== player_name);
620 router.post('/prompts', (request, response) => {
621 const game = request.game;
623 const result = game.add_prompt(request.body.items, request.body.prompt);
625 response.json(result);
628 router.post('/vote/:prompt_id([0-9]+)', (request, response) => {
629 const game = request.game;
630 const prompt_id = parseInt(request.params.prompt_id, 10);
632 if (game.toggle_vote(prompt_id, request.session.id))
635 response.sendStatus(404);
638 router.post('/vote_against/:prompt_id([0-9]+)', (request, response) => {
639 const game = request.game;
640 const prompt_id = parseInt(request.params.prompt_id, 10);
642 if (game.toggle_vote_against(prompt_id, request.session.id))
645 response.sendStatus(404);
648 router.post('/start/:prompt_id([0-9]+)', (request, response) => {
649 const game = request.game;
650 const prompt_id = parseInt(request.params.prompt_id, 10);
652 if (game.start(prompt_id))
655 response.sendStatus(404);
658 router.post('/answer/:prompt_id([0-9]+)', (request, response) => {
659 const game = request.game;
660 const prompt_id = parseInt(request.params.prompt_id, 10);
662 const result = game.receive_answer(prompt_id,
664 request.body.answers);
665 response.json(result);
667 /* If every registered player has answered, then there's no need to
668 * wait for anything else. */
669 if (game.state.players_answered.length >= game.active_players)
670 game.perform_judging();
673 router.post('/answering/:prompt_id([0-9]+)', (request, response) => {
674 const game = request.game;
675 const prompt_id = parseInt(request.params.prompt_id, 10);
677 const result = game.receive_answering(prompt_id,
679 response.json(result);
682 router.post('/end-answers/:prompt_id([0-9]+)', (request, response) => {
683 const game = request.game;
684 const prompt_id = parseInt(request.params.prompt_id, 10);
686 if (game.toggle_end_answers(prompt_id, request.session.id))
689 response.sendStatus(404);
691 /* The majority rule here includes all players that have answered as
692 * well as all that have started typing. */
693 const players_involved = (game.state.players_answered.length +
694 game.state.players_answering.size);
696 if (game.state.end_answers.size > players_involved / 2)
697 game.perform_judging();
700 router.post('/judged/:prompt_id([0-9]+)', (request, response) => {
701 const game = request.game;
702 const prompt_id = parseInt(request.params.prompt_id, 10);
704 const result = game.receive_judged(prompt_id,
706 request.body.word_groups);
707 response.json(result);
709 /* If every player who answered has also judged, then there's no
710 * need to wait for anything else. */
711 const judged_set = new Set(game.state.players_judged);
712 if ([...game.state.players_answered].filter(x => !judged_set.has(x)).length === 0)
713 game.compute_scores();
716 router.post('/judging/:prompt_id([0-9]+)', (request, response) => {
717 const game = request.game;
718 const prompt_id = parseInt(request.params.prompt_id, 10);
720 const result = game.receive_judging(prompt_id,
722 response.json(result);
725 router.post('/end-judging/:prompt_id([0-9]+)', (request, response) => {
726 const game = request.game;
727 const prompt_id = parseInt(request.params.prompt_id, 10);
729 if (game.toggle_end_judging(prompt_id, request.session.id))
732 response.sendStatus(404);
734 if (game.state.end_judging.size > (game.state.players_answered.length / 2))
735 game.compute_scores();
738 router.post('/new-game/:prompt_id([0-9]+)', (request, response) => {
739 const game = request.game;
740 const prompt_id = parseInt(request.params.prompt_id, 10);
742 if (game.toggle_new_game(prompt_id, request.session.id))
745 response.sendStatus(404);
747 if (game.state.new_game_votes.size > (game.state.players_answered.length / 2))
751 router.post('/reset', (request, response) => {
752 const game = request.game;
760 identifier: "empathy",
763 exports.Game = Empathy;