From 3d65195b5713c9e6f58b250069e685e4dca448c2 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Sat, 4 Apr 2020 19:26:20 +0200
Subject: [PATCH] Fix missing name when launching a game from Hall (TODO:
 understand why it fails sometimes)

---
 client/src/utils/gameStorage.js |  1 +
 client/src/views/Game.vue       | 40 +++++++++++++++++++++++++++++----
 client/src/views/Hall.vue       |  5 ++++-
 3 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/client/src/utils/gameStorage.js b/client/src/utils/gameStorage.js
index f69de2e2..42f43884 100644
--- a/client/src/utils/gameStorage.js
+++ b/client/src/utils/gameStorage.js
@@ -89,6 +89,7 @@ export const GameStorage = {
             else if (k == "chat") game.chats.push(obj[k]);
             else if (k == "chatRead") game.chatRead = Date.now();
             else if (k == "delchat") game.chats = [];
+            else if (k == "playerName") game.players[obj.idx] = obj.name;
             else game[k] = obj[k];
           });
           objectStore.put(game); //save updated data
diff --git a/client/src/views/Game.vue b/client/src/views/Game.vue
index 398a0a27..eefb3375 100644
--- a/client/src/views/Game.vue
+++ b/client/src/views/Game.vue
@@ -545,7 +545,8 @@ export default {
             if (sid != this.st.user.sid) {
               this.send("askidentity", { target: sid });
               this.people[sid] = { tmpIds: data.sockIds[sid] };
-            } else {
+            }
+            else {
               // Complete my tmpIds:
               Object.assign(this.people[sid].tmpIds, data.sockIds[sid]);
             }
@@ -611,6 +612,23 @@ export default {
           // player.tmpIds is already set
           player.name = user.name;
           player.id = user.id;
+          if (this.game.type == "live") {
+            const myGidx =
+              this.game.players.findIndex(p => p.sid == this.st.user.sid);
+            // Sometimes a player name isn't stored yet (TODO: why?)
+            if (
+              myGidx >= 0 &&
+              !this.game.players[1 - myGidx].name &&
+              this.game.players[1 - myGidx].sid == user.sid &&
+              !!user.name
+            ) {
+              this.game.players[1-myGidx].name = user.name;
+              GameStorage.update(
+                this.gameRef,
+                { playerName: { idx: 1 - myGidx, name: user.name } }
+              );
+            }
+          }
           this.$forceUpdate(); //TODO: shouldn't be required
           // If I multi-connect, kill current connexion if no mark (I'm older)
           if (this.newConnect[user.sid]) {
@@ -1042,6 +1060,19 @@ export default {
       const myIdx = game.players.findIndex(p => {
         return p.sid == this.st.user.sid || p.id == this.st.user.id;
       });
+      // Sometimes the name isn't stored yet (TODO: why?)
+      if (
+        myIdx >= 0 &&
+        gtype == "live" &&
+        !game.players[myIdx].name &&
+        !!this.st.user.name
+      ) {
+        game.players[myIdx].name = this.st.user.name;
+        GameStorage.update(
+          game.id,
+          { playerName: { idx: myIdx, name: this.st.user.name } }
+        );
+      }
       // "mycolor" is undefined for observers
       const mycolor = [undefined, "w", "b"][myIdx + 1];
       if (gtype == "corr") {
@@ -1067,9 +1098,10 @@ export default {
           game.clocks = [tc.mainTime, tc.mainTime];
           if (myIdx >= 0) {
             // I play in this live game
-            GameStorage.update(game.id, {
-              clocks: game.clocks
-            });
+            GameStorage.update(
+              game.id,
+              { clocks: game.clocks }
+            );
           }
         } else {
           if (!!game.initime)
diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue
index fd4bd2da..04ac6b84 100644
--- a/client/src/views/Hall.vue
+++ b/client/src/views/Hall.vue
@@ -1182,10 +1182,13 @@ export default {
     // NOTE: when launching game, the challenge is already being deleted
     launchGame: function(c) {
       // White player index 0, black player index 1:
-      const players =
+      let players =
         !!c.mycolor
           ? (c.mycolor == "w" ? [c.seat, c.from] : [c.from, c.seat])
           : shuffle([c.from, c.seat]);
+      players.forEach(p => {
+        if (!!p["tmpIds"]) delete p["tmpIds"];
+      });
       // These game informations will be shared
       let gameInfo = {
         id: getRandString(),
-- 
2.44.0