diff --git a/public/js/listapartidas.js b/public/js/listapartidas.js index 987b8e6..a5162ad 100644 --- a/public/js/listapartidas.js +++ b/public/js/listapartidas.js @@ -6,14 +6,6 @@ export class ListaPartidas { agregarPartida(partida) { this._partidas.push(partida); } - actualizarPartida(partidaActualizada) { - const index = this._partidas.findIndex( - (partida) => partida.uuid === partidaActualizada.uuid, - ); - if (index !== -1) { - this._partidas[index] = partidaActualizada; - } - } eliminarPartida(uuid) { const index = this._partidas.findIndex((partida) => partida.uuid === uuid); if (index !== -1) { @@ -23,4 +15,5 @@ export class ListaPartidas { get partidas() { return this._partidas; } + } diff --git a/public/js/salaespera.js b/public/js/salaespera.js index d98359a..44a848a 100644 --- a/public/js/salaespera.js +++ b/public/js/salaespera.js @@ -13,6 +13,7 @@ const socket = io('/salachat', { closeOnBeforeunload: true, auth: { userId } }); let listaUsuarios; let listaPartidasAbiertas; +let listaPartidasEnCurso; // Función para generar elementos de lista de usuarios function generarElementoListaUsuario(usuario, claseCSS) { @@ -125,8 +126,8 @@ const handlers = { listaPartidasAbiertas.agregarPartida(partida); actualizarListaPartidasAbiertas(); }, - onJoinPartida: (partida) => { - listaPartidasAbiertas.eliminarPartida(partida); + onJoinPartida: (uuid) => { + listaPartidasAbiertas.eliminarPartida(uuid); actualizarListaPartidasAbiertas(); }, onCancelPartida: (uuid) => { @@ -145,6 +146,8 @@ Object.entries(handlers).forEach(([event, handler]) => { socket.on(event, handler); }); + + document.querySelector('#btnCerrar').addEventListener('click', () => { handlers.disconnect('buttonClick'); }); diff --git a/src/sala-chat/sala-chat.gateway.ts b/src/sala-chat/sala-chat.gateway.ts index 8a881ca..d6fe50a 100644 --- a/src/sala-chat/sala-chat.gateway.ts +++ b/src/sala-chat/sala-chat.gateway.ts @@ -7,7 +7,6 @@ import { } from '@nestjs/websockets'; import { SalaChatService } from './sala-chat.service'; import { Server, Socket } from 'socket.io'; -import { Resp } from 'src/interfaces/resp'; @WebSocketGateway({ namespace: 'salachat' }) export class SalaChatGateway @@ -19,12 +18,16 @@ export class SalaChatGateway handleConnection(client: Socket) { const userId = client.handshake.auth.userId; - const userConectado = this.salaChatService.conectaUsuarioUUID(userId); + const userConectado = this.salaChatService.conectaUsuarioUUID( + userId, + client.id, + ); if (userConectado) { client.join('chat_general'); client.emit('onConnectRoom', { - usuarios: this.salaChatService.listaUsuariosSinPartidas, + usuarios: this.salaChatService.listaUsuarios, partidas: this.salaChatService.partidasAbiertas, + partidasEnCurso: '', }); client.broadcast .to('chat_general') @@ -85,7 +88,8 @@ export class SalaChatGateway handleJoinPartida(client: Socket, uuidPartida: string) { const userId = client.handshake.auth.userId; const partida = this.salaChatService.unirsePartida(userId, uuidPartida); - client.broadcast.to('chat_general').emit('onJoinPartida', partida); - return partida; + + client.broadcast.to('chat_general').emit('onJoinPartida', partida.uuid); + return partida.uuid; } -} \ No newline at end of file +} diff --git a/src/sala-chat/sala-chat.service.ts b/src/sala-chat/sala-chat.service.ts index 334b9db..1ef1b0e 100644 --- a/src/sala-chat/sala-chat.service.ts +++ b/src/sala-chat/sala-chat.service.ts @@ -9,27 +9,23 @@ export class SalaChatService { private readonly partidasService: PartidasService, ) {} - get listaUsuariosSinPartidas() { - return Array.from(this.usuariosService.usuarios.values()).map( - ({ uuid, nickname, conectado }) => ({ - uuid, - nickname, - conectado, - }), - ); + get listaUsuarios() { + return Array.from(this.usuariosService.usuarios.values()); } get partidasAbiertas(){ return this.partidasService.partidasAbiertas; } + getPartidasEnCurso getUsuarioUUID(uuid: string) { return this.usuariosService.getUsuarioByUUID(uuid); } - conectaUsuarioUUID(uuid: string) { + conectaUsuarioUUID(uuid: string,socketId:string) { const user = this.usuariosService.getUsuarioByUUID(uuid); if (user) { + user.socketId=socketId; user.conectado = true; return { uuid: user.uuid, @@ -40,7 +36,6 @@ export class SalaChatService { return undefined; } } - desconectaUsuarioUUID(uuid: string) { const user = this.usuariosService.getUsuarioByUUID(uuid); if (user) { @@ -59,11 +54,9 @@ export class SalaChatService { const partida = this.partidasService.creaPartida(uuidJugadorCreador); return partida; } - unirsePartida(uuidJugadorB:string,uuidPartida:string){ return this.partidasService.unirsePartida(uuidJugadorB,uuidPartida); } - eliminarPartida(uuidJugador:string, uuidPartida:string){ return this.partidasService.eliminarPartida(uuidJugador,uuidPartida); } diff --git a/src/shared/partidas.service.ts b/src/shared/partidas.service.ts index df0327b..aee84a2 100644 --- a/src/shared/partidas.service.ts +++ b/src/shared/partidas.service.ts @@ -24,6 +24,12 @@ export class PartidasService { (partida) => partida.abierta, ); } + + get partidasCerradadas(): Partida[] { + return Array.from(this._partidas.values()).filter( + (partida) => !partida.abierta, + ); + } creaPartida(uuidJugadorA: string) { const uuid = uuidv4(); @@ -43,7 +49,6 @@ export class PartidasService { return partida; } - eliminarPartida(uuidJugador: string, uuidPartida: string) { if (!this._partidas.has(uuidPartida)) { return null; @@ -58,4 +63,5 @@ export class PartidasService { this._partidas.delete(uuidPartida); return uuidPartida; } + } diff --git a/src/shared/usuarios.service.ts b/src/shared/usuarios.service.ts index 52d8cc9..fcdf5bc 100644 --- a/src/shared/usuarios.service.ts +++ b/src/shared/usuarios.service.ts @@ -6,7 +6,16 @@ class Usuario { public uuid: string, public nickname: string, public conectado: boolean = false, + public socketId: string = '', ) {} + + toJSON() { + return { + uuid: this.uuid, + nickname: this.nickname, + conectado: this.conectado, + }; + } } @Injectable()