From 50bd11841587246cbedecf19097cfe5c4e5e163c Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Sun, 12 Apr 2020 17:31:30 +0200
Subject: [PATCH] Fix chat colors when anonymous, and games receiving

---
 client/src/components/Chat.vue | 33 +++++++++++++++++++++++++++------
 client/src/views/Hall.vue      | 20 +++++++++++++++-----
 2 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/client/src/components/Chat.vue b/client/src/components/Chat.vue
index a31f8d0b..25898cec 100644
--- a/client/src/components/Chat.vue
+++ b/client/src/components/Chat.vue
@@ -9,7 +9,7 @@ div
   )
   button(@click="sendChat()") {{ st.tr["Send"] }}
   p(v-for="chat in chats.concat(pastChats)")
-    span.name {{ chat.name }} :&nbsp;
+    span.name {{ chat.name || "@nonymous" }} :&nbsp;
     span(
       :class="classObject(chat)"
       v-html="chat.msg"
@@ -31,11 +31,28 @@ export default {
   methods: {
     classObject: function(chat) {
       return {
-        "my-chatmsg": chat.name == this.st.user.name,
+        "my-chatmsg": (
+          !!chat.name && chat.name == this.st.user.name ||
+          !!chat.sid && chat.sid == this.st.user.sid
+        ),
         "opp-chatmsg":
           !!this.players &&
           this.players.some(
-            p => p.name == chat.name && p.name != this.st.user.name
+            p => {
+              return (
+                (
+                  !!p.name &&
+                  p.name == chat.name &&
+                  p.name != this.st.user.name
+                )
+                ||
+                (
+                  !!p.sid &&
+                  p.sid == chat.sid &&
+                  p.sid != this.st.user.sid
+                )
+              );
+            }
           )
       };
     },
@@ -45,13 +62,17 @@ export default {
       chatInput.focus(); //required on smartphones
       if (chatTxt == "") return; //nothing to send
       chatInput.value = "";
-      const chat = { msg: chatTxt, name: this.st.user.name || "@nonymous" };
+      const chat = {
+        msg: chatTxt,
+        name: this.st.user.name,
+        // SID is required only for anonymous users (in live games)
+        sid: this.st.user.id  == 0 ? this.st.user.sid : null
+      };
       this.$emit("mychat", chat);
       this.chats.unshift(chat);
     },
     newChat: function(chat) {
-      if (chat.msg != "")
-        this.chats.unshift({ msg: chat.msg, name: chat.name || "@nonymous" });
+      if (chat.msg != "") this.chats.unshift(chat);
     },
     clearHistory: function() {
       this.chats = [];
diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue
index ee8d53e5..411407f6 100644
--- a/client/src/views/Hall.vue
+++ b/client/src/views/Hall.vue
@@ -820,9 +820,12 @@ export default {
           // Ignore games where I play (will go in MyGames page),
           // and also games that I already received.
           if (
-            game.players.every(p =>
-              p.sid != this.st.user.sid && p.id != this.st.user.id) &&
-            this.games.findIndex(g => g.id == game.id) == -1
+            game.players.every(p => {
+              return (
+                p.sid != this.st.user.sid &&
+                (p.id == 0 || p.id != this.st.user.id)
+              );
+            }) && this.games.findIndex(g => g.id == game.id) == -1
           ) {
             let newGame = game;
             newGame.type = this.classifyObject(game);
@@ -1014,7 +1017,11 @@ export default {
       let chall = Object.assign({}, this.newchallenge);
       // Add only if not already issued (not counting target or FEN):
       if (this.challenges.some(c =>
-        (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id) &&
+        (
+          c.from.sid == this.st.user.sid ||
+          (c.from.id > 0 && c.from.id == this.st.user.id)
+        )
+        &&
         c.vid == chall.vid &&
         c.cadence == chall.cadence &&
         c.randomness == chall.randomness
@@ -1036,7 +1043,10 @@ export default {
           const c = this.challenges[i];
           if (
             c.type == ctype &&
-            (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id)
+            (
+              c.from.sid == this.st.user.sid ||
+              (c.from.id > 0 && c.from.id == this.st.user.id)
+            )
           ) {
             countMyChalls++;
             if (c.added < oldestAdded) {
-- 
2.44.0