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 this.state.active_prompt = prompt;
48 this.broadcast_event_object('start', prompt);
54 Empathy.router = express.Router();
55 const router = Empathy.router;
58 constructor(id, items, prompt) {
65 toggle_vote(player_name) {
66 if (this.votes.find(v => v === player_name))
67 this.votes = this.votes.filter(v => v !== player_name);
69 this.votes.push(player_name);
73 router.post('/prompts', (request, response) => {
74 const game = request.game;
76 game.add_prompt(request.body.items, request.body.prompt);
79 router.post('/vote/:prompt_id([0-9]+)', (request, response) => {
80 const game = request.game;
81 const prompt_id = parseInt(request.params.prompt_id, 10);
83 if (game.toggle_vote(prompt_id, request.session.id))
84 response.sendStatus(200);
86 response.sendStatus(404);
89 router.post('/start/:prompt_id([[0-9]+)', (request, response) => {
90 const game = request.game;
91 const prompt_id = parseInt(request.params.prompt_id, 10);
93 if (game.start(prompt_id))
94 response.sendStatus(200);
96 response.sendStatus(404);
101 identifier: "empathy",
104 exports.Game = Empathy;