Commit 59d32ebf authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix: move all game init inside DOMContentLoaded to fix local mode

External scripts load after inline code; LudoConstants was referenced
before its script tag was parsed in local mode.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 1dcfdbc8
...@@ -29,7 +29,7 @@ $extraJs = ['/public/js/ludo-constants.js', '/public/js/ludo-ui.js', '/public/js ...@@ -29,7 +29,7 @@ $extraJs = ['/public/js/ludo-constants.js', '/public/js/ludo-ui.js', '/public/js
</div> </div>
<script> <script>
(function() { document.addEventListener('DOMContentLoaded', function() {
var params = new URLSearchParams(window.location.search); var params = new URLSearchParams(window.location.search);
var mode = params.get('mode') || 'local'; var mode = params.get('mode') || 'local';
var playerCount = parseInt(params.get('players') || '4', 10); var playerCount = parseInt(params.get('players') || '4', 10);
...@@ -56,52 +56,49 @@ $extraJs = ['/public/js/ludo-constants.js', '/public/js/ludo-ui.js', '/public/js ...@@ -56,52 +56,49 @@ $extraJs = ['/public/js/ludo-constants.js', '/public/js/ludo-ui.js', '/public/js
activePlayers = allPlayers.slice(0, playerCount); activePlayers = allPlayers.slice(0, playerCount);
bots = {}; bots = {};
playerNames = {}; playerNames = {};
activePlayers.forEach(function(p, idx) { activePlayers.forEach(function(p) {
playerNames[p] = LudoConstants.PLAYER_LABELS[p]; playerNames[p] = LudoConstants.PLAYER_LABELS[p];
}); });
} }
document.addEventListener('DOMContentLoaded', function() { LudoUI.renderBoard('#ludo-board');
LudoUI.renderBoard('#ludo-board');
var isMobile = window.innerWidth < 768; var isMobile = window.innerWidth < 768;
var diceContainer = isMobile ? '#ludo-dice-container-mobile' : '#ludo-dice-container'; var diceContainer = isMobile ? '#ludo-dice-container-mobile' : '#ludo-dice-container';
var turnEl = isMobile ? document.getElementById('ludo-turn-mobile') : document.getElementById('ludo-turn'); var turnEl = isMobile ? document.getElementById('ludo-turn-mobile') : document.getElementById('ludo-turn');
LudoUI.renderDice(diceContainer); LudoUI.renderDice(diceContainer);
LudoUI.setTurnElement(turnEl); LudoUI.setTurnElement(turnEl);
LudoUI.setLogElement(document.getElementById('ludo-log')); LudoUI.setLogElement(document.getElementById('ludo-log'));
LudoUI.renderPlayerCards( LudoUI.renderPlayerCards(
document.getElementById('ludo-top-players'), document.getElementById('ludo-top-players'),
document.getElementById('ludo-bottom-players'), document.getElementById('ludo-bottom-players'),
activePlayers, activePlayers,
playerNames playerNames
); );
LudoGame.init({ LudoGame.init({
players: activePlayers, players: activePlayers,
mode: mode, mode: mode,
bots: bots, bots: bots,
difficulty: difficulty, difficulty: difficulty,
playerNames: playerNames, playerNames: playerNames,
onGameEnd: function(winners) { onGameEnd: function(winners) {
var playAgainBtn = document.getElementById('ludo-play-again'); var playAgainBtn = document.getElementById('ludo-play-again');
if (playAgainBtn) { if (playAgainBtn) {
playAgainBtn.addEventListener('click', function() { playAgainBtn.addEventListener('click', function() {
LudoGame.restart({ LudoGame.restart({
players: activePlayers, players: activePlayers,
mode: mode, mode: mode,
bots: bots, bots: bots,
difficulty: difficulty, difficulty: difficulty,
playerNames: playerNames, playerNames: playerNames
onGameEnd: arguments.callee
});
}); });
} });
} }
}); }
}); });
})(); });
</script> </script>
<?php require __DIR__ . '/../includes/footer.php'; ?> <?php require __DIR__ . '/../includes/footer.php'; ?>
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