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 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<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 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],
};
}