From 54d53a1d7384c9bb2b87d5a486444eb0c90b7dbb Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Sun, 5 Mar 2023 18:49:43 +0100 Subject: [PATCH] matchs result save in back and display in profile --- back/volume/src/pong/entity/result.entity.ts | 20 +++++++++++ back/volume/src/pong/game/Game.ts | 8 +++-- back/volume/src/pong/game/constants.ts | 1 + back/volume/src/pong/pong.module.ts | 10 +++++- back/volume/src/pong/pong.service.ts | 38 ++++++++++++++++++++ back/volume/src/users/dto/user.dto.ts | 1 + back/volume/src/users/entity/user.entity.ts | 14 ++++++++ back/volume/src/users/users.service.ts | 10 ++++-- front/volume/src/App.svelte | 6 ++-- front/volume/src/components/Profile.svelte | 8 ++--- 10 files changed, 102 insertions(+), 14 deletions(-) create mode 100644 back/volume/src/pong/entity/result.entity.ts create mode 100644 back/volume/src/pong/pong.service.ts diff --git a/back/volume/src/pong/entity/result.entity.ts b/back/volume/src/pong/entity/result.entity.ts new file mode 100644 index 0000000..e93cb3f --- /dev/null +++ b/back/volume/src/pong/entity/result.entity.ts @@ -0,0 +1,20 @@ +import { + Entity, + PrimaryGeneratedColumn, + Column, + ManyToMany, +} from 'typeorm' + +import User from 'src/users/entity/user.entity' + +@Entity() +export default class Result { + @PrimaryGeneratedColumn() + id:number + + @ManyToMany(() => User, (player: User) => player.results) + players: User[] + + @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 9335c1a..09fb411 100644 --- a/back/volume/src/pong/game/Game.ts +++ b/back/volume/src/pong/game/Game.ts @@ -7,15 +7,15 @@ import { DEFAULT_PADDLE_SIZE, DEFAULT_PLAYER_X_OFFSET, DEFAULT_WIN_SCORE, - GAME_EVENTS + GAME_EVENTS, + GAME_TICKS } from './constants' import { randomUUID } from 'crypto' import { Spectator } from './Spectator' import { type MapDtoValidated } from '../dtos/MapDtoValidated' import { type GameUpdate } from '../dtos/GameUpdate' import { type GameInfo } from '../dtos/GameInfo' - -const GAME_TICKS = 30 +import { PongService } from '../pong.service' function gameLoop (game: Game): void { const canvasRect: Rect = new Rect( @@ -50,6 +50,7 @@ function gameLoop (game: Game): void { } export class Game { + private readonly pongService: PongService id: string timer: NodeJS.Timer | null map: MapDtoValidated @@ -147,6 +148,7 @@ export class Game { stop (): void { if (this.timer !== null) { 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/constants.ts b/back/volume/src/pong/game/constants.ts index bab1b53..ff44063 100644 --- a/back/volume/src/pong/game/constants.ts +++ b/back/volume/src/pong/game/constants.ts @@ -17,3 +17,4 @@ export const DEFAULT_PADDLE_SIZE = new Point(6, 50) export const DEFAULT_BALL_SIZE = new Point(20, 20) export const DEFAULT_PLAYER_X_OFFSET = 50 export const DEFAULT_WIN_SCORE = 5 +export const GAME_TICKS = 30 diff --git a/back/volume/src/pong/pong.module.ts b/back/volume/src/pong/pong.module.ts index a05a86d..a7123d9 100644 --- a/back/volume/src/pong/pong.module.ts +++ b/back/volume/src/pong/pong.module.ts @@ -1,7 +1,15 @@ import { Module } from '@nestjs/common' import { PongGateway } from './pong.gateway' +import Result from './entity/result.entity' +import {TypeOrmModule } from '@nestjs/typeorm' +import {PongService } from './pong.service' +import { UsersModule } from 'src/users/users.module' @Module({ - providers: [PongGateway] + imports: [ + UsersModule, + TypeOrmModule.forFeature([Result]) + ], + providers: [PongGateway, PongService], }) export class PongModule {} diff --git a/back/volume/src/pong/pong.service.ts b/back/volume/src/pong/pong.service.ts new file mode 100644 index 0000000..d026374 --- /dev/null +++ b/back/volume/src/pong/pong.service.ts @@ -0,0 +1,38 @@ +import { Inject, Injectable } from "@nestjs/common"; +import { InjectRepository } from "@nestjs/typeorm"; +import { Repository } from 'typeorm' +import { UsersService } from 'src/users/users.service' +import Result from './entity/result.entity' +import User from 'src/users/entity/user.entity' +import { Player } from './game/Player' + + +@Injectable() +export class PongService { + constructor( + @InjectRepository(Result)private readonly resultsRepository: Repository, + private readonly usersService: UsersService + ) { } + + async saveResult(players: Player[]) { + let result = new Result; + 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.resultsRepository.save(result) + this.usersService.save(result.players[0],) + this.usersService.save(result.players[1]) + } +} diff --git a/back/volume/src/users/dto/user.dto.ts b/back/volume/src/users/dto/user.dto.ts index 399dc2a..12215c0 100644 --- a/back/volume/src/users/dto/user.dto.ts +++ b/back/volume/src/users/dto/user.dto.ts @@ -14,6 +14,7 @@ export class UserDto { @IsOptional() readonly status: string + } export class AvatarUploadDto { diff --git a/back/volume/src/users/entity/user.entity.ts b/back/volume/src/users/entity/user.entity.ts index e4d068a..c224dd3 100644 --- a/back/volume/src/users/entity/user.entity.ts +++ b/back/volume/src/users/entity/user.entity.ts @@ -9,6 +9,7 @@ import { import Message from 'src/chat/entity/message.entity' import Channel from 'src/chat/entity/channel.entity' +import Result from 'src/pong/entity/result.entity' @Entity() export class User { @@ -27,6 +28,19 @@ export class User { @Column({ name: 'avatar' }) public avatar?: string + @Column({default: 0}) + wins: number + + @Column({default: 0}) + looses: number + + @Column({default: 0}) + matchs: number + + @ManyToMany(() => Result, (result: Result) => result.players) + @JoinTable() + results: Result[] + @OneToMany(() => Message, (message: Message) => message.author) messages: Message[] diff --git a/back/volume/src/users/users.service.ts b/back/volume/src/users/users.service.ts index c8e617a..df40509 100644 --- a/back/volume/src/users/users.service.ts +++ b/back/volume/src/users/users.service.ts @@ -11,12 +11,18 @@ export class UsersService { @InjectRepository(User) private readonly usersRepository: Repository ) {} + save(user: User) { + this.usersRepository.save(user) + } + async findUsers (): Promise { return await this.usersRepository.find({}) } - async findUserByName (username: string): Promise { - return await this.usersRepository.findOneBy({ username }) + async findUserByName (username: string): Promise { + let user = await this.usersRepository.findOneBy({ username }) + if (!user) return Promise.reject() + else return user; } async findUser (ftId: number): Promise { diff --git a/front/volume/src/App.svelte b/front/volume/src/App.svelte index 75348ba..ff3883e 100644 --- a/front/volume/src/App.svelte +++ b/front/volume/src/App.svelte @@ -167,9 +167,9 @@ > diff --git a/front/volume/src/components/Profile.svelte b/front/volume/src/components/Profile.svelte index d0a3560..4bac292 100644 --- a/front/volume/src/components/Profile.svelte +++ b/front/volume/src/components/Profile.svelte @@ -1,10 +1,10 @@