diff --git a/srcs/requirements/nestjs/api_back/src/game/game.controller.ts b/srcs/requirements/nestjs/api_back/src/game/game.controller.ts index 57a87978..ab5f787e 100644 --- a/srcs/requirements/nestjs/api_back/src/game/game.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/game/game.controller.ts @@ -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) diff --git a/srcs/requirements/nestjs/api_back/src/game/game.module.ts b/srcs/requirements/nestjs/api_back/src/game/game.module.ts index e834599f..ac4971eb 100644 --- a/srcs/requirements/nestjs/api_back/src/game/game.module.ts +++ b/srcs/requirements/nestjs/api_back/src/game/game.module.ts @@ -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] }) diff --git a/srcs/requirements/nestjs/api_back/src/game/game.service.ts b/srcs/requirements/nestjs/api_back/src/game/game.service.ts index 37297153..00ac5e78 100644 --- a/srcs/requirements/nestjs/api_back/src/game/game.service.ts +++ b/srcs/requirements/nestjs/api_back/src/game/game.service.ts @@ -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, @InjectRepository(User) private readonly userRepository : Repository, + @InjectRepository(MatchHistory) + private readonly matchHistory : Repository, @InjectRepository(TokenGame) private readonly tokenGameRepository : Repository, 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 } diff --git a/srcs/requirements/nestjs/api_back/src/users/class/matchHistory.class.ts b/srcs/requirements/nestjs/api_back/src/users/class/matchHistory.class.ts new file mode 100644 index 00000000..a0bc4a4e --- /dev/null +++ b/srcs/requirements/nestjs/api_back/src/users/class/matchHistory.class.ts @@ -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; + }; +} \ No newline at end of file diff --git a/srcs/requirements/nestjs/api_back/src/users/entities/matchHistory.entity.ts b/srcs/requirements/nestjs/api_back/src/users/entities/matchHistory.entity.ts new file mode 100644 index 00000000..c1516f25 --- /dev/null +++ b/srcs/requirements/nestjs/api_back/src/users/entities/matchHistory.entity.ts @@ -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; +} \ No newline at end of file diff --git a/srcs/requirements/nestjs/api_back/src/users/entities/user.entity.ts b/srcs/requirements/nestjs/api_back/src/users/entities/user.entity.ts index 24a268be..f56b81ca 100644 --- a/srcs/requirements/nestjs/api_back/src/users/entities/user.entity.ts +++ b/srcs/requirements/nestjs/api_back/src/users/entities/user.entity.ts @@ -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 + } diff --git a/srcs/requirements/nestjs/api_back/src/users/users.service.ts b/srcs/requirements/nestjs/api_back/src/users/users.service.ts index d84d81c4..f2065216 100644 --- a/srcs/requirements/nestjs/api_back/src/users/users.service.ts +++ b/srcs/requirements/nestjs/api_back/src/users/users.service.ts @@ -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 {