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 (
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
}
}

14
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,12 +108,12 @@ 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)
.saveResult(this.players, this.ranked, DEFAULT_WIN_SCORE)
.then(() => {
this.gameStoppedCallback(this.players[0].name)
this.players = []
@ -126,7 +123,6 @@ export class Game {
this.players = []
})
}
}
movePaddle (name: string | undefined, position: Point): void {
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 {
if (this.waitingForTimeout) {
return

2
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) {

14
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<void> {
async updateStats (player: User, i: number, result: Result, maxScore: number): Promise<void> {
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<void> {
async updatePlayer (i: number, result: Result, maxScore: number): Promise<void> {
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<void> {
async saveResult (players: Player[], ranked: boolean, maxScore: number): Promise<void> {
const result = new Result()
const ply = new Array<User | null>()
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 (

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.size.x / 3, this.rect.size.y)
);
render_rect.draw(context, "yellow");
render_rect.draw(context, color);
}
move(e: MouseEvent) {

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

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

Loading…
Cancel
Save