From 595a52b709cd715ba7352ebf1a36a75d82065a3d Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Mon, 6 Mar 2023 08:53:07 +0100 Subject: [PATCH] fix unsaved results --- back/volume/src/pong/entity/result.entity.ts | 5 +++-- back/volume/src/pong/pong.service.ts | 18 +++++++++--------- back/volume/src/users/entity/user.entity.ts | 5 ++++- back/volume/src/users/users.service.ts | 8 ++++---- front/volume/src/App.svelte | 2 +- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/back/volume/src/pong/entity/result.entity.ts b/back/volume/src/pong/entity/result.entity.ts index 2a95b64..2a9eade 100644 --- a/back/volume/src/pong/entity/result.entity.ts +++ b/back/volume/src/pong/entity/result.entity.ts @@ -3,7 +3,8 @@ import { PrimaryGeneratedColumn, Column, ManyToMany, - CreateDateColumn + CreateDateColumn, + JoinTable } from 'typeorm' import User from 'src/users/entity/user.entity' @@ -13,7 +14,7 @@ export default class Result { @PrimaryGeneratedColumn() id: number - @ManyToMany(() => User, (player: User) => player.results) + @ManyToMany(() => User, (player: User) => player.results, { cascade: true}) players: Array // TODO: change to User[] for final version @Column('text', { array: true }) diff --git a/back/volume/src/pong/pong.service.ts b/back/volume/src/pong/pong.service.ts index 085a0b0..45f44c8 100644 --- a/back/volume/src/pong/pong.service.ts +++ b/back/volume/src/pong/pong.service.ts @@ -21,21 +21,21 @@ export class PongService { if (result.score[i] > result.score[Math.abs(i - 1)]) player.wins++ else player.looses++ player.winrate = (100 * player.wins) / player.matchs - player.results.push(result) + player.rank = await this.usersService.getRank(player.ftId) + 1 + //player.results.push(result) this.usersService.save(player) } async saveResult (players: Player[]) { - const 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) + let result = new Result() + let ply = new Array + ply.push(await this.usersService.findUserByName(players[0].name)) + ply.push(await this.usersService.findUserByName(players[1].name)) + result.players = ply; + result.score = [players[0].score, players[1].score] + this.resultsRepository.save(result) this.updatePlayer(0, result) this.updatePlayer(1, result) - this.resultsRepository.save(result) } async getHistory (): Promise { diff --git a/back/volume/src/users/entity/user.entity.ts b/back/volume/src/users/entity/user.entity.ts index b0fda9e..384b43d 100644 --- a/back/volume/src/users/entity/user.entity.ts +++ b/back/volume/src/users/entity/user.entity.ts @@ -37,7 +37,10 @@ export class User { @Column({ default: 0 }) matchs: number - @Column({ default: 0 }) + @Column({ default: 0}) + rank: number + + @Column({ default: 0 , type: "double precision"}) winrate: number @ManyToMany(() => Result, (result: Result) => result.players) diff --git a/back/volume/src/users/users.service.ts b/back/volume/src/users/users.service.ts index 377c5af..fdda2eb 100644 --- a/back/volume/src/users/users.service.ts +++ b/back/volume/src/users/users.service.ts @@ -1,12 +1,13 @@ -import { Injectable, NotFoundException } from '@nestjs/common' +import { Catch, Injectable, NotFoundException } from '@nestjs/common' import { InjectRepository } from '@nestjs/typeorm' -import { Repository } from 'typeorm' +import { EntityNotFoundError, QueryFailedError, Repository } from 'typeorm' import { User } from './entity/user.entity' import { type UserDto } from './dto/user.dto' import { type Channel } from 'src/chat/entity/channel.entity' import Result from 'src/pong/entity/result.entity' @Injectable() +@Catch(QueryFailedError, EntityNotFoundError) export class UsersService { constructor ( @InjectRepository(User) private readonly usersRepository: Repository, @@ -25,8 +26,7 @@ export class UsersService { where: { username }, relations: { results: true } }) - if (user == null) return null - else return user + return user } async findUser (ftId: number): Promise { diff --git a/front/volume/src/App.svelte b/front/volume/src/App.svelte index cdcb096..0ebb2b6 100644 --- a/front/volume/src/App.svelte +++ b/front/volume/src/App.svelte @@ -168,7 +168,7 @@ wins={$store.wins} losses={$store.looses} winrate={$store.winrate} - rank={23} + rank={$store.rank} is2faEnabled={false} />