From 99eb3c3fd206ca16e950f1a07e6f189db9cfdaba Mon Sep 17 00:00:00 2001
From: Carl Worth <cworth@cworth.org>
Date: Mon, 29 Jun 2020 14:23:28 -0700
Subject: [PATCH] empathy: Don't let any player give kudos to themself

Which would not be very sporting of course, so we don't allow it.

Note that this restriction is applied after the majority-ruled
matching. So a player can't even give kudos to a word they didn't
submit and then hope to get matched into it.

It's also worth noting that the group can decide to split a group
where one player gave kudos. That will mean that both sides of the
split get kudos from the player, which seems to match the kudos-giving
player's intent.
---
 empathy.js | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/empathy.js b/empathy.js
index 01836df..d6b007b 100644
--- a/empathy.js
+++ b/empathy.js
@@ -348,8 +348,12 @@ class Empathy extends Game {
     for (let group of word_groups) {
 
       const words = group.words;
-      if (group.kudos)
-        this.kudos[player.name] = [...words];
+      if (group.kudos) {
+        this.kudos[player.name] = {
+          player: player,
+          words: [...words]
+        };
+      }
 
       for (let i = 0; i < words.length - 1; i++) {
         for (let j = i + 1; j < words.length; j++) {
@@ -526,12 +530,16 @@ class Empathy extends Game {
     /* Apply kudos from each player to the word maps, (using a set so
      * that no word_map can get multiple kudos from a single
      * player). */
-    for (let player of Object.keys(this.kudos)) {
-      for (let word of this.kudos[player]) {
+    for (let kudos of Object.values(this.kudos)) {
+      for (let word of kudos.words) {
         const word_canon = this.canonize(word);
         if (! word_maps[word_canon])
           continue;
-        word_maps[word_canon].kudos.add(player);
+        /* Don't let any player give kudos to a group where they
+         * submitted a word themself. That just wouldn't be right. */
+        if (! word_maps[word_canon].players.has(kudos.player)) {
+          word_maps[word_canon].kudos.add(kudos.player);
+        }
       }
     }
 
@@ -611,7 +619,7 @@ class Empathy extends Game {
         return {
           word: group.words.join('/'),
           players: Array.from(group.players).map(p => p.name),
-          kudos: Array.from(group.kudos)
+          kudos: Array.from(group.kudos).map(p => p.name)
         };
       }
     );
-- 
2.45.2