]> git.cworth.org Git - empires-server/blobdiff - empathy.js
Force all submitted judging words to lowercase before mapping
[empires-server] / empathy.js
index 25d8f77b975e86280a66c393c2587ed9c5c86941..cfe0618d76f211a0e3ceeb69b017c3c90a187100 100644 (file)
@@ -144,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);
   }
@@ -180,11 +182,13 @@ class Empathy extends Game {
             eq = [group[j], group[i]];
           }
 
-          const exist = this.equivalencies[`${eq[0]}:${eq[1]}`];
+          const key=`${this.canonize(eq[0])}:${this.canonize(eq[1])}`;
+
+          const exist = this.equivalencies[key];
           if (exist) {
             exist.count++;
           } else {
-            this.equivalencies[`${eq[0]}:${eq[1]}`] = {
+            this.equivalencies[key] = {
               count: 1,
               words: eq
             };
@@ -219,19 +223,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]);
       }
     }
@@ -240,13 +246,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;
         }
       }
     }
@@ -261,7 +268,8 @@ class Empathy extends Game {
      * multiple times). In contrast, iterating over"word_groups" will
      * have you visit each group only once. */
     const word_groups = Object.entries(word_maps).filter(
-      entry => entry[0] === entry[1].words[0]).map(entry => entry[1]);
+      entry => entry[0] === this.canonize(entry[1].words[0]))
+          .map(entry => entry[1]);
 
     /* Now, go through each word group and assign the scores out to
      * the corresponding players.