49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
|
|
import {
|
||
|
|
Body,
|
||
|
|
Controller,
|
||
|
|
Get,
|
||
|
|
HttpCode,
|
||
|
|
HttpStatus,
|
||
|
|
NotFoundException,
|
||
|
|
Post,
|
||
|
|
UseGuards,
|
||
|
|
} from '@nestjs/common';
|
||
|
|
import {
|
||
|
|
MissionStateResponse,
|
||
|
|
MissionStateResponseSchema,
|
||
|
|
StartMissionRequestSchema,
|
||
|
|
} from '@fog-explorer/api-interfaces';
|
||
|
|
import { TwitchClaims, TwitchJwtGuard, TwitchJwtPayload } from '../auth/twitch-jwt.guard';
|
||
|
|
import { MissionStoreService } from './mission-store.service';
|
||
|
|
import { MissionsService } from './missions.service';
|
||
|
|
|
||
|
|
@Controller('missions')
|
||
|
|
@UseGuards(TwitchJwtGuard)
|
||
|
|
export class MissionsController {
|
||
|
|
constructor(
|
||
|
|
private readonly store: MissionStoreService,
|
||
|
|
private readonly missions: MissionsService
|
||
|
|
) {}
|
||
|
|
|
||
|
|
@Get('state')
|
||
|
|
async getState(
|
||
|
|
@TwitchClaims() claims: TwitchJwtPayload
|
||
|
|
): Promise<MissionStateResponse> {
|
||
|
|
const state = await this.store.getStateForChannel(claims.channel_id);
|
||
|
|
return MissionStateResponseSchema.parse(state);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Post('start')
|
||
|
|
@HttpCode(HttpStatus.CREATED)
|
||
|
|
async startMission(
|
||
|
|
@TwitchClaims() claims: TwitchJwtPayload,
|
||
|
|
@Body() body: unknown
|
||
|
|
): Promise<MissionStateResponse> {
|
||
|
|
if (!claims.opaque_user_id.startsWith('U')) {
|
||
|
|
throw new NotFoundException('Anonymous viewers cannot start missions');
|
||
|
|
}
|
||
|
|
const { difficulty } = StartMissionRequestSchema.parse(body);
|
||
|
|
return this.missions.startMission(claims, difficulty);
|
||
|
|
}
|
||
|
|
}
|