This commit is contained in:
Dmitry 2026-01-07 17:32:51 +03:00
parent e36f76bbc8
commit a09e9ad73c
4 changed files with 66 additions and 9 deletions

View file

@ -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}`);

View file

@ -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 {}

View file

@ -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);
}
}
}

View file

@ -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;