Skip to content

Commit

Permalink
init game
Browse files Browse the repository at this point in the history
  • Loading branch information
Saad eddyne El abdari committed Oct 31, 2023
1 parent 301dbe4 commit faf2fca
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
7 changes: 7 additions & 0 deletions backend/code/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class AppController {
<link href="css/style.css" rel="stylesheet" />
</head>
<body>
<input type="text" onkeydown="hamid()" />
<script type="module">
import { io } from 'https://cdn.socket.io/4.4.1/socket.io.esm.min.js';
Expand Down Expand Up @@ -67,6 +68,12 @@ socket.on('error', (error) => {
function hamid() {
this.scoket.emit('move', {x: 1, y: 2});
}
</script>
</body>
</html>
Expand Down
16 changes: 10 additions & 6 deletions backend/code/src/game/game.service.ts
Original file line number Diff line number Diff line change
@@ -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[] = [];
Expand All @@ -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) {
Expand Down
35 changes: 35 additions & 0 deletions backend/code/src/game/game.ts
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 8 additions & 0 deletions backend/code/src/gateways/gateways.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -20,6 +21,8 @@ export class Gateways implements OnGatewayConnection {
private prisma: PrismaService,
private readonly eventEmitter: EventEmitter2,
) {}

private games_map = new Map<string, Game>();
handleConnection(client: Socket) {
const userId = client.data.user.sub;
const rooms = this.prisma.roomMember.findMany({
Expand Down Expand Up @@ -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);
}
}

0 comments on commit faf2fca

Please sign in to comment.