1 const express = require("express");
2 const Game = require("./game.js");
4 const MAX_PROMPT_ITEMS = 20;
6 class Empathy extends Game {
18 this.next_prompt_id = 1;
19 this.equivalencies = {};
24 /* Before closing out the current round, we accumulate that score
25 * for each player into their runnning total. */
26 for (let score of this.state.scores.scores) {
27 const player = this.players.find(p => p.name === score.player);
29 player.score += score.score;
31 player.score = score.score;
33 /* And broadcast that new score out. */
34 this.broadcast_event('player-update', player.info_json());
37 /* Now that we're done with the active prompt, we remove it from
38 * the list of prompts and also remove any prompts that received
39 * no votes. This keeps the list of prompts clean.
41 const active_id = this.state.active_prompt.id;
43 this.state.prompts.filter(
44 p => p.id !== active_id && p.votes.length > 0
47 this.state.active_prompt = null;
48 this.state.players_answered = 0;
49 this.state.ambiguities = 0;
50 this.state.players_judged = 0;
51 this.state.scores = null;
54 this.equivalencies = {};
56 this.broadcast_event_object('game-state', this.state);
59 add_prompt(items, prompt_string) {
60 if (items > MAX_PROMPT_ITEMS)
63 message: `Maximum number of items is ${MAX_PROMPT_ITEMS}`
66 const prompt = new Prompt(this.next_prompt_id, items, prompt_string);
67 this.next_prompt_id++;
69 this.state.prompts.push(prompt);
71 this.broadcast_event_object('prompt', prompt);
79 /* Returns true if vote toggled, false for player or prompt not found */
80 toggle_vote(prompt_id, session_id) {
81 const player = this.players_by_session[session_id];
83 const prompt = this.state.prompts.find(p => p.id === prompt_id);
84 if (! prompt || ! player)
87 prompt.toggle_vote(player.name);
89 this.broadcast_event_object('prompt', prompt);
94 /* Returns true on success, false for prompt not found. */
96 const prompt = this.state.prompts.find(p => p.id === prompt_id);
100 /* Ignore any start request that comes in while a prompt is
101 * already being played. */
102 if (this.state.active_prompt)
105 this.state.active_prompt = prompt;
107 this.broadcast_event_object('start', prompt);
112 receive_answer(prompt_id, session_id, answers) {
113 const player = this.players_by_session[session_id];
115 return { valid: false, message: "Player not found" };
117 const prompt = this.state.prompts.find(p => p.id === prompt_id);
119 return { valid: false, message: "Prompt not found" };
121 if (prompt !== this.state.active_prompt)
122 return { valid: false, message: "Prompt no longer active" };
124 /* Save the complete answers for our own use later. */
130 /* And notify players how many players have answered. */
131 this.state.players_answered++;
132 this.broadcast_event_object('answered', this.state.players_answered);
134 return { valid: true };
140 for (let a of this.answers) {
141 for (let word of a.answers) {
142 const key = this.canonize(word);
143 word_map[key] = word;
147 this.state.ambiguities = Object.values(word_map).sort((a,b) => {
148 return a.toLowerCase().localeCompare(b.toLowerCase());
151 this.broadcast_event_object('ambiguities', this.state.ambiguities);
154 receive_judging(prompt_id, session_id, word_groups) {
155 const player = this.players_by_session[session_id];
157 return { valid: false, message: "Player not found" };
159 const prompt = this.state.prompts.find(p => p.id === prompt_id);
161 return { valid: false, message: "Prompt not found" };
163 if (prompt !== this.state.active_prompt)
164 return { valid: false, message: "Prompt no longer active" };
166 /* Each player submits some number of groups of answers that
167 * should be considered equivalent. The server expands that into
168 * the set of pair-wise equivalencies that are expressed. The
169 * reason for that is so that the server can determine which
170 * pair-wise equivalencies have majority support.
172 for (let group of word_groups) {
174 for (let i = 0; i < group.length - 1; i++) {
175 for (let j = i + 1; j < group.length; j++) {
176 let eq = [group[i], group[j]];
178 /* Put the two words into a reliable order so that we don't
179 * miss a pair of equivalent equivalencies just because they
180 * happen to be in the opposite order. */
181 if (eq[0].localeCompare(eq[1]) > 0) {
182 eq = [group[j], group[i]];
185 const exist = this.equivalencies[`${eq[0]}:${eq[1]}`];
189 this.equivalencies[`${eq[0]}:${eq[1]}`] = {
199 /* And notify players how many players have completed judging. */
200 this.state.players_judged++;
201 this.broadcast_event_object('judged', this.state.players_judged);
203 return { valid: true };
207 return word.toLowerCase();
211 const word_submitters = {};
213 /* Perform a (non-strict) majority ruling on equivalencies,
214 * dropping all that didn't get enough votes. */
215 const quorum = Math.floor((this.players.length + 1)/2);
216 const agreed_equivalencies = Object.values(this.equivalencies).filter(
217 eq => eq.count >= quorum);
219 /* And with that agreed set of equivalencies, construct the full
221 const word_maps = {};
223 for (let e of agreed_equivalencies) {
224 const word0_canon = this.canonize(e.words[0]);
225 const word1_canon = this.canonize(e.words[1]);
226 let group = word_maps[word0_canon];
228 group = word_maps[word1_canon];
230 group = { words: [], players: new Set()};
232 if (! word_maps[word0_canon]) {
233 word_maps[word0_canon] = group;
234 group.words.push(e.words[0]);
237 if (! word_maps[word1_canon]) {
238 word_maps[word1_canon] = group;
239 group.words.push(e.words[1]);
243 /* Now go through answers from each player and add the player
244 * to the set corresponding to each word group. */
245 for (let a of this.answers) {
246 for (let word of a.answers) {
247 const word_canon = this.canonize(word);
248 /* If there's no group yet, this is a singleton word. */
249 if (word_maps[word_canon]) {
250 word_maps[word_canon].players.add(a.player);
252 const group = { words: [word], players: new Set() };
253 group.players.add(a.player);
254 word_maps[word_canon] = group;
259 /* Now that we've assigned the players to these word maps, we now
260 * want to collapse the groups down to a single array of
263 * The difference between "word_maps" and "word_groups" is that
264 * the former has a property for every word that maps to a group,
265 * (so if you iterate over the keys you will see the same group
266 * multiple times). In contrast, iterating over"word_groups" will
267 * have you visit each group only once. */
268 const word_groups = Object.entries(word_maps).filter(
269 entry => entry[0] === this.canonize(entry[1].words[0]))
270 .map(entry => entry[1]);
272 /* Now, go through each word group and assign the scores out to
273 * the corresponding players.
275 * Note: We do this by going through the word groups, (as opposed
276 * to the list of words from the players again), specifically to
277 * avoid giving a player points for a wrod group twice (in the
278 * case where a player submits two different words that the group
279 * ends up judging as equivalent).
281 this.players.forEach(p => p.round_score = 0);
282 for (let group of word_groups) {
283 group.players.forEach(p => p.round_score += group.players.size);
286 const scores = this.players.map(p => {
293 scores.sort((a,b) => {
294 return b.score - a.score;
297 /* Put the word groups into a form the client can consume.
299 const words_submitted = word_groups.map(
302 word: group.words.join('/'),
303 players: Array.from(group.players).map(p => p.name)
308 words_submitted.sort((a,b) => {
309 return b.players.length - a.players.length;
312 /* Put this round's scores into the game state object so it will
313 * be sent to any new clients that join. */
314 this.state.scores = {
316 words: words_submitted
319 /* And broadcast the scores to all connected clients. */
320 this.broadcast_event_object('scores', this.state.scores);
324 Empathy.router = express.Router();
325 const router = Empathy.router;
328 constructor(id, items, prompt) {
331 this.prompt = prompt;
335 toggle_vote(player_name) {
336 if (this.votes.find(v => v === player_name))
337 this.votes = this.votes.filter(v => v !== player_name);
339 this.votes.push(player_name);
343 router.post('/prompts', (request, response) => {
344 const game = request.game;
346 const result = game.add_prompt(request.body.items, request.body.prompt);
348 response.json(result);
351 router.post('/vote/:prompt_id([0-9]+)', (request, response) => {
352 const game = request.game;
353 const prompt_id = parseInt(request.params.prompt_id, 10);
355 if (game.toggle_vote(prompt_id, request.session.id))
358 response.sendStatus(404);
361 router.post('/start/:prompt_id([0-9]+)', (request, response) => {
362 const game = request.game;
363 const prompt_id = parseInt(request.params.prompt_id, 10);
365 if (game.start(prompt_id))
368 response.sendStatus(404);
371 router.post('/answer/:prompt_id([0-9]+)', (request, response) => {
372 const game = request.game;
373 const prompt_id = parseInt(request.params.prompt_id, 10);
375 const result = game.receive_answer(prompt_id,
377 request.body.answers);
378 response.json(result);
380 if (game.state.players_answered >= game.players.length)
381 game.perform_judging();
384 router.post('/judging/:prompt_id([0-9]+)', (request, response) => {
385 const game = request.game;
386 const prompt_id = parseInt(request.params.prompt_id, 10);
388 const result = game.receive_judging(prompt_id,
390 request.body.word_groups);
391 response.json(result);
393 if (game.state.players_judged >= game.players.length)
394 game.compute_scores();
397 router.post('/reset', (request, response) => {
398 const game = request.game;
404 identifier: "empathy",
407 exports.Game = Empathy;