Hinzufügen der Game Logik: Hinzufügen von Spieler-Management, Socket.IO-Integration und UI-Updates
This commit is contained in:
@@ -25,4 +25,4 @@ CREATE TABLE users (
|
|||||||
|
|
||||||
|
|
||||||
/**@param {} x*/
|
/**@param {} x*/
|
||||||
/**@typeof {} x*/
|
/**@type {} x*/
|
||||||
@@ -44,7 +44,8 @@ class ExpressManager{
|
|||||||
/**@param {express.Request} req @param {express.Response} res @param {express.NextFunction} next*/
|
/**@param {express.Request} req @param {express.Response} res @param {express.NextFunction} next*/
|
||||||
logger(req, res, next){
|
logger(req, res, next){
|
||||||
const date = new Date();
|
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();
|
next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,11 @@ class ClientHandler {
|
|||||||
|
|
||||||
joinGame(){
|
joinGame(){
|
||||||
const response = this.gameManager.joinGame(this.user);
|
const response = this.gameManager.joinGame(this.user);
|
||||||
|
|
||||||
|
if(response === 1) return this.socket.disconnect();
|
||||||
|
this.currentGameCode = response;
|
||||||
|
|
||||||
|
this.socket.join(`game-${this.currentGameCode}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultDisconnect(){
|
defaultDisconnect(){
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
const socketIO = require("socket.io");
|
||||||
|
const SocketUser = require("../../Classes/SocketUser");
|
||||||
const GameManager = require("../GameManager");
|
const GameManager = require("../GameManager");
|
||||||
|
|
||||||
class Game{
|
class Game{
|
||||||
@@ -6,6 +8,21 @@ class Game{
|
|||||||
this.io = io;
|
this.io = io;
|
||||||
this.gameManager = gameManager;
|
this.gameManager = gameManager;
|
||||||
this.code = code;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
|
const socketIO = require("socket.io");
|
||||||
const SocketUser = require("../Classes/SocketUser");
|
const SocketUser = require("../Classes/SocketUser");
|
||||||
|
const TemporaryLobby = require("../LobbyManager/Classes/TemporaryLobby");
|
||||||
const LobbyManager = require("../LobbyManager/LobbyManager");
|
const LobbyManager = require("../LobbyManager/LobbyManager");
|
||||||
const Game = require("./Game/Game");
|
const Game = require("./Game/Game");
|
||||||
|
|
||||||
@@ -14,8 +16,11 @@ class GameManager {
|
|||||||
|
|
||||||
/** @param {SocketUser} user */
|
/** @param {SocketUser} user */
|
||||||
joinGame(user){
|
joinGame(user){
|
||||||
const wasInLobby = false;
|
let wasInLobby = false;
|
||||||
const oldLobbySave = undefined;
|
|
||||||
|
/** @type {TemporaryLobby} */
|
||||||
|
let oldLobbySave = undefined;
|
||||||
|
|
||||||
this.lobbyManager.oldLobbys.forEach(oldLobby => {
|
this.lobbyManager.oldLobbys.forEach(oldLobby => {
|
||||||
oldLobby.users.forEach(lobbyUser => {
|
oldLobby.users.forEach(lobbyUser => {
|
||||||
if(lobbyUser.id === user.id){
|
if(lobbyUser.id === user.id){
|
||||||
@@ -25,10 +30,22 @@ class GameManager {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
if(!wasInLobby) user.socket.disconnect();
|
console.log(oldLobbySave)
|
||||||
|
|
||||||
// Checken ob lobby existiert
|
if(!wasInLobby) return 1;
|
||||||
// neuen Lobby erstellen Aufgabe
|
|
||||||
|
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() {
|
generateNonExistingCode() {
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ class TemporaryLobby{
|
|||||||
this.users = users;
|
this.users = users;
|
||||||
this.lobbyManager = lobbyManager;
|
this.lobbyManager = lobbyManager;
|
||||||
|
|
||||||
|
this.gameCode = null;
|
||||||
|
|
||||||
setTimeout(this.selfDestroy.bind(this), 4000);
|
setTimeout(this.selfDestroy.bind(this), 4000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ class SocketIOManager {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on("hereForGame", () => {
|
socket.on("hereForGame", () => {
|
||||||
new ClientHandlerGame(socket, this.lobbyManager);
|
new ClientHandlerGame(socket, this.gameManager);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ class StartGameHandler {
|
|||||||
this.countDownText.innerText = data.currentSecond;
|
this.countDownText.innerText = data.currentSecond;
|
||||||
|
|
||||||
if(data.needRedirect){
|
if(data.needRedirect){
|
||||||
|
this.socket.disconnect();
|
||||||
window.location.pathname = `/game`;
|
window.location.pathname = `/game`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,8 @@
|
|||||||
<link rel="icon" type="image/png" href="../assets/Logo.png">
|
<link rel="icon" type="image/png" href="../assets/Logo.png">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<h1 id="pc"></h1>
|
||||||
|
<h1 id="rd"></h1>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
<script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>
|
<script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>
|
||||||
|
|||||||
@@ -4,8 +4,19 @@ class ServerConnectionManager {
|
|||||||
this.socket = io(`${window.location.protocol}//${window.location.hostname}:${window.location.port}`);
|
this.socket = io(`${window.location.protocol}//${window.location.hostname}:${window.location.port}`);
|
||||||
|
|
||||||
this.basicSetup();
|
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() {
|
basicSetup() {
|
||||||
this.socket.emit("hereForGame");
|
this.socket.emit("hereForGame");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user