2023-03-03 23:02:52 +00:00
|
|
|
import { combineReducers, configureStore, createSlice } from "@reduxjs/toolkit";
|
|
|
|
import R from "remeda";
|
|
|
|
|
|
|
|
import * as propulsion from "./propulsion";
|
|
|
|
import * as structure from "./structure";
|
|
|
|
import * as weaponry from "./weaponry/index.js";
|
|
|
|
import * as carrier from "./carrier";
|
2023-03-20 15:59:50 +00:00
|
|
|
import * as identification from "./identification.js";
|
|
|
|
import * as shipReqs from "./shipReqs";
|
|
|
|
import * as engine from "./engine";
|
2023-03-03 23:02:52 +00:00
|
|
|
|
|
|
|
const shipSlice = createSlice({
|
|
|
|
name: "ship",
|
|
|
|
initialState: {},
|
|
|
|
reducers: {},
|
|
|
|
extraReducers(builder) {
|
|
|
|
builder.addMatcher(
|
|
|
|
() => true,
|
|
|
|
combineReducers({
|
2023-03-20 15:59:50 +00:00
|
|
|
identification: identification.reducer,
|
2023-03-03 23:02:52 +00:00
|
|
|
propulsion: propulsion.reducer,
|
|
|
|
structure: structure.reducer,
|
|
|
|
weaponry: weaponry.reducer,
|
|
|
|
carrier: carrier.reducer,
|
2023-03-20 15:59:50 +00:00
|
|
|
engine: engine.reducer,
|
2023-03-03 23:02:52 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export function createStore() {
|
|
|
|
return configureStore({
|
|
|
|
reducer: shipSlice.reducer,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-20 15:59:50 +00:00
|
|
|
export const actions = {
|
|
|
|
...shipReqs.actions,
|
|
|
|
...shipSlice.actions,
|
|
|
|
...identification.actions,
|
|
|
|
...engine.actions,
|
|
|
|
...propulsion.actions,
|
|
|
|
};
|
2023-03-03 23:02:52 +00:00
|
|
|
|
|
|
|
/**
|
2023-03-03 18:23:13 +00:00
|
|
|
import * as propulsion from "./propulsion/index.js";
|
|
|
|
import { calculateDriveReqs } from "./propulsion/drive.js";
|
|
|
|
import { ftlReqsReaction } from "./propulsion/ftl.js";
|
|
|
|
import * as structure from "./structure/index.js";
|
|
|
|
import * as carrier from "./carrier.js";
|
|
|
|
import * as weaponry from "./weaponry/index.js";
|
2023-03-03 23:02:52 +00:00
|
|
|
import * as shipReqs from "./shipReqs.js";
|
2023-03-03 18:23:13 +00:00
|
|
|
import { screensReqsReaction } from "./structure/screens.js";
|
2023-03-03 23:02:52 +00:00
|
|
|
import { createSlice } from "@reduxjs/toolkit";
|
|
|
|
|
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
propulsion: propulsion.initialState,
|
|
|
|
identification: identification.initialState,
|
|
|
|
structure: structure.initialState,
|
|
|
|
carrier: carrier.initialState,
|
|
|
|
weaponry: weaponry.initialState,
|
|
|
|
shipReqs: shipReqs.initialState,
|
|
|
|
}
|
|
|
|
|
|
|
|
const shipSlice = createSlice({
|
|
|
|
name: "ship",
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-03-03 18:23:13 +00:00
|
|
|
|
|
|
|
const dux = new Updux({
|
|
|
|
subduxes: {
|
|
|
|
identification,
|
|
|
|
propulsion,
|
|
|
|
structure,
|
|
|
|
carrier,
|
|
|
|
weaponry,
|
|
|
|
},
|
|
|
|
initial: {
|
|
|
|
reqs: { cost: 0, mass: 10, usedMass: 0 },
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
setShipReqs: null,
|
|
|
|
setUITransform: null,
|
|
|
|
resetLayout: null,
|
|
|
|
resetShip: null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
function resetUITransform(thing) {
|
|
|
|
if (typeof thing !== "object") return thing;
|
|
|
|
|
|
|
|
return u.map(
|
|
|
|
(v, k) => (k === "uiTransform" ? "" : resetUITransform(v)),
|
|
|
|
thing
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
dux.setMutation("resetShip", () => () => dux.initial);
|
|
|
|
|
|
|
|
dux.setMutation("resetLayout", () => resetUITransform);
|
|
|
|
|
|
|
|
dux.setMutation("setUITransform", ({ system, systemId, translate }) => {
|
|
|
|
const transform = translate
|
|
|
|
? `translate(${translate[0]}px,${translate[1]}px)`
|
|
|
|
: "";
|
|
|
|
|
|
|
|
switch (system) {
|
|
|
|
case "firecons":
|
|
|
|
return u.updateIn("weaponry.firecons.uiTransform", transform);
|
|
|
|
|
|
|
|
case "weapon":
|
|
|
|
return u.updateIn(
|
|
|
|
"weaponry.weapons",
|
|
|
|
u.map(u.if(({ id }) => id === systemId, u({ uiTransform: transform })))
|
|
|
|
);
|
|
|
|
|
|
|
|
case "screens":
|
|
|
|
return u.updateIn("structure.screens.uiTransform", transform);
|
|
|
|
|
|
|
|
case "hull":
|
|
|
|
return u.updateIn("structure.hull.uiTransform", transform);
|
|
|
|
|
|
|
|
case "internalSystems":
|
|
|
|
const path = "structure.uiTransform";
|
|
|
|
return u.updateIn(path, transform);
|
|
|
|
|
|
|
|
case "ftl":
|
|
|
|
return u.updateIn("propulsion.ftl.uiTransform", transform);
|
|
|
|
|
|
|
|
case "drive":
|
|
|
|
return u.updateIn("propulsion.drive.uiTransform", transform);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (state) => state;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
dux.addReaction(calculateDriveReqs);
|
|
|
|
dux.addReaction(ftlReqsReaction);
|
|
|
|
dux.addReaction(screensReqsReaction);
|
|
|
|
|
|
|
|
dux.addReaction((store) => (state) => {
|
|
|
|
let cost = 0;
|
|
|
|
let mass = 0;
|
|
|
|
|
|
|
|
let subsystems = Object.values(state);
|
|
|
|
|
|
|
|
while (subsystems.length > 0) {
|
|
|
|
const subsystem = subsystems.shift();
|
|
|
|
if (typeof subsystem !== "object") continue;
|
|
|
|
|
|
|
|
if (subsystem.reqs) {
|
|
|
|
cost += subsystem.reqs.cost;
|
|
|
|
mass += subsystem.reqs.mass;
|
|
|
|
}
|
|
|
|
|
|
|
|
subsystems.push(...Object.values(subsystem));
|
|
|
|
}
|
|
|
|
|
|
|
|
store.dispatch.setShipReqs({ cost, usedMass: mass });
|
|
|
|
});
|
|
|
|
|
|
|
|
export default dux;
|
2023-03-03 23:02:52 +00:00
|
|
|
*/
|