X-Git-Url: https://git.xangelo.ca/?p=sketchy-heroes.git;a=blobdiff_plain;f=src%2Froutes%2Fplayer%2Fstat-increase.ts;fp=src%2Froutes%2Fplayer%2Fstat-increase.ts;h=e43ddf5df4db3f0cec043443c25d09a5b1646b3e;hp=0000000000000000000000000000000000000000;hb=7aa7248bc4f3f59a002beb98fa889a9da3c25866;hpb=9cec2c639563092ed050716db1e7e4657f937bf5 diff --git a/src/routes/player/stat-increase.ts b/src/routes/player/stat-increase.ts new file mode 100644 index 0000000..e43ddf5 --- /dev/null +++ b/src/routes/player/stat-increase.ts @@ -0,0 +1,85 @@ +import { server } from '../../lib/server'; +import { Static, Type } from '@sinclair/typebox'; +import { prisma } from '../../lib/db'; +import { ForbiddenError } from '../../lib/http-errors'; +import {Player} from '@prisma/client'; +import {maxHp, maxStamina} from '../../formulas'; + +const PlayerInput = Type.Object({ + params: Type.Object({ + playerId: Type.String() + }), + body: Type.Object({ + stat: Type.String() + }) +}); + +type PlayerInputType = Static; + +export const increaseStat = server.post('/v1/accounts/:playerId/stat', { + schema: PlayerInput, + authenicated: true +}, async req => { + const player = await prisma.player.findUnique({ + where: { + id: req.params.playerId + } + }); + + if(!player) { + throw new ForbiddenError('Not Allowed'); + } + + if(player.statPoints <= 0) { + throw new ForbiddenError('Insufficient Stat Points'); + } + + player.statPoints--; + + const stats = ['pow', 'zest', 'aha', 'luck', 'woosh', 'wow']; + if(stats.indexOf(req.body.stat) < 0) { + throw new ForbiddenError('That is not a stat'); + } + + switch(req.body.stat) { + case 'pow': + player.pow++; + break; + case 'zest': + player.zest++; + player.hp = maxHp(player.level, player.zest); + break; + case 'woosh': + player.woosh++; + player.stamina = maxStamina(player.level, player.woosh); + break; + case 'aha': + player.aha++; + break; + case 'luck': + player.luck++; + break; + case 'wow': + player.wow++; + break; + } + + await prisma.player.update({ + where: { + id: player.id + }, + data: { + pow: player.pow, + zest: player.zest, + woosh: player.woosh, + aha: player.aha, + luck: player.luck, + wow: player.wow, + hp: player.hp, + stamina: player.stamina, + statPoints: player.statPoints + } + }); + + return player; +});