diff --git a/play.pokemonshowdown.com/src/client-connection.ts b/play.pokemonshowdown.com/src/client-connection.ts index 503db7f15..2329b834c 100644 --- a/play.pokemonshowdown.com/src/client-connection.ts +++ b/play.pokemonshowdown.com/src/client-connection.ts @@ -207,6 +207,7 @@ export class PSConnection { static connect() { if (PS.connection?.socket) return; + if (PS.connection?.connected) return; // already connected via worker, don't call doAutojoin on live session PS.isOffline = false; if (!PS.connection) { PS.connection = new PSConnection(); diff --git a/play.pokemonshowdown.com/src/client-main.ts b/play.pokemonshowdown.com/src/client-main.ts index 503e281e7..eef27bfbf 100644 --- a/play.pokemonshowdown.com/src/client-main.ts +++ b/play.pokemonshowdown.com/src/client-main.ts @@ -614,6 +614,7 @@ class PSUser extends PSStreamModel { avatar = "lucas"; challstr = ''; loggingIn: string | null = null; + loginKey = 0; initializing = true; gapiLoaded = false; nameRegExp: RegExp | null = null; @@ -674,16 +675,19 @@ class PSUser extends PSStreamModel { this.update({ success: true }); return; } + const key = ++this.loginKey; this.loggingIn = name; this.update(null); PSLoginServer.rawQuery( 'getassertion', { userid, challstr: this.challstr } ).then(res => { + if (this.loginKey !== key) return; this.handleAssertion(name, res); this.updateRegExp(); }); } changeNameWithPassword(name: string, password: string, special: PSLoginState = { needsPassword: true }) { + const key = ++this.loginKey; this.loggingIn = name; if (!password && !special) { this.updateLogin({ @@ -696,6 +700,7 @@ class PSUser extends PSStreamModel { PSLoginServer.query( 'login', { name, pass: password, challstr: this.challstr } ).then(data => { + if (this.loginKey !== key) return; this.loggingIn = null; if (data?.curuser?.loggedin) { // success! @@ -1228,8 +1233,8 @@ export class PSRoom extends PSStreamModel implements RoomOptions { return this.errorReply(`You are already connected.`); } - if (!PS.isOffline) { - // connect to room + if (!PS.isOffline && PS.connection?.connected) { + // server connection is active; attempt room-level reconnection try { this.connect(); } catch (err: any) { diff --git a/play.pokemonshowdown.com/src/panel-chat.tsx b/play.pokemonshowdown.com/src/panel-chat.tsx index da82cac50..baeeb4fc1 100644 --- a/play.pokemonshowdown.com/src/panel-chat.tsx +++ b/play.pokemonshowdown.com/src/panel-chat.tsx @@ -177,11 +177,11 @@ export class ChatRoom extends PSRoom { } else { let lines = msg.split('\n'); - // cut off starting lines until we get to PS.lastMessage timestamp + // cut off starting lines until we get to this room's last message timestamp // then cut off roomintro from the end let cutOffStart = 0; let cutOffEnd = lines.length; - const cutOffTime = parseInt(PS.lastMessageTime); + const cutOffTime = (this.lastMessage?.[0] === 'c:') ? parseInt(this.lastMessage[1]) : 0; const cutOffExactLine = this.lastMessage ? '|' + this.lastMessage?.join('|') : ''; let reconnectMessage = '|raw|
You reconnected.
'; for (let i = 0; i < lines.length; i++) { @@ -192,7 +192,7 @@ export class ChatRoom extends PSRoom { cutOffStart = i + 1; } else if (lines[i].startsWith(`|c:|`)) { const time = parseInt(lines[i].split('|')[2] || ''); - if (time < cutOffTime) cutOffStart = i; + if (time < cutOffTime) cutOffStart = i + 1; } if (lines[i].startsWith('|raw|
You joined ')) { reconnectMessage = `|raw|
You reconnected to ${lines[i].slice(38)}`; @@ -200,14 +200,6 @@ export class ChatRoom extends PSRoom { if (!lines[i - 1]) cutOffEnd = i - 1; } } - console.log("Reconnection log splice:"); - console.log([ - ...lines.slice(0, cutOffStart), - '====================', - ...lines.slice(cutOffStart, cutOffEnd), - '====================', - ...lines.slice(cutOffEnd), - ].join('\n')); lines = lines.slice(cutOffStart, cutOffEnd); if (lines.length) { diff --git a/play.pokemonshowdown.com/src/panel-mainmenu.tsx b/play.pokemonshowdown.com/src/panel-mainmenu.tsx index 52a87e816..23bd51c5e 100644 --- a/play.pokemonshowdown.com/src/panel-mainmenu.tsx +++ b/play.pokemonshowdown.com/src/panel-mainmenu.tsx @@ -125,9 +125,12 @@ export class MainMenuRoom extends PSRoom { case 'challstr': { const [, challstr] = args; PS.user.challstr = challstr; + PS.user.loginKey++; // stale callbacks check this key and early-return if it no longer matches + const upkeepKey = PS.user.loginKey; PSLoginServer.query( 'upkeep', { challstr } ).then(res => { + if (PS.user.loginKey !== upkeepKey) return; if (!res?.username) { PS.user.initializing = false; return; @@ -137,13 +140,15 @@ export class MainMenuRoom extends PSRoom { if (res.loggedin) { PS.user.registered = { name: res.username, userid: toID(res.username) }; } - PS.user.handleAssertion(res.username, res.assertion); + // preserve current nick on reconnect rather than reverting to registered name + const nameToUse = PS.user.named ? PS.user.name : res.username; + PS.user.handleAssertion(nameToUse, res.assertion); }); return; } case 'updateuser': { const [, fullName, namedCode, avatar] = args; const named = namedCode === '1'; - if (named) PS.user.initializing = false; + PS.user.initializing = false; PS.user.setName(fullName, named, avatar); PS.teams.loadRemoteTeams(); return;