This commit is contained in:
Dmitry 2026-01-07 17:23:06 +03:00
parent 313dde163e
commit e36f76bbc8
4 changed files with 36 additions and 6 deletions

View file

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

View file

@ -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],

View file

@ -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') {

View file

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