1 const express = require("express");
2 const Game = require("./game.js");
4 class Empathy extends Game {
13 this.next_prompt_id = 1;
17 this.state.active_prompt = null;
18 this.state.players_answered = 0;
21 this.broadcast_event_object('game-state', this.state);
24 add_prompt(items, prompt_string) {
25 const prompt = new Prompt(this.next_prompt_id, items, prompt_string);
26 this.next_prompt_id++;
28 this.state.prompts.push(prompt);
30 this.broadcast_event_object('prompt', prompt);
35 /* Returns true if vote toggled, false for player or prompt not found */
36 toggle_vote(prompt_id, session_id) {
37 const player = this.players_by_session[session_id];
39 const prompt = this.state.prompts.find(p => p.id === prompt_id);
40 if (! prompt || ! player)
43 prompt.toggle_vote(player.name);
45 this.broadcast_event_object('prompt', prompt);
50 /* Returns true on success, false for prompt not found. */
52 const prompt = this.state.prompts.find(p => p.id === prompt_id);
56 /* Ignore any start request that comes in while a prompt is
57 * already being played. */
58 if (this.state.active_prompt)
61 this.state.active_prompt = prompt;
63 this.broadcast_event_object('start', prompt);
68 receive_answer(prompt_id, session_id, answers) {
69 const player = this.players_by_session[session_id];
71 return { valid: false, message: "Player not found" };
73 const prompt = this.state.prompts.find(p => p.id === prompt_id);
75 return { valid: false, message: "Prompt not found" };
77 if (prompt !== this.state.active_prompt)
78 return { valid: false, message: "Prompt no longer active" };
80 /* Save the complete answers for our own use later. */
86 /* And notify players how many players have answered. */
87 this.state.players_answered++;
88 this.broadcast_event_object('answered', this.state.players_answered);
90 return { valid: true };
94 const word_submitters = {};
97 for (let a of this.answers) {
98 for (let word of a.answers) {
99 if (word_submitters[word])
100 word_submitters[word].push(a.player.name);
102 word_submitters[word] = [a.player.name];
106 for (let a of this.answers) {
108 for (let word of a.answers) {
109 score += word_submitters[word].length;
112 player: a.player.name,
117 scores.sort((a,b) => {
118 return b.score - a.score;
121 const word_submitters_arr = [];
122 for (let word in word_submitters)
123 word_submitters_arr.push({word: word, players: word_submitters[word]});
125 word_submitters_arr.sort((a,b) => {
126 return b.players.length - a.players.length;
129 this.state.scores = {
131 words: word_submitters_arr
134 this.broadcast_event_object('scores', this.state.scores);
138 Empathy.router = express.Router();
139 const router = Empathy.router;
142 constructor(id, items, prompt) {
145 this.prompt = prompt;
149 toggle_vote(player_name) {
150 if (this.votes.find(v => v === player_name))
151 this.votes = this.votes.filter(v => v !== player_name);
153 this.votes.push(player_name);
157 router.post('/prompts', (request, response) => {
158 const game = request.game;
160 game.add_prompt(request.body.items, request.body.prompt);
163 router.post('/vote/:prompt_id([0-9]+)', (request, response) => {
164 const game = request.game;
165 const prompt_id = parseInt(request.params.prompt_id, 10);
167 if (game.toggle_vote(prompt_id, request.session.id))
168 response.sendStatus(200);
170 response.sendStatus(404);
173 router.post('/start/:prompt_id([0-9]+)', (request, response) => {
174 const game = request.game;
175 const prompt_id = parseInt(request.params.prompt_id, 10);
177 if (game.start(prompt_id))
178 response.sendStatus(200);
180 response.sendStatus(404);
183 router.post('/answer/:prompt_id([0-9]+)', (request, response) => {
184 const game = request.game;
185 const prompt_id = parseInt(request.params.prompt_id, 10);
187 const result = game.receive_answer(prompt_id,
189 request.body.answers);
190 response.json(result);
192 if (game.answers.length >= game.players.length)
193 game.compute_scores();
196 router.post('/reset', (request, response) => {
197 const game = request.game;
203 identifier: "empathy",
206 exports.Game = Empathy;