add carrier to dux
This commit is contained in:
parent
7cc85f2572
commit
5602441364
@ -53,6 +53,14 @@ exports[`state has the expected shape 1`] = `
|
|||||||
},
|
},
|
||||||
"space": 0,
|
"space": 0,
|
||||||
},
|
},
|
||||||
|
"carrier": {
|
||||||
|
"nbrBays": 0,
|
||||||
|
"reqs": {
|
||||||
|
"cost": 0,
|
||||||
|
"mass": 0,
|
||||||
|
},
|
||||||
|
"squadrons": [],
|
||||||
|
},
|
||||||
"hull": {
|
"hull": {
|
||||||
"max": 0,
|
"max": 0,
|
||||||
"min": 0,
|
"min": 0,
|
||||||
|
@ -17,11 +17,13 @@ test("kicking the tires", () => {
|
|||||||
store.dispatch.setNbrCarrierBays(2);
|
store.dispatch.setNbrCarrierBays(2);
|
||||||
expect(store.getState().carrier.nbrBays).toEqual(2);
|
expect(store.getState().carrier.nbrBays).toEqual(2);
|
||||||
|
|
||||||
|
expect(store.getState.isCarrier()).toBe(false);
|
||||||
|
|
||||||
|
store.dispatch.setCarrier(true);
|
||||||
store.dispatch.setSquadronType(2, "fast");
|
store.dispatch.setSquadronType(2, "fast");
|
||||||
|
|
||||||
expect(store.getState().carrier.squadrons[1]).toHaveProperty("type", "fast");
|
|
||||||
|
|
||||||
expect(store.getState.isCarrier()).toBe(true);
|
expect(store.getState.isCarrier()).toBe(true);
|
||||||
|
expect(store.getState().carrier.squadrons[1]).toHaveProperty("type", "fast");
|
||||||
|
|
||||||
store.dispatch.setStreamlining("partial");
|
store.dispatch.setStreamlining("partial");
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@ const structure = new Updux({
|
|||||||
hull: hullDux,
|
hull: hullDux,
|
||||||
screens: screensDux,
|
screens: screensDux,
|
||||||
armor: armorDux,
|
armor: armorDux,
|
||||||
|
carrier: carrierDux,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -3,116 +3,89 @@ import u from "@yanick/updeep-remeda";
|
|||||||
import { reqs, type Reqs } from "$lib/shipDux/reqs";
|
import { reqs, type Reqs } from "$lib/shipDux/reqs";
|
||||||
|
|
||||||
type Squadron = {
|
type Squadron = {
|
||||||
type: string;
|
type: string;
|
||||||
reqs: Reqs;
|
reqs: Reqs;
|
||||||
};
|
};
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
nbrBays: 0,
|
nbrBays: 0,
|
||||||
squadrons: [] as Squadron[],
|
squadrons: [] as Squadron[],
|
||||||
reqs,
|
reqs,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const squadronTypes = [
|
export const squadronTypes = [
|
||||||
{ type: "standard", cost: 3 },
|
{ type: "standard", cost: 3 },
|
||||||
{ type: "fast", cost: 4 },
|
{ type: "fast", cost: 4 },
|
||||||
{ type: "heavy", cost: 5 },
|
{ type: "heavy", cost: 5 },
|
||||||
{ type: "interceptor", cost: 3 },
|
{ type: "interceptor", cost: 3 },
|
||||||
{ type: "attack", cost: 4 },
|
{ type: "attack", cost: 4 },
|
||||||
{ type: "long range", cost: 4 },
|
{ type: "long range", cost: 4 },
|
||||||
{ type: "torpedo", cost: 6 },
|
{ 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) {
|
||||||
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].type,
|
||||||
reqs: {
|
reqs: {
|
||||||
cost: 6 * squadronTypes[0].cost,
|
cost: 6 * squadronTypes[0].cost,
|
||||||
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: string) {
|
||||||
return {
|
return {
|
||||||
mass: 6,
|
mass: 6,
|
||||||
cost: 6 * squadronTypes.find((s) => s.type === type)?.cost,
|
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),
|
|
||||||
};
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import Updux, { createAction, withPayload } from "updux";
|
import Updux, { createAction, withPayload } from "updux";
|
||||||
import u from "@yanick/updeep-remeda";
|
import u from "@yanick/updeep-remeda";
|
||||||
import * as R from "remeda";
|
|
||||||
import { carrierDux } from "./carrier";
|
import { carrierDux } from "./carrier";
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
@ -17,6 +16,7 @@ const initialState = {
|
|||||||
const setShipClass = createAction("setShipClass", withPayload<string>());
|
const setShipClass = createAction("setShipClass", withPayload<string>());
|
||||||
const updateIdentification = createAction("updateIdentification");
|
const updateIdentification = createAction("updateIdentification");
|
||||||
const setShipReqs = createAction("setShipReqs", withPayload());
|
const setShipReqs = createAction("setShipReqs", withPayload());
|
||||||
|
const setCarrier = createAction("setCarrier", withPayload<boolean>());
|
||||||
|
|
||||||
export const identificationDux = new Updux({
|
export const identificationDux = new Updux({
|
||||||
initialState,
|
initialState,
|
||||||
@ -24,6 +24,7 @@ export const identificationDux = new Updux({
|
|||||||
setShipClass,
|
setShipClass,
|
||||||
updateIdentification,
|
updateIdentification,
|
||||||
setShipReqs,
|
setShipReqs,
|
||||||
|
setCarrier,
|
||||||
},
|
},
|
||||||
selectors: {
|
selectors: {
|
||||||
getShipMass: (state) => state.reqs.mass,
|
getShipMass: (state) => state.reqs.mass,
|
||||||
@ -33,12 +34,15 @@ export const identificationDux = new Updux({
|
|||||||
|
|
||||||
identificationDux.addMutation(setShipClass, (shipClass) => u({ shipClass }));
|
identificationDux.addMutation(setShipClass, (shipClass) => u({ shipClass }));
|
||||||
identificationDux.addMutation(updateIdentification, (update) => u(update));
|
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(setShipReqs, (reqs) => u({ reqs }));
|
||||||
|
|
||||||
identificationDux.addMutation(carrierDux.actions.setNbrCarrierBays, (nbrBays) =>
|
|
||||||
u({
|
|
||||||
isCarrier: nbrBays > 0,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
export default identificationDux;
|
export default identificationDux;
|
||||||
|
Loading…
Reference in New Issue
Block a user