43 lines
2.0 KiB
TypeScript
43 lines
2.0 KiB
TypeScript
import * as z from "zod";
|
|
|
|
const key = z.string().min(1).max(64);
|
|
const member = z.object({
|
|
characterKey: key,
|
|
weaponKey: key,
|
|
artifactKey: key,
|
|
constellation: z.enum(["0", "1", "2", "3", "4", "5", "6", "0-6"]).default("0"),
|
|
refinement: z.enum(["1", "2", "3", "4", "5", "1-5"]).default("1"),
|
|
});
|
|
const team = z.object({ members: z.array(member).length(4) }).refine(
|
|
({ members }) => new Set(members.map((item) => item.characterKey)).size === 4,
|
|
"A team needs four different characters",
|
|
);
|
|
const weaponComparison = z.object({
|
|
characterKey: key,
|
|
weaponKeys: z.array(key).min(2).max(100),
|
|
refinements: z.record(key, z.enum(["1", "2", "3", "4", "5"])).optional(),
|
|
}).refine(({ weaponKeys }) => new Set(weaponKeys).size === weaponKeys.length, "Choose different weapons")
|
|
.refine(({ weaponKeys, refinements }) =>
|
|
Object.keys(refinements ?? {}).every((weaponKey) => weaponKeys.includes(weaponKey)),
|
|
"Refinements must belong to selected weapons");
|
|
const constellationComparison = z.object({
|
|
characterKey: key,
|
|
levels: z.array(z.number().int().min(0).max(6)).min(1).max(7),
|
|
}).refine(({ levels }) => new Set(levels).size === levels.length, "Choose different constellation levels");
|
|
|
|
export const commissionRequestSchema = z.object({
|
|
teams: z.array(team).max(20),
|
|
weapons: z.array(weaponComparison).max(20),
|
|
constellations: z.array(constellationComparison).max(20),
|
|
}).refine((value) => value.teams.length + value.weapons.length + value.constellations.length > 0,
|
|
"Choose at least one commission item");
|
|
|
|
export type CommissionRequest = z.infer<typeof commissionRequestSchema>;
|
|
|
|
export function commissionPrice(request: CommissionRequest): number {
|
|
const hasItems = request.teams.length + request.weapons.length + request.constellations.length > 0;
|
|
return (hasItems ? Math.max(1, request.teams.length) * 100 : 0) +
|
|
request.weapons.reduce((sum, item) => sum + item.weaponKeys.filter(Boolean).length * 20, 0) +
|
|
request.constellations.reduce((sum, item) => sum + item.levels.length * 20, 0);
|
|
}
|