Browse Source

* Added speed factor in custom games

master
vvandenb 2 years ago
parent
commit
fd53c9ce80
  1. 17
      back/volume/src/pong/dtos/GameCreationDtoValidated.ts
  2. 9
      back/volume/src/pong/game/Ball.ts
  3. 16
      back/volume/src/pong/game/Game.ts
  4. 6
      back/volume/src/pong/game/Games.ts
  5. 6
      back/volume/src/pong/game/MatchmakingQueue.ts
  6. 19
      back/volume/src/pong/pong.service.ts
  7. 30
      front/volume/src/components/Pong/GameCreation.svelte
  8. 2
      front/volume/src/components/Pong/dtos/GameCreationDto.ts

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

9
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

16
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()

6
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)

6
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

19
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<void> {
async updateStats (
player: User,
i: number,
result: Result,
maxScore: number
): Promise<void> {
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<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, maxScore)
@ -37,7 +46,11 @@ export class PongService {
await this.usersService.save(player)
}
async saveResult (players: Player[], ranked: boolean, maxScore: number): 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))

30
front/volume/src/components/Pong/GameCreation.svelte

@ -1,6 +1,11 @@
<script lang="ts">
import { Map } from "./Map";
import { DEFAULT_MAP_SIZE, GAME_EVENTS } from "./constants";
import {
DEFAULT_BALL_INITIAL_SPEED,
DEFAULT_MAP_SIZE,
DEFAULT_MAX_BALL_SPEED,
GAME_EVENTS,
} from "./constants";
import MapCustomization from "./MapCustomization.svelte";
import type { GameCreationDto } from "./dtos/GameCreationDto";
import { store } from "../../Auth";
@ -10,11 +15,15 @@
export let invitedUsername: string;
let map: Map = new Map(DEFAULT_MAP_SIZE.clone(), []);
let initialBallSpeedX: number = DEFAULT_BALL_INITIAL_SPEED.x;
let initialBallSpeedY: number = DEFAULT_BALL_INITIAL_SPEED.y;
function createGame() {
const data: GameCreationDto = {
playerNames: [$store.username, invitedUsername],
map,
initialBallSpeedX,
initialBallSpeedY,
};
socket.emit(GAME_EVENTS.CREATE_GAME, data);
}
@ -27,6 +36,25 @@
<button on:click={createGame}>
Create game vs {invitedUsername}
</button>
<div>
<span>Initial ball's speed (X): {initialBallSpeedX}</span>
<br />
<input
type="range"
bind:value={initialBallSpeedX}
min={DEFAULT_BALL_INITIAL_SPEED.x}
max={DEFAULT_MAX_BALL_SPEED.x}
/>
<br />
<span>Initial ball's speed (Y): {initialBallSpeedY}</span>
<br />
<input
type="range"
bind:value={initialBallSpeedY}
min={DEFAULT_BALL_INITIAL_SPEED.y}
max={DEFAULT_MAX_BALL_SPEED.y}
/>
</div>
<MapCustomization {map} />
</div>
</div>

2
front/volume/src/components/Pong/dtos/GameCreationDto.ts

@ -3,4 +3,6 @@ import type { Map } from "../Map";
export class GameCreationDto {
playerNames: string[];
map: Map;
initialBallSpeedX: number;
initialBallSpeedY: number;
}

Loading…
Cancel
Save