diff --git a/backend/src/game/game.gateway.ts b/backend/src/game/game.gateway.ts index c4ca16b..af0758e 100644 --- a/backend/src/game/game.gateway.ts +++ b/backend/src/game/game.gateway.ts @@ -4,9 +4,11 @@ import { SubscribeMessage, OnGatewayConnection, OnGatewayDisconnect, + OnGatewayInit, } from '@nestjs/websockets'; import { Server, Socket } from 'socket.io'; import { RoomsService } from '../rooms/rooms.service'; +import { RoomEventsService } from './room-events.service'; @WebSocketGateway({ cors: { @@ -16,11 +18,18 @@ import { RoomsService } from '../rooms/rooms.service'; credentials: true, }, }) -export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect { +export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect, OnGatewayInit { @WebSocketServer() server: Server; - constructor(private roomsService: RoomsService) {} + constructor( + private roomsService: RoomsService, + private roomEventsService: RoomEventsService, + ) {} + + afterInit(server: Server) { + this.roomEventsService.setServer(server); + } handleConnection(client: Socket) { console.log(`Client connected: ${client.id}`); diff --git a/backend/src/game/game.module.ts b/backend/src/game/game.module.ts index 638d727..411662a 100644 --- a/backend/src/game/game.module.ts +++ b/backend/src/game/game.module.ts @@ -1,10 +1,11 @@ import { Module, forwardRef } from '@nestjs/common'; import { GameGateway } from './game.gateway'; +import { RoomEventsService } from './room-events.service'; import { RoomsModule } from '../rooms/rooms.module'; @Module({ imports: [forwardRef(() => RoomsModule)], - providers: [GameGateway], - exports: [GameGateway], + providers: [GameGateway, RoomEventsService], + exports: [RoomEventsService], }) export class GameModule {} diff --git a/backend/src/game/room-events.service.ts b/backend/src/game/room-events.service.ts new file mode 100644 index 0000000..dc48261 --- /dev/null +++ b/backend/src/game/room-events.service.ts @@ -0,0 +1,47 @@ +import { Injectable } from '@nestjs/common'; +import { Server } from 'socket.io'; + +@Injectable() +export class RoomEventsService { + private server: Server; + + setServer(server: Server) { + this.server = server; + } + + emitRoomUpdate(roomCode: string, room: any) { + if (this.server) { + this.server.to(roomCode).emit('roomUpdate', room); + } + } + + emitGameStarted(roomCode: string, room: any) { + if (this.server) { + this.server.to(roomCode).emit('gameStarted', room); + } + } + + emitAnswerRevealed(roomCode: string, data: any) { + if (this.server) { + this.server.to(roomCode).emit('answerRevealed', data); + } + } + + emitScoreUpdated(roomCode: string, data: any) { + if (this.server) { + this.server.to(roomCode).emit('scoreUpdated', data); + } + } + + emitQuestionChanged(roomCode: string, data: any) { + if (this.server) { + this.server.to(roomCode).emit('questionChanged', data); + } + } + + emitGameEnded(roomCode: string, data: any) { + if (this.server) { + this.server.to(roomCode).emit('gameEnded', data); + } + } +} diff --git a/backend/src/rooms/rooms.service.ts b/backend/src/rooms/rooms.service.ts index f5c8f7c..6bb7269 100644 --- a/backend/src/rooms/rooms.service.ts +++ b/backend/src/rooms/rooms.service.ts @@ -1,7 +1,7 @@ import { Injectable, Inject, forwardRef } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; import { customAlphabet } from 'nanoid'; -import { GameGateway } from '../game/game.gateway'; +import { RoomEventsService } from '../game/room-events.service'; const nanoid = customAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 6); @@ -9,8 +9,8 @@ const nanoid = customAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 6); export class RoomsService { constructor( private prisma: PrismaService, - @Inject(forwardRef(() => GameGateway)) - private gameGateway: GameGateway, + @Inject(forwardRef(() => RoomEventsService)) + private roomEventsService: RoomEventsService, ) {} async createRoom(hostId: string, questionPackId?: string, settings?: any) { @@ -85,8 +85,8 @@ export class RoomsService { }); // Отправляем событие roomUpdate всем клиентам в комнате - if (room && this.gameGateway?.server) { - this.gameGateway.server.to(room.code).emit('roomUpdate', room); + if (room) { + this.roomEventsService.emitRoomUpdate(room.code, room); } return participant;