From 90559b038842c4484010a430ebd171d68d554f2e Mon Sep 17 00:00:00 2001 From: "julien.bouin.etu" <julien.bouin.etu@univ-lille.fr> Date: Sun, 7 Apr 2024 14:39:24 +0200 Subject: [PATCH] fix bugs et afficher les 10 meilleurs scores --- client/src/GestionScoresClient.js | 19 ------- client/src/scoreboard.json | 93 +++++++++++++++++++++++++++++++ client/src/setHtml.js | 1 - server/ScoreBoard.js | 14 ++++- server/index.js | 15 +++-- 5 files changed, 113 insertions(+), 29 deletions(-) delete mode 100644 client/src/GestionScoresClient.js diff --git a/client/src/GestionScoresClient.js b/client/src/GestionScoresClient.js deleted file mode 100644 index 3e58122..0000000 --- a/client/src/GestionScoresClient.js +++ /dev/null @@ -1,19 +0,0 @@ -export class GestionScore { - async afficherScores(fichier) { - try { - const response = await fetch(fichier); - const scores = await response.json(); - let html = - '<table><thead><tr><th>Nom</th><th>Score</th></tr></thead><tbody>'; - scores.results.forEach(result => { - html += `<tr><td>${result.nom}</td><td>${result.score}</td></tr>`; - }); - html += '</tbody></table>'; - console.log(html); - return html; - } catch (error) { - console.error('Erreur lors de la lecture du fichier JSON :', error); - return null; - } - } -} diff --git a/client/src/scoreboard.json b/client/src/scoreboard.json index 1f14fbd..1f9b964 100644 --- a/client/src/scoreboard.json +++ b/client/src/scoreboard.json @@ -23,6 +23,99 @@ { "nom": "test", "score": -15 + }, + { + "nom": "pierre", + "score": 20 + }, + { + "nom": "rayane", + "score": -15 + }, + { + "nom": "p", + "score": 30 + }, + { + "nom": "pp", + "score": -15 + }, + { + "nom": "ma", + "score": 70 + }, + { + "nom": "ppapa", + "score": 40 + }, + { + "nom": "mm", + "score": 55 + }, + { + "score": -15 + }, + { + "nom": "testtt", + "score": -15 + }, + { + "score": -5 + }, + { + "nom": "test", + "score": -15 + }, + { + "nom": "m", + "score": 20 + }, + { + "nom": "m`", + "score": -15 + }, + { + "nom": "p", + "score": 5 + }, + { + "nom": "m", + "score": -25 + }, + { + "nom": "test", + "score": -10 + }, + { + "nom": "p", + "score": -15 + }, + { + "nom": "^", + "score": -15 + }, + { + "nom": "m", + "score": -5 + }, + { + "nom": "tes", + "score": -15 + }, + { + "score": -10 + }, + { + "nom": "p", + "score": 0 + }, + { + "nom": "m", + "score": 5 + }, + { + "nom": "p", + "score": -10 } ] } \ No newline at end of file diff --git a/client/src/setHtml.js b/client/src/setHtml.js index 8235335..de2121c 100644 --- a/client/src/setHtml.js +++ b/client/src/setHtml.js @@ -1,4 +1,3 @@ -import { GestionScore } from './GestionScoresClient.js'; export default class setHtml { static credits() { return ` diff --git a/server/ScoreBoard.js b/server/ScoreBoard.js index f7fa6fc..09e510a 100644 --- a/server/ScoreBoard.js +++ b/server/ScoreBoard.js @@ -9,13 +9,21 @@ export class GestionScore { try { const data = fs.readFileSync(this.jsonFilePath); const scores = JSON.parse(data); + + // Trier les scores par ordre décroissant + scores.results.sort((a, b) => b.score - a.score); + let html = '<table><thead><tr><th>Nom</th><th>Score</th></tr></thead><tbody>'; - scores.results.forEach(result => { + + // Afficher les 10 premiers résultats + const numberOfResults = Math.min(10, scores.results.length); + for (let i = 0; i < numberOfResults; i++) { + const result = scores.results[i]; html += `<tr><td>${result.nom}</td><td>${result.score}</td></tr>`; - }); - html += '</tbody></table>'; + } + html += '</tbody></table>'; return html; } catch (error) { console.error('Erreur lors de la lecture du fichier JSON :', error); diff --git a/server/index.js b/server/index.js index d453023..d0f9adb 100644 --- a/server/index.js +++ b/server/index.js @@ -7,7 +7,7 @@ import enemi from './enemis.js'; import { Coordinate } from '../common/Coordinate.js'; import timer from './timer.js'; import Bonus from './bonus.js'; -import { bonusImages, bonusNoms, bonusTaille } from '../common/utils.js'; +import { bonusNoms } from '../common/utils.js'; import { GestionScore } from './ScoreBoard.js'; const app = express(); @@ -183,7 +183,8 @@ setInterval(() => { enemis.forEach(enemi => { if ( enemi.hitbox.colision(avatar.hitbox) && - avatar.getStatut() != 'invincibilite' + avatar.getStatut() != 'invincibilite' && + !avatar.spectateur ) { if (canLostLifeAvatar) { avatar.decrementScore(5); @@ -201,14 +202,16 @@ setInterval(() => { io.emit('dead', avatar.id); } } - if (enemi.getVies() < 0) { - avatar.incrementScore(5); - enemis.splice(enemis.indexOf(enemi), 1); - } + enemi.deplacer(); avatar.colision(enemi.hitbox); avatar.projectiles.forEach(projectile => { if (projectile.hitbox.colision(enemi.hitbox)) { + if (enemi.getVies() < 0) { + console.log(avatar.id); + avatar.incrementScore(5); + enemis.splice(enemis.indexOf(enemi), 1); + } avatar.projectiles.splice( avatar.projectiles.indexOf(projectile), 1 -- GitLab