1 const express = require("express");
2 const Game = require("./game.js");
4 class Empathy extends Game {
14 this.next_prompt_id = 1;
18 this.state.active_prompt = null;
19 this.state.players_answered = 0;
20 this.state.scores = null;
24 this.broadcast_event_object('game-state', this.state);
27 add_prompt(items, prompt_string) {
28 const prompt = new Prompt(this.next_prompt_id, items, prompt_string);
29 this.next_prompt_id++;
31 this.state.prompts.push(prompt);
33 this.broadcast_event_object('prompt', prompt);
38 /* Returns true if vote toggled, false for player or prompt not found */
39 toggle_vote(prompt_id, session_id) {
40 const player = this.players_by_session[session_id];
42 const prompt = this.state.prompts.find(p => p.id === prompt_id);
43 if (! prompt || ! player)
46 prompt.toggle_vote(player.name);
48 this.broadcast_event_object('prompt', prompt);
53 /* Returns true on success, false for prompt not found. */
55 const prompt = this.state.prompts.find(p => p.id === prompt_id);
59 /* Ignore any start request that comes in while a prompt is
60 * already being played. */
61 if (this.state.active_prompt)
64 this.state.active_prompt = prompt;
66 this.broadcast_event_object('start', prompt);
71 receive_answer(prompt_id, session_id, answers) {
72 const player = this.players_by_session[session_id];
74 return { valid: false, message: "Player not found" };
76 const prompt = this.state.prompts.find(p => p.id === prompt_id);
78 return { valid: false, message: "Prompt not found" };
80 if (prompt !== this.state.active_prompt)
81 return { valid: false, message: "Prompt no longer active" };
83 /* Save the complete answers for our own use later. */
89 /* And notify players how many players have answered. */
90 this.state.players_answered++;
91 this.broadcast_event_object('answered', this.state.players_answered);
93 return { valid: true };
97 const word_submitters = {};
100 for (let a of this.answers) {
101 for (let word of a.answers) {
102 if (word_submitters[word])
103 word_submitters[word].push(a.player.name);
105 word_submitters[word] = [a.player.name];
109 for (let a of this.answers) {
111 for (let word of a.answers) {
112 score += word_submitters[word].length;
115 player: a.player.name,
120 scores.sort((a,b) => {
121 return b.score - a.score;
124 const word_submitters_arr = [];
125 for (let word in word_submitters)
126 word_submitters_arr.push({word: word, players: word_submitters[word]});
128 word_submitters_arr.sort((a,b) => {
129 return b.players.length - a.players.length;
132 this.state.scores = {
134 words: word_submitters_arr
137 this.broadcast_event_object('scores', this.state.scores);
141 Empathy.router = express.Router();
142 const router = Empathy.router;
145 constructor(id, items, prompt) {
148 this.prompt = prompt;
152 toggle_vote(player_name) {
153 if (this.votes.find(v => v === player_name))
154 this.votes = this.votes.filter(v => v !== player_name);
156 this.votes.push(player_name);
160 router.post('/prompts', (request, response) => {
161 const game = request.game;
163 game.add_prompt(request.body.items, request.body.prompt);
166 router.post('/vote/:prompt_id([0-9]+)', (request, response) => {
167 const game = request.game;
168 const prompt_id = parseInt(request.params.prompt_id, 10);
170 if (game.toggle_vote(prompt_id, request.session.id))
171 response.sendStatus(200);
173 response.sendStatus(404);
176 router.post('/start/:prompt_id([0-9]+)', (request, response) => {
177 const game = request.game;
178 const prompt_id = parseInt(request.params.prompt_id, 10);
180 if (game.start(prompt_id))
181 response.sendStatus(200);
183 response.sendStatus(404);
186 router.post('/answer/:prompt_id([0-9]+)', (request, response) => {
187 const game = request.game;
188 const prompt_id = parseInt(request.params.prompt_id, 10);
190 const result = game.receive_answer(prompt_id,
192 request.body.answers);
193 response.json(result);
195 if (game.answers.length >= game.players.length)
196 game.compute_scores();
199 router.post('/reset', (request, response) => {
200 const game = request.game;
206 identifier: "empathy",
209 exports.Game = Empathy;