exploring/fighting is functional
[sketchy-heroes.git] / src / routes / fight / start.ts
1 import { server } from '../../lib/server';
2 import { Static, Type } from '@sinclair/typebox';
3 import { prisma } from '../../lib/db';
4 import { ForbiddenError } from '../../lib/http-errors';
5
6 const FightStartInput = Type.Object({
7   params: Type.Object({
8     playerId: Type.String(),
9     fightId: Type.String()
10   })
11 });
12
13 type FightStartInputType = Static<typeof FightStartInput>;
14
15 export type FightStartType = {
16   id: String
17 }
18
19 export const startFight = server.post<FightStartInputType, FightStartType>('/v1/accounts/:playerId/fight/:fightId', {
20   schema: FightStartInput,
21   authenicated: true
22 }, async req => {
23   
24   const { playerId } = req.params;
25
26
27   const res = await prisma.fight.update({
28     where: {
29       id: req.params.fightId
30     },
31     data: {
32       playerId: playerId
33     }
34   });
35
36   if(!res) {
37     throw new ForbiddenError('You can not start a fight with a monster that does not exist');
38   }
39
40   return {
41     id: res.id
42   }
43 });