From e36f76bbc87e0f15dd93615975a23a2851b7f8a3 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 7 Jan 2026 17:23:06 +0300 Subject: [PATCH] stuff --- backend/src/game/game.module.ts | 5 +++-- backend/src/rooms/rooms.module.ts | 4 +++- backend/src/rooms/rooms.service.ts | 30 +++++++++++++++++++++++++++--- src/hooks/useRoom.js | 3 +++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/backend/src/game/game.module.ts b/backend/src/game/game.module.ts index 0bec456..638d727 100644 --- a/backend/src/game/game.module.ts +++ b/backend/src/game/game.module.ts @@ -1,9 +1,10 @@ -import { Module } from '@nestjs/common'; +import { Module, forwardRef } from '@nestjs/common'; import { GameGateway } from './game.gateway'; import { RoomsModule } from '../rooms/rooms.module'; @Module({ - imports: [RoomsModule], + imports: [forwardRef(() => RoomsModule)], providers: [GameGateway], + exports: [GameGateway], }) export class GameModule {} diff --git a/backend/src/rooms/rooms.module.ts b/backend/src/rooms/rooms.module.ts index 499a52b..f01534c 100644 --- a/backend/src/rooms/rooms.module.ts +++ b/backend/src/rooms/rooms.module.ts @@ -1,8 +1,10 @@ -import { Module } from '@nestjs/common'; +import { Module, forwardRef } from '@nestjs/common'; import { RoomsService } from './rooms.service'; import { RoomsController } from './rooms.controller'; +import { GameModule } from '../game/game.module'; @Module({ + imports: [forwardRef(() => GameModule)], controllers: [RoomsController], providers: [RoomsService], exports: [RoomsService], diff --git a/backend/src/rooms/rooms.service.ts b/backend/src/rooms/rooms.service.ts index 953c373..f5c8f7c 100644 --- a/backend/src/rooms/rooms.service.ts +++ b/backend/src/rooms/rooms.service.ts @@ -1,12 +1,17 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, Inject, forwardRef } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; import { customAlphabet } from 'nanoid'; +import { GameGateway } from '../game/game.gateway'; const nanoid = customAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 6); @Injectable() export class RoomsService { - constructor(private prisma: PrismaService) {} + constructor( + private prisma: PrismaService, + @Inject(forwardRef(() => GameGateway)) + private gameGateway: GameGateway, + ) {} async createRoom(hostId: string, questionPackId?: string, settings?: any) { const code = nanoid(); @@ -58,7 +63,7 @@ export class RoomsService { } async joinRoom(roomId: string, userId: string, name: string, role: 'PLAYER' | 'SPECTATOR') { - return this.prisma.participant.create({ + const participant = await this.prisma.participant.create({ data: { userId, roomId, @@ -66,6 +71,25 @@ export class RoomsService { role, }, }); + + // Получаем обновленную комнату со всеми участниками + const room = await this.prisma.room.findUnique({ + where: { id: roomId }, + include: { + host: true, + participants: { + include: { user: true }, + }, + questionPack: true, + }, + }); + + // Отправляем событие roomUpdate всем клиентам в комнате + if (room && this.gameGateway?.server) { + this.gameGateway.server.to(room.code).emit('roomUpdate', room); + } + + return participant; } async updateRoomStatus(roomId: string, status: 'WAITING' | 'PLAYING' | 'FINISHED') { diff --git a/src/hooks/useRoom.js b/src/hooks/useRoom.js index a289f5c..f2751e8 100644 --- a/src/hooks/useRoom.js +++ b/src/hooks/useRoom.js @@ -34,6 +34,9 @@ export const useRoom = (roomCode) => { // Connect to WebSocket socketService.connect(); + // Join the room via WebSocket + socketService.joinRoom(roomCode); + // Listen for room updates const handleRoomUpdate = (updatedRoom) => { setRoom(updatedRoom);