Refactorizaciones

This commit is contained in:
Marklogo 2024-04-05 13:51:24 +02:00
parent ab6068d578
commit 7f60c7651a
6 changed files with 37 additions and 29 deletions

View File

@ -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;
}
}

View File

@ -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');
});

View File

@ -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;
}
}
}

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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()