1 const express = require("express");
2 const Game = require("./game.js");
4 class Empathy extends Game {
14 this.next_prompt_id = 1;
19 /* Before closing out the current round, we accumulate that score
20 * for each player into their runnning total. */
21 for (let score of this.state.scores.scores) {
22 const player = this.players.find(p => p.name === score.player);
24 player.score += score.score;
26 player.score = score.score;
28 /* And broadcast that new score out. */
29 this.broadcast_event('player-update', player.info_json());
32 /* Now that we're done with the active prompt, we remove it from
33 * the list of prompts and also remove any prompts that received
34 * no votes. This keeps the list of prompts clean.
36 const active_id = this.state.active_prompt.id;
38 this.state.prompts.filter(
39 p => p.id !== active_id && p.votes.length > 0
42 this.state.active_prompt = null;
43 this.state.players_answered = 0;
44 this.state.scores = null;
48 this.broadcast_event_object('game-state', this.state);
51 add_prompt(items, prompt_string) {
52 const prompt = new Prompt(this.next_prompt_id, items, prompt_string);
53 this.next_prompt_id++;
55 this.state.prompts.push(prompt);
57 this.broadcast_event_object('prompt', prompt);
62 /* Returns true if vote toggled, false for player or prompt not found */
63 toggle_vote(prompt_id, session_id) {
64 const player = this.players_by_session[session_id];
66 const prompt = this.state.prompts.find(p => p.id === prompt_id);
67 if (! prompt || ! player)
70 prompt.toggle_vote(player.name);
72 this.broadcast_event_object('prompt', prompt);
77 /* Returns true on success, false for prompt not found. */
79 const prompt = this.state.prompts.find(p => p.id === prompt_id);
83 /* Ignore any start request that comes in while a prompt is
84 * already being played. */
85 if (this.state.active_prompt)
88 this.state.active_prompt = prompt;
90 this.broadcast_event_object('start', prompt);
95 receive_answer(prompt_id, session_id, answers) {
96 const player = this.players_by_session[session_id];
98 return { valid: false, message: "Player not found" };
100 const prompt = this.state.prompts.find(p => p.id === prompt_id);
102 return { valid: false, message: "Prompt not found" };
104 if (prompt !== this.state.active_prompt)
105 return { valid: false, message: "Prompt no longer active" };
107 /* Save the complete answers for our own use later. */
113 /* And notify players how many players have answered. */
114 this.state.players_answered++;
115 this.broadcast_event_object('answered', this.state.players_answered);
117 return { valid: true };
121 const word_submitters = {};
124 for (let a of this.answers) {
125 for (let word of a.answers) {
126 if (word_submitters[word])
127 word_submitters[word].push(a.player.name);
129 word_submitters[word] = [a.player.name];
133 for (let a of this.answers) {
135 for (let word of a.answers) {
136 score += word_submitters[word].length;
139 player: a.player.name,
144 scores.sort((a,b) => {
145 return b.score - a.score;
148 const word_submitters_arr = [];
149 for (let word in word_submitters)
150 word_submitters_arr.push({word: word, players: word_submitters[word]});
152 word_submitters_arr.sort((a,b) => {
153 return b.players.length - a.players.length;
156 /* Put this round's scores into the game state object so it will
157 * be sent to any new clients that join. */
158 this.state.scores = {
160 words: word_submitters_arr
163 /* And broadcast the scores to all connected clients. */
164 this.broadcast_event_object('scores', this.state.scores);
168 Empathy.router = express.Router();
169 const router = Empathy.router;
172 constructor(id, items, prompt) {
175 this.prompt = prompt;
179 toggle_vote(player_name) {
180 if (this.votes.find(v => v === player_name))
181 this.votes = this.votes.filter(v => v !== player_name);
183 this.votes.push(player_name);
187 router.post('/prompts', (request, response) => {
188 const game = request.game;
190 game.add_prompt(request.body.items, request.body.prompt);
193 router.post('/vote/:prompt_id([0-9]+)', (request, response) => {
194 const game = request.game;
195 const prompt_id = parseInt(request.params.prompt_id, 10);
197 if (game.toggle_vote(prompt_id, request.session.id))
198 response.sendStatus(200);
200 response.sendStatus(404);
203 router.post('/start/:prompt_id([0-9]+)', (request, response) => {
204 const game = request.game;
205 const prompt_id = parseInt(request.params.prompt_id, 10);
207 if (game.start(prompt_id))
208 response.sendStatus(200);
210 response.sendStatus(404);
213 router.post('/answer/:prompt_id([0-9]+)', (request, response) => {
214 const game = request.game;
215 const prompt_id = parseInt(request.params.prompt_id, 10);
217 const result = game.receive_answer(prompt_id,
219 request.body.answers);
220 response.json(result);
222 if (game.answers.length >= game.players.length)
223 game.compute_scores();
226 router.post('/reset', (request, response) => {
227 const game = request.game;
233 identifier: "empathy",
236 exports.Game = Empathy;