From e9f472026c8c928b7a00ae5e0b8bb2949959937d Mon Sep 17 00:00:00 2001 From: "Federico M." Date: Thu, 31 Oct 2024 02:45:44 +0100 Subject: [PATCH 1/2] Domande duplicate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ora non appaiono domande se sono già apparse nella partita precedente. --- .../components/finalresults/FinalResults.tsx | 2 -- Server/src/socket.ts | 30 ++++++++++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Scoprimi/src/components/finalresults/FinalResults.tsx b/Scoprimi/src/components/finalresults/FinalResults.tsx index 4e43786..952a682 100644 --- a/Scoprimi/src/components/finalresults/FinalResults.tsx +++ b/Scoprimi/src/components/finalresults/FinalResults.tsx @@ -58,9 +58,7 @@ const FinalResults: React.FC = () => { } useEffect(() => { - console.log('Lazza pelato'); socket.on(SocketEvents.ASK_TO_JOIN, (data) => { - console.log('Ancora Lazza pelato ', data); const datatoSend = { lobbyCode: data, diff --git a/Server/src/socket.ts b/Server/src/socket.ts index 535efc9..d3ae7cb 100644 --- a/Server/src/socket.ts +++ b/Server/src/socket.ts @@ -58,7 +58,7 @@ function checkLobbiesAge(io: any) { }); } -function myCreateLobby(socket, io, data: { code: string, numQuestionsParam: number, categories: string[], admin: string }) { +function myCreateLobby(socket, io, data: { code: string, numQuestionsParam: number, categories: string[], admin: string }, oldQuestions: Question[]) { console.log('Creo la lobby con [codice - domande - admin]: ', data.code, ' - ', data.numQuestionsParam, ' - ', data.admin); console.log('Categorie scelte: ', data.categories); actualGameManager.createGame(data.code, data.admin); @@ -116,7 +116,20 @@ function myCreateLobby(socket, io, data: { code: string, numQuestionsParam: numb }) .flat(); // Appiattisce l'array - actualGameManager.getGame(data.code).selectedQuestions = shuffle(allSelectedQuestions).slice(0, data.numQuestionsParam); + if (oldQuestions === undefined) { + console.log('Selezionando nuove domande'); + actualGameManager.getGame(data.code).selectedQuestions = shuffle(allSelectedQuestions).slice(0, data.numQuestionsParam); + } else { + console.log('Tolgo le domande vecchie'); + + // Filtra le domande nuove escludendo quelle già presenti in oldQuestions + const filteredQuestions = allSelectedQuestions.filter(newQuestion => + !oldQuestions.some(oldQuestion => oldQuestion.text === newQuestion.text) + ); + + // Mescola e seleziona il numero richiesto di domande, senza duplicati + actualGameManager.getGame(data.code).selectedQuestions = shuffle(filteredQuestions).slice(0, data.numQuestionsParam); + } const lobbies = actualGameManager.listGames(); io.emit(SocketEvents.RENDER_LOBBIES, { lobbies }); @@ -227,7 +240,11 @@ export function setupSocket(io: any) { callback(false); }); + // Creazione di un gioco successivo + // Creato da "Gioca ancora" socket.on(SocketEvents.SET_NEXT_GAME, (data: { code: string, playerName: string, image: string }) => { + console.log('Dati partita vecchia: ', actualGameManager.getGame(data.code).selectedQuestions); + const thisGame = actualGameManager.getGame(data.code); if (!thisGame) { socket.emit(SocketEvents.FORCE_RESET); @@ -243,13 +260,10 @@ export function setupSocket(io: any) { } if (thisGame.nextGame === undefined) { - // crea lobby - // e qua ci va il valore thisGame.nextGame - - myCreateLobby(socket, io, dataCreateLobby); + // crea lobby per partita successiva + myCreateLobby(socket, io, dataCreateLobby, actualGameManager.getGame(data.code).selectedQuestions); thisGame.nextGame = codeTmp; } else { - // TODO Roba del cantiere della sburra socket.emit(SocketEvents.ASK_TO_JOIN, thisGame.nextGame); } // return del new game? @@ -257,7 +271,7 @@ export function setupSocket(io: any) { // TODO check params on react socket.on(SocketEvents.CREATE_LOBBY, async (data: { code: string, numQuestionsParam: number, categories: string[], admin: string }) => { - myCreateLobby(socket, io, data); + myCreateLobby(socket, io, data, undefined); }); socket.on(SocketEvents.REQUEST_TO_JOIN_LOBBY, (data: { lobbyCode: string; playerName: string, image: string }) => { From 0f0297e2a6de2521bd508315a2757887c2f00a3a Mon Sep 17 00:00:00 2001 From: "Federico M." Date: Thu, 31 Oct 2024 12:11:29 +0100 Subject: [PATCH 2/2] Update socket.ts --- Server/src/socket.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Server/src/socket.ts b/Server/src/socket.ts index c02171f..087edbc 100644 --- a/Server/src/socket.ts +++ b/Server/src/socket.ts @@ -63,7 +63,7 @@ function myCreateLobby(socket, io, data: { code: string, numQuestionsParam: numb console.log('Creo la lobby con [codice - domande - admin]: ', data.code, ' - ', data.numQuestionsParam, ' - ', data.admin); actualGameManager.createGame(data.code, data.admin); const thisGame = actualGameManager.getGame(data.code); - thisGame.gamesGenre = data.categories; + //thisGame.gamesGenre = data.categories; Non so cosa sia const allSelectedQuestions = data.categories .map(category => { @@ -259,12 +259,6 @@ export function setupSocket(io: any) { // crea lobby per partita successiva myCreateLobby(socket, io, dataCreateLobby, actualGameManager.getGame(data.code).selectedQuestions); thisGame.nextGame = codeTmp; - } else { - socket.emit(SocketEvents.ASK_TO_JOIN, thisGame.nextGame); - // la lobby non esiste, crea lobby - // e qua ci va il valore thisGame.nextGame - myCreateLobby(socket, io, dataCreateLobby); - thisGame.nextGame = codeTmp; } else { // gia esiste il game, gli restituisco quello che esiste socket.emit(SocketEvents.RETURN_NEWGAME, { lobbyCode: thisGame.nextGame });