first commit

This commit is contained in:
2024-04-01 01:56:05 +02:00
commit 5c64b4e147
102 changed files with 10838 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
import {
OnGatewayConnection,
OnGatewayDisconnect,
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
} from '@nestjs/websockets';
import { SalaChatService } from './sala-chat.service';
import { Server, Socket } from 'socket.io';
@WebSocketGateway({ namespace: 'salachat' })
export class SalaChatGateway
implements OnGatewayConnection, OnGatewayDisconnect
{
@WebSocketServer()
server: Server;
constructor(private readonly salaChatService: SalaChatService) {}
handleConnection(client: Socket) {
const userId = client.handshake.auth.userId;
const userConectado = this.salaChatService.conectaUsuarioUUID(userId);
if (userConectado){
client.join('chat_general');
client.emit('onConnectRoom',this.salaChatService.listaUsuariosSinPartidas);
client.broadcast.to('chat_general').emit('onUserConnectRoom',userConectado);
}else{
client.disconnect();
}
}
handleDisconnect(client: Socket) {
const userId = client.handshake.auth.userId;
const userDesconectado = this.salaChatService.desconectaUsuarioUUID(userId);
if (userDesconectado){
client.broadcast.to('chat_general').emit('onUserDisconnectRoom',userDesconectado);
}
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 });
} else {
client.disconnect();
}
}
sendBroadcastMsg(msg:string){
this.server.to('chat_general').emit('broadcastMsg',msg);
}
@SubscribeMessage('creaPartida')
handleCreaPartida(client: Socket) {
const userId = client.handshake.auth.userId;
const partida= this.salaChatService.creaPartida(userId);
this.server.to('chat_general').emit('on_create_partida',partida);
}
}

View File

@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { SalaChatService } from './sala-chat.service';
import { SalaChatGateway } from './sala-chat.gateway';
import { SharedModule } from 'src/shared/shared.module';
@Module({
imports:[SharedModule],
providers: [SalaChatGateway, SalaChatService],
})
export class SalaChatModule {}

View File

@@ -0,0 +1,58 @@
import { Injectable } from '@nestjs/common';
import { Partida, PartidasService } from 'src/shared/partidas.service';
import { UsuariosService } from 'src/shared/usuarios.service';
@Injectable()
export class SalaChatService {
constructor(
private readonly usuariosService: UsuariosService,
private readonly partidasService: PartidasService,
) {}
get listaUsuariosSinPartidas() {
return Array.from(this.usuariosService.usuarios.values()).map(
({ uuid, nickname, conectado }) => ({
uuid,
nickname,
conectado,
}),
);
}
getUsuarioUUID(uuid: string) {
return this.usuariosService.getUsuarioByUUID(uuid);
}
conectaUsuarioUUID(uuid: string) {
const user = this.usuariosService.getUsuarioByUUID(uuid);
if (user) {
user.conectado = true;
return {
uuid: user.uuid,
nickname: user.nickname,
conectado: user.conectado,
};
} else {
return undefined;
}
}
desconectaUsuarioUUID(uuid: string) {
const user = this.usuariosService.getUsuarioByUUID(uuid);
if (user) {
user.conectado = false;
return {
uuid: user.uuid,
nickname: user.nickname,
conectado: user.conectado,
};
} else {
return undefined;
}
}
creaPartida(uuidJugadorCreador: string) {
const partida = this.partidasService.creaPartida(uuidJugadorCreador);
this.usuariosService.addPartidaToUsuario(uuidJugadorCreador, partida);
return partida;
}
}