From: Carl Worth Date: Sat, 27 Jun 2020 23:30:23 +0000 (-0700) Subject: empathy: Add a /new-game endpoint for a majority-rules decision to start again X-Git-Url: https://git.cworth.org/git?p=lmno-server;a=commitdiff_plain;h=471ad8aebe1ce9a933b68cfadaffe0f68a01c255 empathy: Add a /new-game endpoint for a majority-rules decision to start again This should be a lot friendlier than a single player clicking "new game" and other players saying, "wait! I was still looking at that". --- diff --git a/empathy.js b/empathy.js index 3e32a8b..fdf62a1 100644 --- a/empathy.js +++ b/empathy.js @@ -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();