stuff
This commit is contained in:
parent
313dde163e
commit
e36f76bbc8
4 changed files with 36 additions and 6 deletions
|
|
@ -1,9 +1,10 @@
|
||||||
import { Module } from '@nestjs/common';
|
import { Module, forwardRef } from '@nestjs/common';
|
||||||
import { GameGateway } from './game.gateway';
|
import { GameGateway } from './game.gateway';
|
||||||
import { RoomsModule } from '../rooms/rooms.module';
|
import { RoomsModule } from '../rooms/rooms.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [RoomsModule],
|
imports: [forwardRef(() => RoomsModule)],
|
||||||
providers: [GameGateway],
|
providers: [GameGateway],
|
||||||
|
exports: [GameGateway],
|
||||||
})
|
})
|
||||||
export class GameModule {}
|
export class GameModule {}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
import { Module } from '@nestjs/common';
|
import { Module, forwardRef } from '@nestjs/common';
|
||||||
import { RoomsService } from './rooms.service';
|
import { RoomsService } from './rooms.service';
|
||||||
import { RoomsController } from './rooms.controller';
|
import { RoomsController } from './rooms.controller';
|
||||||
|
import { GameModule } from '../game/game.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
|
imports: [forwardRef(() => GameModule)],
|
||||||
controllers: [RoomsController],
|
controllers: [RoomsController],
|
||||||
providers: [RoomsService],
|
providers: [RoomsService],
|
||||||
exports: [RoomsService],
|
exports: [RoomsService],
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,17 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable, Inject, forwardRef } from '@nestjs/common';
|
||||||
import { PrismaService } from '../prisma/prisma.service';
|
import { PrismaService } from '../prisma/prisma.service';
|
||||||
import { customAlphabet } from 'nanoid';
|
import { customAlphabet } from 'nanoid';
|
||||||
|
import { GameGateway } from '../game/game.gateway';
|
||||||
|
|
||||||
const nanoid = customAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 6);
|
const nanoid = customAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 6);
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class RoomsService {
|
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) {
|
async createRoom(hostId: string, questionPackId?: string, settings?: any) {
|
||||||
const code = nanoid();
|
const code = nanoid();
|
||||||
|
|
@ -58,7 +63,7 @@ export class RoomsService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async joinRoom(roomId: string, userId: string, name: string, role: 'PLAYER' | 'SPECTATOR') {
|
async joinRoom(roomId: string, userId: string, name: string, role: 'PLAYER' | 'SPECTATOR') {
|
||||||
return this.prisma.participant.create({
|
const participant = await this.prisma.participant.create({
|
||||||
data: {
|
data: {
|
||||||
userId,
|
userId,
|
||||||
roomId,
|
roomId,
|
||||||
|
|
@ -66,6 +71,25 @@ export class RoomsService {
|
||||||
role,
|
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') {
|
async updateRoomStatus(roomId: string, status: 'WAITING' | 'PLAYING' | 'FINISHED') {
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,9 @@ export const useRoom = (roomCode) => {
|
||||||
// Connect to WebSocket
|
// Connect to WebSocket
|
||||||
socketService.connect();
|
socketService.connect();
|
||||||
|
|
||||||
|
// Join the room via WebSocket
|
||||||
|
socketService.joinRoom(roomCode);
|
||||||
|
|
||||||
// Listen for room updates
|
// Listen for room updates
|
||||||
const handleRoomUpdate = (updatedRoom) => {
|
const handleRoomUpdate = (updatedRoom) => {
|
||||||
setRoom(updatedRoom);
|
setRoom(updatedRoom);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue