From 2c9a14db9a329e5d6238490736294ebdab06cba2 Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 1 Apr 2025 16:16:26 +0200 Subject: [PATCH] =?UTF-8?q?Hinzuf=C3=BCgen=20der=20Game=20Logik:=20Hinzuf?= =?UTF-8?q?=C3=BCgen=20von=20Spieler-Management,=20Socket.IO-Integration?= =?UTF-8?q?=20und=20UI-Updates?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Speicher.txt | 2 +- backend/src/Express/ExpressManager.js | 3 ++- .../src/SocketIO/GameManager/ClientHandler.js | 5 ++++ backend/src/SocketIO/GameManager/Game/Game.js | 17 ++++++++++++ .../src/SocketIO/GameManager/GameManager.js | 27 +++++++++++++++---- .../LobbyManager/Classes/TemporaryLobby.js | 2 ++ backend/src/SocketIO/SocketIOManager.js | 2 +- .../lobby/scripts/Handler/StartGameHandler.js | 1 + frontend/game/index.html | 3 ++- frontend/game/scripts/index.js | 11 ++++++++ 10 files changed, 64 insertions(+), 9 deletions(-) diff --git a/Speicher.txt b/Speicher.txt index 65fd1ca..41574c4 100644 --- a/Speicher.txt +++ b/Speicher.txt @@ -25,4 +25,4 @@ CREATE TABLE users ( /**@param {} x*/ -/**@typeof {} x*/ \ No newline at end of file +/**@type {} x*/ \ No newline at end of file diff --git a/backend/src/Express/ExpressManager.js b/backend/src/Express/ExpressManager.js index 3c13605..639d274 100644 --- a/backend/src/Express/ExpressManager.js +++ b/backend/src/Express/ExpressManager.js @@ -44,7 +44,8 @@ class ExpressManager{ /**@param {express.Request} req @param {express.Response} res @param {express.NextFunction} next*/ logger(req, res, next){ const date = new Date(); - console.log(`${date.toTimeString().slice(0, 8)} | ${req.method} | ${req.url}`); + // TODO: Lösche Kommentar wenn fertig mit testen + //console.log(`${date.toTimeString().slice(0, 8)} | ${req.method} | ${req.url}`); next(); } diff --git a/backend/src/SocketIO/GameManager/ClientHandler.js b/backend/src/SocketIO/GameManager/ClientHandler.js index b6ab95f..60061df 100644 --- a/backend/src/SocketIO/GameManager/ClientHandler.js +++ b/backend/src/SocketIO/GameManager/ClientHandler.js @@ -24,6 +24,11 @@ class ClientHandler { joinGame(){ const response = this.gameManager.joinGame(this.user); + + if(response === 1) return this.socket.disconnect(); + this.currentGameCode = response; + + this.socket.join(`game-${this.currentGameCode}`); } defaultDisconnect(){ diff --git a/backend/src/SocketIO/GameManager/Game/Game.js b/backend/src/SocketIO/GameManager/Game/Game.js index 8f63516..6c74db1 100644 --- a/backend/src/SocketIO/GameManager/Game/Game.js +++ b/backend/src/SocketIO/GameManager/Game/Game.js @@ -1,3 +1,5 @@ +const socketIO = require("socket.io"); +const SocketUser = require("../../Classes/SocketUser"); const GameManager = require("../GameManager"); class Game{ @@ -6,6 +8,21 @@ class Game{ this.io = io; this.gameManager = gameManager; this.code = code; + + /**@type {Array} */ + this.players = [] + + // TODO: 5 Seconds after initialization, check if game has 2 players. if not stop game + + 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); + }, 1000); + } + + addUser(user){ + if(this.players.length >= 2) return 1; + this.players.push(user); } } diff --git a/backend/src/SocketIO/GameManager/GameManager.js b/backend/src/SocketIO/GameManager/GameManager.js index f66aaa8..ff0d811 100644 --- a/backend/src/SocketIO/GameManager/GameManager.js +++ b/backend/src/SocketIO/GameManager/GameManager.js @@ -1,4 +1,6 @@ +const socketIO = require("socket.io"); const SocketUser = require("../Classes/SocketUser"); +const TemporaryLobby = require("../LobbyManager/Classes/TemporaryLobby"); const LobbyManager = require("../LobbyManager/LobbyManager"); const Game = require("./Game/Game"); @@ -14,8 +16,11 @@ class GameManager { /** @param {SocketUser} user */ joinGame(user){ - const wasInLobby = false; - const oldLobbySave = undefined; + let wasInLobby = false; + + /** @type {TemporaryLobby} */ + let oldLobbySave = undefined; + this.lobbyManager.oldLobbys.forEach(oldLobby => { oldLobby.users.forEach(lobbyUser => { if(lobbyUser.id === user.id){ @@ -25,10 +30,22 @@ class GameManager { }); }); - if(!wasInLobby) user.socket.disconnect(); + console.log(oldLobbySave) - // Checken ob lobby existiert - // neuen Lobby erstellen Aufgabe + if(!wasInLobby) return 1; + + if(!oldLobbySave.gameCode){ + const code = this.generateNonExistingCode(); + const game = new Game(this.io, this, code); + this.games.set(code, game); + + oldLobbySave.gameCode = code; + } + + const response = this.games.get(oldLobbySave.gameCode).addUser(user); + if(response === 1) return 1; + + return oldLobbySave.gameCode; } generateNonExistingCode() { diff --git a/backend/src/SocketIO/LobbyManager/Classes/TemporaryLobby.js b/backend/src/SocketIO/LobbyManager/Classes/TemporaryLobby.js index a3e4d29..a9f5488 100644 --- a/backend/src/SocketIO/LobbyManager/Classes/TemporaryLobby.js +++ b/backend/src/SocketIO/LobbyManager/Classes/TemporaryLobby.js @@ -9,6 +9,8 @@ class TemporaryLobby{ this.users = users; this.lobbyManager = lobbyManager; + this.gameCode = null; + setTimeout(this.selfDestroy.bind(this), 4000); } diff --git a/backend/src/SocketIO/SocketIOManager.js b/backend/src/SocketIO/SocketIOManager.js index f178eec..03f8268 100644 --- a/backend/src/SocketIO/SocketIOManager.js +++ b/backend/src/SocketIO/SocketIOManager.js @@ -67,7 +67,7 @@ class SocketIOManager { }); socket.on("hereForGame", () => { - new ClientHandlerGame(socket, this.lobbyManager); + new ClientHandlerGame(socket, this.gameManager); }); } } diff --git a/frontend/dashboard/lobby/scripts/Handler/StartGameHandler.js b/frontend/dashboard/lobby/scripts/Handler/StartGameHandler.js index 409e9ef..199ea13 100644 --- a/frontend/dashboard/lobby/scripts/Handler/StartGameHandler.js +++ b/frontend/dashboard/lobby/scripts/Handler/StartGameHandler.js @@ -14,6 +14,7 @@ class StartGameHandler { this.countDownText.innerText = data.currentSecond; if(data.needRedirect){ + this.socket.disconnect(); window.location.pathname = `/game`; } } diff --git a/frontend/game/index.html b/frontend/game/index.html index 543aae3..4d64b50 100644 --- a/frontend/game/index.html +++ b/frontend/game/index.html @@ -9,7 +9,8 @@ - +

+

diff --git a/frontend/game/scripts/index.js b/frontend/game/scripts/index.js index b9eead3..7abd232 100644 --- a/frontend/game/scripts/index.js +++ b/frontend/game/scripts/index.js @@ -4,8 +4,19 @@ class ServerConnectionManager { this.socket = io(`${window.location.protocol}//${window.location.hostname}:${window.location.port}`); this.basicSetup(); + + this.socket.on("waitForStart", (data) => {this.waitForStart(data)}); + this.socket.on("randomTest", (data) => {this.randomTest(data)}); } + waitForStart(data) { + document.getElementById("pc").innerText = data; + } + + randomTest(data) { + document.getElementById("rd").innerText = data; + } + basicSetup() { this.socket.emit("hereForGame");