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 = {};
201 /* Perform a (non-strict) majority ruling on equivalencies,
202 * dropping all that didn't get enough votes. */
203 const quorum = Math.floor((this.players.length + 1)/2);
204 const agreed_equivalencies = Object.values(this.equivalencies).filter(
205 eq => eq.count >= quorum);
207 /* And with that agreed set of equivalencies, construct the full
209 const word_groups = {};
211 for (let e of agreed_equivalencies) {
212 let group = word_groups[e.words[0]];
214 group = word_groups[e.words[1]];
216 group = { words: [], players: new Set()};
218 if (! word_groups[e.words[0]]) {
219 word_groups[e.words[0]] = group;
220 group.words.push(e.words[0]);
223 if (! word_groups[e.words[1]]) {
224 word_groups[e.words[1]] = group;
225 group.words.push(e.words[1]);
229 /* Now go through answers from each player and add the player name
230 * to the set corresponding to each word group. */
231 for (let a of this.answers) {
232 for (let word of a.answers) {
233 /* If there's no group yet, this is a singleton word. */
234 if (word_groups[word]) {
235 word_groups[word].players.add(a.player.name);
237 const group = { words: [word], players: new Set() };
238 group.players.add(a.player.name);
239 word_groups[word] = group;
244 /* Finally, go through the answers one more time, this time taking
245 * the length of the player set as a score for the player's
246 * submission of that word. */
247 for (let a of this.answers) {
249 for (let word of a.answers) {
250 score += word_groups[word].players.size;
253 player: a.player.name,
258 scores.sort((a,b) => {
259 return b.score - a.score;
262 /* Put the word groups into a form the client can consume.
264 * Most significantly, we only want one entry for each group (even
265 * though our current "word_groups" object has a property for each
266 * word considered equivalent).
268 const words_submitted = Object.entries(word_groups).filter(
269 group => group[0] === group[1].words[0]).map(
272 word: group[1].words.join('/'),
273 players: Array.from(group[1].players)
278 words_submitted.sort((a,b) => {
279 return b.players.length - a.players.length;
282 /* Put this round's scores into the game state object so it will
283 * be sent to any new clients that join. */
284 this.state.scores = {
286 words: words_submitted
289 /* And broadcast the scores to all connected clients. */
290 this.broadcast_event_object('scores', this.state.scores);
294 Empathy.router = express.Router();
295 const router = Empathy.router;
298 constructor(id, items, prompt) {
301 this.prompt = prompt;
305 toggle_vote(player_name) {
306 if (this.votes.find(v => v === player_name))
307 this.votes = this.votes.filter(v => v !== player_name);
309 this.votes.push(player_name);
313 router.post('/prompts', (request, response) => {
314 const game = request.game;
316 prompt = game.add_prompt(request.body.items, request.body.prompt);
318 response.json({ id: prompt.id});
321 router.post('/vote/:prompt_id([0-9]+)', (request, response) => {
322 const game = request.game;
323 const prompt_id = parseInt(request.params.prompt_id, 10);
325 if (game.toggle_vote(prompt_id, request.session.id))
328 response.sendStatus(404);
331 router.post('/start/:prompt_id([0-9]+)', (request, response) => {
332 const game = request.game;
333 const prompt_id = parseInt(request.params.prompt_id, 10);
335 if (game.start(prompt_id))
338 response.sendStatus(404);
341 router.post('/answer/:prompt_id([0-9]+)', (request, response) => {
342 const game = request.game;
343 const prompt_id = parseInt(request.params.prompt_id, 10);
345 const result = game.receive_answer(prompt_id,
347 request.body.answers);
348 response.json(result);
350 if (game.state.players_answered >= game.players.length)
351 game.perform_judging();
354 router.post('/judging/: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_judging(prompt_id,
361 response.json(result);
363 if (game.state.players_judged >= game.players.length)
364 game.compute_scores();
367 router.post('/reset', (request, response) => {
368 const game = request.game;
374 identifier: "empathy",
377 exports.Game = Empathy;