FIX: Prevent multiplayer initialization before game is ready

- Add polling mechanism to wait for window.game initialization
- Disable multiplayer button until game is fully initialized
- Enable button in DOMContentLoaded after game creation
- Prevents 'Game instance not found' error on production

This fixes the critical issue where clicking Multiplayer too quickly
would fail because window.game wasn't ready yet.
This commit is contained in:
2025-12-14 12:07:13 +11:00
parent 8b275de519
commit bc76e7aab1
2 changed files with 18 additions and 3 deletions

View File

@@ -48,7 +48,7 @@
<button class="mode-btn active" id="localModeBtn" onclick="toggleGameMode('local')">
🎮 Local Play
</button>
<button class="mode-btn" id="multiplayerModeBtn" onclick="toggleGameMode('multiplayer')">
<button class="mode-btn" id="multiplayerModeBtn" onclick="toggleGameMode('multiplayer')" disabled>
🌐 Multiplayer
</button>
</div>
@@ -217,7 +217,16 @@
// Initialize multiplayer if not already
if (!window.multiplayerClient) {
if (!window.game) {
console.error("Game instance not found!");
console.log('⏳ Waiting for game to initialize...');
// Poll for game to be ready
const checkGame = setInterval(() => {
if (window.game) {
clearInterval(checkGame);
console.log('✅ Game ready, initializing multiplayer...');
window.multiplayerClient = new MultiplayerClient(window.game);
window.multiplayerClient.connect();
}
}, 100); // Check every 100ms
return;
}
window.multiplayerClient = new MultiplayerClient(window.game);