diff --git a/API/src/socket.io/ServerIO.ts b/API/src/socket.io/ServerIO.ts index 89d068359d85dafb9428d4e02ab1117d799e1f44..560819bd73380314853400febe1138cfc0ee7e91 100644 --- a/API/src/socket.io/ServerIO.ts +++ b/API/src/socket.io/ServerIO.ts @@ -1,11 +1,10 @@ import * as IO from 'socket.io'; -import logger from '../logging/WinstonLogger'; -import http from 'http'; -import Server, {SocketIoInfo} from "../express/Server"; +import logger from '../logging/WinstonLogger'; +import http from 'http'; +import {SocketIoInfo} from "../express/Server"; import {UserInfo} from "./UserInfo" import {Database} from "../database/Database"; import {Question} from "../database/models/Question"; -import {response} from "express"; //TODO: In this file you can add/edit all things about socket.io @@ -33,24 +32,7 @@ class ServerIO extends IO.Server { this.on('connection', (socket: SocketIoInfo) => { logger.info(`Nouveau socket vers ${socket.client.conn.remoteAddress}`); console.log(`Socket info: ${socket.user.username} // ${socket.user.firstname} // ${socket.user.lastname}`); - const playerKey = socket.user.username; - // Vérifiez si le joueur existe déjà dans le dictionnaire - if (this.players[playerKey]) { - // Le joueur est déjà connecté, vous pouvez effectuer une action appropriée ici - console.log(`Player ${playerKey} is already connected.`); - } else { - // Le joueur n'est pas encore connecté, ajoutez-le au dictionnaire - this.players[playerKey] = new UserInfo( - socket.user.username, - socket.user.firstname, - socket.user.lastname - ); - this.playersReady[playerKey] = false; - this.playersScore[playerKey] = 0; - this.nbQuestion=0; - console.log(`Player ${playerKey} is connected.`); - } - + this.initializeGame(socket); this.testNumberofPlayer(); this.registerEventsOnSocket(socket); }); @@ -58,6 +40,29 @@ class ServerIO extends IO.Server { } + initializeGame(socket: SocketIoInfo){ + const playerKey = socket.user.username; + // Vérifiez si le joueur existe déjà dans le dictionnaire + if (this.players[playerKey]) { + // Le joueur est déjà connecté, vous pouvez effectuer une action appropriée ici + console.log(`Player ${playerKey} is already connected.`); + } else { + // Le joueur n'est pas encore connecté, ajoutez-le au dictionnaire + this.players[playerKey] = new UserInfo( + socket.user.username, + socket.user.firstname, + socket.user.lastname + ); + if(!this.playersReady.hasOwnProperty(playerKey)){ + this.playersReady[playerKey] = false; + } + if(!this.playersScore.hasOwnProperty(playerKey)) { + this.playersScore[playerKey] = 0; + } + this.nbQuestion=0; + console.log(`Player ${playerKey} is connected.`); + } + } private registerEventsOnSocket(socket: SocketIoInfo) { const playerKey = socket.user.username; socket.on("player-ready", ()=>{ @@ -91,26 +96,27 @@ class ServerIO extends IO.Server { } else{ let question={...this.currentQuestion}; - question=question.dataValues as Question; + question.correctResponse=-1; randomQuestion = question; } - console.log("send question", randomQuestion); this.emit("question", randomQuestion); - this.nbQuestion++; } }); socket.on("validate-question", async (responseSelected) => { - let randomQuestion = await this.getRandomQuestion(); + console.log("current question:", this.currentQuestion); if (responseSelected === this.currentQuestion.correctResponse) { // le joueur gagne 1 point + console.log(`joueur ${playerKey} gagne 1 point`) this.playersScore[playerKey]+=1; }else{ + console.log(`joueur ${playerKey} perd 2 point`) // le joueur perd deux points this.playersScore[playerKey]-=2; } - this.nbQuestion++; - if(this.nbQuestion<10){ + console.log("playerScore:", this.playersScore); + let randomQuestion = await this.getRandomQuestion(); + if(this.nbQuestion<=10){ this.emit("question", randomQuestion); }else{ const playersScoreFormatted = Object.keys(this.playersScore).reduce((formatted:any, key) => { @@ -119,7 +125,13 @@ class ServerIO extends IO.Server { }, {}); this.emit("game-finished", playersScoreFormatted); } + this.nbQuestion++; + }); + socket.on("restart-game", ()=>{ + if(Object.keys(this.players).length === 3) + this.players={}; + this.initializeGame(socket); }) socket.on('disconnect', () => { @@ -133,6 +145,7 @@ class ServerIO extends IO.Server { // Le joueur est connecté, retirez-le du dictionnaire delete this.players[playerKey]; delete this.playersReady[playerKey]; + delete this.playersScore[playerKey]; console.log(`Player ${playerKey} is disconnected.`); } else { // Le joueur n'est pas trouvé dans le dictionnaire, cela peut être un cas anormal @@ -170,12 +183,10 @@ class ServerIO extends IO.Server { } const randomIndex = Math.floor(Math.random() * this.questions.length); - this.currentQuestion = this.questions[randomIndex]; + this.currentQuestion = this.questions[randomIndex].dataValues; this.questions.splice(randomIndex, 1); // pour ne pas envoyer au frontend la reponse attendue - let randomQuestion={...this.currentQuestion}; - randomQuestion=randomQuestion.dataValues as Question; - return randomQuestion; + return {...this.currentQuestion}; } private testNumberOfReady() {