Start Logik

This commit is contained in:
2025-04-02 15:25:43 +02:00
parent c9f43c537e
commit 74ba1730f5
3 changed files with 42 additions and 17 deletions

View File

@@ -9,17 +9,44 @@ class Game{
this.gameManager = gameManager;
this.code = code;
this.waitingSeconds = 5;
this.gameStart = false;
/**@type {Array<SocketUser>} */
this.players = []
// TODO: 5 Seconds after initialization, check if game has 2 players. if not stop game
setTimeout(() => { this.waitingForPlayers(true) }, 100)
}
setInterval(() => {
this.io.to(`game-${this.code}`).emit("waitForStart", this.players.length);
this.io.to(`game-${this.code}`).emit("randomTest", Math.floor(Math.random() * 1000) + 1);
}, 100);
waitingForPlayers(changeTime = false){
if(this.waitingSeconds === 0){
if(this.players.length < 2) {
this.io.to(`game-${this.code}`).emit("gameEnd", "Das Spiel ist zu Ende, da nicht genug Spieler beigetreten sind!");
this.gameManager.games.delete(this.code);
return;
}
this.gameStart = true;
return this.io.to(`game-${this.code}`).emit("gameStart");
}
let msg = "";
this.players.forEach(player => {
if(msg === ""){
msg = `Schon beigetreten sind: ${player.username}`;
}
else{
msg += ` und ${player.username}, es geht Sofort los!`;
}
});
this.io.to(`game-${this.code}`).emit("waitingForPlayers", {
msg: msg,
waitingSeconds: this.waitingSeconds
});
if (changeTime) this.waitingSeconds--;
setTimeout(() => { this.waitingForPlayers(true) }, 1000)
}
@@ -28,6 +55,8 @@ class Game{
addUser(user){
if(this.players.length >= 2) return 1;
this.waitingForPlayers();
this.players.push(user);
}

View File

@@ -9,8 +9,8 @@
<link rel="icon" type="image/png" href="../assets/Logo.png">
</head>
<body>
<h1 id="pc"></h1>
<h1 id="rd"></h1>
<h1 id="wM"></h1>
<h1 id="wS"></h1>
</body>
<script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>

View File

@@ -5,20 +5,16 @@ class ServerConnectionManager {
this.basicSetup();
this.socket.on("waitForStart", (data) => { this.waitForStart(data) });
this.socket.on("randomTest", (data) => { this.randomTest(data) });
// Socket.on Handler und Routen
this.socket.on("waitingForPlayers", (msg) => {
const { msg: message, waitingSeconds } = msg;
document.getElementById("wM").innerText = message;
document.getElementById("wS").innerText = waitingSeconds;
});
this.socket.on("gameEnd", (msg) => { this.gameEnd(msg) });
}
waitForStart(data) {
document.getElementById("pc").innerText = data;
}
randomTest(data) {
document.getElementById("rd").innerText = data;
}
gameEnd(msg){
this.socket.disconnect();
confirm(msg)