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 {
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);
}
}
}
......@@ -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],
})
......
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