Initial commit
This commit is contained in:
90
script.js
Normal file
90
script.js
Normal file
@@ -0,0 +1,90 @@
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const rollButton = document.getElementById('roll-button');
|
||||
const assassinateButton = document.getElementById('assassinate-button');
|
||||
const newGameButton = document.getElementById('new-game-button');
|
||||
const themeSwitchButton = document.getElementById('theme-switch');
|
||||
const flamesScore = document.getElementById('flames');
|
||||
const desolationScore = document.getElementById('desolation');
|
||||
const relocationScore = document.getElementById('relocation');
|
||||
const logList = document.getElementById('log-list');
|
||||
|
||||
const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||
const savedTheme = localStorage.getItem('theme');
|
||||
|
||||
if (savedTheme) {
|
||||
document.body.setAttribute('data-theme', savedTheme);
|
||||
} else if (prefersDark) {
|
||||
document.body.setAttribute('data-theme', 'dark');
|
||||
}
|
||||
|
||||
themeSwitchButton.addEventListener('click', () => {
|
||||
const currentTheme = document.body.getAttribute('data-theme');
|
||||
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
|
||||
document.body.setAttribute('data-theme', newTheme);
|
||||
localStorage.setItem('theme', newTheme);
|
||||
});
|
||||
|
||||
rollButton.addEventListener('click', () => {
|
||||
fetch('game.php?action=roll_dice')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
updateScores(data.scores);
|
||||
logEvent(data.event);
|
||||
checkGameState(data.gameState);
|
||||
});
|
||||
});
|
||||
|
||||
assassinateButton.addEventListener('click', () => {
|
||||
fetch('game.php?action=assassinate')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
logEvent(data.event);
|
||||
checkGameState(data.gameState);
|
||||
});
|
||||
});
|
||||
|
||||
newGameButton.addEventListener('click', () => {
|
||||
fetch('game.php?action=reset_game')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
updateScores(data.scores);
|
||||
logList.innerHTML = '';
|
||||
rollButton.disabled = false;
|
||||
assassinateButton.disabled = false;
|
||||
newGameButton.style.display = 'none';
|
||||
});
|
||||
});
|
||||
|
||||
function updateScores(scores) {
|
||||
flamesScore.textContent = scores.flames;
|
||||
desolationScore.textContent = scores.desolation;
|
||||
relocationScore.textContent = scores.relocation;
|
||||
}
|
||||
|
||||
function logEvent(event) {
|
||||
const li = document.createElement('li');
|
||||
li.textContent = event;
|
||||
logList.prepend(li);
|
||||
}
|
||||
|
||||
function checkGameState(gameState) {
|
||||
if (gameState !== 'ongoing') {
|
||||
rollButton.disabled = true;
|
||||
assassinateButton.disabled = true;
|
||||
newGameButton.style.display = 'block';
|
||||
let message = '';
|
||||
if (gameState === 'win') {
|
||||
message = 'You have escaped the city! You win!';
|
||||
} else if (gameState === 'loss_flames') {
|
||||
message = 'You were burned alive. You lose.';
|
||||
} else if (gameState === 'loss_desolation') {
|
||||
message = 'You were killed in the rampage. You lose.';
|
||||
} else if (gameState === 'win_assassination') {
|
||||
message = 'You have assassinated the emperor! You die a martyr, but you win!';
|
||||
} else if (gameState === 'loss_assassination') {
|
||||
message = 'Your attempt to assassinate the emperor has failed. You are executed. You lose.';
|
||||
}
|
||||
setTimeout(() => alert(message), 100);
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user