diff --git a/back/volume/src/pong/dtos/GameCreationDtoValidated.ts b/back/volume/src/pong/dtos/GameCreationDtoValidated.ts index ca8c3ae..3b55da4 100644 --- a/back/volume/src/pong/dtos/GameCreationDtoValidated.ts +++ b/back/volume/src/pong/dtos/GameCreationDtoValidated.ts @@ -3,9 +3,16 @@ import { ArrayMaxSize, ArrayMinSize, IsNotEmptyObject, + IsNumber, IsString, + Max, + Min, ValidateNested } from 'class-validator' +import { + DEFAULT_BALL_INITIAL_SPEED, + DEFAULT_MAX_BALL_SPEED +} from '../game/constants' import { MapDtoValidated } from './MapDtoValidated' export class GameCreationDtoValidated { @@ -18,4 +25,14 @@ export class GameCreationDtoValidated { @ValidateNested() @Type(() => MapDtoValidated) map!: MapDtoValidated + + @IsNumber() + @Min(DEFAULT_BALL_INITIAL_SPEED.x) + @Max(DEFAULT_MAX_BALL_SPEED.x) + initialBallSpeedX!: number + + @IsNumber() + @Min(DEFAULT_BALL_INITIAL_SPEED.y) + @Max(DEFAULT_MAX_BALL_SPEED.y) + initialBallSpeedY!: number } diff --git a/back/volume/src/pong/game/Ball.ts b/back/volume/src/pong/game/Ball.ts index 544f075..a93b330 100644 --- a/back/volume/src/pong/game/Ball.ts +++ b/back/volume/src/pong/game/Ball.ts @@ -3,7 +3,6 @@ import { type Point, Rect } from './utils' import { type MapDtoValidated } from '../dtos/MapDtoValidated' import { DEFAULT_BALL_SIZE, - DEFAULT_BALL_INITIAL_SPEED, GAME_TICKS, DEFAULT_BALL_SPEED_INCREMENT, DEFAULT_MAX_BALL_SPEED @@ -19,12 +18,12 @@ export class Ball { constructor ( spawn: Point, - size: Point = DEFAULT_BALL_SIZE, - speed: Point = DEFAULT_BALL_INITIAL_SPEED.clone() + initialSpeed: Point, + size: Point = DEFAULT_BALL_SIZE.clone() ) { this.rect = new Rect(spawn, size) - this.speed = speed - this.initial_speed = speed.clone() + this.speed = initialSpeed.clone() + this.initial_speed = initialSpeed.clone() this.spawn = spawn.clone() this.indexPlayerScored = -1 this.timeoutTime = 0 diff --git a/back/volume/src/pong/game/Game.ts b/back/volume/src/pong/game/Game.ts index 7c3d3ee..16db48c 100644 --- a/back/volume/src/pong/game/Game.ts +++ b/back/volume/src/pong/game/Game.ts @@ -3,6 +3,7 @@ import { type Socket } from 'socket.io' import { Point, Rect } from './utils' import { Player } from './Player' import { + DEFAULT_BALL_INITIAL_SPEED, DEFAULT_BALL_SIZE, DEFAULT_PADDLE_SIZE, DEFAULT_WIN_SCORE, @@ -22,6 +23,7 @@ export class Game { ball: Ball players: Player[] = [] ranked: boolean + initialBallSpeed: Point waitingForTimeout: boolean gameStoppedCallback: (name: string) => void @@ -32,7 +34,8 @@ export class Game { map: MapDtoValidated, gameStoppedCallback: (name: string) => void, private readonly pongService: PongService, - ranked: boolean + ranked: boolean, + initialBallSpeed: Point = DEFAULT_BALL_INITIAL_SPEED.clone() ) { this.id = randomUUID() this.timer = null @@ -40,7 +43,11 @@ export class Game { this.waitingForTimeout = false this.map = map this.gameStoppedCallback = gameStoppedCallback - this.ball = new Ball(new Point(this.map.size.x / 2, this.map.size.y / 2)) + this.initialBallSpeed = initialBallSpeed + this.ball = new Ball( + new Point(this.map.size.x / 2, this.map.size.y / 2), + initialBallSpeed + ) for (let i = 0; i < uuids.length; i++) { this.addPlayer(sockets[i], uuids[i], names[i]) } @@ -92,7 +99,10 @@ export class Game { private start (): void { if (this.timer === null && this.players.length === 2) { - this.ball = new Ball(new Point(this.map.size.x / 2, this.map.size.y / 2)) + this.ball = new Ball( + new Point(this.map.size.x / 2, this.map.size.y / 2), + this.initialBallSpeed + ) this.players.forEach((p) => { void this.pongService.setInGame(p.name) p.newGame() diff --git a/back/volume/src/pong/game/Games.ts b/back/volume/src/pong/game/Games.ts index f93f760..fdd5efc 100644 --- a/back/volume/src/pong/game/Games.ts +++ b/back/volume/src/pong/game/Games.ts @@ -37,7 +37,11 @@ export class Games { map, this.deleteGame.bind(this, names[0]), this.pongService, - ranked + ranked, + new Point( + gameCreationDto.initialBallSpeedX, + gameCreationDto.initialBallSpeedY + ) ) ) this.playerNameToGameIndex.set(names[0], this.games.length - 1) diff --git a/back/volume/src/pong/game/MatchmakingQueue.ts b/back/volume/src/pong/game/MatchmakingQueue.ts index d4514a5..a475dc6 100644 --- a/back/volume/src/pong/game/MatchmakingQueue.ts +++ b/back/volume/src/pong/game/MatchmakingQueue.ts @@ -1,6 +1,6 @@ import { type Socket } from 'socket.io' import { type GameCreationDtoValidated } from '../dtos/GameCreationDtoValidated' -import { DEFAULT_MAP_SIZE } from './constants' +import { DEFAULT_BALL_INITIAL_SPEED, DEFAULT_MAP_SIZE } from './constants' import { type Games } from './Games' export class MatchmakingQueue { @@ -48,7 +48,9 @@ export class MatchmakingQueue { map: { size: DEFAULT_MAP_SIZE, walls: [] - } + }, + initialBallSpeedX: DEFAULT_BALL_INITIAL_SPEED.x, + initialBallSpeedY: DEFAULT_BALL_INITIAL_SPEED.y } const ranked = true diff --git a/back/volume/src/pong/pong.service.ts b/back/volume/src/pong/pong.service.ts index 2c59d63..d64b78a 100644 --- a/back/volume/src/pong/pong.service.ts +++ b/back/volume/src/pong/pong.service.ts @@ -15,14 +15,23 @@ export class PongService { private readonly usersService: UsersService ) {} - async updateStats (player: User, i: number, result: Result, maxScore: number): Promise { + async updateStats ( + player: User, + i: number, + result: Result, + maxScore: number + ): Promise { player.matchs++ if (result.score[i] === maxScore) player.wins++ else player.looses++ player.winrate = (100 * player.wins) / player.matchs } - async updatePlayer (i: number, result: Result, maxScore: number): 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, maxScore) @@ -37,7 +46,11 @@ export class PongService { await this.usersService.save(player) } - async saveResult (players: Player[], ranked: boolean, maxScore: number): 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)) diff --git a/front/volume/src/components/Pong/GameCreation.svelte b/front/volume/src/components/Pong/GameCreation.svelte index df9b7e3..d977778 100644 --- a/front/volume/src/components/Pong/GameCreation.svelte +++ b/front/volume/src/components/Pong/GameCreation.svelte @@ -1,6 +1,11 @@