From a8399bd70b26fe34b3bcbab7c4963c7ab00dc7f6 Mon Sep 17 00:00:00 2001 From: "narindra.rajohnso" <narindra-hasimanjaka-david.rajohnson@etu.hesge.ch> Date: Sun, 11 Jun 2023 19:11:02 +0200 Subject: [PATCH] Add questions logic - add point logic - error in calculate point - maybe caused by storage of current question --- API/src/database/database.sqlite | Bin 20480 -> 20480 bytes API/src/socket.io/ServerIO.ts | 51 ++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/API/src/database/database.sqlite b/API/src/database/database.sqlite index 3b0f607132d41b2d59a2ed5c6fefffe988f5b37b..30bcb3756e4ba1b24718dc0f5798be127ea4b8fb 100644 GIT binary patch delta 1887 zcmZozz}T>Wae_2s^F$eE#^#L)OY|kUI36(Yr|});Gvh7bS;c*wTbC=Da}mdbjg3b* z>Kp9^I2i&PmHAaYJp)TK6;g{!6mrxPOG{Eqixg5y6^in66Y~_53@t58loafvmE2SF zi_%jS{ENy{b8?h)lw6DQKw3Ofb2IbO%M&Y=bd<dGg7kv)6hiWIvNKcjlysEb^b~@V z^Giw;+%ik5(o>5Pb5fLI4SX_7N)FerEJ`dXElM>qFf!IPFw-?OR4_ENGO(~Rw$L-Q zG*ZwuFt9Q(z%Scq$Ir>oD4obJ4e~;24%82sdFe%mSEgj9=9MTE6lLZmXBH&pD5Rt+ z<RmH-r6#AAl%y)8)PuPNnN?MZ3ciU&=^3d-nI(xJkNRh(=jDT3ACR9}Rs?1_XC&qp zz=T8ca}!Ham12#ZOEZfz^U!@@Ze?t!XKG@O-3M^ldWZ)b-T62f8Z~42l_6fpNriYK zQK2|LIrH$!lEW(%3QIE;5*1PouPjT=$uB54yiy@2RiU`Dxa9DP+*E}W_0)=joczp^ z#FEVXJO#(Rl%o916p%-JGn0$*i}TY;lysEb^YhblQk8U+919A-bbWAQZgFW|x>BrR zNNRFMUVct~dM3JG%&ZKJ^$g67vHJy0wo#pzlfkx8l3y6)6_Aq)QZw^PK%wrHpPZkU zk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cmZozz}T>Wae_2s@kAMC#^Q|$OY}vU_zp1ehw>fZvu9x934*}QiV8M7^~~$cb;YGU zJ)QG&b5rw56iU<+3kp(mQWZ){?d_wLToTJNQ<QX+d=rbZm14oXfTF`I^YU|z42+C* z4NP?nj1-Ivtc;AUObqnQEG!kY4GgRd4Dib`Fff~&YlEz<$8K+aQdVkmiGn|v!egzW ziIs_oo`D&5YtdyH7?=)1tcBZJkK$dTtu?YTFw-+Lw7}|HLp0eERt^R<YvH!mgF=~D zYYi-|j12Tlj190_i!RH+z<3qiS_TG&MpI{gWlzuC#LT>s)V%t{yb^`{JcW|f;*wPR zXr=tpOeGzqy!^aWB^{-L)Y1~&!>dY)Qk7y26AKD*GLsWaGV}8wk!z-FV5nedY-M6> zWnigiV2T<T5Lpu|V?$8Fur!6axlxFfgCX1<<b>q>+}wIlnoG&gELO-(EK1HuEmBBM zEm1(S%RXAEEVBgUnw-?Ul2lL{Rnk!^N-W4sNmYt9KvIe7Dr0jiV+%b~BMeuWD;Szt znVMM{S?F1s7}Z0AvQdMXlfk}GkRK^4B&X&iLY<{hqMMmxAFULSSfo&LcxBGvl?90< z8JQqwIV$9+Czhp_rsu$UnJK9XDXE|^RY=ay%S+5GE>6r#PAyi7HG(EQh+9o{jdaXF cL1<)PWo)8nYGwp=4O|*kcJohrHUUO90A@21lmGw# diff --git a/API/src/socket.io/ServerIO.ts b/API/src/socket.io/ServerIO.ts index 62c9d21..89d0683 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() { -- GitLab