Skip to content

Commit

Permalink
feat: start save user into database
Browse files Browse the repository at this point in the history
  • Loading branch information
iaurg committed Aug 10, 2023
1 parent 8f42c74 commit 141b692
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions backend/src/game/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { Injectable } from '@nestjs/common';
import { GameDto } from './dto/game.dto';
import { GameMoveDto } from './dto/game.move';
import { Player } from './dto/game.player.dto';
import { PrismaService } from 'src/prisma/prisma.service';

@Injectable()
export class GameService {
constructor(private prismaService: PrismaService) {}

public PADDLE_WIDTH: number;
public PADDLE_HEIGHT: number;
private MAX_SCORE = 10;
Expand Down Expand Up @@ -122,11 +125,35 @@ export class GameService {
return randomAngle;
}

private async storeGameResult(gameDto: GameDto) {
/*
const player1 = await this.prismaService.user.findFirst({
where: { id: gameDto.player1.userId },
});
const player2 = await this.prismaService.user.findFirst({
where: { id: gameDto.player2.userId },
});
*/

//TODO: change to the real winner id from the database
const winner =
gameDto.score.player1 > gameDto.score.player2
? gameDto.player1
: gameDto.player2;

//TODO: add the game result into the user leaderboard database
console.log(
`Winner: ${winner.socketId} with ${gameDto.score.player1} points`,
);
}

isGameFinished(gameDto: GameDto): boolean {
if (
gameDto.score.player1 >= this.MAX_SCORE ||
gameDto.score.player2 >= this.MAX_SCORE
) {
this.storeGameResult(gameDto);
gameDto.finished = true;
return true;
}
Expand Down

0 comments on commit 141b692

Please sign in to comment.