From 873273027e300625a03afd8c0eeca395bca12a3e Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Tue, 6 Jun 2023 11:34:09 -0400 Subject: [PATCH] carrier schema --- src/lib/store/ship/carrier.ts | 157 ++++++++++++++++++++-------------- 1 file changed, 93 insertions(+), 64 deletions(-) diff --git a/src/lib/store/ship/carrier.ts b/src/lib/store/ship/carrier.ts index 2a1cdf6..443cbd8 100644 --- a/src/lib/store/ship/carrier.ts +++ b/src/lib/store/ship/carrier.ts @@ -1,91 +1,120 @@ import Updux, { createPayloadAction } from "updux"; import u from "@yanick/updeep-remeda"; import { reqs, type Reqs } from "$lib/shipDux/reqs"; +import * as j from "json-schema-shorthand"; +import { reqsSchema } from "./reqs"; +import type { FromSchema } from "json-schema-to-ts"; -type Squadron = { - type: string; - reqs: Reqs; +const squadronTypes = [ + "standard", + "fast", + "heavy", + "interceptor", + "attack", + "long range", + "torpedo", +] as const; + +type SquadronType = (typeof squadronTypes)[number]; + +const squadronCost = { + standard: 3, + fast: 4, + heavy: 5, + interceptor: 3, + attack: 4, + "long range": 4, + torpedo: 6, +} as const; + +const squadronSchema = j.object({ + type: { + type: "string", + enum: squadronTypes, + }, + reqs: reqsSchema, +} as const); + +type SquadronSchema = FromSchema; + +const carrierSchema = j.object({ + nbrBays: j.number({ default: 0 }), + squadrons: j.array(squadronSchema, { default: [] }), + reqs: reqsSchema, +} as const); + +type CarrierSchema = FromSchema; + +const initialState: CarrierSchema = { + nbrBays: 0, + squadrons: [], + reqs, }; -const initialState = { - nbrBays: 0, - squadrons: [] as Squadron[], - reqs, -}; - -export const squadronTypes = [ - { type: "standard", cost: 3 }, - { type: "fast", cost: 4 }, - { type: "heavy", cost: 5 }, - { type: "interceptor", cost: 3 }, - { type: "attack", cost: 4 }, - { type: "long range", cost: 4 }, - { type: "torpedo", cost: 6 }, -]; - const setNbrCarrierBays = createPayloadAction("setNbrCarrierBays"); + const setSquadronType = createPayloadAction( - "setSquadronType", - (id: number, type: string) => ({ id, type }) + "setSquadronType", + (id: number, type: string) => ({ id, type }) ); export const carrierDux = new Updux({ - initialState, - actions: { setNbrCarrierBays, setSquadronType }, + initialState, + actions: { setNbrCarrierBays, setSquadronType }, }); -function calcBaysReqs(bays) { - return { - mass: 9 * bays, - cost: 18 * bays, - }; +function calcBaysReqs(bays: number): Reqs { + return { + mass: 9 * bays, + cost: 18 * bays, + }; } const adjustSquadrons = (bays: number) => (squadrons) => { - if (squadrons.length === bays) return squadrons; + if (squadrons.length === bays) return squadrons; - if (squadrons.length > bays) { - return squadrons.slice(0, bays); - } + if (squadrons.length > bays) { + return squadrons.slice(0, bays); + } - return [ - ...squadrons, - ...Array.from({ length: bays - squadrons.length }) - .fill({ - type: squadronTypes[0].type, - reqs: { - cost: 6 * squadronTypes[0].cost, - mass: 6, - }, - }) - .map((s, i) => ({ ...s, id: squadrons.length + i + 1 })), - ]; + return [ + ...squadrons, + ...Array.from({ length: bays - squadrons.length }) + .fill({ + type: squadronTypes[0], + reqs: { + cost: 6 * squadronCost[squadronTypes[0]], + mass: 6, + }, + }) + .map((s, i) => ({ ...s, id: squadrons.length + i + 1 })), + ]; }; carrierDux.addMutation(setNbrCarrierBays, (nbrBays) => - u({ - nbrBays, - reqs: calcBaysReqs(nbrBays), - squadrons: adjustSquadrons(nbrBays), - }) + u({ + nbrBays, + reqs: calcBaysReqs(nbrBays), + squadrons: adjustSquadrons(nbrBays), + }) ); carrierDux.addMutation(setSquadronType, ({ id, type }) => { - return u({ - squadrons: u.map( - u.if(u.matches({ id }), (state) => { - return u(state, { - type, - reqs: squadronReqs(type), - }); - }) - ), - }); + return u({ + squadrons: u.map( + u.if(u.matches({ id }), (state) => { + return u(state, { + type, + reqs: squadronReqs(type), + }); + }) + ), + }); }); -function squadronReqs(type: string) { - return { - mass: 6, - cost: 6 * squadronTypes.find((s) => s.type === type)?.cost, - }; +function squadronReqs(type: SquadronType): Reqs { + return { + mass: 6, + cost: 6 * squadronCost[type], + }; }