From 2690233f317b87e7336b0404ae711837e726d5cf Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Thu, 11 Jun 2020 07:22:29 -0700 Subject: [PATCH] Add a game phase to perform judging This is after all answers are submitted, and before scores are computed. The list of unique words are sent to each player so that they can group them into sets of equivalent words. The server isn't yet _doing_ anything with the word groups that are submitted from judging, but that will be the next step. --- empathy.js | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/empathy.js b/empathy.js index b12c3e7..3b613e6 100644 --- a/empathy.js +++ b/empathy.js @@ -8,6 +8,8 @@ class Empathy extends Game { prompts: [], active_prompt: null, players_answered: 0, + ambiguities: null, + players_judged: 0, scores: null }; this.answers = []; @@ -41,6 +43,8 @@ class Empathy extends Game { this.state.active_prompt = null; this.state.players_answered = 0; + this.state.ambiguities = 0; + this.state.players_judged = 0; this.state.scores = null; this.answers = []; @@ -117,6 +121,33 @@ class Empathy extends Game { return { valid: true }; } + perform_judging() { + const word_map = {}; + + for (let a of this.answers) { + for (let word of a.answers) { + const key = this.canonize(word); + word_map[key] = word; + } + } + + this.state.ambiguities = Object.values(word_map); + + this.broadcast_event_object('ambiguities', this.state.ambiguities); + } + + receive_judging() { + /* And notify players how many players have completed judging. */ + this.state.players_judged++; + this.broadcast_event_object('judged', this.state.players_judged); + + return { valid: true }; + } + + canonize(word) { + return word.replace(/[^a-zA-Z0-9]/g, '').toLowerCase(); + } + compute_scores() { const word_submitters = {}; const scores = []; @@ -219,7 +250,20 @@ router.post('/answer/:prompt_id([0-9]+)', (request, response) => { request.body.answers); response.json(result); - if (game.answers.length >= game.players.length) + if (game.state.players_answered >= game.players.length) + game.perform_judging(); +}); + +router.post('/judging/:prompt_id([0-9]+)', (request, response) => { + const game = request.game; + const prompt_id = parseInt(request.params.prompt_id, 10); + + const result = game.receive_judging(prompt_id, + request.session.id, + request.body); + response.json(result); + + if (game.state.players_judged >= game.players.length) game.compute_scores(); }); -- 2.43.0