import { Controller, Post, UseGuards, Body, Get, Param, UsePipes, Put, UseInterceptors, ClassSerializerInterceptor, Delete, UploadedFile, Res, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; import { diskStorage } from 'multer'; import { extname } from 'path'; import { GameService } from './game.service'; import { AuthGuard } from '../shared/auth.guard'; import { User } from '../user/user.decorator'; import { GameDTO, FlagboxEventDTO, GameStateDTO, newGameDTO } from './game.dto'; import { ValidationPipe } from '../shared/validation.pipe'; import { Roles, GameStates } from '../shared/guard.decorator'; import { GameEntity } from './game.entity'; ///////////////////////////////////////////////////////////////////////// /// GameController /// /// /// /// Functions for creating, editing, deleting and listing games. /// /// Also there are functions to get objective point info and list /// /// of Factions in game /// /// /// ///////////////////////////////////////////////////////////////////////// @Controller('game') export class GameController { constructor(private gameservice: GameService) {} @Post('new') @UseGuards(new AuthGuard()) @UsePipes(new ValidationPipe()) async newGame(@User('id') person, @Body() body: newGameDTO) { return this.gameservice.createNewGame(person, body); } @Put('edit/:id') @Roles('admin') @GameStates('CREATED') @UsePipes(new ValidationPipe()) async editGame(@Param('id') id: string, @Body() body: GameDTO) { body.id = id; return this.gameservice.editGame(id, body); } @Delete('delete/:id') @Roles('admin') @GameStates('CREATED') async deleteGame(@Param('id') id: string) { return this.gameservice.deleteGame(id); } @Put('edit-state/:id') @Roles('admin') @UsePipes(new ValidationPipe()) async updateGameState(@Param('id') id: string, @Body() body: GameStateDTO) { return this.gameservice.updateGameStatus(body); } @Get('listgames') async listGames(state) { return this.gameservice.listGames(state); } @Get('listgames/:state') async listGamesState(@Param('state') state: string) { return this.gameservice.listGames(state); } // ClassSerializerInterceptor removes excluded columns set in Entities @UseInterceptors(ClassSerializerInterceptor) @Get(':id') async returnGameInfo(@Param('id') id: string) { return this.gameservice.returnGameInfo(id); } @Get('get-factions/:id') @Roles('admin') async returnGameFactions(@Param('id') id: GameEntity) { return this.gameservice.listFactions(id); } @Get('flag-events/:id') async returnFlagboxInfo(@Param('id') id: GameEntity) { return this.gameservice.returnObjectivePointInfo(id); } @Get('flag/:id') async flagboxQuery(@Param('id') id: string) { return this.gameservice.flagboxQuery(id); } @Post('flag/:id') @GameStates('STARTED') async flagboxEvent(@Param('id') id: string, @Body() data: FlagboxEventDTO) { return this.gameservice.flagboxEvent(id, data); } @Post('upload') @UseInterceptors( FileInterceptor('image', { storage: diskStorage({ destination: './images', filename: (req, file, cb) => { // Generating a 32 random chars long string const randomName = Array(32) .fill(null) .map(() => Math.round(Math.random() * 16).toString(16)) .join(''); //Calling the callback passing the random name generated with the original extension name cb(null, `${randomName}${extname(file.originalname)}`); }, }), }), ) uploadImage(@UploadedFile() image) { return image; } @Get('images/:img') returnImage(@Param('img') image, @Res() res) { return res.sendFile(image, { root: 'images' }); } }