1 const express = require("express");
2 const Game = require("./game.js");
4 class Empathy extends Game {
10 this.next_prompt_id = 1;
13 add_prompt(items, prompt_string) {
14 const prompt = new Prompt(this.next_prompt_id, items, prompt_string);
15 this.next_prompt_id++;
17 this.state.prompts.push(prompt);
19 this.broadcast_event_object('prompt', prompt);
24 /* Returns true if vote toggled, false for player or prompt not found */
25 toggle_vote(prompt_id, session_id) {
26 const player = this.players_by_session[session_id];
28 const prompt = this.state.prompts.find(p => p.id === prompt_id);
29 if (! prompt || ! player)
32 prompt.toggle_vote(player.name);
34 this.broadcast_event_object('prompt', prompt);
40 Empathy.router = express.Router();
41 const router = Empathy.router;
44 constructor(id, items, prompt) {
51 toggle_vote(player_name) {
52 if (this.votes.find(v => v === player_name))
53 this.votes = this.votes.filter(v => v !== player_name);
55 this.votes.push(player_name);
59 router.post('/prompts', (request, response) => {
60 const game = request.game;
62 game.add_prompt(request.body.items, request.body.prompt);
65 router.post('/vote/:prompt_id([0-9]+)', (request, response) => {
66 const game = request.game;
67 const prompt_id = parseInt(request.params.prompt_id, 10);
69 if (game.toggle_vote(prompt_id, request.session.id))
70 response.sendStatus(200);
72 response.sendStatus(404);
77 identifier: "empathy",
80 exports.Game = Empathy;