From: Carl Worth Date: Wed, 10 Jun 2020 02:41:23 +0000 (-0700) Subject: Empathy: Add a new /answer route to accept answers from a user X-Git-Url: https://git.cworth.org/git?p=empires-server;a=commitdiff_plain;h=d4cda1d79dd27aea818e1e35f30bcc3d745208a3 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. --- 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",