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

10 questions and restart logic add

parent aaf4b226
No related branches found
No related tags found
No related merge requests found
import * as IO from 'socket.io'; import * as IO from 'socket.io';
import logger from '../logging/WinstonLogger'; import logger from '../logging/WinstonLogger';
import http from 'http'; import http from 'http';
import Server, {SocketIoInfo} from "../express/Server"; import {SocketIoInfo} from "../express/Server";
import {UserInfo} from "./UserInfo" import {UserInfo} from "./UserInfo"
import {Database} from "../database/Database"; import {Database} from "../database/Database";
import {Question} from "../database/models/Question"; import {Question} from "../database/models/Question";
import {response} from "express";
//TODO: In this file you can add/edit all things about socket.io //TODO: In this file you can add/edit all things about socket.io
...@@ -33,6 +32,15 @@ class ServerIO extends IO.Server { ...@@ -33,6 +32,15 @@ class ServerIO extends IO.Server {
this.on('connection', (socket: SocketIoInfo) => { this.on('connection', (socket: SocketIoInfo) => {
logger.info(`Nouveau socket vers ${socket.client.conn.remoteAddress}`); logger.info(`Nouveau socket vers ${socket.client.conn.remoteAddress}`);
console.log(`Socket info: ${socket.user.username} // ${socket.user.firstname} // ${socket.user.lastname}`); console.log(`Socket info: ${socket.user.username} // ${socket.user.firstname} // ${socket.user.lastname}`);
this.initializeGame(socket);
this.testNumberofPlayer();
this.registerEventsOnSocket(socket);
});
}
initializeGame(socket: SocketIoInfo){
const playerKey = socket.user.username; const playerKey = socket.user.username;
// Vérifiez si le joueur existe déjà dans le dictionnaire // Vérifiez si le joueur existe déjà dans le dictionnaire
if (this.players[playerKey]) { if (this.players[playerKey]) {
...@@ -45,19 +53,16 @@ class ServerIO extends IO.Server { ...@@ -45,19 +53,16 @@ class ServerIO extends IO.Server {
socket.user.firstname, socket.user.firstname,
socket.user.lastname socket.user.lastname
); );
if(!this.playersReady.hasOwnProperty(playerKey)){
this.playersReady[playerKey] = false; this.playersReady[playerKey] = false;
}
if(!this.playersScore.hasOwnProperty(playerKey)) {
this.playersScore[playerKey] = 0; this.playersScore[playerKey] = 0;
}
this.nbQuestion=0; this.nbQuestion=0;
console.log(`Player ${playerKey} is connected.`); console.log(`Player ${playerKey} is connected.`);
} }
this.testNumberofPlayer();
this.registerEventsOnSocket(socket);
});
} }
private registerEventsOnSocket(socket: SocketIoInfo) { private registerEventsOnSocket(socket: SocketIoInfo) {
const playerKey = socket.user.username; const playerKey = socket.user.username;
socket.on("player-ready", ()=>{ socket.on("player-ready", ()=>{
...@@ -91,26 +96,27 @@ class ServerIO extends IO.Server { ...@@ -91,26 +96,27 @@ class ServerIO extends IO.Server {
} }
else{ else{
let question={...this.currentQuestion}; let question={...this.currentQuestion};
question=question.dataValues as Question; question.correctResponse=-1;
randomQuestion = question; randomQuestion = question;
} }
console.log("send question", randomQuestion);
this.emit("question", randomQuestion); this.emit("question", randomQuestion);
this.nbQuestion++;
} }
}); });
socket.on("validate-question", async (responseSelected) => { socket.on("validate-question", async (responseSelected) => {
let randomQuestion = await this.getRandomQuestion(); console.log("current question:", this.currentQuestion);
if (responseSelected === this.currentQuestion.correctResponse) { if (responseSelected === this.currentQuestion.correctResponse) {
// le joueur gagne 1 point // le joueur gagne 1 point
console.log(`joueur ${playerKey} gagne 1 point`)
this.playersScore[playerKey]+=1; this.playersScore[playerKey]+=1;
}else{ }else{
console.log(`joueur ${playerKey} perd 2 point`)
// le joueur perd deux points // le joueur perd deux points
this.playersScore[playerKey]-=2; this.playersScore[playerKey]-=2;
} }
this.nbQuestion++; console.log("playerScore:", this.playersScore);
if(this.nbQuestion<10){ let randomQuestion = await this.getRandomQuestion();
if(this.nbQuestion<=10){
this.emit("question", randomQuestion); this.emit("question", randomQuestion);
}else{ }else{
const playersScoreFormatted = Object.keys(this.playersScore).reduce((formatted:any, key) => { const playersScoreFormatted = Object.keys(this.playersScore).reduce((formatted:any, key) => {
...@@ -119,7 +125,13 @@ class ServerIO extends IO.Server { ...@@ -119,7 +125,13 @@ class ServerIO extends IO.Server {
}, {}); }, {});
this.emit("game-finished", playersScoreFormatted); 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', () => { socket.on('disconnect', () => {
...@@ -133,6 +145,7 @@ class ServerIO extends IO.Server { ...@@ -133,6 +145,7 @@ class ServerIO extends IO.Server {
// Le joueur est connecté, retirez-le du dictionnaire // Le joueur est connecté, retirez-le du dictionnaire
delete this.players[playerKey]; delete this.players[playerKey];
delete this.playersReady[playerKey]; delete this.playersReady[playerKey];
delete this.playersScore[playerKey];
console.log(`Player ${playerKey} is disconnected.`); console.log(`Player ${playerKey} is disconnected.`);
} else { } else {
// Le joueur n'est pas trouvé dans le dictionnaire, cela peut être un cas anormal // Le joueur n'est pas trouvé dans le dictionnaire, cela peut être un cas anormal
...@@ -170,12 +183,10 @@ class ServerIO extends IO.Server { ...@@ -170,12 +183,10 @@ class ServerIO extends IO.Server {
} }
const randomIndex = Math.floor(Math.random() * this.questions.length); 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); this.questions.splice(randomIndex, 1);
// pour ne pas envoyer au frontend la reponse attendue // pour ne pas envoyer au frontend la reponse attendue
let randomQuestion={...this.currentQuestion}; return {...this.currentQuestion};
randomQuestion=randomQuestion.dataValues as Question;
return randomQuestion;
} }
private testNumberOfReady() { private testNumberOfReady() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment