exploring/fighting is functional
[sketchy-heroes.git] / src / routes / player / stat-increase.ts
diff --git a/src/routes/player/stat-increase.ts b/src/routes/player/stat-increase.ts
new file mode 100644 (file)
index 0000000..e43ddf5
--- /dev/null
@@ -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<typeof PlayerInput>;
+
+export const increaseStat = server.post<PlayerInputType, Player>('/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;
+});