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,
128 /* Now that we've computed the score for a player for this
129 * round, also accumulate that score into the players runnning
132 a.player.score += score;
134 a.player.score = score;
136 /* And broadcast that new score out. */
137 this.broadcast_event('player-update', a.player.info_json());
140 scores.sort((a,b) => {
141 return b.score - a.score;
144 const word_submitters_arr = [];
145 for (let word in word_submitters)
146 word_submitters_arr.push({word: word, players: word_submitters[word]});
148 word_submitters_arr.sort((a,b) => {
149 return b.players.length - a.players.length;
152 /* Put this round's scores into the game state object so it will
153 * be sent to any new clients that join. */
154 this.state.scores = {
156 words: word_submitters_arr
159 /* And broadcast the scores to all connected clients. */
160 this.broadcast_event_object('scores', this.state.scores);
164 Empathy.router = express.Router();
165 const router = Empathy.router;
168 constructor(id, items, prompt) {
171 this.prompt = prompt;
175 toggle_vote(player_name) {
176 if (this.votes.find(v => v === player_name))
177 this.votes = this.votes.filter(v => v !== player_name);
179 this.votes.push(player_name);
183 router.post('/prompts', (request, response) => {
184 const game = request.game;
186 game.add_prompt(request.body.items, request.body.prompt);
189 router.post('/vote/:prompt_id([0-9]+)', (request, response) => {
190 const game = request.game;
191 const prompt_id = parseInt(request.params.prompt_id, 10);
193 if (game.toggle_vote(prompt_id, request.session.id))
194 response.sendStatus(200);
196 response.sendStatus(404);
199 router.post('/start/:prompt_id([0-9]+)', (request, response) => {
200 const game = request.game;
201 const prompt_id = parseInt(request.params.prompt_id, 10);
203 if (game.start(prompt_id))
204 response.sendStatus(200);
206 response.sendStatus(404);
209 router.post('/answer/:prompt_id([0-9]+)', (request, response) => {
210 const game = request.game;
211 const prompt_id = parseInt(request.params.prompt_id, 10);
213 const result = game.receive_answer(prompt_id,
215 request.body.answers);
216 response.json(result);
218 if (game.answers.length >= game.players.length)
219 game.compute_scores();
222 router.post('/reset', (request, response) => {
223 const game = request.game;
229 identifier: "empathy",
232 exports.Game = Empathy;