Skip to content
Snippets Groups Projects
Commit 97cbe2b8 authored by Ronnie Friman's avatar Ronnie Friman
Browse files

tied notifications to gameIds

parent 095178e0
No related branches found
No related tags found
3 merge requests!59Development to master,!36Development,!33Small fixes
...@@ -6,12 +6,15 @@ import { ...@@ -6,12 +6,15 @@ import {
OnGatewayConnection, OnGatewayConnection,
OnGatewayDisconnect, OnGatewayDisconnect,
} from '@nestjs/websockets'; } from '@nestjs/websockets';
import { Logger } from '@nestjs/common'; import { Logger, UsePipes } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { Server, Socket } from 'socket.io'; import { Server, Socket } from 'socket.io';
import { Repository } from 'typeorm'; import { Repository } from 'typeorm';
import { NotificationEntity } from './notification.entity'; import { NotificationEntity } from './notification.entity';
import { GameEntity } from '../game/game.entity';
import { NotificationdDTO } from './notification.dto';
import { ValidationPipe } from '../shared/validation.pipe';
@WebSocketGateway() @WebSocketGateway()
export class NotificationGateway export class NotificationGateway
...@@ -20,6 +23,8 @@ export class NotificationGateway ...@@ -20,6 +23,8 @@ export class NotificationGateway
//create references to tables as repositories //create references to tables as repositories
@InjectRepository(NotificationEntity) @InjectRepository(NotificationEntity)
private notificationRepository: Repository<NotificationEntity>, private notificationRepository: Repository<NotificationEntity>,
@InjectRepository(GameEntity)
private gameRepository: Repository<GameEntity>,
) {} ) {}
@WebSocketServer() @WebSocketServer()
server: Server; server: Server;
...@@ -40,14 +45,17 @@ export class NotificationGateway ...@@ -40,14 +45,17 @@ export class NotificationGateway
} }
// notifications for when game needs to be paused / stopped // notifications for when game needs to be paused / stopped
@SubscribeMessage('shutItDown') @SubscribeMessage('game-info')
async handleMessage(client: Socket, text: string) { @UsePipes(new ValidationPipe())
this.logger.log(text); async handleMessage(client: Socket, data: NotificationdDTO) {
// send the message to all clients listening to warningToPlayers branch // check if the game exists and is either started or paused
this.server.emit('warningToPlayers', text); const game = await this.gameRepository.findOne({ id: data.game });
// create entity properties if (game && ['STARTED', 'PAUSED'].includes(game.state)) {
const message = await this.notificationRepository.create({ message: text }); // send the message to all clients listening to gameId branch
// insert created entity NOTE: insert method doesn't check for duplicates. this.server.emit(data.game, data);
await this.notificationRepository.insert(message); // create entry for notification in db
const message = await this.notificationRepository.create(data);
await this.notificationRepository.insert(message);
}
} }
} }
...@@ -3,9 +3,10 @@ import { TypeOrmModule } from '@nestjs/typeorm'; ...@@ -3,9 +3,10 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { NotificationGateway } from './notifications.gateway'; import { NotificationGateway } from './notifications.gateway';
import { NotificationEntity } from './notification.entity'; import { NotificationEntity } from './notification.entity';
import { GameEntity } from '../game/game.entity';
@Module({ @Module({
imports: [TypeOrmModule.forFeature([NotificationEntity])], imports: [TypeOrmModule.forFeature([NotificationEntity, GameEntity])],
providers: [NotificationGateway], providers: [NotificationGateway],
exports: [NotificationGateway], exports: [NotificationGateway],
}) })
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment