Skip to content
Snippets Groups Projects
Commit a8399bd7 authored by narindra.rajohnso's avatar narindra.rajohnso
Browse files

Add questions logic

- add point logic
- error in calculate point
- maybe caused by storage of current question
parent fe1e2b8e
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -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!: 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() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment