diff --git a/src/notifications/notifications.gateway.ts b/src/notifications/notifications.gateway.ts index c30114895ffa8d4b40c3cedeaf827c5b3401cb25..5e952d6c78c6f35977f362a7459c0ebf65d1ab5e 100644 --- a/src/notifications/notifications.gateway.ts +++ b/src/notifications/notifications.gateway.ts @@ -6,12 +6,15 @@ import { OnGatewayConnection, OnGatewayDisconnect, } from '@nestjs/websockets'; -import { Logger } from '@nestjs/common'; +import { Logger, UsePipes } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Server, Socket } from 'socket.io'; import { Repository } from 'typeorm'; import { NotificationEntity } from './notification.entity'; +import { GameEntity } from '../game/game.entity'; +import { NotificationdDTO } from './notification.dto'; +import { ValidationPipe } from '../shared/validation.pipe'; @WebSocketGateway() export class NotificationGateway @@ -20,6 +23,8 @@ export class NotificationGateway //create references to tables as repositories @InjectRepository(NotificationEntity) private notificationRepository: Repository<NotificationEntity>, + @InjectRepository(GameEntity) + private gameRepository: Repository<GameEntity>, ) {} @WebSocketServer() server: Server; @@ -40,14 +45,17 @@ export class NotificationGateway } // notifications for when game needs to be paused / stopped - @SubscribeMessage('shutItDown') - async handleMessage(client: Socket, text: string) { - this.logger.log(text); - // send the message to all clients listening to warningToPlayers branch - this.server.emit('warningToPlayers', text); - // create entity properties - const message = await this.notificationRepository.create({ message: text }); - // insert created entity NOTE: insert method doesn't check for duplicates. - await this.notificationRepository.insert(message); + @SubscribeMessage('game-info') + @UsePipes(new ValidationPipe()) + async handleMessage(client: Socket, data: NotificationdDTO) { + // check if the game exists and is either started or paused + const game = await this.gameRepository.findOne({ id: data.game }); + if (game && ['STARTED', 'PAUSED'].includes(game.state)) { + // send the message to all clients listening to gameId branch + this.server.emit(data.game, data); + // create entry for notification in db + const message = await this.notificationRepository.create(data); + await this.notificationRepository.insert(message); + } } } diff --git a/src/notifications/notifications.module.ts b/src/notifications/notifications.module.ts index 194c5c38155d0f976d286456a53814a08f517050..41e2fcaa36d1928dff9a60bd2c6853e240baf16f 100644 --- a/src/notifications/notifications.module.ts +++ b/src/notifications/notifications.module.ts @@ -3,9 +3,10 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { NotificationGateway } from './notifications.gateway'; import { NotificationEntity } from './notification.entity'; +import { GameEntity } from '../game/game.entity'; @Module({ - imports: [TypeOrmModule.forFeature([NotificationEntity])], + imports: [TypeOrmModule.forFeature([NotificationEntity, GameEntity])], providers: [NotificationGateway], exports: [NotificationGateway], })