From d4bf8b14e7407a96f9b035ad48cb31a2393a36ec Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Tue, 9 Jun 2020 08:06:37 -0700 Subject: [PATCH] Empathy: Add support for starting the actual game When a request is received at "/start" to start a particular prompt, the server responds by broadcasting a "start" event to all clients telling them to start the game. The prompt is also set as the active prompt in the game state so that if any new client joins while a prompt is active they will become aware of that. --- empathy.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/empathy.js b/empathy.js index 7a75b71..8ecf9e1 100644 --- a/empathy.js +++ b/empathy.js @@ -5,7 +5,8 @@ class Empathy extends Game { constructor(id) { super(id); this.state = { - prompts: [] + prompts: [], + active_prompt: null }; this.next_prompt_id = 1; } @@ -35,6 +36,19 @@ class Empathy extends Game { return true; } + + /* Returns true on success, false for prompt not found. */ + start(prompt_id) { + const prompt = this.state.prompts.find(p => p.id === prompt_id); + if (! prompt) + return false; + + this.state.active_prompt = prompt; + + this.broadcast_event_object('start', prompt); + + return true; + } } Empathy.router = express.Router(); @@ -72,6 +86,16 @@ router.post('/vote/:prompt_id([0-9]+)', (request, response) => { response.sendStatus(404); }); +router.post('/start/:prompt_id([[0-9]+)', (request, response) => { + const game = request.game; + const prompt_id = parseInt(request.params.prompt_id, 10); + + if (game.start(prompt_id)) + response.sendStatus(200); + else + response.sendStatus(404); +}); + Empathy.meta = { name: "Empathy", identifier: "empathy", -- 2.43.0