Skip to content

Commit 76ff901

Browse files
committed
fix: Resolve race condition in user stats recording and update navigation button behavior
- Updated UserStatsService to use newElo if available, preventing stale ELO reads during ranked duels. - Modified Game class to pass newElo directly to user stats to ensure accurate recording. - Changed navigation button in UserProfilePage to use window.location.href for improved reliability.
1 parent e173eb9 commit 76ff901

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

components/utils/userStatsService.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ class UserStatsService {
1818
return null;
1919
}
2020

21+
// Use provided newElo if available (for ranked duels where setElo may not have completed yet)
22+
// This avoids race condition where we read stale ELO from DB before setElo writes complete
23+
const eloToRecord = gameData.newElo ?? user.elo ?? 1000;
24+
2125
// Get current rankings
2226
const xpRank = await this.calculateXPRank(user.totalXp);
23-
const eloRankResult = await this.calculateELORank(user.elo || 1000);
27+
const eloRankResult = await this.calculateELORank(eloToRecord);
2428
const eloRank = typeof eloRankResult === 'object' ? eloRankResult.rank : eloRankResult;
2529

2630
// Record the stats snapshot
@@ -29,7 +33,7 @@ class UserStatsService {
2933
timestamp: new Date(),
3034
totalXp: user.totalXp || 0,
3135
xpRank: xpRank,
32-
elo: user.elo || 1000,
36+
elo: eloToRecord,
3337
eloRank: eloRank,
3438
triggerEvent: gameData?.triggerEvent || 'game_completed',
3539
gameId: gameId

pages/user.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ export default function UserProfilePage() {
188188
<div className="back-button-container">
189189
<button
190190
className="back-to-wg-button"
191-
onClick={() => router.push('/')}
191+
// onClick={() => router.push('/')}
192+
// use window.location.href
193+
onClick={() => window.location.href = '/'}
192194
>
193195
← Go to WorldGuessr
194196
</button>

ws/classes/Game.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,8 @@ export default class Game {
11911191
opponent: this.accountIds.p2,
11921192
eloChange: p1NewElo ? (p1NewElo - p1OldElo) : 0,
11931193
finalScore: player1Data?.score || 0,
1194-
duration: this.endTime - this.startTime
1194+
duration: this.endTime - this.startTime,
1195+
newElo: p1NewElo // Pass new ELO directly to avoid race condition with setElo
11951196
}
11961197
);
11971198
console.log(`Created userstats for player 1: ${this.accountIds.p1}`);
@@ -1208,7 +1209,8 @@ export default class Game {
12081209
opponent: this.accountIds.p1,
12091210
eloChange: p2NewElo ? (p2NewElo - p2OldElo) : 0,
12101211
finalScore: player2Data?.score || 0,
1211-
duration: this.endTime - this.startTime
1212+
duration: this.endTime - this.startTime,
1213+
newElo: p2NewElo // Pass new ELO directly to avoid race condition with setElo
12121214
}
12131215
);
12141216
console.log(`Created userstats for player 2: ${this.accountIds.p2}`);

0 commit comments

Comments
 (0)