]> git.cworth.org Git - empires-server/blobdiff - empathy.js
Empathy: Add support for starting the actual game
[empires-server] / empathy.js
index 7a75b71ecdd12a071945cdb89c0a8530cf38ce94..8ecf9e10fc9bb26e2a2068a65dfb87a420138b15 100644 (file)
@@ -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",