Version 1.0: Funktioniere Bestenliste und sonst auch alles Funktionell

This commit is contained in:
2025-05-02 18:42:48 +02:00
parent b36d7a4274
commit a422ef879e
12 changed files with 124 additions and 1402 deletions

View File

@@ -1,7 +1,8 @@
class Score{
/** @param {UserManager} usermanager */
constructor(scoreJson){
constructor(scoreJson, rank = 0){
this.id = scoreJson.id;
this.rank = rank;
this.user1ID = scoreJson.user1;
this.user2ID = scoreJson.user2;
this.score = scoreJson.score;

View File

@@ -5,11 +5,13 @@ class ScoreManager{
/**@param {mySql.Connection} connection*/
constructor(connection) {
this.connection = connection;
this.splitLimit = 25;
}
async getAllScores(){
const response = await this.connection.promise().query("SELECT * FROM scores ORDER BY score DESC");
const sortedScores = response[0].map(scoreData => new Score(scoreData));
const sortedScores = response[0].map((scoreData, index) => new Score(scoreData, index + 1));
return sortedScores;
}
@@ -32,24 +34,27 @@ class ScoreManager{
return new Score(response.rows[0][0]);
}
async getScoreRang(id){
const allSortetScores = await this.getAllScores();
const score = allSortetScores.find(score => score.id === id);
return allSortetScores.indexOf(score) + 1;
}
async getSplitedScoreCount(){
const allSortetScores = await this.getAllScores();
const count = Math.ceil(allSortetScores.lenght / 25);
const count = Math.ceil(allSortetScores.length / 25);
return count;
}
async getSpiltedScore(count){
const offset = (count - 1) * this.splitLimit;
const response = await this.connection.promise().query(
`SELECT * FROM scores ORDER BY score DESC LIMIT ${this.splitLimit} OFFSET ${offset}`,
);
const sortedScoreSplit = response[0].map((scoreData, index) => {
const rank = offset + index + 1;
return new Score(scoreData, rank);
});
return sortedScoreSplit;
}
}

View File

@@ -10,6 +10,8 @@ class HighScoreRoute {
this.db = dbManager;
this.router.get("/", async (req, res) => await this.getHighscores(req, res));
this.router.get("/split/:count", async (req, res) => await this.getSplitedHighScores(req, res));
this.router.get("/count", async (req, res) => await this.getSplitedHighScoreCount(req, res));
}
/**@param {express.Request} req @param {express.Response} res*/
@@ -22,29 +24,64 @@ class HighScoreRoute {
}
for(const highscore of highscores){
const user1 = await this.db.usermanager.getUser({id: highscore.user1ID});
const user2 = await this.db.usermanager.getUser({id: highscore.user2ID});
const rang = highscores.indexOf(highscore) + 1;
returnJSON.scores.push({
id: highscore.id,
score: highscore.score,
rang: rang,
user1: {
id: user1.id,
username: user1.username,
fullName: user1.fullName
},
user2: {
id: user2.id,
username: user2.username,
fullName: user2.fullName
},
});
const highscoreJSON = await this.formatHighScore(highscore);
returnJSON.scores.push(highscoreJSON);
}
res.json(returnJSON);
}
/**@param {express.Request} req @param {express.Response} res*/
async getSplitedHighScores(req, res){
const count = req.params.count;
/** @type {Array<Score>} */
const highscores = await this.db.scoremanager.getSpiltedScore(count);
let returnJSON = {
scores: []
}
for(const highscore of highscores){
const highscoreJSON = await this.formatHighScore(highscore);
returnJSON.scores.push(highscoreJSON);
}
res.json(returnJSON);
}
/**@param {express.Request} req @param {express.Response} res*/
async getSplitedHighScoreCount(req, res){
const count = await this.db.scoremanager.getSplitedScoreCount();
res.json({
count: count
});
}
/** @param {Score} highscore */
async formatHighScore(highscore){
const user1 = await this.db.usermanager.getUser({id: highscore.user1ID});
const user2 = await this.db.usermanager.getUser({id: highscore.user2ID});
const rang = highscore.rank;
const returnJSON = {
id: highscore.id,
score: highscore.score,
rang: rang,
user1: {
id: user1.id,
username: user1.username,
fullName: user1.fullName
},
user2: {
id: user2.id,
username: user2.username,
fullName: user2.fullName
},
}
return returnJSON;
}
}
module.exports = HighScoreRoute;