Hinzufügen der Game Logik: Hinzufügen von Spieler-Management, Socket.IO-Integration und UI-Updates

This commit is contained in:
2025-04-01 16:16:26 +02:00
parent 5a2eaf74ca
commit 2c9a14db9a
10 changed files with 64 additions and 9 deletions

View File

@@ -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(){

View File

@@ -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<SocketUser>} */
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);
}
}

View File

@@ -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() {