Füge Countdown-Logik für den Spielstart hinzu und verbessere die Benutzerverwaltung in Lobbys
This commit is contained in:
@@ -8,15 +8,22 @@ class Lobby {
|
||||
this.code = code;
|
||||
/** @type {Array<SocketUser>} */
|
||||
this.users = [];
|
||||
|
||||
this.gameStart = false;
|
||||
this.currentSecond = 5;
|
||||
}
|
||||
|
||||
addUser(user) {
|
||||
if (this.users.length >= 2) return 1;
|
||||
|
||||
this.users.push(user);
|
||||
|
||||
this.sendLobbyUserUpdate();
|
||||
|
||||
if(this.users.length === 2) {
|
||||
console.log("Starte Spiel");
|
||||
setTimeout(this.startGame.bind(this), 2000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -45,6 +52,49 @@ class Lobby {
|
||||
|
||||
this.io.to(`lobby-${this.code}`).emit("lobbyUserUpdate", data);
|
||||
}
|
||||
|
||||
startGame() {
|
||||
this.users.forEach(user => {
|
||||
user.socket.on("disconnect", this.interruptStart.bind(this));
|
||||
});
|
||||
|
||||
this.gameStart = true;
|
||||
|
||||
setTimeout(this.sendStartGameUpdate.bind(this), 1000);
|
||||
}
|
||||
|
||||
sendStartGameUpdate() {
|
||||
if(!this.gameStart) return;
|
||||
|
||||
const data = {
|
||||
code: this.code,
|
||||
needRedirect: this.currentSecond <= 0,
|
||||
currentSecond: this.currentSecond
|
||||
}
|
||||
|
||||
this.io.to(`lobby-${this.code}`).emit("startGame", data);
|
||||
this.currentSecond--;
|
||||
|
||||
if(this.currentSecond === -2) {
|
||||
this.gameStart = false;
|
||||
this.currentSecond = 5;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
setTimeout(this.sendStartGameUpdate.bind(this), 1000);
|
||||
}
|
||||
|
||||
interruptStart() {
|
||||
this.gameStart = false;
|
||||
this.currentSecond = 5;
|
||||
|
||||
this.users.forEach(user => {
|
||||
user.socket.off("disconnect", this.interruptStart.bind(this));
|
||||
});
|
||||
|
||||
this.io.to(`lobby-${this.code}`).emit("interruptStart");
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Lobby;
|
||||
@@ -5,7 +5,7 @@ class SocketUser {
|
||||
constructor(id, username, socket) {
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
this.socket = socket;
|
||||
this.socket = socket || null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
class TemporaryLobby{
|
||||
/** @param { string } code @param {Array<SocketUser>} users */
|
||||
constructor(code, users){
|
||||
this.code = code;
|
||||
this.users = users;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TemporaryLobby;
|
||||
@@ -20,7 +20,7 @@ class ClientHandler {
|
||||
this.socket.on("createLobby", () => { this.createLobby() })
|
||||
this.socket.on("joinLobby", (code) => { this.joinLobby(code) })
|
||||
this.socket.on("refreshLobby", () => { this.refreshLobby() })
|
||||
this.socket.on("disconnect", () => { this.disconnect() })
|
||||
this.socket.on("disconnect", () => { this.defaultDisconnect() })
|
||||
}
|
||||
|
||||
createLobby(){
|
||||
@@ -43,7 +43,7 @@ class ClientHandler {
|
||||
|
||||
this.currentLobbyCode = response;
|
||||
|
||||
this.socket.join(`lobby-${response}`)
|
||||
this.socket.join(`lobby-${response}`);
|
||||
this.socket.emit("lobbyJoined", response);
|
||||
this.refreshLobby();
|
||||
}
|
||||
@@ -52,7 +52,7 @@ class ClientHandler {
|
||||
this.lobbyManager.refreshLobby(this.currentLobbyCode);
|
||||
}
|
||||
|
||||
disconnect(){
|
||||
defaultDisconnect(){
|
||||
this.lobbyManager.removeFromLobby(this.currentLobbyCode, this.user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const socketIO = require("socket.io");
|
||||
const Lobby = require("./Classes/Lobby");
|
||||
const SocketUser = require("./Classes/SocketUser");
|
||||
const TemporaryLobby = require("./Classes/TemporaryLobby");
|
||||
|
||||
class LobbyManager {
|
||||
/** @param {socketIO.Server} io */
|
||||
@@ -10,6 +11,9 @@ class LobbyManager {
|
||||
/** @type {Map<string, Lobby>}*/
|
||||
this.lobbys = new Map();
|
||||
|
||||
/** @type {Array<TemporaryLobby>}*/
|
||||
this.oldLobbys = [];
|
||||
|
||||
// Zeigt die Anzahl der Lobbys an | Für Testing
|
||||
// setInterval(() => {
|
||||
// console.log(this.lobbys.size);
|
||||
|
||||
Reference in New Issue
Block a user