added reset button, various bug fixes
This commit is contained in:
32
backend.js
32
backend.js
@ -123,7 +123,8 @@ app.put("/api/ready/:sessionId", (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.get("/api/getWords", (req, res) => {
|
app.get("/api/getWords", (req, res) => {
|
||||||
db.get(`SELECT word, imposterWord FROM words WHERE inUse = 1`,
|
db.get(
|
||||||
|
`SELECT word, imposterWord FROM words WHERE inUse = 1`,
|
||||||
(err, rows) => {
|
(err, rows) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
@ -131,21 +132,28 @@ app.get("/api/getWords", (req, res) => {
|
|||||||
if (rows) {
|
if (rows) {
|
||||||
res.status(200).json(rows);
|
res.status(200).json(rows);
|
||||||
} else {
|
} else {
|
||||||
db.run(`UPDATE words SET inUse = 1 WHERE id = (SELECT id FROM words ORDER BY RANDOM() LIMIT 1)`)
|
db.run(
|
||||||
res.status(201).json({update: true});
|
`UPDATE words SET inUse = 1 WHERE id = (SELECT id FROM words ORDER BY RANDOM() LIMIT 1)`,
|
||||||
console.log("update")
|
);
|
||||||
|
res.status(201).json({ update: true });
|
||||||
|
console.log("update");
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
app.get("/api/reset/:sessionId", (req, res) => {
|
||||||
|
const { sessionId } = req.params;
|
||||||
|
|
||||||
|
db.run(`UPDATE "${sessionId}" SET state = 0 WHERE state = 1`);
|
||||||
|
db.run(`UPDATE "${sessionId}" SET ready = 0 WHERE ready = 1`);
|
||||||
|
db.run(`UPDATE words SET inUse = 0 WHERE inUse = 1`);
|
||||||
|
return res.status(201).json({ success: true });
|
||||||
|
});
|
||||||
|
|
||||||
app.get("/:sessionId/:username/game", (req, res) => {
|
app.get("/:sessionId/:username/game", (req, res) => {
|
||||||
res.sendFile(join(__dirname, "game", "game.html"));
|
res.sendFile(join(__dirname, "game", "game.html"));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
app.get("/:sessionId/:username", (req, res) => {
|
app.get("/:sessionId/:username", (req, res) => {
|
||||||
const { sessionId, username } = req.params;
|
const { sessionId, username } = req.params;
|
||||||
|
|
||||||
@ -186,9 +194,13 @@ app.get("/:sessionId/:username", (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
//beim neuladen der seite ready auf false setzen
|
//beim neuladen der seite ready auf false setzen
|
||||||
db.run(`UPDATE "${sessionId}" SET ready = false WHERE user = ?`, [username], () => {
|
db.run(
|
||||||
res.sendFile(join(__dirname, "session", "session.html"));
|
`UPDATE "${sessionId}" SET ready = false WHERE user = ?`,
|
||||||
});
|
[username],
|
||||||
|
() => {
|
||||||
|
res.sendFile(join(__dirname, "session", "session.html"));
|
||||||
|
},
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(3000, () => {
|
app.listen(3000, () => {
|
||||||
|
|||||||
@ -1,16 +1,16 @@
|
|||||||
<!DOCTYPE html>
|
<!doctype html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<base href="/game/">
|
<base href="/game/" />
|
||||||
<link rel="stylesheet" href="style.css">
|
<link rel="stylesheet" href="style.css" />
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>game</title>
|
<title>game</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="imposterState" class="imposterState"></div>
|
<div id="imposterState" class="imposterState"></div>
|
||||||
<div id="word" class="word"></div>
|
<div id="word" class="word"></div>
|
||||||
|
<button onclick="reset()" class="resetButton">Neues Spiel</button>
|
||||||
<script src="./game.js" ></script>
|
<script src="./game.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
52
game/game.js
52
game/game.js
@ -2,56 +2,68 @@ const parts = window.location.pathname.split("/").filter(Boolean);
|
|||||||
const sessionId = parts[0] ?? null;
|
const sessionId = parts[0] ?? null;
|
||||||
const username = parts[1] ?? null;
|
const username = parts[1] ?? null;
|
||||||
|
|
||||||
async function getState() {
|
show_user()
|
||||||
const res = await fetch(`/api/${sessionId}/${username}/state`);
|
|
||||||
const state = await res.json();
|
|
||||||
}
|
|
||||||
|
|
||||||
async function show_user() {
|
async function show_user() {
|
||||||
const res = await fetch(`/api/user/${sessionId}`);
|
const res = await fetch(`/api/user/${sessionId}`);
|
||||||
const users = await res.json();
|
const users = await res.json();
|
||||||
const allUsers = users.map((u) => u.state);
|
const allUsers = users.map((u) => u.state);
|
||||||
if (allUsers.every((v) => v === 0)) {
|
if (allUsers.every((v) => v === 0)) {
|
||||||
|
createImposter();
|
||||||
async function createImposter() {
|
async function createImposter() {
|
||||||
const res = await fetch(`/api/${sessionId}/${username}/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() {
|
async function getImposter() {
|
||||||
const res = await fetch(`/api/${sessionId}/${username}/getImposter`);
|
const res = await fetch(`/api/${sessionId}/${username}/getImposter`);
|
||||||
const stateObj = await res.json();
|
const stateObj = await res.json();
|
||||||
state = stateObj.state;
|
state = stateObj.state;
|
||||||
const ImposterStateDiv = document.getElementById('imposterState');
|
console.log("state=" + state)
|
||||||
|
const ImposterStateDiv = document.getElementById("imposterState");
|
||||||
|
|
||||||
if (state == 1) {
|
if (state == 1) {
|
||||||
console.log("du bist imposter");
|
console.log("du bist imposter");
|
||||||
ImposterStateDiv.textContent = 'Du bist der Imposter!'
|
ImposterStateDiv.textContent = "Du bist der Imposter!";
|
||||||
return true
|
return true;
|
||||||
} else {
|
} else {
|
||||||
console.log("du bist kein imposter");
|
console.log("du bist kein imposter");
|
||||||
ImposterStateDiv.textContent = 'Du bist kein Imposter!'
|
ImposterStateDiv.textContent = "Du bist kein Imposter!";
|
||||||
return false
|
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) {
|
async function getWords(imposterStatus) {
|
||||||
const res = await fetch(`/api/getWords`);
|
const res = await fetch(`/api/getWords`);
|
||||||
const words = await res.json();
|
const words = await res.json();
|
||||||
console.log(words);
|
console.log(words);
|
||||||
const WordDiv = document.getElementById('word');
|
const WordDiv = document.getElementById("word");
|
||||||
if (res.status == 201){
|
if (res.status == 201) {
|
||||||
getWords();
|
getWords();
|
||||||
} else if (imposterStatus) {
|
} else if (imposterStatus) {
|
||||||
console.log(words.imposterWord)
|
console.log(words.imposterWord);
|
||||||
WordDiv.textContent = "dein tip:" + words.imposterWord;
|
WordDiv.textContent = "dein tip:" + words.imposterWord;
|
||||||
}else if (!imposterStatus){
|
} else if (!imposterStatus) {
|
||||||
console.log(words.word)
|
console.log(words.word);
|
||||||
WordDiv.textContent = "dein wort:" + 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}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -22,4 +22,15 @@ body {
|
|||||||
margin: 5px auto;
|
margin: 5px auto;
|
||||||
font-size: 30px;
|
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;
|
||||||
|
|
||||||
}
|
}
|
||||||
BIN
imposter.db
BIN
imposter.db
Binary file not shown.
@ -10,7 +10,7 @@
|
|||||||
<body>
|
<body>
|
||||||
<div class="hello_username" id="hello_username"></div>
|
<div class="hello_username" id="hello_username"></div>
|
||||||
|
|
||||||
<button class="button" onclick="show_user()">show user</button>
|
|
||||||
|
|
||||||
<div id = "users"></div>
|
<div id = "users"></div>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user