This commit is contained in:
Yanick Champoux 2023-03-26 13:02:22 -04:00
parent 87c923cac3
commit e3c0b402d1
6 changed files with 93 additions and 74 deletions

View File

@ -1,13 +1,16 @@
<Section label="structure"> <Section label="structure">
<Streamlining {...streamlining} /> <Hull {...hull} />
<Cargo {...cargo} /> <Cargo {...cargo} />
<Streamlining {...streamlining} />
</Section> </Section>
<script lang="ts"> <script lang="ts">
import Section from "$lib/components/Section.svelte"; import Section from "$lib/components/Section.svelte";
import Streamlining from "./Structure/Streamlining.svelte"; import Streamlining from "./Structure/Streamlining.svelte";
import Cargo from "./Structure/Cargo.svelte"; import Cargo from "./Structure/Cargo.svelte";
import Hull from "./Structure/Hull.svelte";
export let streamlining = {}; export let streamlining = {};
export let cargo = {}; export let cargo = {};
export let hull = {};
</script> </script>

View File

@ -1,23 +1,23 @@
<ShipItem {...reqs}> <ShipItem {...reqs}>
<Field label="hull"> <Field label="hull">
<input class="short" bind:value={rating} type="number" {min} {max} /> <input class="short" bind:value={rating} type="number" {min} {max} />
</Field> </Field>
</ShipItem> </ShipItem>
<script> <script>
import { getContext } from "svelte"; import { getContext } from "svelte";
import ShipItem from '$lib/components/ShipItem/index.svelte'; import ShipItem from "$lib/components/ShipItem.svelte";
import Field from '$lib/components/Field/index.svelte'; import Field from "$lib/components/Field.svelte";
export let rating = 0; export let rating = 0;
export let reqs = {}; export let reqs = {};
export let min = 0; export let min = 0;
export let max = 1; export let max = 1;
const ship = getContext('ship'); const api = getContext("api");
$: ship.dispatch.setHull(rating); $: api.dispatch?.setHull?.(rating);
</script> </script>
<style> <style>

View File

@ -1,33 +0,0 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { reqs } from "../reqs";
const initialState = {
rating: 0,
min: 0,
max: 0,
reqs,
};
const hull = createSlice({
name: "hull",
initialState,
reducers: {
setHull: (state, action: PayloadAction<number>) => {
state.rating = action.payload;
state.reqs = {
mass: action.payload,
cost: 2 * action.payload,
};
},
setShipMass: (state, action: PayloadAction<number>) => {
const mass = action.payload;
let { rating } = state;
if (rating > mass) state.rating = mass;
state.min = Math.ceil(mass / 10);
if (state.rating < state.min) state.rating = state.min;
state.max = mass;
},
},
});
export const { actions, reducer } = hull;

View File

