Browse Source

fix unsaved results

master
nicolas-arnaud 2 years ago
parent
commit
595a52b709
  1. 5
      back/volume/src/pong/entity/result.entity.ts
  2. 18
      back/volume/src/pong/pong.service.ts
  3. 3
      back/volume/src/users/entity/user.entity.ts
  4. 8
      back/volume/src/users/users.service.ts
  5. 2
      front/volume/src/App.svelte

5
back/volume/src/pong/entity/result.entity.ts

@ -3,7 +3,8 @@ import {
PrimaryGeneratedColumn, PrimaryGeneratedColumn,
Column, Column,
ManyToMany, ManyToMany,
CreateDateColumn CreateDateColumn,
JoinTable
} from 'typeorm' } from 'typeorm'
import User from 'src/users/entity/user.entity' import User from 'src/users/entity/user.entity'
@ -13,7 +14,7 @@ export default class Result {
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
id: number id: number
@ManyToMany(() => User, (player: User) => player.results) @ManyToMany(() => User, (player: User) => player.results, { cascade: true})
players: Array<User | null> // TODO: change to User[] for final version players: Array<User | null> // TODO: change to User[] for final version
@Column('text', { array: true }) @Column('text', { array: true })

18
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++ if (result.score[i] > result.score[Math.abs(i - 1)]) player.wins++
else player.looses++ else player.looses++
player.winrate = (100 * player.wins) / player.matchs 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) this.usersService.save(player)
} }
async saveResult (players: Player[]) { async saveResult (players: Player[]) {
const result = new Result() let result = new Result()
result.players = await Promise.all( let ply = new Array<User | null>
players.map(async (p): Promise<User | null> => { ply.push(await this.usersService.findUserByName(players[0].name))
return await this.usersService.findUserByName(p.name) ply.push(await this.usersService.findUserByName(players[1].name))
}) result.players = ply;
) result.score = [players[0].score, players[1].score]
result.score = players.map((p) => p.score) this.resultsRepository.save(result)
this.updatePlayer(0, result) this.updatePlayer(0, result)
this.updatePlayer(1, result) this.updatePlayer(1, result)
this.resultsRepository.save(result)
} }
async getHistory (): Promise<Result[]> { async getHistory (): Promise<Result[]> {

3
back/volume/src/users/entity/user.entity.ts

@ -38,6 +38,9 @@ export class User {
matchs: number matchs: number
@Column({ default: 0}) @Column({ default: 0})
rank: number
@Column({ default: 0 , type: "double precision"})
winrate: number winrate: number
@ManyToMany(() => Result, (result: Result) => result.players) @ManyToMany(() => Result, (result: Result) => result.players)

8
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 { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm' import { EntityNotFoundError, QueryFailedError, Repository } from 'typeorm'
import { User } from './entity/user.entity' import { User } from './entity/user.entity'
import { type UserDto } from './dto/user.dto' import { type UserDto } from './dto/user.dto'
import { type Channel } from 'src/chat/entity/channel.entity' import { type Channel } from 'src/chat/entity/channel.entity'
import Result from 'src/pong/entity/result.entity' import Result from 'src/pong/entity/result.entity'
@Injectable() @Injectable()
@Catch(QueryFailedError, EntityNotFoundError)
export class UsersService { export class UsersService {
constructor ( constructor (
@InjectRepository(User) private readonly usersRepository: Repository<User>, @InjectRepository(User) private readonly usersRepository: Repository<User>,
@ -25,8 +26,7 @@ export class UsersService {
where: { username }, where: { username },
relations: { results: true } relations: { results: true }
}) })
if (user == null) return null return user
else return user
} }
async findUser (ftId: number): Promise<User | null> { async findUser (ftId: number): Promise<User | null> {

2
front/volume/src/App.svelte

@ -168,7 +168,7 @@
wins={$store.wins} wins={$store.wins}
losses={$store.looses} losses={$store.looses}
winrate={$store.winrate} winrate={$store.winrate}
rank={23} rank={$store.rank}
is2faEnabled={false} is2faEnabled={false}
/> />
</div> </div>

Loading…
Cancel
Save