Files
imposter/game/game.js
2025-12-15 20:45:41 +01:00

64 lines
1.8 KiB
JavaScript

const parts = window.location.pathname.split("/").filter(Boolean);
const sessionId = parts[0] ?? null;
const username = parts[1] ?? null;
show_user()
async function show_user() {
const res = await fetch(`/api/user/${sessionId}`);
const users = await res.json();
//todo: der check ob es schon einen imposter gibt sollte auf dem Server stattfinden!
const allUsers = users.map((u) => u.state);
if (allUsers.every((v) => v === 0)) {
createImposter();
async function createImposter() {
const res = await fetch(`/api/${sessionId}/${username}/createImposter`);
getImposter();
}
}else {
getImposter();
}
}
async function getImposter() {
const res = await fetch(`/api/${sessionId}/${username}/getImposter`);
const stateObj = await res.json();
state = stateObj.state;
console.log("state=" + state)
const ImposterStateDiv = document.getElementById("imposterState");
if (state == 1) {
console.log("du bist imposter");
ImposterStateDiv.textContent = "Du bist der Imposter!";
getWords(true);
} else {
console.log("du bist kein imposter");
ImposterStateDiv.textContent = "Du bist kein Imposter!";
getWords(false);
}
}
async function getWords(imposterStatus) {
const res = await fetch(`/api/getWords`);
const words = await res.json();
console.log(words);
const WordDiv = document.getElementById("word");
if (res.status == 201) {
getWords();
} else if (imposterStatus) {
console.log(words.imposterWord);
WordDiv.textContent = "dein tip:" + words.imposterWord;
} else if (!imposterStatus) {
console.log(words.word);
WordDiv.textContent = "dein wort:" + words.word;
}
}
async function reset() {
const res = await fetch(`/api/reset/${sessionId}`);
if (res.status == 201) {
window.location.href = window.location.href = `/${sessionId}/${username}`;
}
}