Browse Source

* Fixed many pong bugs

master
vvandenb 2 years ago
parent
commit
a5e89a9065
  1. 16
      back/volume/src/pong/game/Ball.ts
  2. 14
      back/volume/src/pong/game/Game.ts
  3. 2
      back/volume/src/pong/pong.gateway.ts
  4. 14
      back/volume/src/pong/pong.service.ts
  5. 2
      front/volume/src/components/Pong/Paddle.ts
  6. 1
      front/volume/src/components/Pong/Pong.svelte

16
back/volume/src/pong/game/Ball.ts

@ -20,7 +20,7 @@ export class Ball {
constructor ( constructor (
spawn: Point, spawn: Point,
size: Point = DEFAULT_BALL_SIZE, 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.rect = new Rect(spawn, size)
this.speed = speed this.speed = speed
@ -93,21 +93,23 @@ export class Ball {
playerScored (): number { playerScored (): number {
let indexPlayerScored: number let indexPlayerScored: number
if (this.rect.center.x <= this.spawn.x) { if (this.rect.center.x <= this.spawn.x) {
indexPlayerScored = 1 indexPlayerScored = 1
this.speed.x = this.initial_speed.x
} else { } else {
indexPlayerScored = 0 indexPlayerScored = 0
this.speed.x = -this.initial_speed.x
} }
this.rect.center = this.spawn.clone() if (this.speed.y < 0) {
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
this.speed.y = this.initial_speed.y this.speed.y = this.initial_speed.y
} else {
this.speed.y = -this.initial_speed.y
} }
this.rect.center = this.spawn.clone()
return indexPlayerScored return indexPlayerScored
} }
} }

14
back/volume/src/pong/game/Game.ts

@ -21,7 +21,6 @@ export class Game {
map: MapDtoValidated map: MapDtoValidated
ball: Ball ball: Ball
players: Player[] = [] players: Player[] = []
playing: boolean
ranked: boolean ranked: boolean
waitingForTimeout: boolean waitingForTimeout: boolean
gameStoppedCallback: (name: string) => void gameStoppedCallback: (name: string) => void
@ -37,7 +36,6 @@ export class Game {
) { ) {
this.id = randomUUID() this.id = randomUUID()
this.timer = null this.timer = null
this.playing = false
this.ranked = ranked this.ranked = ranked
this.waitingForTimeout = false this.waitingForTimeout = false
this.map = map this.map = map
@ -99,7 +97,6 @@ export class Game {
void this.pongService.setInGame(p.name) void this.pongService.setInGame(p.name)
p.newGame() p.newGame()
}) })
this.playing = true
this.broadcastGame(GAME_EVENTS.START_GAME) this.broadcastGame(GAME_EVENTS.START_GAME)
this.timer = setInterval(this.gameLoop.bind(this), 1000 / GAME_TICKS) this.timer = setInterval(this.gameLoop.bind(this), 1000 / GAME_TICKS)
console.log(`Game ${this.id} starting in 3 seconds`) console.log(`Game ${this.id} starting in 3 seconds`)
@ -111,12 +108,12 @@ export class Game {
} }
stop (): void { stop (): void {
if (this.timer !== null && this.playing) { if (this.timer !== null) {
this.playing = false
clearInterval(this.timer) clearInterval(this.timer)
}
this.timer = null this.timer = null
this.pongService this.pongService
.saveResult(this.players, this.ranked) .saveResult(this.players, this.ranked, DEFAULT_WIN_SCORE)
.then(() => { .then(() => {
this.gameStoppedCallback(this.players[0].name) this.gameStoppedCallback(this.players[0].name)
this.players = [] this.players = []
@ -126,7 +123,6 @@ export class Game {
this.players = [] this.players = []
}) })
} }
}
movePaddle (name: string | undefined, position: Point): void { movePaddle (name: string | undefined, position: Point): void {
const playerIndex: number = this.players.findIndex((p) => p.name === name) const playerIndex: number = this.players.findIndex((p) => p.name === name)
@ -142,10 +138,6 @@ export class Game {
}) })
} }
isPlaying (): boolean {
return this.playing
}
private gameLoop (): void { private gameLoop (): void {
if (this.waitingForTimeout) { if (this.waitingForTimeout) {
return return

2
back/volume/src/pong/pong.gateway.ts

@ -46,7 +46,7 @@ export class PongGateway implements OnGatewayConnection, OnGatewayDisconnect {
): void { ): void {
const name: string | undefined = this.socketToPlayerName.get(client) const name: string | undefined = this.socketToPlayerName.get(client)
const game: Game | undefined = this.games.playerGame(name) const game: Game | undefined = this.games.playerGame(name)
if (game?.isPlaying() !== undefined) { if (game !== undefined) {
game.stop() game.stop()
} }
if (name !== undefined) { if (name !== undefined) {

14
back/volume/src/pong/pong.service.ts

@ -15,18 +15,18 @@ export class PongService {
private readonly usersService: UsersService private readonly usersService: UsersService
) {} ) {}
async updateStats (player: User, i: number, result: Result): Promise<void> { async updateStats (player: User, i: number, result: Result, maxScore: number): Promise<void> {
player.matchs++ player.matchs++
if (result.score[i] > result.score[Math.abs(i - 1)]) player.wins++ if (result.score[i] === maxScore) player.wins++
else player.looses++ else player.looses++
player.winrate = (100 * player.wins) / player.matchs player.winrate = (100 * player.wins) / player.matchs
player.rank = (await this.usersService.getRank(player.ftId)) + 1 player.rank = (await this.usersService.getRank(player.ftId)) + 1
} }
async updatePlayer (i: number, result: Result): Promise<void> { async updatePlayer (i: number, result: Result, maxScore: number): Promise<void> {
const player: User | null = result.players[i] const player: User | null = result.players[i]
if (player == null) return 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.results.push(result)
player.status = 'online' player.status = 'online'
await this.usersService.save(player) await this.usersService.save(player)
@ -38,7 +38,7 @@ export class PongService {
await this.usersService.save(player) await this.usersService.save(player)
} }
async saveResult (players: Player[], ranked: boolean): Promise<void> { async saveResult (players: Player[], ranked: boolean, maxScore: number): Promise<void> {
const result = new Result() const result = new Result()
const ply = new Array<User | null>() const ply = new Array<User | null>()
ply.push(await this.usersService.findUserByName(players[0].name)) ply.push(await this.usersService.findUserByName(players[0].name))
@ -47,8 +47,8 @@ export class PongService {
result.players = ply result.players = ply
result.score = [players[0].score, players[1].score] result.score = [players[0].score, players[1].score]
await this.resultsRepository.save(result) await this.resultsRepository.save(result)
await this.updatePlayer(0, result) await this.updatePlayer(0, result, maxScore)
await this.updatePlayer(1, result) await this.updatePlayer(1, result, maxScore)
} }
async getHistory ( async getHistory (

2
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.center.x + offset, this.rect.center.y),
new Point(this.rect.size.x / 3, this.rect.size.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) { move(e: MouseEvent) {

1
front/volume/src/components/Pong/Pong.svelte

@ -70,6 +70,7 @@
} }
if (data.yourPaddleIndex !== -2) { if (data.yourPaddleIndex !== -2) {
gamePlaying = true; gamePlaying = true;
game.ranked = data.ranked;
game.setInfo(data); game.setInfo(data);
} }
} }

Loading…
Cancel
Save