refactorizaciones
This commit is contained in:
parent
b460bd6c65
commit
c27de6f4f0
|
|
@ -97,11 +97,10 @@ const actualizaUsuarios = (usuario) => {
|
||||||
? agregaMsgBroadcast(`${usuario.nickname} se ha conectado`)
|
? agregaMsgBroadcast(`${usuario.nickname} se ha conectado`)
|
||||||
: agregaMsgBroadcast(`${usuario.nickname} se ha desconectado`);
|
: agregaMsgBroadcast(`${usuario.nickname} se ha desconectado`);
|
||||||
};
|
};
|
||||||
const agregarPartida = (partida) =>{
|
const agregarPartida = (partida) => {
|
||||||
listaPartidas.agregarPartida(partida);
|
listaPartidas.agregarPartida(partida);
|
||||||
listarPartidasAbiertas();
|
listarPartidasAbiertas();
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
// Cerrar sesión
|
// Cerrar sesión
|
||||||
const cerrarSesion = (reason) => {
|
const cerrarSesion = (reason) => {
|
||||||
|
|
@ -115,7 +114,7 @@ function enviarMensaje(event) {
|
||||||
if (event.key === 'Enter') {
|
if (event.key === 'Enter') {
|
||||||
const mensaje = event.target.value.trim();
|
const mensaje = event.target.value.trim();
|
||||||
if (mensaje != '') {
|
if (mensaje != '') {
|
||||||
socket.emit('chatMsg', mensaje);
|
socket.emit('chatMsg', mensaje, agregaMsg);
|
||||||
}
|
}
|
||||||
event.target.value = '';
|
event.target.value = '';
|
||||||
}
|
}
|
||||||
|
|
@ -128,7 +127,7 @@ function crearPartida() {
|
||||||
socket.on('onConnectRoom', entraSala);
|
socket.on('onConnectRoom', entraSala);
|
||||||
socket.on('onUserConnectRoom', actualizaUsuarios);
|
socket.on('onUserConnectRoom', actualizaUsuarios);
|
||||||
socket.on('chatMsg', agregaMsg);
|
socket.on('chatMsg', agregaMsg);
|
||||||
socket.on('onCrearPartida',agregarPartida);
|
socket.on('onCrearPartida', agregarPartida);
|
||||||
socket.on('onUserDisconnectRoom', actualizaUsuarios);
|
socket.on('onUserDisconnectRoom', actualizaUsuarios);
|
||||||
socket.on('disconnect', cerrarSesion);
|
socket.on('disconnect', cerrarSesion);
|
||||||
|
|
||||||
|
|
|
||||||
19
src/interfaces/resp.ts
Normal file
19
src/interfaces/resp.ts
Normal 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;
|
||||||
|
}
|
||||||
|
|
@ -7,6 +7,7 @@ import {
|
||||||
} from '@nestjs/websockets';
|
} from '@nestjs/websockets';
|
||||||
import { SalaChatService } from './sala-chat.service';
|
import { SalaChatService } from './sala-chat.service';
|
||||||
import { Server, Socket } from 'socket.io';
|
import { Server, Socket } from 'socket.io';
|
||||||
|
import { Resp } from 'src/interfaces/resp';
|
||||||
|
|
||||||
@WebSocketGateway({ namespace: 'salachat' })
|
@WebSocketGateway({ namespace: 'salachat' })
|
||||||
export class SalaChatGateway
|
export class SalaChatGateway
|
||||||
|
|
@ -43,13 +44,14 @@ export class SalaChatGateway
|
||||||
}
|
}
|
||||||
client.leave('chat_general');
|
client.leave('chat_general');
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeMessage('chatMsg')
|
@SubscribeMessage('chatMsg')
|
||||||
handleMsg(client: Socket, msg: string) {
|
handleMsg(client: Socket, msg: string) {
|
||||||
const userId = client.handshake.auth.userId;
|
const userId = client.handshake.auth.userId;
|
||||||
const user = this.salaChatService.getUsuarioUUID(userId);
|
const user = this.salaChatService.getUsuarioUUID(userId);
|
||||||
if (user) {
|
if (user) {
|
||||||
this.server.to('chat_general').emit('chatMsg', { uuid: userId, msg });
|
client.broadcast.to('chat_general').emit('chatMsg',{ uuid: userId, msg });
|
||||||
|
return { uuid: userId, msg };
|
||||||
} else {
|
} else {
|
||||||
client.disconnect();
|
client.disconnect();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,6 @@ export class SalaChatService {
|
||||||
|
|
||||||
creaPartida(uuidJugadorCreador: string) {
|
creaPartida(uuidJugadorCreador: string) {
|
||||||
const partida = this.partidasService.creaPartida(uuidJugadorCreador);
|
const partida = this.partidasService.creaPartida(uuidJugadorCreador);
|
||||||
this.usuariosService.addPartidaToUsuario(uuidJugadorCreador, partida);
|
|
||||||
return partida;
|
return partida;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
import { Injectable, Scope } from '@nestjs/common';
|
import { Injectable, Scope } from '@nestjs/common';
|
||||||
import { Partida } from './partidas.service';
|
|
||||||
|
|
||||||
class Usuario {
|
class Usuario {
|
||||||
partidas: Partida[] = [];
|
|
||||||
constructor(
|
constructor(
|
||||||
public uuid: string,
|
public uuid: string,
|
||||||
public nickname: string,
|
public nickname: string,
|
||||||
|
|
@ -38,12 +36,4 @@ export class UsuariosService {
|
||||||
}
|
}
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
addPartidaToUsuario(uuid: string, partida: Partida) {
|
|
||||||
this._usuarios.get(uuid).partidas.push(partida);
|
|
||||||
}
|
|
||||||
|
|
||||||
getPartidasUsuario(uuid: string) {
|
|
||||||
return this._usuarios.get(uuid).partidas;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user