]> git.cworth.org Git - empires-server/commitdiff
Empathy: Add a new /answer route to accept answers from a user
authorCarl Worth <cworth@cworth.org>
Wed, 10 Jun 2020 02:41:23 +0000 (19:41 -0700)
committerCarl Worth <cworth@cworth.org>
Wed, 10 Jun 2020 02:41:23 +0000 (19:41 -0700)
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

index d91225d7e49e7da9a8e8f7a850d420e007931bff..ca93b30f8be5343197a7877c002fa7181cfb2a02 100644 (file)
@@ -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",