refactorizaciones

This commit is contained in:
Marklogo 2024-04-04 02:02:37 +02:00
parent b460bd6c65
commit c27de6f4f0
5 changed files with 28 additions and 19 deletions

View File

@ -97,11 +97,10 @@ const actualizaUsuarios = (usuario) => {
? agregaMsgBroadcast(`${usuario.nickname} se ha conectado`)
: agregaMsgBroadcast(`${usuario.nickname} se ha desconectado`);
};
const agregarPartida = (partida) =>{
const agregarPartida = (partida) => {
listaPartidas.agregarPartida(partida);
listarPartidasAbiertas();
}
};
// Cerrar sesión
const cerrarSesion = (reason) => {
@ -115,7 +114,7 @@ function enviarMensaje(event) {
if (event.key === 'Enter') {
const mensaje = event.target.value.trim();
if (mensaje != '') {
socket.emit('chatMsg', mensaje);
socket.emit('chatMsg', mensaje, agregaMsg);
}
event.target.value = '';
}
@ -128,7 +127,7 @@ function crearPartida() {
socket.on('onConnectRoom', entraSala);
socket.on('onUserConnectRoom', actualizaUsuarios);
socket.on('chatMsg', agregaMsg);
socket.on('onCrearPartida',agregarPartida);
socket.on('onCrearPartida', agregarPartida);
socket.on('onUserDisconnectRoom', actualizaUsuarios);
socket.on('disconnect', cerrarSesion);

19
src/interfaces/resp.ts Normal file
View File

@ -0,0 +1,19 @@
export class Resp<T> {
constructor({
status = 200,
msg = '',
data,
}: {
status?: number;
msg?: string;
data: T;
}) {
this.status = status;
this.msg = msg;
this.data = data;
}
status?: number;
msg?: string;
data: T;
}

View File

@ -7,6 +7,7 @@ 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
@ -43,13 +44,14 @@ export class SalaChatGateway
}
client.leave('chat_general');
}
@SubscribeMessage('chatMsg')
handleMsg(client: Socket, msg: string) {
const userId = client.handshake.auth.userId;
const user = this.salaChatService.getUsuarioUUID(userId);
if (user) {
this.server.to('chat_general').emit('chatMsg', { uuid: userId, msg });
if (user) {
client.broadcast.to('chat_general').emit('chatMsg',{ uuid: userId, msg });
return { uuid: userId, msg };
} else {
client.disconnect();
}

View File

@ -57,7 +57,6 @@ export class SalaChatService {
creaPartida(uuidJugadorCreador: string) {
const partida = this.partidasService.creaPartida(uuidJugadorCreador);
this.usuariosService.addPartidaToUsuario(uuidJugadorCreador, partida);
return partida;
}
}

View File

@ -1,9 +1,7 @@
import { v4 as uuidv4 } from 'uuid';
import { Injectable, Scope } from '@nestjs/common';
import { Partida } from './partidas.service';
class Usuario {
partidas: Partida[] = [];
constructor(
public uuid: string,
public nickname: string,
@ -38,12 +36,4 @@ export class UsuariosService {
}
return undefined;
}
addPartidaToUsuario(uuid: string, partida: Partida) {
this._usuarios.get(uuid).partidas.push(partida);
}
getPartidasUsuario(uuid: string) {
return this._usuarios.get(uuid).partidas;
}
}