diff --git a/back/volume/src/pong/game/Ball.ts b/back/volume/src/pong/game/Ball.ts index fe5f76f..544f075 100644 --- a/back/volume/src/pong/game/Ball.ts +++ b/back/volume/src/pong/game/Ball.ts @@ -20,7 +20,7 @@ export class Ball { constructor ( spawn: Point, size: Point = DEFAULT_BALL_SIZE, - speed: Point = DEFAULT_BALL_INITIAL_SPEED + speed: Point = DEFAULT_BALL_INITIAL_SPEED.clone() ) { this.rect = new Rect(spawn, size) this.speed = speed @@ -93,21 +93,23 @@ export class Ball { playerScored (): number { let indexPlayerScored: number + if (this.rect.center.x <= this.spawn.x) { indexPlayerScored = 1 + this.speed.x = this.initial_speed.x } else { indexPlayerScored = 0 + this.speed.x = -this.initial_speed.x } - this.rect.center = this.spawn.clone() - if (this.speed.x < 0) { - this.speed.x = this.initial_speed.x - this.speed.y = -this.initial_speed.y - } else { - this.speed.x = -this.initial_speed.x + if (this.speed.y < 0) { this.speed.y = this.initial_speed.y + } else { + this.speed.y = -this.initial_speed.y } + this.rect.center = this.spawn.clone() + return indexPlayerScored } } diff --git a/back/volume/src/pong/game/Game.ts b/back/volume/src/pong/game/Game.ts index 4336be4..7c3d3ee 100644 --- a/back/volume/src/pong/game/Game.ts +++ b/back/volume/src/pong/game/Game.ts @@ -21,7 +21,6 @@ export class Game { map: MapDtoValidated ball: Ball players: Player[] = [] - playing: boolean ranked: boolean waitingForTimeout: boolean gameStoppedCallback: (name: string) => void @@ -37,7 +36,6 @@ export class Game { ) { this.id = randomUUID() this.timer = null - this.playing = false this.ranked = ranked this.waitingForTimeout = false this.map = map @@ -99,7 +97,6 @@ export class Game { void this.pongService.setInGame(p.name) p.newGame() }) - this.playing = true this.broadcastGame(GAME_EVENTS.START_GAME) this.timer = setInterval(this.gameLoop.bind(this), 1000 / GAME_TICKS) console.log(`Game ${this.id} starting in 3 seconds`) @@ -111,21 +108,20 @@ export class Game { } stop (): void { - if (this.timer !== null && this.playing) { - this.playing = false + if (this.timer !== null) { clearInterval(this.timer) - this.timer = null - this.pongService - .saveResult(this.players, this.ranked) - .then(() => { - this.gameStoppedCallback(this.players[0].name) - this.players = [] - }) - .catch(() => { - this.gameStoppedCallback(this.players[0].name) - this.players = [] - }) } + this.timer = null + this.pongService + .saveResult(this.players, this.ranked, DEFAULT_WIN_SCORE) + .then(() => { + this.gameStoppedCallback(this.players[0].name) + this.players = [] + }) + .catch(() => { + this.gameStoppedCallback(this.players[0].name) + this.players = [] + }) } movePaddle (name: string | undefined, position: Point): void { @@ -142,10 +138,6 @@ export class Game { }) } - isPlaying (): boolean { - return this.playing - } - private gameLoop (): void { if (this.waitingForTimeout) { return diff --git a/back/volume/src/pong/pong.gateway.ts b/back/volume/src/pong/pong.gateway.ts index 99c3e1d..f2df12b 100644 --- a/back/volume/src/pong/pong.gateway.ts +++ b/back/volume/src/pong/pong.gateway.ts @@ -46,7 +46,7 @@ export class PongGateway implements OnGatewayConnection, OnGatewayDisconnect { ): void { const name: string | undefined = this.socketToPlayerName.get(client) const game: Game | undefined = this.games.playerGame(name) - if (game?.isPlaying() !== undefined) { + if (game !== undefined) { game.stop() } if (name !== undefined) { diff --git a/back/volume/src/pong/pong.service.ts b/back/volume/src/pong/pong.service.ts index d024c07..b0f8a06 100644 --- a/back/volume/src/pong/pong.service.ts +++ b/back/volume/src/pong/pong.service.ts @@ -15,18 +15,18 @@ export class PongService { private readonly usersService: UsersService ) {} - async updateStats (player: User, i: number, result: Result): Promise { + async updateStats (player: User, i: number, result: Result, maxScore: number): Promise { player.matchs++ - if (result.score[i] > result.score[Math.abs(i - 1)]) player.wins++ + if (result.score[i] === maxScore) player.wins++ else player.looses++ player.winrate = (100 * player.wins) / player.matchs player.rank = (await this.usersService.getRank(player.ftId)) + 1 } - async updatePlayer (i: number, result: Result): Promise { + async updatePlayer (i: number, result: Result, maxScore: number): Promise { const player: User | null = result.players[i] if (player == null) return - if (result.ranked) await this.updateStats(player, i, result) + if (result.ranked) await this.updateStats(player, i, result, maxScore) player.results.push(result) player.status = 'online' await this.usersService.save(player) @@ -38,7 +38,7 @@ export class PongService { await this.usersService.save(player) } - async saveResult (players: Player[], ranked: boolean): Promise { + async saveResult (players: Player[], ranked: boolean, maxScore: number): Promise { const result = new Result() const ply = new Array() ply.push(await this.usersService.findUserByName(players[0].name)) @@ -47,8 +47,8 @@ export class PongService { result.players = ply result.score = [players[0].score, players[1].score] await this.resultsRepository.save(result) - await this.updatePlayer(0, result) - await this.updatePlayer(1, result) + await this.updatePlayer(0, result, maxScore) + await this.updatePlayer(1, result, maxScore) } async getHistory ( diff --git a/front/volume/src/components/Pong/Paddle.ts b/front/volume/src/components/Pong/Paddle.ts index 727fc69..4f3359c 100644 --- a/front/volume/src/components/Pong/Paddle.ts +++ b/front/volume/src/components/Pong/Paddle.ts @@ -19,7 +19,7 @@ export class Paddle { new Point(this.rect.center.x + offset, this.rect.center.y), new Point(this.rect.size.x / 3, this.rect.size.y) ); - render_rect.draw(context, "yellow"); + render_rect.draw(context, color); } move(e: MouseEvent) { diff --git a/front/volume/src/components/Pong/Pong.svelte b/front/volume/src/components/Pong/Pong.svelte index ef47463..0afd251 100644 --- a/front/volume/src/components/Pong/Pong.svelte +++ b/front/volume/src/components/Pong/Pong.svelte @@ -70,6 +70,7 @@ } if (data.yourPaddleIndex !== -2) { gamePlaying = true; + game.ranked = data.ranked; game.setInfo(data); } }