This commit is contained in:
Yanick Champoux 2023-03-26 11:51:54 -04:00
parent add30bb666
commit 87c923cac3
6 changed files with 43 additions and 24 deletions

View File

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

View File

@ -5,17 +5,17 @@
</ShipItem>
<script>
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";
import { getContext } from "svelte";
export let ship = getContext("ship");
export let api = getContext("api");
export let space = 0;
export let reqs = {};
$: ship.dispatch.setCargo(space);
$: api?.dispatch?.setCargo?.(space);
</script>
<style>

View File

@ -1,17 +0,0 @@
import { createSlice } from "@reduxjs/toolkit";
import { reqs } from "../reqs";
const initialState = { space: 0, reqs };
const cargo = createSlice({
name: "cargo",
initialState,
reducers: {
setCargo: (state, action) => {
state.space = action.payload;
state.reqs = { cost: 0, mass: action.payload };
},
},
});
export const { actions, reducer } = cargo;

View File

@ -3,8 +3,6 @@ import ship from "./ship";
test("kicking the tires", () => {
const store = ship.createStore();
console.log(store.getState());
console.log(store.getState.getStreamlining());
store.dispatch.setFtlType("standard");
expect(store.getState().propulsion.ftl.reqs.mass).toEqual(1);
@ -29,4 +27,13 @@ test("kicking the tires", () => {
expect(store.getState.getStreamlining()).toBe("partial");
expect(store.selectors.getStreamlining(store.getState())).toBe("partial");
store.dispatch.setCargo(3);
expect(store.getState().structure.cargo).toEqual({
space: 3,
reqs: {
mass: 3,
cost: 0,
},
});
});

View File

@ -9,6 +9,7 @@ import { calcDriveReqs } from "$lib/shipDux/engine";
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";
const shipDux = new Updux({
subduxes: {
@ -17,6 +18,7 @@ const shipDux = new Updux({
initialState: {},
subduxes: {
streamlining,
cargo: cargoDux,
},
}),
propulsion: new Updux({
@ -75,7 +77,6 @@ shipDux.addReaction((api) =>
api.selectors.getShipMass,
api.selectors.getStreamlining,
(mass, type) => {
console.log("AH!", mass, type);
api.dispatch.setStreamliningReqs(calcStreamliningReqs(type, mass));
}
)

View File

@ -0,0 +1,25 @@
import { reqs } from "$lib/shipDux/reqs";
import Updux, { createPayloadAction } from "updux";
import u from "@yanick/updeep-remeda";
const initialState = { space: 0, reqs };
const setCargo = createPayloadAction<number>("setCargo");
export const cargoDux = new Updux({
initialState,
actions: { setCargo },
reducers: {
setCargo: (state, action) => {
state.space = action.payload;
state.reqs = { cost: 0, mass: action.payload };
},
},
});
cargoDux.addMutation(setCargo, (space) =>
u({
space,
reqs: { cost: 0, mass: space },
})
);