nicolas-arnaud
2 years ago
10 changed files with 102 additions and 14 deletions
@ -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[] |
|||
} |
@ -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 {} |
|||
|
@ -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<Result>, |
|||
private readonly usersService: UsersService |
|||
) { } |
|||
|
|||
async saveResult(players: Player[]) { |
|||
let result = new Result; |
|||
result.players = await Promise.all(players.map(async (p): Promise<User> => { |
|||
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]) |
|||
} |
|||
} |
Loading…
Reference in new issue