added reset button, various bug fixes

This commit is contained in:
adrian
2025-12-11 22:19:48 +01:00
parent da965065ef
commit f8db2e7366
6 changed files with 81 additions and 46 deletions

View File

@ -1,16 +1,16 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<base href="/game/">
<link rel="stylesheet" href="style.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>game</title>
</head>
<body>
<div id="imposterState" class="imposterState"></div>
<div id="word" class="word"></div>
<script src="./game.js" ></script>
</body>
</html>
<head>
<base href="/game/" />
<link rel="stylesheet" href="style.css" />
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>game</title>
</head>
<body>
<div id="imposterState" class="imposterState"></div>
<div id="word" class="word"></div>
<button onclick="reset()" class="resetButton">Neues Spiel</button>
<script src="./game.js"></script>
</body>
</html>

View File

@ -2,56 +2,68 @@ const parts = window.location.pathname.split("/").filter(Boolean);
const sessionId = parts[0] ?? null;
const username = parts[1] ?? null;
async function getState() {
const res = await fetch(`/api/${sessionId}/${username}/state`);
const state = await res.json();
}
show_user()
async function show_user() {
const res = await fetch(`/api/user/${sessionId}`);
const users = await res.json();
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`);
var imposterStatus = getImposter(); //var für globale variable, muss in dieser funktion sein wegen komischer race condition bug
}
}else {
var imposterStatus = getImposter(); //var für globale variable, muss in dieser funktion sein wegen komischer race condition bug
}
}
const imposterStatus = getImposter();
//const imposterStatus = getImposter();
async function getImposter() {
const res = await fetch(`/api/${sessionId}/${username}/getImposter`);
const stateObj = await res.json();
state = stateObj.state;
const ImposterStateDiv = document.getElementById('imposterState');
console.log("state=" + state)
const ImposterStateDiv = document.getElementById("imposterState");
if (state == 1) {
console.log("du bist imposter");
ImposterStateDiv.textContent = 'Du bist der Imposter!'
return true
ImposterStateDiv.textContent = "Du bist der Imposter!";
return true;
} else {
console.log("du bist kein imposter");
ImposterStateDiv.textContent = 'Du bist kein Imposter!'
return false
ImposterStateDiv.textContent = "Du bist kein Imposter!";
return false;
}
}
getWords(imposterStatus)
// Initialisierung: await getImposter(), dann getWords aufrufen
async function init() {
const imposterStatus = await getImposter();
await getWords(imposterStatus);
}
init();
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){
const WordDiv = document.getElementById("word");
if (res.status == 201) {
getWords();
} else if (imposterStatus) {
console.log(words.imposterWord)
console.log(words.imposterWord);
WordDiv.textContent = "dein tip:" + words.imposterWord;
}else if (!imposterStatus){
console.log(words.word)
} 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}`;
}
}

View File

@ -22,4 +22,15 @@ body {
margin: 5px auto;
font-size: 30px;
}
.resetButton {
display: flex;
justify-content: center;
background-color: white;
border-radius: 5px;
border-width: 4px;
max-width: fit-content;
margin: 60px auto;
font-size: 30px;
}