add carrier to dux

This commit is contained in:
Yanick Champoux 2023-04-21 15:35:46 -04:00
parent 7cc85f2572
commit 5602441364
5 changed files with 79 additions and 91 deletions

View File

@ -53,6 +53,14 @@ exports[`state has the expected shape 1`] = `
},
"space": 0,
},
"carrier": {
"nbrBays": 0,
"reqs": {
"cost": 0,
"mass": 0,
},
"squadrons": [],
},
"hull": {
"max": 0,
"min": 0,

View File

@ -17,11 +17,13 @@ test("kicking the tires", () => {
store.dispatch.setNbrCarrierBays(2);
expect(store.getState().carrier.nbrBays).toEqual(2);
expect(store.getState.isCarrier()).toBe(false);
store.dispatch.setCarrier(true);
store.dispatch.setSquadronType(2, "fast");
expect(store.getState().carrier.squadrons[1]).toHaveProperty("type", "fast");
expect(store.getState.isCarrier()).toBe(true);
expect(store.getState().carrier.squadrons[1]).toHaveProperty("type", "fast");
store.dispatch.setStreamlining("partial");

View File

@ -30,6 +30,7 @@ const structure = new Updux({
hull: hullDux,
screens: screensDux,
armor: armorDux,
carrier: carrierDux,
},
});

View File

@ -89,30 +89,3 @@ function squadronReqs(type: string) {
cost: 6 * squadronTypes.find((s) => s.type === type)?.cost,
};
}
/*
export const { actions, reducer } = createSlice({
name: "carrier",
initialStateState,
reducers: {
setCarrierBays: (state, action: PayloadAction<number>) => {
state.bays = action.payload;
state.reqs = calcBaysReqs(action.payload);
state.squadrons = adjustSquadrons(action.payload)(state.squadrons);
},
setSquadronType: (
state,
action: PayloadAction<{ type: string; id: number }>
) => {
state.squadrons[action.payload.id - 1] = {
type: action.payload.type,
reqs: squadronReqs(action.payload.type),
};
},
},
});
*/

View File

@ -1,6 +1,5 @@
import Updux, { createAction, withPayload } from "updux";
import u from "@yanick/updeep-remeda";
import * as R from "remeda";
import { carrierDux } from "./carrier";
const initialState = {
@ -17,6 +16,7 @@ const initialState = {
const setShipClass = createAction("setShipClass", withPayload<string>());
const updateIdentification = createAction("updateIdentification");
const setShipReqs = createAction("setShipReqs", withPayload());
const setCarrier = createAction("setCarrier", withPayload<boolean>());
export const identificationDux = new Updux({
initialState,
@ -24,6 +24,7 @@ export const identificationDux = new Updux({
setShipClass,
updateIdentification,
setShipReqs,
setCarrier,
},
selectors: {
getShipMass: (state) => state.reqs.mass,
@ -33,12 +34,15 @@ export const identificationDux = new Updux({
identificationDux.addMutation(setShipClass, (shipClass) => u({ shipClass }));
identificationDux.addMutation(updateIdentification, (update) => u(update));
identificationDux.addMutation(setCarrier, (isCarrier) => u({ isCarrier }));
identificationDux.addEffect(setCarrier, (api) => (next) => (action) => {
next(action);
if (!action.payload) {
api.dispatch(carrierDux.actions.setNbrCarrierBays(0));
}
});
identificationDux.addMutation(setShipReqs, (reqs) => u({ reqs }));
identificationDux.addMutation(carrierDux.actions.setNbrCarrierBays, (nbrBays) =>
u({
isCarrier: nbrBays > 0,
})
);
export default identificationDux;