aotds-docks/src/dux/index.js

136 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-07-19 20:21:28 +00:00
import Updux from "updux";
import { action, payload } from "ts-action";
2021-05-17 13:48:31 +00:00
import u from "@yanick/updeep";
2020-07-19 20:21:28 +00:00
import { createSelector } from "reselect";
import ftl from "./ftl";
import engine, { calc_drive_reqs } from "./engine";
2022-03-02 00:14:01 +00:00
import weaponry from "./weaponry";
2020-07-19 20:21:28 +00:00
import { calc_ftl_reqs } from "./ftl/rules";
import { calc_ship_req } from "./utils";
2022-03-02 00:14:01 +00:00
import { candidate_ship_types } from "./ship_types";
import structure from "./structure";
import cargo from "./cargo";
import streamlining from "./streamlining";
import carrier from "./carrier";
import { ceil } from "./utils";
2020-07-19 20:21:28 +00:00
const set_ship_mass = action("set_ship_mass", payload());
const set_name = action("set_name", payload());
const set_ship_reqs = action("set_ship_reqs", payload());
const set_hull = action("set_hull", payload());
2022-03-02 00:14:01 +00:00
const set_ship_type = action("set_ship_type", payload());
2020-07-19 20:21:28 +00:00
2022-03-02 00:14:01 +00:00
const reset = action("reset");
2020-07-19 20:21:28 +00:00
const initial = {
2022-03-02 00:14:01 +00:00
general: {
ship_class: "",
name: "",
ship_type: "",
mass: 10,
used_mass: 0,
cost: 10,
},
};
2020-07-19 20:21:28 +00:00
const dux = new Updux({
2020-07-27 17:42:19 +00:00
subduxes: { ftl, engine, weaponry, structure, cargo, streamlining, carrier },
2022-03-02 00:14:01 +00:00
initial,
2020-07-19 20:21:28 +00:00
});
2022-03-02 00:14:01 +00:00
dux.addMutation(reset, () => () => initial);
2020-07-19 20:21:28 +00:00
2022-03-02 00:14:01 +00:00
dux.addMutation(set_hull, ({ rating }) => (state) => {
2020-07-19 20:21:28 +00:00
return u.updateIn("structure.hull", {
cost: 2 * rating,
rating,
mass: rating,
})(state);
});
dux.addMutation(set_ship_mass, (mass) => u.updateIn("general", { mass }));
dux.addMutation(set_name, (name) => u.updateIn("general", { name }));
2022-03-02 00:14:01 +00:00
dux.addMutation(action("set_ship_class", payload()), (ship_class) =>
u.updateIn("general", { ship_class })
2020-07-19 20:21:28 +00:00
);
dux.addMutation(set_ship_reqs, ({ cost, mass: used_mass }) =>
u.updateIn("general", { cost, used_mass })
);
// set ship's req
dux.addSubscription((store) =>
createSelector(calc_ship_req, (reqs) => store.dispatch(set_ship_reqs(reqs)))
);
2020-07-26 22:22:05 +00:00
2020-07-24 18:11:18 +00:00
dux.addSubscription((store) =>
2022-03-02 00:14:01 +00:00
createSelector(
(store) => store.general.mass,
(store) => store.general.ship_type,
(store) => store.carrier.bays,
(mass, type, bays) => {
console.log({ bays });
const candidates = candidate_ship_types(mass, bays > 0);
console.log({ candidates });
if (candidates.length === 0) return;
if (candidates.find(({ name }) => name === type)) return;
store.dispatch(store.actions.set_ship_type(candidates[0].name));
}
)
2020-07-19 20:21:28 +00:00
);
2022-03-02 00:14:01 +00:00
dux.addMutation(set_ship_type, (type) => u.updateIn("general.ship_type", type));
2020-07-19 20:21:28 +00:00
dux.addSubscription((store) =>
createSelector(
[(ship) => ship.general.mass, (ship) => ship.ftl.type],
(ship_mass, type) =>
2022-03-02 00:14:01 +00:00
store.dispatch(ftl.actions.set_ftl_reqs(calc_ftl_reqs(type, ship_mass)))
2020-07-19 20:21:28 +00:00
)
);
2022-03-02 00:14:01 +00:00
dux.addSubscription((store) =>
createSelector(
(ship) => ship.general.mass,
(ship) => ship.structure.screens.standard,
(ship) => ship.structure.screens.advanced,
(mass, standard, advanced) => {
const standard_mass = standard * Math.max(3, ceil(0.05 * mass));
const advanced_mass = advanced * Math.max(4, ceil(0.075 * mass));
2020-07-24 18:11:18 +00:00
2022-03-02 00:14:01 +00:00
store.dispatch(
dux.actions.set_screens_reqs({
mass: standard_mass + advanced_mass,
cost: 3 * standard_mass + 4 * advanced_mass,
})
);
2020-07-24 18:11:18 +00:00
}
2022-03-02 00:14:01 +00:00
)
);
2020-07-24 18:11:18 +00:00
2020-07-19 20:21:28 +00:00
dux.addSubscription((store) =>
createSelector(
[
(ship) => ship.general.mass,
(ship) => ship.engine.rating,
(ship) => ship.engine.advanced,
],
(ship_mass, rating, advanced) =>
store.dispatch(
dux.actions.set_drive_reqs(calc_drive_reqs(ship_mass, rating, advanced))
)
)
);
export default dux.asDux;
export const actions = dux.actions;