stuff
This commit is contained in:
parent
e36f76bbc8
commit
a09e9ad73c
4 changed files with 66 additions and 9 deletions
|
|
@ -4,9 +4,11 @@ import {
|
||||||
SubscribeMessage,
|
SubscribeMessage,
|
||||||
OnGatewayConnection,
|
OnGatewayConnection,
|
||||||
OnGatewayDisconnect,
|
OnGatewayDisconnect,
|
||||||
|
OnGatewayInit,
|
||||||
} from '@nestjs/websockets';
|
} from '@nestjs/websockets';
|
||||||
import { Server, Socket } from 'socket.io';
|
import { Server, Socket } from 'socket.io';
|
||||||
import { RoomsService } from '../rooms/rooms.service';
|
import { RoomsService } from '../rooms/rooms.service';
|
||||||
|
import { RoomEventsService } from './room-events.service';
|
||||||
|
|
||||||
@WebSocketGateway({
|
@WebSocketGateway({
|
||||||
cors: {
|
cors: {
|
||||||
|
|
@ -16,11 +18,18 @@ import { RoomsService } from '../rooms/rooms.service';
|
||||||
credentials: true,
|
credentials: true,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect {
|
export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect, OnGatewayInit {
|
||||||
@WebSocketServer()
|
@WebSocketServer()
|
||||||
server: Server;
|
server: Server;
|
||||||
|
|
||||||
constructor(private roomsService: RoomsService) {}
|
constructor(
|
||||||
|
private roomsService: RoomsService,
|
||||||
|
private roomEventsService: RoomEventsService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
afterInit(server: Server) {
|
||||||
|
this.roomEventsService.setServer(server);
|
||||||
|
}
|
||||||
|
|
||||||
handleConnection(client: Socket) {
|
handleConnection(client: Socket) {
|
||||||
console.log(`Client connected: ${client.id}`);
|
console.log(`Client connected: ${client.id}`);
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
import { Module, forwardRef } from '@nestjs/common';
|
import { Module, forwardRef } from '@nestjs/common';
|
||||||
import { GameGateway } from './game.gateway';
|
import { GameGateway } from './game.gateway';
|
||||||
|
import { RoomEventsService } from './room-events.service';
|
||||||
import { RoomsModule } from '../rooms/rooms.module';
|
import { RoomsModule } from '../rooms/rooms.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [forwardRef(() => RoomsModule)],
|
imports: [forwardRef(() => RoomsModule)],
|
||||||
providers: [GameGateway],
|
providers: [GameGateway, RoomEventsService],
|
||||||
exports: [GameGateway],
|
exports: [RoomEventsService],
|
||||||
})
|
})
|
||||||
export class GameModule {}
|
export class GameModule {}
|
||||||
|
|
|
||||||
47
backend/src/game/room-events.service.ts
Normal file
47
backend/src/game/room-events.service.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { Injectable, Inject, forwardRef } 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';
|
import { RoomEventsService } from '../game/room-events.service';
|
||||||
|
|
||||||
const nanoid = customAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 6);
|
const nanoid = customAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 6);
|
||||||
|
|
||||||
|
|
@ -9,8 +9,8 @@ const nanoid = customAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 6);
|
||||||
export class RoomsService {
|
export class RoomsService {
|
||||||
constructor(
|
constructor(
|
||||||
private prisma: PrismaService,
|
private prisma: PrismaService,
|
||||||
@Inject(forwardRef(() => GameGateway))
|
@Inject(forwardRef(() => RoomEventsService))
|
||||||
private gameGateway: GameGateway,
|
private roomEventsService: RoomEventsService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async createRoom(hostId: string, questionPackId?: string, settings?: any) {
|
async createRoom(hostId: string, questionPackId?: string, settings?: any) {
|
||||||
|
|
@ -85,8 +85,8 @@ export class RoomsService {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Отправляем событие roomUpdate всем клиентам в комнате
|
// Отправляем событие roomUpdate всем клиентам в комнате
|
||||||
if (room && this.gameGateway?.server) {
|
if (room) {
|
||||||
this.gameGateway.server.to(room.code).emit('roomUpdate', room);
|
this.roomEventsService.emitRoomUpdate(room.code, room);
|
||||||
}
|
}
|
||||||
|
|
||||||
return participant;
|
return participant;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue