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. 5
      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,
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<User | null> // TODO: change to User[] for final version
@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++
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<User | null> => {
return await this.usersService.findUserByName(p.name)
})
)
result.score = players.map((p) => p.score)
let result = new Result()
let ply = new Array<User | null>
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<Result[]> {

5
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)

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 { 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<User>,
@ -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<User | null> {

2
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}
/>
</div>

Loading…
Cancel
Save