Template
Initial commit
This commit is contained in:
@@ -12,14 +12,11 @@ import {
|
||||
Body,
|
||||
Sse,
|
||||
} from '@nestjs/common';
|
||||
import { LobbyService } from '../services/games.lobby.service';
|
||||
import { LobbyService } from '../services/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';
|
||||
|
||||
@@ -36,7 +33,7 @@ export class LobbyController {
|
||||
@ApiResponse({
|
||||
status: 201,
|
||||
description: 'Game successfully created.',
|
||||
type: ReadGameDto,
|
||||
// type: ReadGameDto,
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 400,
|
||||
@@ -45,7 +42,7 @@ export class LobbyController {
|
||||
@ApiResponse({ status: 403, description: 'Forbidden.' })
|
||||
@UseGuards(AuthGuard)
|
||||
async create(@Session() session: Record<string, any>) {
|
||||
const games = await this.lobbyService.findByOwner(session.user._id);
|
||||
const games = await this.lobbyService.findByOwner(session.user);
|
||||
if (games) {
|
||||
throw new HttpException(
|
||||
'You can only create one game at a time',
|
||||
@@ -76,25 +73,8 @@ export class LobbyController {
|
||||
@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);
|
||||
return game;
|
||||
// return new ReadGameDto(game);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@@ -103,57 +83,15 @@ export class LobbyController {
|
||||
async remove(
|
||||
@Param('id') id: string,
|
||||
@Session() session: Record<string, any>,
|
||||
@Param('id', GamePipe) game: Document<Game> & Game,
|
||||
@Param('id', GamePipe) game: Game,
|
||||
) {
|
||||
if (game.owner._id.toString() != session.user._id) {
|
||||
if (game.owner.userId == session.user.userId) {
|
||||
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;
|
||||
await this.lobbyService.remove(game);
|
||||
}
|
||||
|
||||
@Post(':id/join')
|
||||
@@ -162,17 +100,13 @@ export class LobbyController {
|
||||
async join(
|
||||
@Param('id') id: string,
|
||||
@Session() session: Record<string, any>,
|
||||
@Param('id', GamePipe) game: Document<Game> & Game,
|
||||
@Param('id', GamePipe) 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)
|
||||
) {
|
||||
if (game.players.some((p) => p.user.userId == session.user.userId)) {
|
||||
throw new HttpException(
|
||||
'You are already in this game',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
@@ -180,4 +114,18 @@ export class LobbyController {
|
||||
}
|
||||
return await this.lobbyService.joinGame(game, session.user);
|
||||
}
|
||||
|
||||
@Post(':id/leave')
|
||||
@ApiOperation({ summary: "Leave a game that hasn't already started" })
|
||||
@UseGuards(AuthGuard)
|
||||
async leave(
|
||||
@Param('id') id: string,
|
||||
@Session() session: Record<string, any>,
|
||||
@Param('id', GamePipe) game: Game,
|
||||
) {
|
||||
if (game.started) {
|
||||
throw new HttpException('Game already started', HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return await this.lobbyService.leaveGame(game, session.user);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user