nicolas-arnaud
2 years ago
15 changed files with 180 additions and 99 deletions
@ -1,40 +1,52 @@ |
|||||
import { Inject, Injectable } from "@nestjs/common"; |
import { Inject, Injectable, forwardRef } from '@nestjs/common' |
||||
import { InjectRepository } from "@nestjs/typeorm"; |
import { InjectRepository } from '@nestjs/typeorm' |
||||
import { Repository } from 'typeorm' |
import { Repository } from 'typeorm' |
||||
import { UsersService } from 'src/users/users.service' |
import { UsersService } from 'src/users/users.service' |
||||
import Result from './entity/result.entity' |
import Result from './entity/result.entity' |
||||
import User from 'src/users/entity/user.entity' |
import type User from 'src/users/entity/user.entity' |
||||
import { Player } from './game/Player' |
import { type Player } from './game/Player' |
||||
|
|
||||
|
|
||||
@Injectable() |
@Injectable() |
||||
export class PongService { |
export class PongService { |
||||
constructor( |
constructor ( |
||||
@InjectRepository(Result)private readonly resultsRepository: Repository<Result>, |
@InjectRepository(Result) |
||||
|
private readonly resultsRepository: Repository<Result>, |
||||
private readonly usersService: UsersService |
private readonly usersService: UsersService |
||||
) { } |
) {} |
||||
|
|
||||
async updatePlayer(i: number, result: Result) { |
async updatePlayer (i: number, result: Result) { |
||||
let player: User | null = result.players[i] |
const player: User | null = result.players[i] |
||||
if (!player) return |
if (player == null) return |
||||
player.matchs++ |
player.matchs++ |
||||
if (result.score[i] > result.score[Math.abs(i-1)]) |
if (result.score[i] > result.score[Math.abs(i - 1)]) player.wins++ |
||||
player.wins++; |
else player.looses++ |
||||
else |
player.winrate = (100 * player.wins) / player.matchs |
||||
player.looses++; |
|
||||
player.results.push(result) |
player.results.push(result) |
||||
this.usersService.save(player) |
this.usersService.save(player) |
||||
|
|
||||
} |
} |
||||
|
|
||||
async saveResult(players: Player[]) { |
async saveResult (players: Player[]) { |
||||
let result = new Result; |
const result = new Result() |
||||
result.players = await Promise.all(players.map(async (p): Promise<User | null> => { |
result.players = await Promise.all( |
||||
return await this.usersService.findUserByName(p.name) |
players.map(async (p): Promise<User | null> => { |
||||
})) |
return await this.usersService.findUserByName(p.name) |
||||
result.score = players.map((p) => p.score); |
}) |
||||
|
) |
||||
|
result.score = players.map((p) => p.score) |
||||
this.updatePlayer(0, result) |
this.updatePlayer(0, result) |
||||
this.updatePlayer(1, result) |
this.updatePlayer(1, result) |
||||
this.resultsRepository.save(result) |
this.resultsRepository.save(result) |
||||
} |
} |
||||
|
|
||||
|
async getHistory (): Promise<Result[]> { |
||||
|
return await this.resultsRepository.find({ |
||||
|
order: { date: 'DESC' } |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
async getHistoryById (ftId: number): Promise<Result[]> { |
||||
|
const results = await this.usersService.getResults(ftId) |
||||
|
return results.sort((a, b) => (a.date < b.date ? 1 : -1)) |
||||
|
} |
||||
|
|
||||
} |
} |
||||
|
@ -1,13 +1,16 @@ |
|||||
import { Module } from '@nestjs/common' |
import { forwardRef, Module } from '@nestjs/common' |
||||
import { TypeOrmModule } from '@nestjs/typeorm' |
import { TypeOrmModule } from '@nestjs/typeorm' |
||||
import { User } from './entity/user.entity' |
import { User } from './entity/user.entity' |
||||
import { UsersController } from './users.controller' |
import { UsersController } from './users.controller' |
||||
import { UsersService } from './users.service' |
import { UsersService } from './users.service' |
||||
|
import { PongModule } from 'src/pong/pong.module' |
||||
|
|
||||
@Module({ |
@Module({ |
||||
imports: [TypeOrmModule.forFeature([User])], |
imports: [ |
||||
|
forwardRef(() => PongModule), |
||||
|
TypeOrmModule.forFeature([User])], |
||||
controllers: [UsersController], |
controllers: [UsersController], |
||||
providers: [UsersService], |
providers: [UsersService], |
||||
exports: [UsersService] |
exports: [UsersService] |
||||
}) |
}) |
||||
export class UsersModule {} |
export class UsersModule { } |
||||
|
Loading…
Reference in new issue