feat: working on messages

This commit is contained in:
2024-12-30 18:16:25 +01:00
parent cfafcfaddf
commit bafe228911
18 changed files with 201 additions and 33 deletions
+44 -4
View File
@@ -1,6 +1,7 @@
import {
Body,
Controller,
Get,
HttpException,
HttpStatus,
Param,
@@ -11,7 +12,7 @@ import {
} from '@nestjs/common';
import { LobbyService } from '../services/lobby.service';
import { AuthGuard } from '../../users/guards/auth.guard';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Game } from '../entities/game.entity';
import { GamePipe, GameStartedPipe } from '../pipe/game.pipe';
@@ -20,6 +21,8 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
import { GameService } from '../services/games.service';
import { ConfigService } from '@nestjs/config';
import { SessionData } from 'express-session';
import { WordResponseDTO } from 'events/dto/wordresponse.dto';
import { WordRecieveDTO } from 'src/word/dto/wordrecieve.dto';
@ApiTags('game')
@Controller('games/:id')
@@ -60,7 +63,7 @@ export class GamesController {
HttpStatus.BAD_REQUEST,
);
}
if (game.owner.id != session.user.id) {
if (game.ownerId != session.user?.id) {
throw new HttpException(
'You can only start games you own',
HttpStatus.FORBIDDEN,
@@ -88,12 +91,12 @@ export class GamesController {
@ApiOperation({ summary: 'Kick a player' })
@UseGuards(AuthGuard)
kick(
@Param('id') id: string,
@Session() session: SessionData,
@Param('id') id: string,
@Param('id', GamePipe) game: Game,
@Param('playerId') playerId: string,
) {
if (session.user.id != game.owner.id) {
if (session.user.id != game.ownerId) {
throw new HttpException(
'You do do not own this game',
HttpStatus.FORBIDDEN,
@@ -102,6 +105,43 @@ export class GamesController {
return this.lobbyService.kickPlayer(game, session.user, playerId);
}
@Get('currentWord')
@ApiOperation({ summary: 'Read Current Word' })
@UseGuards(AuthGuard)
getCurrentWord(
@Session() session: SessionData,
@Param('id') id: string,
@Param('id', GamePipe) game: Game,
) {
if (session.user.id != game.currentPlayer.userId) {
throw new HttpException(
'It is not your turn to play',
HttpStatus.FORBIDDEN,
);
}
return new WordResponseDTO(game.currentWord);
}
@Post('/message')
@ApiOperation({ summary: 'try to guess a word' })
@ApiBody({ type: WordRecieveDTO })
@UseGuards(AuthGuard)
async postMessage(
@Session() session: SessionData,
@Param('id') id: string,
@Param('id', GamePipe) game: Game,
@Body('value') value: string,
) {
const player = game.players.find((p) => p.userId == session.user.id);
if (!game.started) {
throw new HttpException('Game not started', HttpStatus.BAD_REQUEST);
}
if (!player) {
throw new HttpException('You are not in this game', HttpStatus.FORBIDDEN);
}
await this.gameService.postMessage(game, player, value);
}
@Sse('/subscribe')
subscribe(@Param('id') id: string): Observable<{ data: string }> {
return fromEvent(this.eventEmitter, 'sse.game.' + id).pipe(