aotds-docks/src/dux/index.js

169 lines
4.4 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";
import weaponry from './weaponry';
import { calc_ftl_reqs } from "./ftl/rules";
import { calc_ship_req } from "./utils";
import { candidate_ship_types } from './ship_types';
2020-07-24 18:11:18 +00:00
import structure from './structure';
2020-07-26 16:02:15 +00:00
import cargo from './cargo';
2020-07-26 22:22:05 +00:00
import streamlining from './streamlining';
2020-07-27 17:42:19 +00:00
import carrier from './carrier';
2021-05-17 13:48:31 +00:00
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());
const set_ship_type = action('set_ship_type',payload());
const reset = action('reset' );
const initial = {
general: {
ship_class: "",
name: "",
ship_type: "",
mass: 10,
used_mass: 0,
cost: 10,
},
};
2021-05-17 13:48:31 +00:00
console.log(Updux);
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 },
2020-07-19 20:21:28 +00:00
initial
});
dux.addMutation( reset, () => () => initial );
dux.addMutation(set_hull, ({rating}) => (state) => {
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 }));
dux.addMutation( action('set_ship_class',payload() ),
ship_class => u.updateIn('general',{ship_class})
);
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
dux.addSubscription((store) =>
createSelector(
store => store.general.mass,
store => store.streamlining.type,
(ship_mass, streamlining ) => {
2021-05-01 22:45:27 +00:00
const mass = ceil( ship_mass * (
2020-07-26 22:22:05 +00:00
streamlining === 'none' ? 0
2021-05-01 22:45:27 +00:00
: streamlining === 'partial' ? 5 : 10
2020-07-26 22:22:05 +00:00
) / 100 );
const cost = 2 * mass;
store.dispatch( dux.actions.set_streamlining_cost_mass({cost,mass}) );
}
)
);
2020-07-24 18:11:18 +00:00
dux.addSubscription((store) =>
2020-07-19 20:21:28 +00:00
createSelector(
store => store.general.mass,
store => store.general.ship_type,
2020-07-27 17:42:19 +00:00
store => store.carrier.bays,
(mass,type,bays) => {
console.log({bays});
const candidates = candidate_ship_types(mass,bays > 0);
console.log({candidates});
2020-07-19 20:21:28 +00:00
if( candidates.length === 0 ) return;
if( candidates.find( ({name}) => name === type ) ) return;
store.dispatch(
2020-07-24 18:11:18 +00:00
store.actions.set_ship_type(
2020-07-19 20:21:28 +00:00
candidates[0].name
)
)
2020-07-24 18:11:18 +00:00
2020-07-19 20:21:28 +00:00
}
)
);
dux.addMutation(set_ship_type, type => u.updateIn('general.ship_type',type) );
dux.addSubscription((store) =>
createSelector(
[(ship) => ship.general.mass, (ship) => ship.ftl.type],
(ship_mass, type) =>
store.dispatch(ftl.actions.set_ftl_reqs(calc_ftl_reqs(type,ship_mass)))
)
);
2020-07-24 18:11:18 +00:00
dux.addSubscription( store => createSelector(
ship => ship.general.mass,
ship => ship.structure.screens.standard,
ship => ship.structure.screens.advanced,
(mass,standard,advanced) => {
console.log({
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 ));
store.dispatch( dux.actions.set_screens_reqs({
mass: standard_mass + advanced_mass,
cost: 3 * standard_mass + 4 * advanced_mass
}));
}
));
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))
)
)
);
const calc_firecons_reqs = (nbr) => ({
cost: 4 * nbr,
mass: nbr,
});
const set_firecons = action("set_firecons", payload());
dux.addMutation(set_firecons, (nbr) =>
u.updateIn("weaponry.firecons", {
nbr,
...calc_firecons_reqs(nbr),
})
);
export default dux.asDux;
export const actions = dux.actions;