Fix missing name when launching a game from Hall (TODO: understand why it fails somet...
authorBenjamin Auder <benjamin.auder@somewhere>
Sat, 4 Apr 2020 17:26:20 +0000 (19:26 +0200)
committerBenjamin Auder <benjamin.auder@somewhere>
Sat, 4 Apr 2020 17:26:20 +0000 (19:26 +0200)
client/src/utils/gameStorage.js
client/src/views/Game.vue
client/src/views/Hall.vue

index f69de2e..42f4388 100644 (file)
@@ -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
index 398a0a2..eefb337 100644 (file)
@@ -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)
index fd4bd2d..04ac6b8 100644 (file)
@@ -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(),