traintrape-moi/server/src/game/game.controller.ts

91 lines
2.6 KiB
TypeScript

import { Controller, Delete, Get, Post, Put, UseGuards } from '@nestjs/common'
import { GameService } from './game.service'
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard'
import { ApiBearerAuth } from '@nestjs/swagger'
import { GameEntity } from './entities/game.entity'
import { RepairGame } from './dto/repair-game.dto'
@Controller('game')
export class GameController {
constructor(private readonly gameService: GameService) {}
/**
* Récupérer l'objet du jeu
*
* @throws {401} Non authentifié⋅e
*/
@Get()
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
async find(): Promise<GameEntity> {
return new GameEntity(await this.gameService.find())
}
/**
* Démarrer le jeu
* @remarks Lance le jeu, tire au sort læ joueur⋅se de départ et donne un solde initial pour acheter des trains. Le jeu ne doit pas être déjà démarré.
*
* @throws {401} Non authentifié⋅e
* @throws {409} La partie est déjà démarrée
*/
@Post('/start')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
async start(): Promise<GameEntity> {
return new GameEntity(await this.gameService.start())
}
/**
* Change de joueur⋅se en course après une capture
*
* @throws {401} Non authentifié⋅e
* @throws {409} La partie n'est pas démarrée
*/
@Post('/switch-running-player')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
async switchRunningPlayer(): Promise<GameEntity> {
return new GameEntity(await this.gameService.switchRunningPlayer())
}
/**
* Arrêter le jeu
* @remarks Arrête le jeu (si déjà lancé), à n'utiliser qu'en fin de partie.
*
* @throws {401} Non authentifié⋅e
* @throws {409} La partie n'est pas démarrée
*/
@Post('/stop')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
async stop(): Promise<GameEntity> {
return new GameEntity(await this.gameService.stop())
}
/**
* Réinitialiser le jeu
* @remarks Réinitialise toutes les données de jeu, supprimant les trains parcourus et les défis accomplis. À utiliser avec précaution.
*
* @throws {401} Non authentifié⋅e
*/
@Delete('/reset')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
async reset(): Promise<GameEntity> {
return new GameEntity(await this.gameService.reset())
}
/**
* Tente de réparer l'état du jeu
* @remarks , Vérifie que tous les trains ont bien été payés et que tous les défis réussis sont bien accompagnés d'un crédit de points.
*
* @throws {401} Non authentifié⋅e
*/
@Put('/repair')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
async repair(): Promise<RepairGame> {
return await this.gameService.repair()
}
}