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;
@@ -75,6 +78,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;
this.currentSecond = 5;

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;

17
frontend/game/index.html Normal file
View File

@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Doublesnake - Game</title>
<link rel="stylesheet" href="../style/generalStyle.css">
<link rel="stylesheet" href="./style/mainStyle.css">
</head>
<body>
</body>
<script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>
<script type="module" src="./scripts/index.js"></script>
</html>

View File

@@ -0,0 +1,28 @@
class ServerConnectionManager {
constructor() {
/**@type {import("../../../backend/node_modules/socket.io-client".Socket} für Autocompletions VSC*/
this.socket = io(`${window.location.protocol}//${window.location.hostname}:${window.location.port}`);
this.basicSetup();
}
basicSetup() {
this.socket.emit("hereForGame");
this.socket.on("connect", () => {
console.log("Verbindung zum Server hergestellt!");
});
this.socket.on("connect_error", (error) => {
console.log(error);
window.location.pathname = "/dashboard";
});
this.socket.on("disconnect", () => {
console.log("Die verbindung wurde unterbrochen!");
window.location.pathname = "/dashboard";
});
}
}
new ServerConnectionManager();

View File