Skip to content

Commit

Permalink
storeGameResult method implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
GuiMartinelli committed Oct 4, 2023
1 parent 61dde7e commit 9557ddd
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions backend/src/game/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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';
import { User } from '@prisma/client';

@Injectable()
export class GameService {
Expand Down Expand Up @@ -126,28 +127,37 @@ export class GameService {
}

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

const player2 = await this.prismaService.user.findFirst({
where: { id: gameDto.player2.userId },
});
*/
if (this.findWinner(gameDto) == 1) {
winner = await this.prismaService.user.findFirst({
where: { id: gameDto.player1.userId },
});
} else {
winner = 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;
winner.victory++;
const updateUser = await this.prismaService.user.update({
where: {
id: winner.id,
},
data: {
victory: winner.victory,
},
});

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

private findWinner(gameDto: GameDto) {
return gameDto.score.player1 > gameDto.score.player2 ? 1 : 2;
}

isGameFinished(gameDto: GameDto): boolean {
if (
gameDto.score.player1 >= this.MAX_SCORE ||
Expand Down

0 comments on commit 9557ddd

Please sign in to comment.