From 0e76cdd4d33e3abe911d63abdc7ba9f29e5bf82b Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 26 Mar 2023 14:40:20 -0400 Subject: [PATCH] screens --- package.json | 1 + src/lib/store/ship.ts | 4 ++ src/lib/store/ship/structure/screens.ts | 49 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 src/lib/store/ship/structure/screens.ts diff --git a/package.json b/package.json index 7c8c6b4..13371ea 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "histoire": "^0.15.9", "jsdom": "^21.1.1", "lodash": "^4.17.21", + "memoize-one": "^6.0.0", "redux": "^4.1.2", "remeda": "^1.9.1", "reselect": "^4.1.5", diff --git a/src/lib/store/ship.ts b/src/lib/store/ship.ts index 8e32d72..d9e1d88 100644 --- a/src/lib/store/ship.ts +++ b/src/lib/store/ship.ts @@ -12,6 +12,7 @@ import { streamliningDux as streamlining } from "./ship/structure/streamlining"; import { calcStreamliningReqs } from "./ship/structure/rules"; import { cargoDux } from "./ship/structure/cargo"; import { hullDux } from "./ship/structure/hull"; +import { screensDux, screensReqsReaction } from "./ship/structure/screens"; const shipDux = new Updux({ subduxes: { @@ -22,6 +23,7 @@ const shipDux = new Updux({ streamlining, cargo: cargoDux, hull: hullDux, + screens: screensDux, }, }), propulsion: new Updux({ @@ -89,4 +91,6 @@ shipDux.addReaction((api) => ) ); +shipDux.addReaction(screensReqsReaction); + export default shipDux; diff --git a/src/lib/store/ship/structure/screens.ts b/src/lib/store/ship/structure/screens.ts new file mode 100644 index 0000000..cfcc53e --- /dev/null +++ b/src/lib/store/ship/structure/screens.ts @@ -0,0 +1,49 @@ +import { reqs } from "$lib/shipDux/reqs"; +import Updux, { createPayloadAction } from "updux"; +import u from "@yanick/updeep-remeda"; +import { createSelector } from "reselect"; + +const initialState = { + standard: 0, + advanced: 0, + reqs, +}; + +const setScreens = createPayloadAction<{ standard: number; advanced: number }>( + "setScreens" +); +const setScreensReqs = createPayloadAction("setScreensReqs"); + +export const screensDux = new Updux({ + initialState, + actions: { + setScreensReqs, + setScreens, + }, + // reducers: { + // setScreensReqs(state, action) { + // state.reqs = action.payload; + // }, + // }, +}); + +screensDux.addMutation(setScreens, (payload) => u(payload)); +screensDux.addMutation(setScreensReqs, (reqs) => u({ reqs })); + +export const screensReqsReaction = (api) => + createSelector( + (ship) => ship.identification.reqs.mass, + (ship) => ship.structure.screens.standard, + (ship) => ship.structure.screens.advanced, + (...args) => api.dispatch.setScreensReqs(calcScreensReqs(...args)) + ); + +function calcScreensReqs(mass: number, standard: number, advanced: number) { + const standardMass = standard * Math.max(3, Math.ceil(0.05 * mass)); + const advancedMass = advanced * Math.max(4, Math.ceil(0.075 * mass)); + + return { + mass: standardMass + advancedMass, + cost: 3 * standardMass + 4 * advancedMass, + }; +}