diff --git a/API/src/database/database.sqlite b/API/src/database/database.sqlite index 3b0f607132d41b2d59a2ed5c6fefffe988f5b37b..30bcb3756e4ba1b24718dc0f5798be127ea4b8fb 100644 Binary files a/API/src/database/database.sqlite and b/API/src/database/database.sqlite differ diff --git a/API/src/socket.io/ServerIO.ts b/API/src/socket.io/ServerIO.ts index 62c9d210188adbd51d0c9dac0c8ebf25ef89cfb8..89d068359d85dafb9428d4e02ab1117d799e1f44 100644 --- a/API/src/socket.io/ServerIO.ts +++ b/API/src/socket.io/ServerIO.ts @@ -5,6 +5,7 @@ import Server, {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 @@ -14,6 +15,8 @@ import {Question} from "../database/models/Question"; class ServerIO extends IO.Server { players: { [key: string]: UserInfo } = {}; playersReady: { [key: string]: boolean }={}; + playersScore: { [key: string]: number }={}; + nbQuestion!:number; questions!:Question[]; currentQuestion: Question; nbGamer=0; @@ -43,6 +46,8 @@ class ServerIO extends IO.Server { socket.user.lastname ); this.playersReady[playerKey] = false; + this.playersScore[playerKey] = 0; + this.nbQuestion=0; console.log(`Player ${playerKey} is connected.`); } @@ -56,30 +61,65 @@ class ServerIO extends IO.Server { private registerEventsOnSocket(socket: SocketIoInfo) { const playerKey = socket.user.username; socket.on("player-ready", ()=>{ + console.log("nbgamer ready:", this.nbGamer); if(this.playersReady[playerKey] === true){ console.log(`player ${ playerKey } already ready`) }else{ logger.info(`player ${playerKey} ready`); + this.nbGamer++; this.playersReady[playerKey]=true; this.emit("ready-player", playerKey); } this.testNumberOfReady(); }); socket.on("player-not-ready", ()=>{ + console.log("nbgamer not ready:", this.nbGamer); if(this.playersReady[playerKey] === false){ console.log(`player ${ playerKey } already not ready`) }else{ logger.info(`player ${playerKey} not ready`); + this.nbGamer--; this.playersReady[playerKey]=false; this.emit("not-ready-player", playerKey); } - }) + }); socket.on("on-game", async () => { - this.nbGamer++; if (this.nbGamer === 3) { - let randomQuestion = await this.getRandomQuestion(); + let randomQuestion!: any; + if(!this.currentQuestion){ + randomQuestion = await this.getRandomQuestion(); + } + else{ + let question={...this.currentQuestion}; + question=question.dataValues as Question; + randomQuestion = question; + } + console.log("send question", randomQuestion); + this.emit("question", randomQuestion); + this.nbQuestion++; + } + }); + + socket.on("validate-question", async (responseSelected) => { + let randomQuestion = await this.getRandomQuestion(); + if (responseSelected === this.currentQuestion.correctResponse) { + // le joueur gagne 1 point + this.playersScore[playerKey]+=1; + }else{ + // le joueur perd deux points + this.playersScore[playerKey]-=2; + } + this.nbQuestion++; + if(this.nbQuestion<10){ this.emit("question", randomQuestion); + }else{ + const playersScoreFormatted = Object.keys(this.playersScore).reduce((formatted:any, key) => { + formatted[key] = this.playersScore[key]; + return formatted; + }, {}); + this.emit("game-finished", playersScoreFormatted); } + }) socket.on('disconnect', () => { @@ -132,7 +172,10 @@ class ServerIO extends IO.Server { const randomIndex = Math.floor(Math.random() * this.questions.length); this.currentQuestion = this.questions[randomIndex]; this.questions.splice(randomIndex, 1); - return this.currentQuestion; + // pour ne pas envoyer au frontend la reponse attendue + let randomQuestion={...this.currentQuestion}; + randomQuestion=randomQuestion.dataValues as Question; + return randomQuestion; } private testNumberOfReady() {