carrier schema

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

View File

@ -1,91 +1,120 @@
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;
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,
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<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 })
); );
export const carrierDux = new Updux({ export const carrierDux = new Updux({
initialState, initialState,
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,
}; };
} }
const adjustSquadrons = (bays: number) => (squadrons) => { const adjustSquadrons = (bays: number) => (squadrons) => {
if (squadrons.length === bays) return squadrons; if (squadrons.length === bays) return squadrons;
if (squadrons.length > bays) { if (squadrons.length > bays) {
return squadrons.slice(0, bays); return squadrons.slice(0, bays);
} }
return [ return [
...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,
}, },
}) })
.map((s, i) => ({ ...s, id: squadrons.length + i + 1 })), .map((s, i) => ({ ...s, id: squadrons.length + i + 1 })),
]; ];
}; };
carrierDux.addMutation(setNbrCarrierBays, (nbrBays) => carrierDux.addMutation(setNbrCarrierBays, (nbrBays) =>
u({ u({
nbrBays, nbrBays,
reqs: calcBaysReqs(nbrBays), reqs: calcBaysReqs(nbrBays),
squadrons: adjustSquadrons(nbrBays), squadrons: adjustSquadrons(nbrBays),
}) })
); );
carrierDux.addMutation(setSquadronType, ({ id, type }) => { carrierDux.addMutation(setSquadronType, ({ id, type }) => {
return u({ return u({
squadrons: u.map( squadrons: u.map(
u.if(u.matches({ id }), (state) => { u.if(u.matches({ id }), (state) => {
return u(state, { return u(state, {
type, type,
reqs: squadronReqs(type), reqs: squadronReqs(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],
}; };
} }