Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/count user victory #80

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/src/game/dto/game.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { Score } from './game.score.dto';

export class GameDto {
gameId: string;
finished: boolean;
finished: boolean;
player1: Player;
player2: Player;
score: Score;
score: Score;
ball: Ball;
canvas: Canvas;
}
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
Loading