diff --git a/back/volume/src/pong/pong.gateway.ts b/back/volume/src/pong/pong.gateway.ts index a02fee7..bcf518b 100644 --- a/back/volume/src/pong/pong.gateway.ts +++ b/back/volume/src/pong/pong.gateway.ts @@ -20,6 +20,7 @@ import { MatchmakingQueue } from './game/MatchmakingQueue' import { MatchmakingDtoValidated } from './dtos/MatchmakingDtoValidated' import { PongService } from './pong.service' import { UsersService } from 'src/users/users.service' +import type User from 'src/users/entity/user.entity' @WebSocketGateway({ cors: { origin: new RegExp(`^(http|ws)://${process.env.HOST ?? 'localhost'}(:\\d+)?$`) } @@ -65,8 +66,33 @@ export class PongGateway implements OnGatewayConnection, OnGatewayDisconnect { @MessageBody('socketKey') socketKey: StringDtoValidated ): Promise<{ event: string, data: boolean }> { let succeeded: boolean = false - const user = await this.usersService.findUserByName(playerName.value) - // console.log(socketKey.value, user?.socketKey) + let user: User | null = null + try { + user = await this.usersService.findUserByName(playerName.value) + } catch (e) { + console.log('Failed to register player', playerName.value) + } + + // Check that socket key is not already registered + for (const [socket, name] of this.socketToPlayerName) { + try { + const _user: User = await this.usersService.findUserByName(name) + if (_user.socketKey === socketKey.value) { + console.log('Failed to register player', playerName.value, '(socket key already registered)') + } + } catch (e) { + // User does not exist anymore, unregister it + console.log('Disconnected player', name) + this.socketToPlayerName.delete(socket) + const game: Game | undefined = this.games.playerGame(name) + if (game !== undefined) { + game.stop(name) + } + this.matchmakingQueue.removePlayer(name) + this.socketToPlayerName.delete(client) + } + } + if ( user !== null && user.socketKey === socketKey.value && diff --git a/front/volume/src/components/Pong/GameCreation.svelte b/front/volume/src/components/Pong/GameCreation.svelte index 503e398..2e9cca6 100644 --- a/front/volume/src/components/Pong/GameCreation.svelte +++ b/front/volume/src/components/Pong/GameCreation.svelte @@ -10,6 +10,7 @@ import type { GameCreationDto } from "./dtos/GameCreationDto"; import { store } from "../../Auth"; import type { Socket } from "socket.io-client"; + import { show_popup } from "../Alert/content"; export let socket: Socket; export let invitedUsername: string; @@ -19,6 +20,10 @@ let initialBallSpeedY: number = DEFAULT_BALL_INITIAL_SPEED.y; function createGame() { + if ($store.username === invitedUsername) { + show_popup("You can't invite yourself to a game.", false); + return; + } const data: GameCreationDto = { playerNames: [$store.username, invitedUsername], map, diff --git a/front/volume/src/components/Pong/Pong.svelte b/front/volume/src/components/Pong/Pong.svelte index fb52fea..a1c0b3f 100644 --- a/front/volume/src/components/Pong/Pong.svelte +++ b/front/volume/src/components/Pong/Pong.svelte @@ -12,13 +12,17 @@ import { io, Socket } from "socket.io-client"; import type { GameUpdate } from "./dtos/GameUpdate"; import type { GameInfo } from "./dtos/GameInfo"; - import { popup } from "../Alert/content"; + import { popup, show_popup } from "../Alert/content"; import Alert from "../Alert/Alert.svelte"; import { bind } from 'svelte-simple-modal'; export function inviteToGame(event: CustomEvent) { - setAppState(APPSTATE.CREATE_GAME); invitedUsername = event.detail; + if ($store.username === invitedUsername) { + show_popup("You can't invite yourself to a game.", false); + } else { + setAppState(APPSTATE.CREATE_GAME); + } } export function resetGameConnection() {