1 const express = require("express");
2 const Game = require("./game.js");
4 class Empathy extends Game {
16 this.next_prompt_id = 1;
17 this.equivalencies = {};
22 /* Before closing out the current round, we accumulate that score
23 * for each player into their runnning total. */
24 for (let score of this.state.scores.scores) {
25 const player = this.players.find(p => p.name === score.player);
27 player.score += score.score;
29 player.score = score.score;
31 /* And broadcast that new score out. */
32 this.broadcast_event('player-update', player.info_json());
35 /* Now that we're done with the active prompt, we remove it from
36 * the list of prompts and also remove any prompts that received
37 * no votes. This keeps the list of prompts clean.
39 const active_id = this.state.active_prompt.id;
41 this.state.prompts.filter(
42 p => p.id !== active_id && p.votes.length > 0
45 this.state.active_prompt = null;
46 this.state.players_answered = 0;
47 this.state.ambiguities = 0;
48 this.state.players_judged = 0;
49 this.state.scores = null;
52 this.equivalencies = {};
54 this.broadcast_event_object('game-state', this.state);
57 add_prompt(items, prompt_string) {
58 const prompt = new Prompt(this.next_prompt_id, items, prompt_string);
59 this.next_prompt_id++;
61 this.state.prompts.push(prompt);
63 this.broadcast_event_object('prompt', prompt);
68 /* Returns true if vote toggled, false for player or prompt not found */
69 toggle_vote(prompt_id, session_id) {
70 const player = this.players_by_session[session_id];
72 const prompt = this.state.prompts.find(p => p.id === prompt_id);
73 if (! prompt || ! player)
76 prompt.toggle_vote(player.name);
78 this.broadcast_event_object('prompt', prompt);
83 /* Returns true on success, false for prompt not found. */
85 const prompt = this.state.prompts.find(p => p.id === prompt_id);
89 /* Ignore any start request that comes in while a prompt is
90 * already being played. */
91 if (this.state.active_prompt)
94 this.state.active_prompt = prompt;
96 this.broadcast_event_object('start', prompt);
101 receive_answer(prompt_id, session_id, answers) {
102 const player = this.players_by_session[session_id];
104 return { valid: false, message: "Player not found" };
106 const prompt = this.state.prompts.find(p => p.id === prompt_id);
108 return { valid: false, message: "Prompt not found" };
110 if (prompt !== this.state.active_prompt)
111 return { valid: false, message: "Prompt no longer active" };
113 /* Save the complete answers for our own use later. */
119 /* And notify players how many players have answered. */
120 this.state.players_answered++;
121 this.broadcast_event_object('answered', this.state.players_answered);
123 return { valid: true };
129 for (let a of this.answers) {
130 for (let word of a.answers) {
131 const key = this.canonize(word);
132 word_map[key] = word;
136 this.state.ambiguities = Object.values(word_map);
138 this.broadcast_event_object('ambiguities', this.state.ambiguities);
141 receive_judging(prompt_id, session_id, word_groups) {
142 const player = this.players_by_session[session_id];
144 return { valid: false, message: "Player not found" };
146 const prompt = this.state.prompts.find(p => p.id === prompt_id);
148 return { valid: false, message: "Prompt not found" };
150 if (prompt !== this.state.active_prompt)
151 return { valid: false, message: "Prompt no longer active" };
153 /* Each player submits some number of groups of answers that
154 * should be considered equivalent. The server expands that into
155 * the set of pair-wise equivalencies that are expressed. The
156 * reason for that is so that the server can determine which
157 * pair-wise equivalencies have majority support.
159 for (let group of word_groups) {
161 for (let i = 0; i < group.length - 1; i++) {
162 for (let j = i + 1; j < group.length; j++) {
163 let eq = [group[i], group[j]];
165 /* Put the two words into a reliable order so that we don't
166 * miss a pair of equivalent equivalencies just because they
167 * happen to be in the opposite order. */
168 if (eq[0].localeCompare(eq[1]) > 0) {
169 eq = [group[j], group[i]];
172 const exist = this.equivalencies[`${eq[0]}:${eq[1]}`];
176 this.equivalencies[`${eq[0]}:${eq[1]}`] = {
186 /* And notify players how many players have completed judging. */
187 this.state.players_judged++;
188 this.broadcast_event_object('judged', this.state.players_judged);
190 return { valid: true };
194 return word.toLowerCase();
198 const word_submitters = {};
200 /* Perform a (non-strict) majority ruling on equivalencies,
201 * dropping all that didn't get enough votes. */
202 const quorum = Math.floor((this.players.length + 1)/2);
203 const agreed_equivalencies = Object.values(this.equivalencies).filter(
204 eq => eq.count >= quorum);
206 /* And with that agreed set of equivalencies, construct the full
208 const word_maps = {};
210 for (let e of agreed_equivalencies) {
211 let group = word_maps[e.words[0]];
213 group = word_maps[e.words[1]];
215 group = { words: [], players: new Set()};
217 if (! word_maps[e.words[0]]) {
218 word_maps[e.words[0]] = group;
219 group.words.push(e.words[0]);
222 if (! word_maps[e.words[1]]) {
223 word_maps[e.words[1]] = group;
224 group.words.push(e.words[1]);
228 /* Now go through answers from each player and add the player
229 * to the set corresponding to each word group. */
230 for (let a of this.answers) {
231 for (let word of a.answers) {
232 /* If there's no group yet, this is a singleton word. */
233 if (word_maps[word]) {
234 word_maps[word].players.add(a.player);
236 const group = { words: [word], players: new Set() };
237 group.players.add(a.player);
238 word_maps[word] = group;
243 /* Now that we've assigned the players to these word maps, we now
244 * want to collapse the groups down to a single array of
247 * The difference between "word_maps" and "word_groups" is that
248 * the former has a property for every word that maps to a group,
249 * (so if you iterate over the keys you will see the same group
250 * multiple times). In contrast, iterating over"word_groups" will
251 * have you visit each group only once. */
252 const word_groups = Object.entries(word_maps).filter(
253 entry => entry[0] === entry[1].words[0]).map(entry => entry[1]);
255 /* Now, go through each word group and assign the scores out to
256 * the corresponding players.
258 * Note: We do this by going through the word groups, (as opposed
259 * to the list of words from the players again), specifically to
260 * avoid giving a player points for a wrod group twice (in the
261 * case where a player submits two different words that the group
262 * ends up judging as equivalent).
264 this.players.forEach(p => p.round_score = 0);
265 for (let group of word_groups) {
266 group.players.forEach(p => p.round_score += group.players.size);
269 const scores = this.players.map(p => {
276 scores.sort((a,b) => {
277 return b.score - a.score;
280 /* Put the word groups into a form the client can consume.
282 const words_submitted = word_groups.map(
285 word: group.words.join('/'),
286 players: Array.from(group.players).map(p => p.name)
291 words_submitted.sort((a,b) => {
292 return b.players.length - a.players.length;
295 /* Put this round's scores into the game state object so it will
296 * be sent to any new clients that join. */
297 this.state.scores = {
299 words: words_submitted
302 /* And broadcast the scores to all connected clients. */
303 this.broadcast_event_object('scores', this.state.scores);
307 Empathy.router = express.Router();
308 const router = Empathy.router;
311 constructor(id, items, prompt) {
314 this.prompt = prompt;
318 toggle_vote(player_name) {
319 if (this.votes.find(v => v === player_name))
320 this.votes = this.votes.filter(v => v !== player_name);
322 this.votes.push(player_name);
326 router.post('/prompts', (request, response) => {
327 const game = request.game;
329 prompt = game.add_prompt(request.body.items, request.body.prompt);
331 response.json({ id: prompt.id});
334 router.post('/vote/:prompt_id([0-9]+)', (request, response) => {
335 const game = request.game;
336 const prompt_id = parseInt(request.params.prompt_id, 10);
338 if (game.toggle_vote(prompt_id, request.session.id))
341 response.sendStatus(404);
344 router.post('/start/:prompt_id([0-9]+)', (request, response) => {
345 const game = request.game;
346 const prompt_id = parseInt(request.params.prompt_id, 10);
348 if (game.start(prompt_id))
351 response.sendStatus(404);
354 router.post('/answer/:prompt_id([0-9]+)', (request, response) => {
355 const game = request.game;
356 const prompt_id = parseInt(request.params.prompt_id, 10);
358 const result = game.receive_answer(prompt_id,
360 request.body.answers);
361 response.json(result);
363 if (game.state.players_answered >= game.players.length)
364 game.perform_judging();
367 router.post('/judging/:prompt_id([0-9]+)', (request, response) => {
368 const game = request.game;
369 const prompt_id = parseInt(request.params.prompt_id, 10);
371 const result = game.receive_judging(prompt_id,
373 request.body.word_groups);
374 response.json(result);
376 if (game.state.players_judged >= game.players.length)
377 game.compute_scores();
380 router.post('/reset', (request, response) => {
381 const game = request.game;
387 identifier: "empathy",
390 exports.Game = Empathy;