X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=empathy.js;h=759b486f8b58b1814299c1fe6863bb17109b2917;hb=d98c82ec9daca3155b03408d179e88d837093e91;hp=1da9b8ef07ce8fde9b221fcb65715ef11c00e9d4;hpb=507128bb32b242b5a61a180fcb352486b5116280;p=empires-server diff --git a/empathy.js b/empathy.js index 1da9b8e..759b486 100644 --- a/empathy.js +++ b/empathy.js @@ -1,6 +1,8 @@ const express = require("express"); const Game = require("./game.js"); +const MAX_PROMPT_ITEMS = 20; + class Empathy extends Game { constructor(id) { super(id); @@ -55,6 +57,12 @@ class Empathy extends Game { } add_prompt(items, prompt_string) { + if (items > MAX_PROMPT_ITEMS) + return { + valid: false, + message: `Maximum number of items is ${MAX_PROMPT_ITEMS}` + }; + const prompt = new Prompt(this.next_prompt_id, items, prompt_string); this.next_prompt_id++; @@ -62,7 +70,10 @@ class Empathy extends Game { this.broadcast_event_object('prompt', prompt); - return prompt; + return { + valid: true, + id: prompt.id + }; } /* Returns true if vote toggled, false for player or prompt not found */ @@ -133,7 +144,9 @@ class Empathy extends Game { } } - this.state.ambiguities = Object.values(word_map); + this.state.ambiguities = Object.values(word_map).sort((a,b) => { + return a.toLowerCase().localeCompare(b.toLowerCase()); + }); this.broadcast_event_object('ambiguities', this.state.ambiguities); } @@ -208,19 +221,21 @@ class Empathy extends Game { const word_maps = {}; for (let e of agreed_equivalencies) { - let group = word_maps[e.words[0]]; + const word0_canon = this.canonize(e.words[0]); + const word1_canon = this.canonize(e.words[1]); + let group = word_maps[word0_canon]; if (! group) - group = word_maps[e.words[1]]; + group = word_maps[word1_canon]; if (! group) group = { words: [], players: new Set()}; - if (! word_maps[e.words[0]]) { - word_maps[e.words[0]] = group; + if (! word_maps[word0_canon]) { + word_maps[word0_canon] = group; group.words.push(e.words[0]); } - if (! word_maps[e.words[1]]) { - word_maps[e.words[1]] = group; + if (! word_maps[word1_canon]) { + word_maps[word1_canon] = group; group.words.push(e.words[1]); } } @@ -229,13 +244,14 @@ class Empathy extends Game { * to the set corresponding to each word group. */ for (let a of this.answers) { for (let word of a.answers) { + const word_canon = this.canonize(word); /* If there's no group yet, this is a singleton word. */ - if (word_maps[word]) { - word_maps[word].players.add(a.player); + if (word_maps[word_canon]) { + word_maps[word_canon].players.add(a.player); } else { const group = { words: [word], players: new Set() }; group.players.add(a.player); - word_maps[word] = group; + word_maps[word_canon] = group; } } } @@ -326,9 +342,9 @@ class Prompt { router.post('/prompts', (request, response) => { const game = request.game; - prompt = game.add_prompt(request.body.items, request.body.prompt); + const result = game.add_prompt(request.body.items, request.body.prompt); - response.json({ id: prompt.id}); + response.json(result); }); router.post('/vote/:prompt_id([0-9]+)', (request, response) => {