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*/
|
||||
logger(req, res, next){
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ class ClientHandler {
|
||||
}
|
||||
|
||||
defaultDisconnect(){
|
||||
|
||||
this.gameManager.leaveGame(this.user, this.currentGameCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@ class Game{
|
||||
this.gameManager = gameManager;
|
||||
this.code = code;
|
||||
|
||||
this.gameStart = false;
|
||||
|
||||
/**@type {Array<SocketUser>} */
|
||||
this.players = []
|
||||
|
||||
@@ -20,11 +22,29 @@ class Game{
|
||||
}, 100);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** @param {SocketUser} user */
|
||||
addUser(user){
|
||||
if(this.players.length >= 2) return 1;
|
||||
|
||||
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;
|
||||
@@ -12,6 +12,11 @@ class GameManager {
|
||||
|
||||
/** @type {Map<string, Game>}*/
|
||||
this.games = new Map();
|
||||
|
||||
// Für Debugging
|
||||
// setInterval(() => {
|
||||
// console.log(`Es gibt ${this.games.size} Spiele`);
|
||||
// });
|
||||
}
|
||||
|
||||
/** @param {SocketUser} user */
|
||||
@@ -47,6 +52,14 @@ class GameManager {
|
||||
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() {
|
||||
let code;
|
||||
do {
|
||||
|
||||
@@ -7,6 +7,8 @@ class ServerConnectionManager {
|
||||
|
||||
this.socket.on("waitForStart", (data) => { this.waitForStart(data) });
|
||||
this.socket.on("randomTest", (data) => { this.randomTest(data) });
|
||||
|
||||
this.socket.on("gameEnd", (msg) => { this.gameEnd(msg) });
|
||||
}
|
||||
|
||||
waitForStart(data) {
|
||||
@@ -17,6 +19,12 @@ class ServerConnectionManager {
|
||||
document.getElementById("rd").innerText = data;
|
||||
}
|
||||
|
||||
gameEnd(msg){
|
||||
this.socket.disconnect();
|
||||
confirm(msg)
|
||||
window.location.pathname = "/dashboard";
|
||||
}
|
||||
|
||||
basicSetup() {
|
||||
this.socket.emit("hereForGame");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user