From d4cda1d79dd27aea818e1e35f30bcc3d745208a3 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Tue, 9 Jun 2020 19:41:23 -0700 Subject: [PATCH] Empathy: Add a new /answer route to accept answers from a user This is the bare minimum, simply storing the answers. Going forward we'll want to expand this to inform players when another player has submitted, etc. --- empathy.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/empathy.js b/empathy.js index d91225d..ca93b30 100644 --- a/empathy.js +++ b/empathy.js @@ -8,6 +8,7 @@ class Empathy extends Game { prompts: [], active_prompt: null }; + this.answers = []; this.next_prompt_id = 1; } @@ -54,6 +55,27 @@ class Empathy extends Game { return true; } + + receive_answer(prompt_id, session_id, answers) { + const player = this.players_by_session[session_id]; + if (! player) + return { valid: false, message: "Player not found" }; + + const prompt = this.state.prompts.find(p => p.id === prompt_id); + if (! prompt) + return { valid: false, message: "Prompt not found" }; + + if (prompt !== this.state.active_prompt) + return { valid: false, message: "Prompt no longer active" }; + + /* Save the complete answers for our own use later. */ + this.answers.push({ + player: player, + answers: answers + }); + + return { valid: true }; + } } Empathy.router = express.Router(); @@ -101,6 +123,16 @@ router.post('/start/:prompt_id([0-9]+)', (request, response) => { response.sendStatus(404); }); +router.post('/answer/:prompt_id([0-9]+)', (request, response) => { + const game = request.game; + const prompt_id = parseInt(request.params.prompt_id, 10); + + const result = game.receive_answer(prompt_id, + request.session.id, + request.body.answers); + response.json(result); +}); + Empathy.meta = { name: "Empathy", identifier: "empathy", -- 2.43.0