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">
<Streamlining {...streamlining} />
<Hull {...hull} />
<Cargo {...cargo} />
<Streamlining {...streamlining} />
</Section>
<script lang="ts">
import Section from "$lib/components/Section.svelte";
import Streamlining from "./Structure/Streamlining.svelte";
import Cargo from "./Structure/Cargo.svelte";
import Hull from "./Structure/Hull.svelte";
export let streamlining = {};
export let cargo = {};
export let hull = {};
</script>

View File

@ -1,23 +1,23 @@
<ShipItem {...reqs}>
<Field label="hull">
<input class="short" bind:value={rating} type="number" {min} {max} />
</Field>
</ShipItem>
<ShipItem {...reqs}>
<Field label="hull">
<input class="short" bind:value={rating} type="number" {min} {max} />
</Field>
</ShipItem>
<script>
import { getContext } from "svelte";
import ShipItem from '$lib/components/ShipItem/index.svelte';
import Field from '$lib/components/Field/index.svelte';
import ShipItem from "$lib/components/ShipItem.svelte";
import Field from "$lib/components/Field.svelte";
export let rating = 0;
export let reqs = {};
export let min = 0;
export let max = 1;
const ship = getContext('ship');
const api = getContext("api");
$: ship.dispatch.setHull(rating);
$: api.dispatch?.setHull?.(rating);
</script>
<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 Updux from "updux";
import * as R from "remeda";
import memoize from "memoize-one";
import identification from "./ship/identification";
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 { calcStreamliningReqs } from "./ship/structure/rules";
import { cargoDux } from "./ship/structure/cargo";
import { hullDux } from "./ship/structure/hull";
const shipDux = new Updux({
subduxes: {
@ -19,6 +21,7 @@ const shipDux = new Updux({
subduxes: {
streamlining,
cargo: cargoDux,
hull: hullDux,
},
}),
propulsion: new Updux({
@ -32,13 +35,13 @@ const shipDux = new Updux({
},
});
shipDux.addReaction((api) =>
createSelector(
shipDux.addReaction((api) => {
return createSelector(
api.selectors.getFtlType,
api.selectors.getShipMass,
(type, mass) => api.dispatch.setFtlReqs(calcFtlReqs(type, mass))
)
);
);
});
shipDux.addReaction((api) => (state) => {
let cost = 0;
@ -61,6 +64,10 @@ shipDux.addReaction((api) => (state) => {
api.dispatch.setShipReqs({ cost, usedMass: mass });
});
shipDux.addEffect((api) => (next) => (action) => {
next(action);
});
shipDux.addReaction((api) =>
createSelector(
api.selectors.getShipMass,

View File

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