Skip to content

Commit

Permalink
CHECKPOINT
Browse files Browse the repository at this point in the history
  • Loading branch information
ismail-kharrobi committed Nov 12, 2023
1 parent 0295610 commit c9141bb
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 1,835 deletions.
3 changes: 3 additions & 0 deletions backend/code/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { GameModule } from './game/game.module';
import { LeaderBoardModule } from './leaderboard/leaderboard.module';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
import { APP_GUARD } from '@nestjs/core';
import { GameService } from './game/game.service';

@Module({
imports: [
Expand All @@ -37,6 +38,8 @@ import { APP_GUARD } from '@nestjs/core';
],
controllers: [AppController],
providers: [
GameService
,
Gateways,
{
provide: APP_GUARD,
Expand Down
16 changes: 15 additions & 1 deletion backend/code/src/game/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,24 @@ export class GameService {
const two_players = this.waitingPlayers.splice(0, 2);
this.eventEmitter.emit('game.launched', two_players);
console.log(two_players);
// const user = await this.getUser(two_players[0].data.user.sub)
// console.log(user)
}
}, 5027);
}

async getUser(userId:string){
const user = await this.prisma.user.findUnique({
where:{
userId:userId,
},
select:{
userId: true,
Username: true,
avatar: true,
}
})
return user;
}
async getHistory(userId: string, offset: number, limit: number) {
const matches = await this.prisma.match.findMany({
skip: offset,
Expand Down
39 changes: 25 additions & 14 deletions backend/code/src/game/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class Game {
let new_y = y * scale_y

let new_ball_size = ballsize * Math.min(scale_x, scale_y)
return {x:new_x, y:new_y, ballsize:new_ball_size }
return {x:new_x, y:new_y, ballsize:new_ball_size,p1Score:this.p1Score,p2Score:this.p2Score }
}
private async loop() {
if (this.closeGame)
Expand All @@ -29,7 +29,7 @@ export class Game {
console.log(this.p1Res)
console.log(this.p2Res)
console.log(parseFloat((this.p1Res.w / this.p1Res.h).toFixed(1)))
if (parseFloat((this.p1Res.w / this.p1Res.h).toFixed(1)) !== 1.8){
if (parseFloat((this.p1Res.w / this.p1Res.h).toFixed(1)) !== 1.8 && parseFloat((this.p2Res.w / this.p2Res.h).toFixed(1)) !== 1.9){
this.p1socket.emit("screen Error")
this.emitGameEnd("end")
this.p1socket.emit("lose","trying cheat");
Expand All @@ -41,7 +41,7 @@ export class Game {
}
console.log(parseFloat((this.p2Res.w / this.p2Res.h).toFixed(1)))

if (parseFloat((this.p2Res.w / this.p2Res.h).toFixed(1)) !== 1.8){
if (parseFloat((this.p2Res.w / this.p2Res.h).toFixed(1)) !== 1.8 && parseFloat((this.p2Res.w / this.p2Res.h).toFixed(1)) !== 1.9){
this.p1socket.emit("screen Error")
this.emitGameEnd("end")
this.p1socket.emit("win","you win other player try to cheat");
Expand All @@ -52,14 +52,9 @@ export class Game {
this.p2socket.emit("ball",this.screenAdapter(this.p2Res,this.x,this.y,this.ballSize))
}

this.p1socket.on("leave", () => {this.emitGameEnd("end");this.closeGame = true ;
this.p2socket.emit("win","you win other player leave the game");
})
this.p2socket.on("leave", () => {this.emitGameEnd("end");this.closeGame = true ;
this.p1socket.emit("win","you win other player leave the game");
})


await this.sleep(1000);
await this.sleep(30);
this.loop();
}

Expand All @@ -78,16 +73,22 @@ export class Game {
console.log('game started', ngameid);
this.gameid = ngameid;
await this.sleepCounter()
await this.setplayerScokets(this.p1socket, this.p2socket)
// await this.setplayerScokets(this.p1socket, this.p2socket ,)
this.loop();
}

mouseAdapter(h : number){

}
setplayerScokets(p1socket: Socket, p2socket: Socket ) {
setplayerScokets(p1socket: Socket, p2socket: Socket , p1Data:any, p2Data:any) {
this.p1socket = p1socket;
this.p2socket = p2socket;
this.p1Data = p1Data;
this.p2Data = p2Data;
console.log(p1Data);
console.log(p2Data);
this.server.emit("players",[p1Data,p2Data])
console.log("newfunc")
this.p1socket.on('up', (data) => {
console.log('heh');
console.log(data);
Expand All @@ -108,8 +109,16 @@ export class Game {
console.log('p2 disconnected');
this.emitGameEnd('p2 disconnected');
});
this.p1socket.on("leave", () => {this.emitGameEnd("end")})
this.p2socket.on("leave", () => {this.emitGameEnd("end")})
this.p1socket.on("leave", () => {this.emitGameEnd("end");
this.p2socket.emit("win","you win other player leave the game");
this.p1socket.emit("lose","you win other player leave the game");
this.closeGame = true ;
})
this.p2socket.on("leave", () => {this.emitGameEnd("end");
this.p1socket.emit("win","you win other player leave the game");
this.p2socket.emit("lose","you win other player leave the game");
this.closeGame = true ;
})

}

Expand All @@ -131,6 +140,8 @@ export class Game {
private gameid: string;
private p1socket: Socket;
private p2socket: Socket;
private p1Data: any;
private p2Data: any;
private w: number = 1067;
private h: number = 600;
private x: number = this.w / 2;
Expand Down
14 changes: 11 additions & 3 deletions backend/code/src/gateways/gateways.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';
import { PrismaService } from 'src/prisma/prisma.service';
import { Game } from 'src/game/game';
import { $Enums, Notification } from '@prisma/client';
import { GameService } from 'src/game/game.service';

@WebSocketGateway(3004, {
cors: {
Expand All @@ -24,6 +25,7 @@ export class Gateways implements OnGatewayConnection, OnGatewayDisconnect {
constructor(
private prisma: PrismaService,
private readonly eventEmitter: EventEmitter2,
private readonly gameService: GameService
) {}

@WebSocketServer() private server: Server;
Expand Down Expand Up @@ -197,16 +199,22 @@ export class Gateways implements OnGatewayConnection, OnGatewayDisconnect {
console.log(client.data.user);
this.eventEmitter.emit('game.start', client);
}

// @SubscribeMessage('getPlayers')
// getPlayers() {
// this.server.emit("players",[this.p1Data ,this.p2Data])
// }
@OnEvent('game.launched')
handleGameLaunchedEvent(clients: any) {
async 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(this.eventEmitter , this.server);
new_game.setplayerScokets(clients[0], clients[1]);
const user1 = await this.gameService.getUser(clients[0].data.user.sub);
const user2 = await this.gameService.getUser(clients[1].data.user.sub);

new_game.setplayerScokets(clients[0], clients[1], user1 , user2);
new_game.start(game_channel);
this.games_map.set(game_channel, new_game);
this.server.to(game_channel).emit('game.launched', game_channel);
Expand Down
Loading

0 comments on commit c9141bb

Please sign in to comment.