@ -1,6 +1,7 @@
import { createSelector } from "@reduxjs/toolkit"; import { createSelector } from "@reduxjs/toolkit";
import Updux from "updux"; import Updux from "updux";
import * as R from "remeda"; import * as R from "remeda";
import memoize from "memoize-one";
import identification from "./ship/identification"; import identification from "./ship/identification";
import ftl, { calcFtlReqs } from "./ship/propulsion/ftl"; import ftl, { calcFtlReqs } from "./ship/propulsion/ftl";
@ -10,6 +11,7 @@ import { carrierDux } from "./ship/carrier";
import { streamliningDux as streamlining } from "./ship/structure/streamlining"; import { streamliningDux as streamlining } from "./ship/structure/streamlining";
import { calcStreamliningReqs } from "./ship/structure/rules"; import { calcStreamliningReqs } from "./ship/structure/rules";
import { cargoDux } from "./ship/structure/cargo"; import { cargoDux } from "./ship/structure/cargo";
import { hullDux } from "./ship/structure/hull";
const shipDux = new Updux({ const shipDux = new Updux({
subduxes: { subduxes: {
@ -19,6 +21,7 @@ const shipDux = new Updux({
subduxes: { subduxes: {
streamlining, streamlining,
cargo: cargoDux, cargo: cargoDux,
hull: hullDux,
}, },
}), }),
propulsion: new Updux({ propulsion: new Updux({
@ -32,13 +35,13 @@ const shipDux = new Updux({
}, },
}); });
shipDux.addReaction((api) => shipDux.addReaction((api) => {
createSelector( return createSelector(
api.selectors.getFtlType, api.selectors.getFtlType,
api.selectors.getShipMass, api.selectors.getShipMass,
(type, mass) => api.dispatch.setFtlReqs(calcFtlReqs(type, mass)) (type, mass) => api.dispatch.setFtlReqs(calcFtlReqs(type, mass))
) );
); });
shipDux.addReaction((api) => (state) => { shipDux.addReaction((api) => (state) => {
let cost = 0; let cost = 0;
@ -61,6 +64,10 @@ shipDux.addReaction((api) => (state) => {
api.dispatch.setShipReqs({ cost, usedMass: mass }); api.dispatch.setShipReqs({ cost, usedMass: mass });
}); });
shipDux.addEffect((api) => (next) => (action) => {
next(action);
});
shipDux.addReaction((api) => shipDux.addReaction((api) =>
createSelector( createSelector(
api.selectors.getShipMass, api.selectors.getShipMass,

View File

@ -4,41 +4,41 @@ import * as R from "remeda";
import { carrierDux } from "./carrier"; import { carrierDux } from "./carrier";
const initialState = { const initialState = {
shipType: "", shipType: "",
shipClass: "", shipClass: "",
isCarrier: false, isCarrier: false,
reqs: { reqs: {
mass: 10, mass: 10,
cost: 0, cost: 0,
usedMass: 0, usedMass: 0,
}, },
}; };
const setShipClass = createAction("setShipClass", withPayload<string>()); const setShipClass = createAction("setShipClass", withPayload<string>());
const updateIdentification = createAction("updateIdentification"); const updateIdentification = createAction("updateIdentification");
const setShipReqs = createAction("setShipReqs", withPayload()); const setShipReqs = createAction("setShipReqs", withPayload());
export const dux = new Updux({ export const identificationDux = new Updux({
initialState, initialState,
actions: { actions: {
setShipClass, setShipClass,
updateIdentification, updateIdentification,
setShipReqs, setShipReqs,
}, },
selectors: { selectors: {
getShipMass: (state) => state.reqs.mass, getShipMass: (state) => state.reqs.mass,
isCarrier: ({ isCarrier }) => isCarrier, isCarrier: ({ isCarrier }) => isCarrier,
}, },
}); });
dux.addMutation(setShipClass, (shipClass) => u({ shipClass })); identificationDux.addMutation(setShipClass, (shipClass) => u({ shipClass }));
dux.addMutation(updateIdentification, (update) => u(update)); identificationDux.addMutation(updateIdentification, (update) => u(update));
dux.addMutation(setShipReqs, (reqs) => u({ reqs })); identificationDux.addMutation(setShipReqs, (reqs) => u({ reqs }));
dux.addMutation(carrierDux.actions.setNbrCarrierBays, (nbrBays) => identificationDux.addMutation(carrierDux.actions.setNbrCarrierBays, (nbrBays) =>
u({ u({
isCarrier: nbrBays > 0, isCarrier: nbrBays > 0,
}) })
); );
export default dux; export default identificationDux;

View File

@ -0,0 +1,42 @@
import { reqs } from "$lib/shipDux/reqs";
import Updux, { createPayloadAction } from "updux";
import u from "@yanick/updeep-remeda";
import identificationDux from "../identification";
const initialState = {
rating: 0,
min: 0,
max: 0,
reqs,
};
const setHull = createPayloadAction<number>("setHull");
export const hullDux = new Updux({
initialState,
actions: {
setHull,
updateIdentification: identificationDux.actions.updateIdentification,
},
});
hullDux.addMutation(setHull, (rating) =>
u({
rating,
reqs: {
mass: rating,
cost: 2 * rating,
},
})
);
hullDux.addMutation(
identificationDux.actions.updateIdentification,
(payload) => {
let mass = payload?.reqs?.mass ?? 0;
return u({
min: Math.ceil((mass ?? 0) / 10),
max: mass ?? 0,
});
}
);