1 const express = require("express");
2 const Game = require("./game.js");
4 class Empathy extends Game {
14 this.next_prompt_id = 1;
18 /* Now that we're done with the active prompt, we remove it from
19 * the list of prompts and also remove any prompts that received
20 * no votes. This keeps the list of prompts clean.
22 const active_id = this.state.active_prompt.id;
24 this.state.prompts.filter(
25 p => p.id !== active_id && p.votes.length > 0
28 this.state.active_prompt = null;
29 this.state.players_answered = 0;
30 this.state.scores = null;
34 this.broadcast_event_object('game-state', this.state);
37 add_prompt(items, prompt_string) {
38 const prompt = new Prompt(this.next_prompt_id, items, prompt_string);
39 this.next_prompt_id++;
41 this.state.prompts.push(prompt);
43 this.broadcast_event_object('prompt', prompt);
48 /* Returns true if vote toggled, false for player or prompt not found */
49 toggle_vote(prompt_id, session_id) {
50 const player = this.players_by_session[session_id];
52 const prompt = this.state.prompts.find(p => p.id === prompt_id);
53 if (! prompt || ! player)
56 prompt.toggle_vote(player.name);
58 this.broadcast_event_object('prompt', prompt);
63 /* Returns true on success, false for prompt not found. */
65 const prompt = this.state.prompts.find(p => p.id === prompt_id);
69 /* Ignore any start request that comes in while a prompt is
70 * already being played. */
71 if (this.state.active_prompt)
74 this.state.active_prompt = prompt;
76 this.broadcast_event_object('start', prompt);
81 receive_answer(prompt_id, session_id, answers) {
82 const player = this.players_by_session[session_id];
84 return { valid: false, message: "Player not found" };
86 const prompt = this.state.prompts.find(p => p.id === prompt_id);
88 return { valid: false, message: "Prompt not found" };
90 if (prompt !== this.state.active_prompt)
91 return { valid: false, message: "Prompt no longer active" };
93 /* Save the complete answers for our own use later. */
99 /* And notify players how many players have answered. */
100 this.state.players_answered++;
101 this.broadcast_event_object('answered', this.state.players_answered);
103 return { valid: true };
107 const word_submitters = {};
110 for (let a of this.answers) {
111 for (let word of a.answers) {
112 if (word_submitters[word])
113 word_submitters[word].push(a.player.name);
115 word_submitters[word] = [a.player.name];
119 for (let a of this.answers) {
121 for (let word of a.answers) {
122 score += word_submitters[word].length;
125 player: a.player.name,
130 scores.sort((a,b) => {
131 return b.score - a.score;
134 const word_submitters_arr = [];
135 for (let word in word_submitters)
136 word_submitters_arr.push({word: word, players: word_submitters[word]});
138 word_submitters_arr.sort((a,b) => {
139 return b.players.length - a.players.length;
142 this.state.scores = {
144 words: word_submitters_arr
147 this.broadcast_event_object('scores', this.state.scores);
151 Empathy.router = express.Router();
152 const router = Empathy.router;
155 constructor(id, items, prompt) {
158 this.prompt = prompt;
162 toggle_vote(player_name) {
163 if (this.votes.find(v => v === player_name))
164 this.votes = this.votes.filter(v => v !== player_name);
166 this.votes.push(player_name);
170 router.post('/prompts', (request, response) => {
171 const game = request.game;
173 game.add_prompt(request.body.items, request.body.prompt);
176 router.post('/vote/:prompt_id([0-9]+)', (request, response) => {
177 const game = request.game;
178 const prompt_id = parseInt(request.params.prompt_id, 10);
180 if (game.toggle_vote(prompt_id, request.session.id))
181 response.sendStatus(200);
183 response.sendStatus(404);
186 router.post('/start/:prompt_id([0-9]+)', (request, response) => {
187 const game = request.game;
188 const prompt_id = parseInt(request.params.prompt_id, 10);
190 if (game.start(prompt_id))
191 response.sendStatus(200);
193 response.sendStatus(404);
196 router.post('/answer/:prompt_id([0-9]+)', (request, response) => {
197 const game = request.game;
198 const prompt_id = parseInt(request.params.prompt_id, 10);
200 const result = game.receive_answer(prompt_id,
202 request.body.answers);
203 response.json(result);
205 if (game.answers.length >= game.players.length)
206 game.compute_scores();
209 router.post('/reset', (request, response) => {
210 const game = request.game;
216 identifier: "empathy",
219 exports.Game = Empathy;