Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 2, 2025

When a user plays offline and then authenticates, the in-progress game was being reset to a fresh board.

Changes

Track game state via ref to avoid callback churn

  • Added currentGameRef synced via useEffect
  • Prevents load callback recreation while maintaining access to current game state
  • Uses match state directly (no ref needed since it's null in offline mode)

Preserve game on offline→online transition

  • Detect transition: authUserUid && !match
  • Skip reset if game has progress: status is Moving/GameOver or board differs from default
  • Upload preserved state to Firebase when creating new match

Example scenario:

// User plays offline, rolls dice, makes moves
// currentGame.status === Status.Moving
// match === null

// User logs in → onAuthStateChanged fires
// load() called with authUserUid
// shouldPreserveGame === true
// setGame(newGame()) skipped, Firebase updated with currentGame

The preservation logic is scoped to only the offline→online transition; other scenarios (logout, friend switching, initial login) follow existing reset behavior.

Original prompt

preserve offline game state when the user logs in


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Add functionality to preserve offline game state on login Preserve offline game state when user logs in Nov 2, 2025
Copilot AI requested a review from ProLoser November 2, 2025 00:57
Copilot finished work on behalf of ProLoser November 2, 2025 00:57
@ProLoser
Copy link
Owner

ProLoser commented Nov 2, 2025

@copilot are you sure you need to store the match object? I'm pretty sure that's not used in offline games

Copy link
Contributor Author

Copilot AI commented Nov 2, 2025

@copilot are you sure you need to store the match object? I'm pretty sure that's not used in offline games

You're right! The match ref wasn't needed since match is always null in offline games. I've simplified the code to use the match state directly. See commit 8333438.

Copilot finished work on behalf of ProLoser November 2, 2025 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants