diff --git a/back/volume/src/pong/entity/result.entity.ts b/back/volume/src/pong/entity/result.entity.ts index e93cb3f..aaa7ed2 100644 --- a/back/volume/src/pong/entity/result.entity.ts +++ b/back/volume/src/pong/entity/result.entity.ts @@ -13,7 +13,7 @@ export default class Result { id:number @ManyToMany(() => User, (player: User) => player.results) - players: User[] + players: (User | null)[] // TODO: change to User[] for final version @Column('text', {array: true}) public score: number[] diff --git a/back/volume/src/pong/game/Game.ts b/back/volume/src/pong/game/Game.ts index 09fb411..a414ff1 100644 --- a/back/volume/src/pong/game/Game.ts +++ b/back/volume/src/pong/game/Game.ts @@ -16,6 +16,7 @@ import { type MapDtoValidated } from '../dtos/MapDtoValidated' import { type GameUpdate } from '../dtos/GameUpdate' import { type GameInfo } from '../dtos/GameInfo' import { PongService } from '../pong.service' +import { Injectable, Inject } from '@nestjs/common' function gameLoop (game: Game): void { const canvasRect: Rect = new Rect( @@ -50,7 +51,6 @@ function gameLoop (game: Game): void { } export class Game { - private readonly pongService: PongService id: string timer: NodeJS.Timer | null map: MapDtoValidated @@ -65,7 +65,9 @@ export class Game { uuids: string[], names: string[], map: MapDtoValidated, - gameStoppedCallback: (name: string) => void + gameStoppedCallback: (name: string) => void, + private readonly pongService: PongService + ) { this.id = randomUUID() this.timer = null @@ -145,10 +147,10 @@ export class Game { return false } - stop (): void { + async stop (): Promise { if (this.timer !== null) { + await this.pongService.saveResult(this.players) this.gameStoppedCallback(this.players[0].name) - this.pongService.saveResult(this.players) clearInterval(this.timer) this.timer = null diff --git a/back/volume/src/pong/game/Games.ts b/back/volume/src/pong/game/Games.ts index 6734c0e..13d436c 100644 --- a/back/volume/src/pong/game/Games.ts +++ b/back/volume/src/pong/game/Games.ts @@ -4,6 +4,7 @@ import { Point } from './utils' import { type MapDtoValidated as GameMap } from '../dtos/MapDtoValidated' import { type GameCreationDtoValidated } from '../dtos/GameCreationDtoValidated' import { type GameInfo } from '../dtos/GameInfo' +import { PongService } from '../pong.service' import { DEFAULT_BALL_SIZE, DEFAULT_PADDLE_SIZE, @@ -11,7 +12,9 @@ import { DEFAULT_WIN_SCORE } from './constants' + export class Games { + constructor (private readonly pongService : PongService) {} private readonly playerNameToGameIndex = new Map() private readonly games = new Array() @@ -29,7 +32,8 @@ export class Games { uuids, names, map, - this.gameStopped.bind(this, names[0]) + this.gameStopped.bind(this, names[0]), + this.pongService, ) ) this.playerNameToGameIndex.set(names[0], this.games.length - 1) diff --git a/back/volume/src/pong/pong.gateway.ts b/back/volume/src/pong/pong.gateway.ts index b2ef6b9..661301c 100644 --- a/back/volume/src/pong/pong.gateway.ts +++ b/back/volume/src/pong/pong.gateway.ts @@ -1,4 +1,4 @@ -import { UsePipes, ValidationPipe } from '@nestjs/common' +import { Inject, Injectable, UsePipes, ValidationPipe } from '@nestjs/common' import { type WebSocket } from 'ws' import { ConnectedSocket, @@ -20,6 +20,7 @@ import { PointDtoValidated } from './dtos/PointDtoValidated' import { StringDtoValidated } from './dtos/StringDtoValidated' import { MatchmakingQueue } from './game/MatchmakingQueue' import { MatchmakingDtoValidated } from './dtos/MatchmakingDtoValidated' +import { PongService } from './pong.service' interface WebSocketWithId extends WebSocket { id: string @@ -27,7 +28,11 @@ interface WebSocketWithId extends WebSocket { @WebSocketGateway() export class PongGateway implements OnGatewayConnection, OnGatewayDisconnect { - private readonly games: Games = new Games() + constructor( + private readonly pongService: PongService + ) {} + + private readonly games: Games = new Games(this.pongService) private readonly socketToPlayerName = new Map() private readonly matchmakingQueue = new MatchmakingQueue(this.games) diff --git a/back/volume/src/pong/pong.module.ts b/back/volume/src/pong/pong.module.ts index a7123d9..c56b8d5 100644 --- a/back/volume/src/pong/pong.module.ts +++ b/back/volume/src/pong/pong.module.ts @@ -11,5 +11,6 @@ import { UsersModule } from 'src/users/users.module' TypeOrmModule.forFeature([Result]) ], providers: [PongGateway, PongService], + exports: [PongService] }) export class PongModule {} diff --git a/back/volume/src/pong/pong.service.ts b/back/volume/src/pong/pong.service.ts index d026374..73b6816 100644 --- a/back/volume/src/pong/pong.service.ts +++ b/back/volume/src/pong/pong.service.ts @@ -14,25 +14,27 @@ export class PongService { private readonly usersService: UsersService ) { } + async updatePlayer(i: number, result: Result) { + let player: User | null = result.players[i] + if (!player) return + player.matchs++ + if (result.score[i] > result.score[Math.abs(i-1)]) + player.wins++; + else + player.looses++; + player.results.push(result) + this.usersService.save(player) + + } + async saveResult(players: Player[]) { let result = new Result; - result.players = await Promise.all(players.map(async (p): Promise => { + result.players = await Promise.all(players.map(async (p): Promise => { return await this.usersService.findUserByName(p.name) })) result.score = players.map((p) => p.score); - result.players.forEach((p) => p.matchs++) - if (result.score[0] > result.score[1]) { - result.players[0].wins++; - result.players[1].looses++; - } - else if (result.score[1] > result.score[0]) { - result.players[1].wins++ - result.players[0].looses++ - } - result.players[0].results.push(result) - result.players[1].results.push(result) + this.updatePlayer(0, result) + this.updatePlayer(1, result) this.resultsRepository.save(result) - this.usersService.save(result.players[0],) - this.usersService.save(result.players[1]) } } diff --git a/back/volume/src/users/users.service.ts b/back/volume/src/users/users.service.ts index df40509..ce32d06 100644 --- a/back/volume/src/users/users.service.ts +++ b/back/volume/src/users/users.service.ts @@ -19,9 +19,13 @@ export class UsersService { return await this.usersRepository.find({}) } - async findUserByName (username: string): Promise { - let user = await this.usersRepository.findOneBy({ username }) - if (!user) return Promise.reject() + async findUserByName (username: string): Promise { + let user = await this.usersRepository.findOne({ + where: { username: username }, + relations : {results: true} + + }) + if (!user) return null; else return user; } diff --git a/front/volume/src/components/Pong/Pong.svelte b/front/volume/src/components/Pong/Pong.svelte index 8c58ffb..a6fb0dc 100644 --- a/front/volume/src/components/Pong/Pong.svelte +++ b/front/volume/src/components/Pong/Pong.svelte @@ -8,6 +8,7 @@ import SpectateFriend from "./SpectateFriend.svelte"; import Matchmaking from "./Matchmaking.svelte"; import type { MatchmakingDto } from "./dtos/MatchmakingDto"; + import { store } from '../../Auth' const SERVER_URL = `ws://${import.meta.env.VITE_HOST}:${ import.meta.env.VITE_BACK_PORT @@ -21,7 +22,7 @@ let connected: boolean = false; let loggedIn: boolean = false; let socket: WebSocket; - let username: string = "John"; + let username: string = $store.username; function setupSocket( canvas: HTMLCanvasElement,