diff --git a/backend/code/src/app.controller.ts b/backend/code/src/app.controller.ts index e9983e6..f563d8c 100644 --- a/backend/code/src/app.controller.ts +++ b/backend/code/src/app.controller.ts @@ -40,6 +40,7 @@ export class AppController { + diff --git a/backend/code/src/game/game.service.ts b/backend/code/src/game/game.service.ts index 28a3c4a..7c71e23 100644 --- a/backend/code/src/game/game.service.ts +++ b/backend/code/src/game/game.service.ts @@ -1,12 +1,15 @@ import { Injectable } from '@nestjs/common'; -import { OnEvent } from '@nestjs/event-emitter'; +import { EventEmitter2, OnEvent } from '@nestjs/event-emitter'; import { PrismaService } from 'src/prisma/prisma.service'; import { PICTURE } from 'src/profile/dto/profile.dto'; @Injectable() export class GameService { - constructor(private readonly prisma: PrismaService) { - // this.launchGame(); + constructor( + private readonly prisma: PrismaService, + private eventEmitter: EventEmitter2, + ) { + this.launchGame(); } private waitingPlayers: string[] = []; @@ -19,13 +22,14 @@ export class GameService { private launchGame() { setInterval(() => { - console.log('waitingPlayers'); + console.log('waitingPlayers', this.waitingPlayers.length); if (this.waitingPlayers.length >= 2) { console.log('Game launched!'); const two_players = this.waitingPlayers.splice(0, 2); - console.log(two_players); + this.eventEmitter.emit('game.launched', two_players); + // console.log(two_players); } - }, 1000); + }, 5027); } async getHistory(userId: string, offset: number, limit: number) { diff --git a/backend/code/src/game/game.ts b/backend/code/src/game/game.ts new file mode 100644 index 0000000..2abbe0d --- /dev/null +++ b/backend/code/src/game/game.ts @@ -0,0 +1,35 @@ +import { Socket } from 'socket.io'; + +export class Game { + private async loop() { + console.log('loop'); + await this.sleep(5000); + this.loop(); + } + + start(gameid: string) { + console.log('game started', gameid); + this.loop(); + } + + setplayerScokets(p1socket: Socket, p2socket: Socket) { + this.p1socket = p1socket; + this.p2socket = p2socket; + + this.p1socket.on('move', (data) => { + console.log(data); + }); + this.p2socket.on('move', (data) => { + console.log(data); + }); + } + + private sleep(ms: number) { + return new Promise((resolve) => { + setTimeout(resolve, ms); + }); + } + + private p1socket: Socket; + private p2socket: Socket; +} diff --git a/backend/code/src/gateways/gateways.gateway.ts b/backend/code/src/gateways/gateways.gateway.ts index ad2fee1..1280839 100644 --- a/backend/code/src/gateways/gateways.gateway.ts +++ b/backend/code/src/gateways/gateways.gateway.ts @@ -9,6 +9,7 @@ import { MessageFormatDto } from 'src/messages/dto/message-format.dto'; import {} from '@nestjs/platform-socket.io'; import { EventEmitter2, OnEvent } from '@nestjs/event-emitter'; import { PrismaService } from 'src/prisma/prisma.service'; +import { Game } from 'src/game/game'; @WebSocketGateway(3004, { cors: { origin: ['http://localhost:3001'], @@ -20,6 +21,8 @@ export class Gateways implements OnGatewayConnection { private prisma: PrismaService, private readonly eventEmitter: EventEmitter2, ) {} + + private games_map = new Map(); handleConnection(client: Socket) { const userId = client.data.user.sub; const rooms = this.prisma.roomMember.findMany({ @@ -65,9 +68,14 @@ export class Gateways implements OnGatewayConnection { @OnEvent('game.launched') handleGameLaunchedEvent(clients: any) { const game_channel = `Game:${clients[0].id}:${clients[1].id}`; + console.log(game_channel); clients.forEach((client: any) => { client.join(game_channel); }); + const new_game = new Game(); + new_game.setplayerScokets(clients[0], clients[1]); + new_game.start(game_channel); + this.games_map.set(game_channel, new_game); this.server.to(game_channel).emit('game.launched', game_channel); } }