Template
Initial commit
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Param,
|
||||
Delete,
|
||||
UseGuards,
|
||||
Session,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Patch,
|
||||
Body,
|
||||
Sse,
|
||||
} from '@nestjs/common';
|
||||
import { LobbyService } from '../services/games.lobby.service';
|
||||
import { AuthGuard } from '../guards/auth.guard';
|
||||
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { ReadGameDto } from '../dto/read-games.dto';
|
||||
import { UpdateGameDto } from '../dto/update-game.dto';
|
||||
import { GamePipe } from '../pipe/game.pipe';
|
||||
import { Game } from '../schemas/game.schema';
|
||||
import { Document } from 'mongoose';
|
||||
import { Observable, fromEvent, map } from 'rxjs';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
|
||||
@ApiTags('lobby')
|
||||
@Controller('games')
|
||||
export class LobbyController {
|
||||
constructor(
|
||||
private readonly lobbyService: LobbyService,
|
||||
private eventEmitter: EventEmitter2,
|
||||
) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Create game' })
|
||||
@ApiResponse({
|
||||
status: 201,
|
||||
description: 'Game successfully created.',
|
||||
type: ReadGameDto,
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 400,
|
||||
description: 'You can only create one game at a time',
|
||||
})
|
||||
@ApiResponse({ status: 403, description: 'Forbidden.' })
|
||||
@UseGuards(AuthGuard)
|
||||
async create(@Session() session: Record<string, any>) {
|
||||
const games = await this.lobbyService.findByOwner(session.user._id);
|
||||
if (games) {
|
||||
throw new HttpException(
|
||||
'You can only create one game at a time',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
return await this.lobbyService.create(session.user);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'List all games' })
|
||||
findAll() {
|
||||
const games = this.lobbyService.findAll();
|
||||
return games;
|
||||
}
|
||||
|
||||
@Sse('/subscribe')
|
||||
subscribe(): Observable<{ data: string }> {
|
||||
return fromEvent(this.eventEmitter, 'sse.lobby').pipe(
|
||||
map((payload) => {
|
||||
return {
|
||||
data: JSON.stringify(payload),
|
||||
};
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Get game information' })
|
||||
async findOne(@Param('id') id: string, @Param('id', GamePipe) game: Game) {
|
||||
return new ReadGameDto(game);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@ApiOperation({ summary: 'Edit game settings' })
|
||||
@UseGuards(AuthGuard)
|
||||
async update(
|
||||
@Param('id') id: string,
|
||||
@Session() session: Record<string, any>,
|
||||
@Body() updateGameDto: UpdateGameDto,
|
||||
@Param('id', GamePipe) game: Document<Game> & Game,
|
||||
) {
|
||||
if (game.owner._id.toString() != session.user._id) {
|
||||
throw new HttpException(
|
||||
'You can only edit games you own',
|
||||
HttpStatus.FORBIDDEN,
|
||||
);
|
||||
}
|
||||
return this.lobbyService.update(game, updateGameDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@ApiOperation({ summary: 'Delete game' })
|
||||
@UseGuards(AuthGuard)
|
||||
async remove(
|
||||
@Param('id') id: string,
|
||||
@Session() session: Record<string, any>,
|
||||
@Param('id', GamePipe) game: Document<Game> & Game,
|
||||
) {
|
||||
if (game.owner._id.toString() != session.user._id) {
|
||||
throw new HttpException(
|
||||
'You can only delete games you own',
|
||||
HttpStatus.FORBIDDEN,
|
||||
);
|
||||
}
|
||||
const deleted = await this.lobbyService.remove(game);
|
||||
if (deleted) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@Post(':id/start')
|
||||
@ApiOperation({ summary: 'Start game' })
|
||||
@ApiResponse({
|
||||
status: 201,
|
||||
description: 'Game successfully started.',
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 400,
|
||||
description: 'Bad Request',
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 403,
|
||||
description: 'You can only start games you own',
|
||||
})
|
||||
@UseGuards(AuthGuard)
|
||||
async start(
|
||||
@Param('id') id: string,
|
||||
@Session() session: Record<string, any>,
|
||||
@Param('id', GamePipe) game: Document<Game> & Game,
|
||||
) {
|
||||
if (game.started) {
|
||||
throw new HttpException('Game already started', HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
if (game.teams.length < 2 && !process.env.DEV_MODE) {
|
||||
throw new HttpException(
|
||||
'Not enough teams to start the game',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
if (game.owner._id.toString() != session.user._id) {
|
||||
throw new HttpException(
|
||||
'You can only start games you own',
|
||||
HttpStatus.FORBIDDEN,
|
||||
);
|
||||
}
|
||||
await this.lobbyService.start(game);
|
||||
return;
|
||||
}
|
||||
|
||||
@Post(':id/join')
|
||||
@ApiOperation({ summary: 'Join game' })
|
||||
@UseGuards(AuthGuard)
|
||||
async join(
|
||||
@Param('id') id: string,
|
||||
@Session() session: Record<string, any>,
|
||||
@Param('id', GamePipe) game: Document<Game> & Game,
|
||||
) {
|
||||
if (game.started) {
|
||||
throw new HttpException('Game already started', HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
if (
|
||||
game.teams
|
||||
.flatMap((t) => t.players)
|
||||
.some((p) => p.user.userId == session.user.userId)
|
||||
) {
|
||||
throw new HttpException(
|
||||
'You are already in this game',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
return await this.lobbyService.joinGame(game, session.user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user