aotds-docks/src/lib/store/ship/carrier.ts

92 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-03-23 14:44:54 +00:00
import Updux, { createPayloadAction } from "updux";
import u from "@yanick/updeep-remeda";
import { reqs, type Reqs } from "$lib/shipDux/reqs";
type Squadron = {
2023-04-21 19:35:46 +00:00
type: string;
reqs: Reqs;
2023-03-23 14:44:54 +00:00
};
2023-03-24 15:01:04 +00:00
const initialState = {
2023-04-21 19:35:46 +00:00
nbrBays: 0,
squadrons: [] as Squadron[],
reqs,
2023-03-23 14:44:54 +00:00
};
export const squadronTypes = [
2023-04-21 19:35:46 +00:00
{ 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 },
2023-03-23 14:44:54 +00:00
];
const setNbrCarrierBays = createPayloadAction<number>("setNbrCarrierBays");
const setSquadronType = createPayloadAction(
2023-04-21 19:35:46 +00:00
"setSquadronType",
(id: number, type: string) => ({ id, type })
2023-03-23 14:44:54 +00:00
);
export const carrierDux = new Updux({
2023-04-21 19:35:46 +00:00
initialState,
actions: { setNbrCarrierBays, setSquadronType },
2023-03-23 14:44:54 +00:00
});
function calcBaysReqs(bays) {
2023-04-21 19:35:46 +00:00
return {
mass: 9 * bays,
cost: 18 * bays,
};
2023-03-23 14:44:54 +00:00
}
const adjustSquadrons = (bays: number) => (squadrons) => {
2023-04-21 19:35:46 +00:00
if (squadrons.length === bays) return squadrons;
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 })),
];
2023-03-23 14:44:54 +00:00
};
carrierDux.addMutation(setNbrCarrierBays, (nbrBays) =>
2023-04-21 19:35:46 +00:00
u({
nbrBays,
reqs: calcBaysReqs(nbrBays),
squadrons: adjustSquadrons(nbrBays),
})
2023-03-23 14:44:54 +00:00
);
carrierDux.addMutation(setSquadronType, ({ id, type }) => {
2023-04-21 19:35:46 +00:00
return u({
squadrons: u.map(
u.if(u.matches({ id }), (state) => {
return u(state, {
type,
reqs: squadronReqs(type),
});
})
),
});
2023-03-23 14:44:54 +00:00
});
function squadronReqs(type: string) {
2023-04-21 19:35:46 +00:00
return {
mass: 6,
cost: 6 * squadronTypes.find((s) => s.type === type)?.cost,
};
2023-03-23 14:44:54 +00:00
}