exploring/fighting is functional
[sketchy-heroes.git] / src / routes / player / stat-increase.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 import {Player} from '@prisma/client';
6 import {maxHp, maxStamina} from '../../formulas';
7
8 const PlayerInput = Type.Object({
9   params: Type.Object({
10     playerId: Type.String()
11   }),
12   body: Type.Object({
13     stat: Type.String()
14   })
15 });
16
17 type PlayerInputType = Static<typeof PlayerInput>;
18
19 export const increaseStat = server.post<PlayerInputType, Player>('/v1/accounts/:playerId/stat', {
20   schema: PlayerInput,
21   authenicated: true
22 }, async req => {
23   const player = await prisma.player.findUnique({
24     where: {
25       id: req.params.playerId
26     }
27   });
28
29   if(!player) {
30     throw new ForbiddenError('Not Allowed');
31   }
32
33   if(player.statPoints <= 0) {
34     throw new ForbiddenError('Insufficient Stat Points');
35   }
36
37   player.statPoints--;
38
39   const stats = ['pow', 'zest', 'aha', 'luck', 'woosh', 'wow'];
40   if(stats.indexOf(req.body.stat) < 0) {
41     throw new ForbiddenError('That is not a stat');
42   }
43
44   switch(req.body.stat) {
45     case 'pow':
46       player.pow++;
47       break;
48     case 'zest':
49       player.zest++;
50       player.hp = maxHp(player.level, player.zest);
51       break;
52     case 'woosh':
53       player.woosh++;
54       player.stamina = maxStamina(player.level, player.woosh);
55       break;
56     case 'aha':
57       player.aha++;
58       break;
59     case 'luck':
60       player.luck++;
61       break;
62     case 'wow':
63       player.wow++;
64       break;
65   }
66
67   await prisma.player.update({
68     where: {
69       id: player.id
70     },
71     data: {
72       pow: player.pow,
73       zest: player.zest,
74       woosh: player.woosh,
75       aha: player.aha,
76       luck: player.luck,
77       wow: player.wow,
78       hp: player.hp,
79       stamina: player.stamina,
80       statPoints: player.statPoints
81     }
82   });
83
84   return player;
85 });