Skip to content

Commit

Permalink
Merge pull request #102 from Cleo-Tech/80-crea-nuova-partita-automati…
Browse files Browse the repository at this point in the history
…camente

80 crea nuova partita automaticamente
  • Loading branch information
fmanto01 authored Oct 31, 2024
2 parents c305ad8 + 0f0297e commit 367ec32
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
13 changes: 13 additions & 0 deletions Scoprimi/src/components/finalresults/FinalResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ const FinalResults: React.FC = () => {
socket.emit(SocketEvents.SET_NEXT_GAME, { code: currentLobby, playerName: currentPlayer, image: currentPlayerImage });
}

useEffect(() => {
socket.on(SocketEvents.ASK_TO_JOIN, (data) => {

const datatoSend = {
lobbyCode: data,
playerName: currentPlayer,
image: currentPlayerImage,
};
socket.emit(SocketEvents.REQUEST_TO_JOIN_LOBBY, datatoSend);

});
}, [currentPlayer, currentPlayerImage]);

useEffect(() => {
socket.on(SocketEvents.PLAYER_CAN_JOIN, (data) => {
if (data.canJoin) {
Expand Down
32 changes: 24 additions & 8 deletions Server/src/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ function checkLobbiesAge(io: any) {
});
}

function myCreateLobby(socket, io, data: { code: string, numQuestionsParam: number, categories: QuestionGenre[], 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);
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 => {
Expand Down Expand Up @@ -110,7 +110,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 });
Expand Down Expand Up @@ -222,7 +235,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);
Expand All @@ -239,9 +256,8 @@ export function setupSocket(io: any) {
}

if (thisGame.nextGame === undefined) {
// la lobby non esiste, 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 {
// gia esiste il game, gli restituisco quello che esiste
Expand All @@ -250,8 +266,8 @@ export function setupSocket(io: any) {
});

// TODO check params on react
socket.on(SocketEvents.CREATE_LOBBY, async (data: { code: string, numQuestionsParam: number, categories: QuestionGenre[], admin: string }) => {
myCreateLobby(socket, io, data);
socket.on(SocketEvents.CREATE_LOBBY, async (data: { code: string, numQuestionsParam: number, categories: string[], admin: string }) => {
myCreateLobby(socket, io, data, undefined);
});

socket.on(SocketEvents.REQUEST_TO_JOIN_LOBBY, (data: { lobbyCode: string; playerName: string, image: string }) => {
Expand Down

0 comments on commit 367ec32

Please sign in to comment.