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 { random, sample } from 'lodash';
6 import {Biome, Player} from '@prisma/client';
8 const MoveInput = Type.Object({
10 playerId: Type.String()
14 type MoveInputType = Static<typeof MoveInput>;
16 export type MoveOutputType = {
25 const moveStatements = [
27 "You take a few steps",
28 "You pause for a second before hurrying along",
29 "You stumble forward..."
32 export const move = server.post<MoveInputType, MoveOutputType>('/v1/accounts/:playerId/move', {
37 const player = await prisma.player.findUnique({
39 id: req.params.playerId
47 throw new ForbiddenError();
50 if(player.stamina <= 0) {
51 throw new ForbiddenError('Insufficient Stamina');
58 if(player.steps > player.zoneBiome.maxSteps) {
59 // ok, move them to a new zone!
60 const chosenBiome = sample(Object.values(Biome)) as Biome;
61 const biome = await prisma.zoneBiomes.create({
64 maxSteps: random(500, 1500)
68 await prisma.zoneBiomes.delete({
70 id: player.zoneBiomeId
74 player.zoneBiome = biome;
79 await prisma.player.update({
85 stamina: player.stamina
92 id: player.zoneBiome.id,
93 biome: player.zoneBiome.biome
95 displayText: sample(moveStatements) || moveStatements[0]