Commit 4704435b authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix: chess reconnection loads server FEN + opponent profile fetched from match data

Problem 1: Reconnection reset board to starting position
- engine.create() always starts fresh
- FIX: if params.recovered, fetch match from server → engine.load(current_fen)
- Also restores clock times + determines whose turn from FEN

Problem 2: Opponent name stuck on 'جاري التحميل'
- params.opponentId was undefined (not passed on reconnect or matchmaking)
- FIX: if no opponentId in params, fetch match → get white/black_player_id → find opponent
- Uses fetchAndRenderOpponent() helper to update avatar/name/level

Problem 3: Other player can't see moves after reconnect
- This was because board reset to start position
- Now board loads current_fen from server → correct position displayed
- Polling continues from correct move_count
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 5893f13a
......@@ -35,6 +35,23 @@ export function mountGame(el, params) {
engine.create();
clock = new ChessClock(tc.time, tc.increment);
// If recovering from a refresh, fetch current state from server
if (params.recovered && matchId) {
net.post('game.php', { action: 'get', match_id: matchId }).then(data => {
if (data && !data.error && data.current_fen) {
engine.load(data.current_fen);
board?.setPosition(data.current_fen);
gameState.moveCount = data.move_count || 0;
if (data.white_time_remaining_ms) clock.white = data.white_time_remaining_ms;
if (data.black_time_remaining_ms) clock.black = data.black_time_remaining_ms;
// Determine whose turn from FEN
const turnFromFen = data.current_fen.split(' ')[1];
gameState.isPlayerTurn = turnFromFen === playerColor;
if (!gameState.isPlayerTurn) clock.start(turnFromFen);
}
}).catch(() => {});
}
el.innerHTML = `
<div class="chess-layout" style="display:flex;flex-direction:column;height:100%;background:#1a1a2e;">
<!-- Opponent Bar -->
......@@ -214,20 +231,19 @@ export function mountGame(el, params) {
}
// Fetch and render opponent profile bar (photo, name, level)
const opponentId = params.opponentId || (playerColor === 'w' ? params.blackPlayerId : params.whitePlayerId);
if (opponentId) {
mp.fetchOpponentProfile(opponentId).then(opp => {
if (opp && !opp.error) {
const nameEl = el.querySelector('#opponent-name');
const levelEl = el.querySelector('#opponent-level');
const avatarEl = el.querySelector('#opponent-avatar');
if (nameEl) nameEl.textContent = opp.display_name || opp.username || 'خصم';
if (levelEl) levelEl.textContent = `Lv.${opp.level || 1}`;
if (avatarEl && opp.avatar_url) {
avatarEl.innerHTML = `<img src="${opp.avatar_url}" style="width:100%;height:100%;object-fit:cover;border-radius:50%;">`;
}
let opponentId = params.opponentId;
// If no opponentId in params, get it from the match data
if (!opponentId && matchId) {
net.post('game.php', { action: 'get', match_id: matchId }).then(matchData => {
if (matchData && !matchData.error) {
const myId = store.get('auth.userId');
const oppId = matchData.white_player_id === myId ? matchData.black_player_id : matchData.white_player_id;
if (oppId) fetchAndRenderOpponent(el, oppId);
}
});
}).catch(() => {});
}
if (opponentId) {
fetchAndRenderOpponent(el, opponentId);
}
mp.startDisconnectWatch(matchId, 'chess', 60000);
......@@ -583,6 +599,21 @@ function stopLivePolling() {
}
// ===== END LIVE MULTIPLAYER =====
function fetchAndRenderOpponent(el, oppId) {
mp.fetchOpponentProfile(oppId).then(opp => {
if (opp && !opp.error) {
const nameEl = el.querySelector('#opponent-name');
const levelEl = el.querySelector('#opponent-level');
const avatarEl = el.querySelector('#opponent-avatar');
if (nameEl) nameEl.textContent = opp.display_name || opp.username || 'خصم';
if (levelEl) levelEl.textContent = `Lv.${opp.level || 1}`;
if (avatarEl && opp.avatar_url) {
avatarEl.innerHTML = `<img src="${opp.avatar_url}" style="width:100%;height:100%;object-fit:cover;border-radius:50%;">`;
}
}
}).catch(() => {});
}
function endGame(result, reason) {
if (gameState.gameOver) return;
stopLivePolling();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment