ajout d'une méthode pour avoir les résultats des derniers match d'un joueur
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Body, Controller, Get, HttpException, HttpStatus, Post, Req, Res, UseGuards } from '@nestjs/common';
|
||||
import { Body, Controller, Get, HttpException, HttpStatus, Param, Post, Req, Res, UseGuards } from '@nestjs/common';
|
||||
import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
|
||||
import { User } from 'src/users/entities/user.entity';
|
||||
import { Response } from 'express';
|
||||
@@ -21,6 +21,14 @@ export class GameController {
|
||||
return this.gameService.getMatchesForSpectator();
|
||||
}
|
||||
|
||||
@Get('match/history/:username')
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
async getMatchHistory(@Req() req, @Param('username') username: string, @Res() res)
|
||||
{
|
||||
return this.gameService.getMatchHistory(username, res);
|
||||
}
|
||||
|
||||
@Get('ranking')
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Friendship } from 'src/friendship/entities/friendship.entity';
|
||||
import { FriendshipService } from 'src/friendship/friendship.service';
|
||||
import { MatchHistory } from 'src/users/entities/matchHistory.entity';
|
||||
import { User } from 'src/users/entities/user.entity';
|
||||
import { UsersService } from 'src/users/users.service';
|
||||
import { Game } from './entity/game.entity';
|
||||
@@ -10,7 +11,7 @@ import { GameController } from './game.controller';
|
||||
import { GameService } from './game.service';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([TokenGame, User, Game, Friendship])],
|
||||
imports: [TypeOrmModule.forFeature([TokenGame, User, Game, Friendship, MatchHistory])],
|
||||
controllers: [GameController],
|
||||
providers: [GameService, UsersService, FriendshipService]
|
||||
})
|
||||
|
||||
@@ -14,6 +14,8 @@ import { CreateGameDto } from './dto/createGame.dto';
|
||||
import { UpdateGameDto } from './dto/updateGame.dto';
|
||||
import { FriendshipService } from 'src/friendship/friendship.service';
|
||||
import { STATUS } from 'src/common/constants/constants';
|
||||
import { MatchHistory } from 'src/users/entities/matchHistory.entity';
|
||||
import { SendableMatchHistory } from 'src/users/class/matchHistory.class';
|
||||
|
||||
@Injectable()
|
||||
export class GameService {
|
||||
@@ -22,6 +24,8 @@ export class GameService {
|
||||
private readonly gameRepository : Repository<Game>,
|
||||
@InjectRepository(User)
|
||||
private readonly userRepository : Repository<User>,
|
||||
@InjectRepository(MatchHistory)
|
||||
private readonly matchHistory : Repository<MatchHistory>,
|
||||
@InjectRepository(TokenGame)
|
||||
private readonly tokenGameRepository : Repository<TokenGame>,
|
||||
private readonly userService : UsersService,
|
||||
@@ -43,6 +47,26 @@ export class GameService {
|
||||
return gamesToReturn;
|
||||
}
|
||||
|
||||
async getMatchHistory(username : string, @Res() res : Response)
|
||||
{
|
||||
const user = await this.userService.findOne(username);
|
||||
if (!user)
|
||||
return res.status(HttpStatus.NOT_FOUND).json({message : "History for " + username + " not found"});
|
||||
const gameHistory = await this.matchHistory
|
||||
.createQueryBuilder('history')
|
||||
.leftJoinAndSelect('history.playerOne', 'playerOne')
|
||||
.leftJoinAndSelect('history.playerTwo', 'playerTwo')
|
||||
.where('playerOne.id = :playerOneId', { playerOneId: user.id })
|
||||
.orWhere('playerTwo.id = :playerTwoId', { playerTwoId: user.id })
|
||||
.getMany();
|
||||
let sendableHistory : SendableMatchHistory[] = []
|
||||
for (const history of gameHistory)
|
||||
{
|
||||
sendableHistory.push(new SendableMatchHistory(history))
|
||||
}
|
||||
return sendableHistory;
|
||||
}
|
||||
|
||||
async getRankingForAllUsers(currentUser : User) {
|
||||
const users = await this.userRepository.createQueryBuilder("user")
|
||||
.leftJoinAndSelect("user.stats", "stats")
|
||||
@@ -293,6 +317,13 @@ export class GameService {
|
||||
this.userService.incrementVictories(playerOne.id)
|
||||
this.userService.incrementDefeats(playerTwo.id)
|
||||
}
|
||||
const matchHistory = new MatchHistory();
|
||||
matchHistory.playerOne = playerOne;
|
||||
matchHistory.playerTwo = playerTwo;
|
||||
matchHistory.playerOneResult = game.playerOneUsernameResult;
|
||||
matchHistory.playerTwoResult = game.playerTwoUsernameResult;
|
||||
const savedHistory = await this.matchHistory.save(matchHistory);
|
||||
console.log(savedHistory);
|
||||
return HttpStatus.OK
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
import { match } from "assert";
|
||||
import { MatchHistory } from "../entities/matchHistory.entity";
|
||||
|
||||
export class SendableMatchHistory {
|
||||
id: number;
|
||||
date: Date;
|
||||
playerOneUsername: string;
|
||||
playerTwoUsername: string;
|
||||
playerTwoResult : number;
|
||||
playerOneResult : number;
|
||||
|
||||
constructor(matchHistory: MatchHistory) {
|
||||
this.id = matchHistory.id;
|
||||
this.date = matchHistory.date
|
||||
this.playerOneUsername = matchHistory.playerOne.username;
|
||||
this.playerTwoUsername = matchHistory.playerTwo.username;
|
||||
this.playerOneResult = matchHistory.playerOneResult;
|
||||
this.playerTwoResult = matchHistory.playerTwoResult;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Column, CreateDateColumn, Entity, ManyToOne, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { User } from "./user.entity";
|
||||
|
||||
@Entity('matchHistory')
|
||||
export class MatchHistory {
|
||||
@PrimaryGeneratedColumn()
|
||||
id : number;
|
||||
@ManyToOne(type => User, user => user.username, {onDelete: 'CASCADE'})
|
||||
playerOne: User;
|
||||
@ManyToOne(type => User, user => user.username, {onDelete: 'CASCADE'})
|
||||
playerTwo: User;
|
||||
@Column()
|
||||
playerOneResult : number;
|
||||
@Column()
|
||||
playerTwoResult : number;
|
||||
@CreateDateColumn()
|
||||
date : Date;
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { Exclude } from "class-transformer";
|
||||
import { IsEmail, Length } from "class-validator";
|
||||
import { Column, Entity, JoinColumn, JoinTable, ManyToMany, OneToMany, OneToOne, PrimaryGeneratedColumn, Unique } from "typeorm";
|
||||
import { Friendship } from "../../friendship/entities/friendship.entity";
|
||||
import { MatchHistory } from "./matchHistory.entity";
|
||||
import { UserStats } from "./userStat.entities";
|
||||
|
||||
|
||||
@@ -49,12 +50,20 @@ export class User {
|
||||
@OneToMany(type => Friendship , (friendship) => friendship.receiver, {onDelete: 'CASCADE'})
|
||||
receivedFriendRequest: Friendship[];
|
||||
|
||||
@OneToMany(type => MatchHistory , (matchHistory) => matchHistory.playerOne, {onDelete: 'CASCADE'})
|
||||
playerOneMatch: MatchHistory[];
|
||||
|
||||
@OneToMany(type => MatchHistory , (matchHistory) => matchHistory.playerTwo, {onDelete: 'CASCADE'})
|
||||
playerTwoMatch: MatchHistory[];
|
||||
|
||||
@JoinColumn()
|
||||
@OneToOne(() => UserStats, { cascade: true, onDelete: 'CASCADE' })
|
||||
stats: UserStats;
|
||||
|
||||
|
||||
// ROOMS :
|
||||
|
||||
@Column({ nullable: true })
|
||||
currentRoom: string; // chatroom name
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import { CreateUsersDto } from './dto/create-users.dto';
|
||||
import { UpdateUsersDto } from './dto/update-users.dto';
|
||||
import { UserStats } from './entities/userStat.entities';
|
||||
import { FriendshipService } from 'src/friendship/friendship.service';
|
||||
import { MatchHistory } from './entities/matchHistory.entity';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
|
||||
Reference in New Issue
Block a user