Replay Funktion hinzugefügt.

This commit is contained in:
2025-05-06 12:49:22 +02:00
parent a422ef879e
commit 86beaad464
6 changed files with 81 additions and 17 deletions

View File

@@ -25,13 +25,18 @@ class ScoreManager{
const selectSql = `SELECT * FROM scores WHERE id = ${insertId}`;
const selectResult = await this.connection.promise().query(selectSql);
const rank = await this.getRankById(insertId);
return new Score(selectResult[0][0]);
return new Score(selectResult[0][0], rank);
}
async getScoreById(id){
const response = await this.connection.promise().query(`SELECT * FROM scores WHERE id = ${id}`);
return new Score(response.rows[0][0]);
const rank = await this.getRankById(id);
return new Score(response[0][0], rank);
}
async getSplitedScoreCount(){
@@ -56,6 +61,14 @@ class ScoreManager{
return sortedScoreSplit;
}
async getRankById(id){
const rankSql = `SELECT COUNT(*) + 1 AS rank FROM scores WHERE score > (SELECT score FROM scores WHERE id = ${id})`;
const rankResult = await this.connection.promise().query(rankSql);
const rank = rankResult[0][0].rank;
return rank;
}
}
module.exports = ScoreManager;

View File

@@ -1,5 +1,6 @@
const socketIO = require("socket.io");
const SocketUser = require("../../Classes/SocketUser");
const TemporaryLobby = require("../../LobbyManager/Classes/TemporaryLobby");
const GameManager = require("../GameManager");
const GameLoop = require("./GameLoop");
const Snake = require("./Classes/Snake/Snake");
@@ -61,21 +62,46 @@ class Game{
}
async endGame(msg){
// TODO: Spielende in die Datenbank eintragen
this.gameStarted = false;
const scoreDb = await this.gameManager.db.scoremanager.createScore({
user1: this.playerIds[0],
user2: this.playerIds[1],
score: this.score
});
let scoreDb = undefined;
if(this.gameStarted){
scoreDb = await this.gameManager.db.scoremanager.createScore({
user1: this.playerIds[0],
user2: this.playerIds[1],
score: this.score
});
}
const rang = await this.gameManager.db.scoremanager.getScoreRang(scoreDb.id);
this.gameStarted = false;
/*
Hinzufügen der Funktion zum erneuten Spielen
Nur wenn noch beide Spieler da sind
*/
if(this.players.length === 2){
const lobbyManager = this.gameManager.lobbyManager;
const newUserList = [];
this.players.forEach(x => {
const user = new SocketUser(x.id, x.username);
newUserList.push(user);
})
const tempLobby = new TemporaryLobby(
0,
newUserList,
lobbyManager,
10000
);
lobbyManager.oldLobbys.push(tempLobby);
}
this.io.to(`game-${this.code}`).emit("gameEnd", {
msg: msg,
score: this.score,
rang: rang
rang: scoreDb?.rank || "-"
});
}

View File

@@ -89,7 +89,8 @@ class Lobby {
const tempLobby = new TemporaryLobby(
this.code,
newUserList,
this.lobbyManager
this.lobbyManager,
4000
);
this.lobbyManager.oldLobbys.push(tempLobby);

View File

@@ -4,14 +4,14 @@ const SocketUser = require("../../Classes/SocketUser");
class TemporaryLobby{
/** @param { string } code @param {Array<SocketUser>} users @param {LobbyManager} lobbyManager */
constructor(code, users, lobbyManager){
constructor(code, users, lobbyManager, destuctionTime){
this.code = code;
this.users = users;
this.lobbyManager = lobbyManager;
this.gameCode = null;
setTimeout(this.selfDestroy.bind(this), 4000);
setTimeout(this.selfDestroy.bind(this), destuctionTime);
}
selfDestroy(){