Browse Source

fix results save

master
nicolas-arnaud 2 years ago
parent
commit
3a3a102104
  1. 2
      back/volume/src/pong/entity/result.entity.ts
  2. 10
      back/volume/src/pong/game/Game.ts
  3. 6
      back/volume/src/pong/game/Games.ts
  4. 9
      back/volume/src/pong/pong.gateway.ts
  5. 1
      back/volume/src/pong/pong.module.ts
  6. 30
      back/volume/src/pong/pong.service.ts
  7. 10
      back/volume/src/users/users.service.ts
  8. 3
      front/volume/src/components/Pong/Pong.svelte

2
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[]

10
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<void> {
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

6
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<string, number>()
private readonly games = new Array<Game>()
@ -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)

9
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<WebSocketWithId, string>()
private readonly matchmakingQueue = new MatchmakingQueue(this.games)

1
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 {}

30
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<User> => {
result.players = await Promise.all(players.map(async (p): Promise<User | null> => {
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])
}
}

10
back/volume/src/users/users.service.ts

@ -19,9 +19,13 @@ export class UsersService {
return await this.usersRepository.find({})
}
async findUserByName (username: string): Promise<User> {
let user = await this.usersRepository.findOneBy({ username })
if (!user) return Promise.reject()
async findUserByName (username: string): Promise<User | null> {
let user = await this.usersRepository.findOne({
where: { username: username },
relations : {results: true}
})
if (!user) return null;
else return user;
}

3
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,

Loading…
Cancel
Save