Hinzufügen der Spiel-Logik: Implementierung von Benutzerverlassen, Spielende-Logik und Protokollierung
This commit is contained in:
@@ -44,8 +44,7 @@ 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();
|
||||||
// TODO: Lösche Kommentar wenn fertig mit testen
|
console.log(`${date.toTimeString().slice(0, 8)} | ${req.method} | ${req.url}`);
|
||||||
//console.log(`${date.toTimeString().slice(0, 8)} | ${req.method} | ${req.url}`);
|
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ class ClientHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
defaultDisconnect(){
|
defaultDisconnect(){
|
||||||
|
this.gameManager.leaveGame(this.user, this.currentGameCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ class Game{
|
|||||||
this.gameManager = gameManager;
|
this.gameManager = gameManager;
|
||||||
this.code = code;
|
this.code = code;
|
||||||
|
|
||||||
|
this.gameStart = false;
|
||||||
|
|
||||||
/**@type {Array<SocketUser>} */
|
/**@type {Array<SocketUser>} */
|
||||||
this.players = []
|
this.players = []
|
||||||
|
|
||||||
@@ -20,11 +22,29 @@ class Game{
|
|||||||
}, 100);
|
}, 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** @param {SocketUser} user */
|
||||||
addUser(user){
|
addUser(user){
|
||||||
if(this.players.length >= 2) return 1;
|
if(this.players.length >= 2) return 1;
|
||||||
|
|
||||||
this.players.push(user);
|
this.players.push(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @param {SocketUser} user */
|
||||||
|
leaveUser(user){
|
||||||
|
this.players.forEach((player, index) => {
|
||||||
|
if(player.id === user.id){
|
||||||
|
this.players.splice(index, 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if(this.players.length != 2){
|
||||||
|
this.io.to(`game-${this.code}`).emit("gameEnd", "Das Spiel ist zu Ende, da ein Spieler verlassen hat!");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Game;
|
module.exports = Game;
|
||||||
@@ -12,6 +12,11 @@ class GameManager {
|
|||||||
|
|
||||||
/** @type {Map<string, Game>}*/
|
/** @type {Map<string, Game>}*/
|
||||||
this.games = new Map();
|
this.games = new Map();
|
||||||
|
|
||||||
|
// Für Debugging
|
||||||
|
// setInterval(() => {
|
||||||
|
// console.log(`Es gibt ${this.games.size} Spiele`);
|
||||||
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param {SocketUser} user */
|
/** @param {SocketUser} user */
|
||||||
@@ -47,6 +52,14 @@ class GameManager {
|
|||||||
return oldLobbySave.gameCode;
|
return oldLobbySave.gameCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
leaveGame(user, code){
|
||||||
|
const game = this.games.get(code);
|
||||||
|
if(!game) return 1;
|
||||||
|
|
||||||
|
const response = game.leaveUser(user);
|
||||||
|
if(response === 1) this.games.delete(code);
|
||||||
|
}
|
||||||
|
|
||||||
generateNonExistingCode() {
|
generateNonExistingCode() {
|
||||||
let code;
|
let code;
|
||||||
do {
|
do {
|
||||||
|
|||||||
@@ -5,8 +5,10 @@ class ServerConnectionManager {
|
|||||||
|
|
||||||
this.basicSetup();
|
this.basicSetup();
|
||||||
|
|
||||||
this.socket.on("waitForStart", (data) => {this.waitForStart(data)});
|
this.socket.on("waitForStart", (data) => { this.waitForStart(data) });
|
||||||
this.socket.on("randomTest", (data) => {this.randomTest(data)});
|
this.socket.on("randomTest", (data) => { this.randomTest(data) });
|
||||||
|
|
||||||
|
this.socket.on("gameEnd", (msg) => { this.gameEnd(msg) });
|
||||||
}
|
}
|
||||||
|
|
||||||
waitForStart(data) {
|
waitForStart(data) {
|
||||||
@@ -17,6 +19,12 @@ class ServerConnectionManager {
|
|||||||
document.getElementById("rd").innerText = data;
|
document.getElementById("rd").innerText = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gameEnd(msg){
|
||||||
|
this.socket.disconnect();
|
||||||
|
confirm(msg)
|
||||||
|
window.location.pathname = "/dashboard";
|
||||||
|
}
|
||||||
|
|
||||||
basicSetup() {
|
basicSetup() {
|
||||||
this.socket.emit("hereForGame");
|
this.socket.emit("hereForGame");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user