level-up/backend/trpc/crew.ts
2025-04-12 21:51:01 +02:00

75 lines
No EOL
2.5 KiB
TypeScript

import { t } from "./base"
import { z } from "zod"
import { game } from "../game"
import { suggestedInteractionSchema } from "../../shared/mutations"
import { TRPCError } from "@trpc/server"
const crewProcedure = t.procedure.use(({ ctx, next }) => {
if (!ctx.isCrew) throw new TRPCError({ code: "UNAUTHORIZED" })
return next()
})
export const crewRouter = t.router({
switchScene: crewProcedure
.input(z.object({
sceneId: z.string()
}))
.mutation(async ({ input }) => {
game.switchScene(input.sceneId)
}),
interactionScene: t.router({
setObjectVisibility: crewProcedure
.input(z.object({
objectId: z.string(),
isVisible: z.boolean()
}))
.mutation(({ input }) => {
game.withSceneState("interaction", s => s.setObjectVisibility(input.objectId, input.isVisible))
}),
startInteractionExecution: crewProcedure
.input(z.object({
interaction: suggestedInteractionSchema
}))
.mutation(({ input }) => {
game.withSceneState("interaction", s => s.startInteractionExecution(input.interaction))
}),
finishInteractionExecution: crewProcedure
.input(z.object({
interactionId: z.string(),
onlyIfOngoing: z.boolean()
}))
.mutation(({ input }) => {
game.withSceneState("interaction", s => s.finishInteractionExecution(input.interactionId, input.onlyIfOngoing))
}),
cancelInteractionExecution: crewProcedure
.input(z.object({
interactionId: z.string(),
onlyIfOngoing: z.boolean()
}))
.mutation(({ input }) => {
game.withSceneState("interaction", s => s.cancelInteractionExecution(input.interactionId, input.onlyIfOngoing))
}),
removeInteractionFromQueue: crewProcedure
.input(z.object({
interactionId: z.string()
}))
.mutation(({ input }) => {
game.withSceneState("interaction", s => s.removeInteractionFromQueue(input.interactionId))
}),
}),
choiceScene: t.router({
concludeVoting: crewProcedure.mutation(() => {
game.withSceneState("choice", s => s.concludeVoting())
}),
restartVoting: crewProcedure.mutation(() => {
game.withSceneState("choice", s => s.restartVoting())
})
})
})