initial
[sketchy-heroes.git] / src / routes / account / create.ts
1 import { server } from '../../lib/server';
2 import { Static, Type } from '@sinclair/typebox';
3 import { prisma } from '../../lib/db';
4 import { BadInputError } from '../../lib/http-errors';
5 import { random } from 'lodash';
6 import { maxHp, maxStamina } from '../../formulas';
7 import {Biome, Player} from '@prisma/client';
8
9 const AccountInput = Type.Object({
10   body: Type.Object({
11     username: Type.String(),
12     password: Type.String(),
13     confirmation: Type.String()
14   })
15 });
16
17 type AccountInputType = Static<typeof AccountInput>
18
19 export type AccountCreateType = {
20   player: Player,
21   biome: {
22     id: string,
23     biome: Biome
24   }
25 }
26
27 export const createAccount = server.post<AccountInputType, AccountCreateType>('/v1/accounts', {
28   schema: AccountInput
29 }, async (req) => {
30   const {username, password, confirmation} = req.body;
31
32   if(username.length < 3) {
33     throw new BadInputError('Username must be at least 3 characters');
34   }
35   
36   if(password !== confirmation) {
37     throw new BadInputError('Password and confirmation did not match');
38   }
39
40   // we should get the default zone that the player will be in!
41
42   const data = {
43     username: username,
44     password: password,
45     pow: random(3,6),
46     zest: random(3,6),
47     woosh: random(3, 6),
48     luck: random(3, 6),
49     aha: random(3, 6),
50     wow: random(3, 6),
51     hp: 0,
52     stamina: 0,
53     zoneBiomeId: ''
54   };
55   
56
57
58   data.hp = maxHp(1, data.zest);
59   data.stamina = maxStamina(1, data.woosh);
60
61   // create a new biome for this player!
62   const biome = await prisma.zoneBiomes.create({
63     data: {
64       biome: 'PLAINS',
65       maxSteps: 500
66     }
67   });
68
69   data.zoneBiomeId = biome.id;
70
71   const player = await prisma.player.create({ data });
72
73   return {
74     player,
75     biome: {
76       id: biome.id,
77       biome: biome.biome
78     }
79   }
80 });