carrier schema

This commit is contained in:
Yanick Champoux 2023-06-06 11:34:09 -04:00
parent b4293b2736
commit 873273027e

View File

@ -1,29 +1,58 @@
import Updux, { createPayloadAction } from "updux"; import Updux, { createPayloadAction } from "updux";
import u from "@yanick/updeep-remeda"; import u from "@yanick/updeep-remeda";
import { reqs, type Reqs } from "$lib/shipDux/reqs"; 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 = { const squadronTypes = [
type: string; "standard",
reqs: Reqs; "fast",
}; "heavy",
"interceptor",
"attack",
"long range",
"torpedo",
] as const;
const initialState = { 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<typeof squadronSchema>;
const carrierSchema = j.object({
nbrBays: j.number({ default: 0 }),
squadrons: j.array(squadronSchema, { default: [] }),
reqs: reqsSchema,
} as const);
type CarrierSchema = FromSchema<typeof carrierSchema>;
const initialState: CarrierSchema = {
nbrBays: 0, nbrBays: 0,
squadrons: [] as Squadron[], squadrons: [],
reqs, 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<number>("setNbrCarrierBays"); const setNbrCarrierBays = createPayloadAction<number>("setNbrCarrierBays");
const setSquadronType = createPayloadAction( const setSquadronType = createPayloadAction(
"setSquadronType", "setSquadronType",
(id: number, type: string) => ({ id, type }) (id: number, type: string) => ({ id, type })
@ -34,7 +63,7 @@ export const carrierDux = new Updux({
actions: { setNbrCarrierBays, setSquadronType }, actions: { setNbrCarrierBays, setSquadronType },
}); });
function calcBaysReqs(bays) { function calcBaysReqs(bays: number): Reqs {
return { return {
mass: 9 * bays, mass: 9 * bays,
cost: 18 * bays, cost: 18 * bays,
@ -52,9 +81,9 @@ const adjustSquadrons = (bays: number) => (squadrons) => {
...squadrons, ...squadrons,
...Array.from({ length: bays - squadrons.length }) ...Array.from({ length: bays - squadrons.length })
.fill({ .fill({
type: squadronTypes[0].type, type: squadronTypes[0],
reqs: { reqs: {
cost: 6 * squadronTypes[0].cost, cost: 6 * squadronCost[squadronTypes[0]],
mass: 6, mass: 6,
}, },
}) })
@ -83,9 +112,9 @@ carrierDux.addMutation(setSquadronType, ({ id, type }) => {
}); });
}); });
function squadronReqs(type: string) { function squadronReqs(type: SquadronType): Reqs {
return { return {
mass: 6, mass: 6,
cost: 6 * squadronTypes.find((s) => s.type === type)?.cost, cost: 6 * squadronCost[type],
}; };
} }