Refactorizaciones
This commit is contained in:
parent
ab6068d578
commit
7f60c7651a
|
|
@ -6,14 +6,6 @@ export class ListaPartidas {
|
||||||
agregarPartida(partida) {
|
agregarPartida(partida) {
|
||||||
this._partidas.push(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) {
|
eliminarPartida(uuid) {
|
||||||
const index = this._partidas.findIndex((partida) => partida.uuid === uuid);
|
const index = this._partidas.findIndex((partida) => partida.uuid === uuid);
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
|
|
@ -23,4 +15,5 @@ export class ListaPartidas {
|
||||||
get partidas() {
|
get partidas() {
|
||||||
return this._partidas;
|
return this._partidas;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ const socket = io('/salachat', { closeOnBeforeunload: true, auth: { userId } });
|
||||||
|
|
||||||
let listaUsuarios;
|
let listaUsuarios;
|
||||||
let listaPartidasAbiertas;
|
let listaPartidasAbiertas;
|
||||||
|
let listaPartidasEnCurso;
|
||||||
|
|
||||||
// Función para generar elementos de lista de usuarios
|
// Función para generar elementos de lista de usuarios
|
||||||
function generarElementoListaUsuario(usuario, claseCSS) {
|
function generarElementoListaUsuario(usuario, claseCSS) {
|
||||||
|
|
@ -125,8 +126,8 @@ const handlers = {
|
||||||
listaPartidasAbiertas.agregarPartida(partida);
|
listaPartidasAbiertas.agregarPartida(partida);
|
||||||
actualizarListaPartidasAbiertas();
|
actualizarListaPartidasAbiertas();
|
||||||
},
|
},
|
||||||
onJoinPartida: (partida) => {
|
onJoinPartida: (uuid) => {
|
||||||
listaPartidasAbiertas.eliminarPartida(partida);
|
listaPartidasAbiertas.eliminarPartida(uuid);
|
||||||
actualizarListaPartidasAbiertas();
|
actualizarListaPartidasAbiertas();
|
||||||
},
|
},
|
||||||
onCancelPartida: (uuid) => {
|
onCancelPartida: (uuid) => {
|
||||||
|
|
@ -145,6 +146,8 @@ Object.entries(handlers).forEach(([event, handler]) => {
|
||||||
socket.on(event, handler);
|
socket.on(event, handler);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
document.querySelector('#btnCerrar').addEventListener('click', () => {
|
document.querySelector('#btnCerrar').addEventListener('click', () => {
|
||||||
handlers.disconnect('buttonClick');
|
handlers.disconnect('buttonClick');
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ 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
|
||||||
|
|
@ -19,12 +18,16 @@ export class SalaChatGateway
|
||||||
|
|
||||||
handleConnection(client: Socket) {
|
handleConnection(client: Socket) {
|
||||||
const userId = client.handshake.auth.userId;
|
const userId = client.handshake.auth.userId;
|
||||||
const userConectado = this.salaChatService.conectaUsuarioUUID(userId);
|
const userConectado = this.salaChatService.conectaUsuarioUUID(
|
||||||
|
userId,
|
||||||
|
client.id,
|
||||||
|
);
|
||||||
if (userConectado) {
|
if (userConectado) {
|
||||||
client.join('chat_general');
|
client.join('chat_general');
|
||||||
client.emit('onConnectRoom', {
|
client.emit('onConnectRoom', {
|
||||||
usuarios: this.salaChatService.listaUsuariosSinPartidas,
|
usuarios: this.salaChatService.listaUsuarios,
|
||||||
partidas: this.salaChatService.partidasAbiertas,
|
partidas: this.salaChatService.partidasAbiertas,
|
||||||
|
partidasEnCurso: '',
|
||||||
});
|
});
|
||||||
client.broadcast
|
client.broadcast
|
||||||
.to('chat_general')
|
.to('chat_general')
|
||||||
|
|
@ -85,7 +88,8 @@ export class SalaChatGateway
|
||||||
handleJoinPartida(client: Socket, uuidPartida: string) {
|
handleJoinPartida(client: Socket, uuidPartida: string) {
|
||||||
const userId = client.handshake.auth.userId;
|
const userId = client.handshake.auth.userId;
|
||||||
const partida = this.salaChatService.unirsePartida(userId, uuidPartida);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,27 +9,23 @@ export class SalaChatService {
|
||||||
private readonly partidasService: PartidasService,
|
private readonly partidasService: PartidasService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
get listaUsuariosSinPartidas() {
|
get listaUsuarios() {
|
||||||
return Array.from(this.usuariosService.usuarios.values()).map(
|
return Array.from(this.usuariosService.usuarios.values());
|
||||||
({ uuid, nickname, conectado }) => ({
|
|
||||||
uuid,
|
|
||||||
nickname,
|
|
||||||
conectado,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get partidasAbiertas(){
|
get partidasAbiertas(){
|
||||||
return this.partidasService.partidasAbiertas;
|
return this.partidasService.partidasAbiertas;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getPartidasEnCurso
|
||||||
|
|
||||||
getUsuarioUUID(uuid: string) {
|
getUsuarioUUID(uuid: string) {
|
||||||
return this.usuariosService.getUsuarioByUUID(uuid);
|
return this.usuariosService.getUsuarioByUUID(uuid);
|
||||||
}
|
}
|
||||||
conectaUsuarioUUID(uuid: string) {
|
conectaUsuarioUUID(uuid: string,socketId:string) {
|
||||||
const user = this.usuariosService.getUsuarioByUUID(uuid);
|
const user = this.usuariosService.getUsuarioByUUID(uuid);
|
||||||
if (user) {
|
if (user) {
|
||||||
|
user.socketId=socketId;
|
||||||
user.conectado = true;
|
user.conectado = true;
|
||||||
return {
|
return {
|
||||||
uuid: user.uuid,
|
uuid: user.uuid,
|
||||||
|
|
@ -40,7 +36,6 @@ export class SalaChatService {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
desconectaUsuarioUUID(uuid: string) {
|
desconectaUsuarioUUID(uuid: string) {
|
||||||
const user = this.usuariosService.getUsuarioByUUID(uuid);
|
const user = this.usuariosService.getUsuarioByUUID(uuid);
|
||||||
if (user) {
|
if (user) {
|
||||||
|
|
@ -59,11 +54,9 @@ export class SalaChatService {
|
||||||
const partida = this.partidasService.creaPartida(uuidJugadorCreador);
|
const partida = this.partidasService.creaPartida(uuidJugadorCreador);
|
||||||
return partida;
|
return partida;
|
||||||
}
|
}
|
||||||
|
|
||||||
unirsePartida(uuidJugadorB:string,uuidPartida:string){
|
unirsePartida(uuidJugadorB:string,uuidPartida:string){
|
||||||
return this.partidasService.unirsePartida(uuidJugadorB,uuidPartida);
|
return this.partidasService.unirsePartida(uuidJugadorB,uuidPartida);
|
||||||
}
|
}
|
||||||
|
|
||||||
eliminarPartida(uuidJugador:string, uuidPartida:string){
|
eliminarPartida(uuidJugador:string, uuidPartida:string){
|
||||||
return this.partidasService.eliminarPartida(uuidJugador,uuidPartida);
|
return this.partidasService.eliminarPartida(uuidJugador,uuidPartida);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,12 @@ export class PartidasService {
|
||||||
(partida) => partida.abierta,
|
(partida) => partida.abierta,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get partidasCerradadas(): Partida[] {
|
||||||
|
return Array.from(this._partidas.values()).filter(
|
||||||
|
(partida) => !partida.abierta,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
creaPartida(uuidJugadorA: string) {
|
creaPartida(uuidJugadorA: string) {
|
||||||
const uuid = uuidv4();
|
const uuid = uuidv4();
|
||||||
|
|
@ -43,7 +49,6 @@ export class PartidasService {
|
||||||
return partida;
|
return partida;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
eliminarPartida(uuidJugador: string, uuidPartida: string) {
|
eliminarPartida(uuidJugador: string, uuidPartida: string) {
|
||||||
if (!this._partidas.has(uuidPartida)) {
|
if (!this._partidas.has(uuidPartida)) {
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -58,4 +63,5 @@ export class PartidasService {
|
||||||
this._partidas.delete(uuidPartida);
|
this._partidas.delete(uuidPartida);
|
||||||
return uuidPartida;
|
return uuidPartida;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,16 @@ class Usuario {
|
||||||
public uuid: string,
|
public uuid: string,
|
||||||
public nickname: string,
|
public nickname: string,
|
||||||
public conectado: boolean = false,
|
public conectado: boolean = false,
|
||||||
|
public socketId: string = '',
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
return {
|
||||||
|
uuid: this.uuid,
|
||||||
|
nickname: this.nickname,
|
||||||
|
conectado: this.conectado,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user