Lobby Start Mechanik und abfangen aller Möglichen Fehler

This commit is contained in:
2025-03-25 12:01:11 +01:00
parent d299a90067
commit 2e15735b2d
7 changed files with 84 additions and 8 deletions

View File

@@ -32,7 +32,8 @@ class ExpressManager{
// Routen wo man für Angemeldet sein muss
this.authRoutes = [
"/api/dashboard",
"/dashboard"
"/dashboard",
"/game"
];
// Routen Einbinden

View File

@@ -1,10 +1,13 @@
const socketIO = require("socket.io");
const SocketUser = require("./SocketUser");
const LobbyManager = require("../LobbyManager");
const TemporaryLobby = require("./TemporaryLobby");
class Lobby {
/** @param {socketIO.Server} io @param {Map<number, SocketUser>} users*/
constructor(io, code) {
/** @param {socketIO.Server} io @param {LobbyManager} lobbyManager @param {number} code */
constructor(io, lobbyManager, code) {
this.io = io;
this.lobbyManager = lobbyManager;
this.code = code;
/** @type {Array<SocketUser>} */
this.users = [];
@@ -21,7 +24,7 @@ class Lobby {
if(this.users.length === 2) {
console.log("Starte Spiel");
setTimeout(this.startGame.bind(this), 2000);
this.startGame();
}
return 0;
@@ -74,6 +77,23 @@ class Lobby {
this.io.to(`lobby-${this.code}`).emit("startGame", data);
this.currentSecond--;
if(this.currentSecond === 0){
const newUserList = [];
this.users.forEach(x => {
const user = new SocketUser(x.id, x.username);
newUserList.push(user);
})
const tempLobby = new TemporaryLobby(
this.code,
newUserList,
this.lobbyManager
);
this.lobbyManager.oldLobbys.push(tempLobby);
}
if(this.currentSecond === -2) {
this.gameStart = false;

View File

@@ -1,8 +1,18 @@
const LobbyManager = require("../LobbyManager");
class TemporaryLobby{
/** @param { string } code @param {Array<SocketUser>} users */
constructor(code, users){
/** @param { string } code @param {Array<SocketUser>} users @param {LobbyManager} lobbyManager*/
constructor(code, users, lobbyManager){
this.code = code;
this.users = users;
this.lobbyManager = lobbyManager;
setTimeout(this.selfDestroy.bind(this), 5000);
}
selfDestroy(){
const i = this.lobbyManager.oldLobbys.findIndex(x => x.code == this.code);
this.lobbyManager.oldLobbys.splice(i, 1);
}
}

View File

@@ -16,7 +16,7 @@ class LobbyManager {
// Zeigt die Anzahl der Lobbys an | Für Testing
// setInterval(() => {
// console.log(this.lobbys.size);
// console.log(this.oldLobbys.length);
// }, 1000);
}
@@ -24,7 +24,7 @@ class LobbyManager {
/** @param {SocketUser} user */
createLobby(user){
const code = this.generateNonExistingCode();
const lobby = new Lobby(this.io, code);
const lobby = new Lobby(this.io, this ,code);
lobby.addUser(user);
this.lobbys.set(code, lobby);
return code;