1 const express = require("express");
2 const Game = require("./game.js");
4 class Empathy extends Game {
11 this.next_prompt_id = 1;
14 add_prompt(items, prompt_string) {
15 const prompt = new Prompt(this.next_prompt_id, items, prompt_string);
16 this.next_prompt_id++;
18 this.state.prompts.push(prompt);
20 this.broadcast_event_object('prompt', prompt);
25 /* Returns true if vote toggled, false for player or prompt not found */
26 toggle_vote(prompt_id, session_id) {
27 const player = this.players_by_session[session_id];
29 const prompt = this.state.prompts.find(p => p.id === prompt_id);
30 if (! prompt || ! player)
33 prompt.toggle_vote(player.name);
35 this.broadcast_event_object('prompt', prompt);
40 /* Returns true on success, false for prompt not found. */
42 const prompt = this.state.prompts.find(p => p.id === prompt_id);
46 /* Ignore any start request that comes in while a prompt is
47 * already being played. */
48 if (this.state.active_prompt)
51 this.state.active_prompt = prompt;
53 this.broadcast_event_object('start', prompt);
59 Empathy.router = express.Router();
60 const router = Empathy.router;
63 constructor(id, items, prompt) {
70 toggle_vote(player_name) {
71 if (this.votes.find(v => v === player_name))
72 this.votes = this.votes.filter(v => v !== player_name);
74 this.votes.push(player_name);
78 router.post('/prompts', (request, response) => {
79 const game = request.game;
81 game.add_prompt(request.body.items, request.body.prompt);
84 router.post('/vote/:prompt_id([0-9]+)', (request, response) => {
85 const game = request.game;
86 const prompt_id = parseInt(request.params.prompt_id, 10);
88 if (game.toggle_vote(prompt_id, request.session.id))
89 response.sendStatus(200);
91 response.sendStatus(404);
94 router.post('/start/:prompt_id([[0-9]+)', (request, response) => {
95 const game = request.game;
96 const prompt_id = parseInt(request.params.prompt_id, 10);
98 if (game.start(prompt_id))
99 response.sendStatus(200);
101 response.sendStatus(404);
106 identifier: "empathy",
109 exports.Game = Empathy;