aotds-docks/src/dux/weaponry/weapons/index.js

52 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-06-13 15:36:59 +00:00
import matches from 'lodash/matches.js';
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 { weapon_cost_mass } from "../../weapons/rules";
2020-07-19 20:21:28 +00:00
const add_weapon = action("add_weapon", payload());
const remove_weapon = action("remove_weapon", payload());
2020-07-19 20:21:28 +00:00
const uu = (transform) => (state) => transform(state)(state);
2020-07-27 22:17:55 +00:00
const with_reqs = uu((weapon) => u(weapon_cost_mass(weapon)));
2020-07-27 22:17:55 +00:00
2020-07-19 20:21:28 +00:00
const dux = new Updux({
initial: [],
});
2020-07-19 20:21:28 +00:00
2020-07-27 22:17:55 +00:00
const weapon_initial = {
beam: {
weapon_type: "beam",
weapon_class: 1,
arcs: ["F", "A", "FS", "FP", "AS", "AP"],
},
submunition: {
weapon_type: "submunition",
arcs: ["F"],
},
pds: { weapon_type: "pds" },
scattergun: { weapon_type: "scattergun" },
needle: { weapon_type: "needle", arcs: ["F"] },
2020-07-27 22:17:55 +00:00
};
dux.addMutation(add_weapon, (type) => (state) => {
const id = 1 + Math.max(0, ...state.map(({ id }) => id));
return [...state, { ...with_reqs(weapon_initial[type]), id }];
2020-07-19 20:21:28 +00:00
});
dux.addMutation(remove_weapon, (id) => (state) =>
state.filter((w) => w.id !== id)
2020-07-19 20:21:28 +00:00
);
const set_weapon = action("set_weapon", payload());
2020-07-27 22:17:55 +00:00
dux.addMutation(set_weapon, (payload) =>
u.map(
2021-06-13 15:36:59 +00:00
u.if(matches({ id: payload.id }), (state) => with_reqs(u(payload, state)))
)
2020-07-27 22:17:55 +00:00
);
2020-07-19 20:21:28 +00:00
export default dux.asDux;