128 lines
No EOL
3.7 KiB
JavaScript
128 lines
No EOL
3.7 KiB
JavaScript
const nPlayerButton = document.querySelector("#newPlayerButton");
|
|
const playersList = document.querySelector("#playersList");
|
|
const startButton = document.querySelector("#startGame");
|
|
const logArea = document.querySelector("#gameLog");
|
|
const body = document.querySelector("body");
|
|
const hintCheckbox = document.querySelector("#hintCheck");
|
|
|
|
let stage = 0;
|
|
let currentPlayer = 0;
|
|
let impostor;
|
|
let allPlayers;
|
|
let currentName;
|
|
let secret;
|
|
let enableHint;
|
|
|
|
nPlayerButton.addEventListener('click', () => {
|
|
const holder = document.createElement("div");
|
|
const newPlayer = document.createElement("input");
|
|
const killButton = document.createElement("button");
|
|
holder.classList.add("playerHolder");
|
|
killButton.textContent = "X";
|
|
killButton.addEventListener('click', () => {
|
|
holder.remove();
|
|
});
|
|
holder.appendChild(newPlayer);
|
|
holder.appendChild(killButton);
|
|
playersList.appendChild(holder);
|
|
});
|
|
|
|
startButton.addEventListener('click', () => {
|
|
switch (stage) {
|
|
case 0:
|
|
allPlayers = Array.from(document.querySelectorAll("#playersList input"));
|
|
if (allPlayers.some(player => !player.value)) {
|
|
alert("Por favor, preencha o nome de todos os jogadores.");
|
|
return;
|
|
}
|
|
body.classList.add("game");
|
|
enableHint = hintCheckbox.checked;
|
|
clear("Prepare-se para iniciar o jogo.");
|
|
impostor = choose();
|
|
secret = getWordWithHint().then(result => {
|
|
secret = result;
|
|
});
|
|
stage = -1;
|
|
break;
|
|
|
|
case 1:
|
|
if (currentName == impostor) {
|
|
clear(currentName + " é impostor" + (enableHint ? (", sua dica é " + secret.hint + ".") : "."));
|
|
logClass('impostor');
|
|
} else {
|
|
clear(currentName + " é civil, a palavra é " + secret.word);
|
|
logClass('civil');
|
|
}
|
|
|
|
currentPlayer += 1;
|
|
if (currentPlayer == allPlayers.length
|
|
) {
|
|
stage += 1;
|
|
} else {
|
|
stage = -1;
|
|
}
|
|
break;
|
|
|
|
case -1:
|
|
currentName = allPlayers[currentPlayer].value;
|
|
clear(`
|
|
<p>Entregue o dispositivo para:<br><span class="playerName">${currentName}</span><br>para continuar.</p>
|
|
`);
|
|
stage = 1;
|
|
break;
|
|
|
|
case 2: {
|
|
clear("O jogo começou! Cada um deve falar uma palavra relacionada ao tema.\nProssiga após a votação.");
|
|
stage += 1;
|
|
break;
|
|
}
|
|
|
|
case 3: {
|
|
clear(`O impostor era <b>${impostor}</b>; a palavra era <b>${secret.word}</b>; a dica <b>${secret.hint}</b>.`);
|
|
stage += 1;
|
|
break;
|
|
}
|
|
|
|
default:
|
|
body.classList.remove("game");
|
|
stage = 0;
|
|
currentPlayer = 0;
|
|
break;
|
|
}
|
|
});
|
|
|
|
function choose() {
|
|
const randomIndex = Math.floor(Math.random() * allPlayers.length);
|
|
return allPlayers[randomIndex].value;
|
|
}
|
|
|
|
function log(text) {
|
|
logArea.innerHTML = logArea.innerHtml + "<br>" + text;
|
|
}
|
|
|
|
function clear(text) {
|
|
logArea.classList.forEach(item => {
|
|
logArea.classList.remove(item);
|
|
})
|
|
logArea.innerHTML = text;
|
|
}
|
|
|
|
function logClass(className) {
|
|
logArea.classList.toggle(className);
|
|
}
|
|
|
|
async function getWordWithHint() {
|
|
try {
|
|
const response = await fetch('words.json');
|
|
const data = await response.json();
|
|
const randomWordObj = data[Math.floor(Math.random() * data.length)];
|
|
const randomHint = randomWordObj.hints[Math.floor(Math.random() * randomWordObj.hints.length)];
|
|
|
|
return {
|
|
word: randomWordObj.word,
|
|
hint: randomHint
|
|
};
|
|
} catch (error) {
|
|
console.error('Error loading words:', error);
|
|
}
|
|
} |