]> git.cworth.org Git - lmno-server/commitdiff
empathy: Add a /new-game endpoint for a majority-rules decision to start again
authorCarl Worth <cworth@cworth.org>
Sat, 27 Jun 2020 23:30:23 +0000 (16:30 -0700)
committerCarl Worth <cworth@cworth.org>
Sat, 27 Jun 2020 23:30:23 +0000 (16:30 -0700)
This should be a lot friendlier than a single player clicking "new
game" and other players saying, "wait! I was still looking at that".

empathy.js

index 3e32a8bc294e31268c4fa3a0bbd9a802e308db5a..fdf62a18d41abb42e1a41741ee2625e313b9faf0 100644 (file)
@@ -36,7 +36,8 @@ class Empathy extends Game {
       players_judging: new Set(),
       judging_idle: false,
       end_judging: new Set(),
-      scores: null
+      scores: null,
+      new_game_votes: new Set()
     };
     this.answers = [];
     this.answering_idle_timer = 0;
@@ -83,6 +84,7 @@ class Empathy extends Game {
     this.state.judging_idle = false;
     this.state.end_judging = new Set();
     this.state.scores = null;
+    this.state.new_game_votes = new Set();
 
     this.answers = [];
     if (this.answering_idle_timer) {
@@ -401,6 +403,25 @@ class Empathy extends Game {
     return true;
   }
 
+  /* Returns true if vote toggled, false for player or prompt not found */
+  toggle_new_game(prompt_id, session_id) {
+    const player = this.players_by_session[session_id];
+
+    const prompt = this.state.prompts.find(p => p.id === prompt_id);
+    if (! prompt || ! player)
+      return false;
+
+    if (this.state.new_game_votes.has(player.name)) {
+      this.state.new_game_votes.delete(player.name);
+      this.broadcast_event_object('unvote-new-game', player.name);
+    } else {
+      this.state.new_game_votes.add(player.name);
+      this.broadcast_event_object('vote-new-game', player.name);
+    }
+
+    return true;
+  }
+
   canonize(word) {
     return word.trim().toLowerCase();
   }
@@ -645,6 +666,19 @@ router.post('/end-judging/:prompt_id([0-9]+)', (request, response) => {
     game.compute_scores();
 });
 
+router.post('/new-game/:prompt_id([0-9]+)', (request, response) => {
+  const game = request.game;
+  const prompt_id = parseInt(request.params.prompt_id, 10);
+
+  if (game.toggle_new_game(prompt_id, request.session.id))
+    response.send('');
+  else
+    response.sendStatus(404);
+
+  if (game.state.new_game_votes.size > (game.state.players_answered.length / 2))
+    game.reset();
+});
+
 router.post('/reset', (request, response) => {
   const game = request.game;
   game.reset();