identification store
This commit is contained in:
parent
0acce21e2a
commit
4a7877c433
@ -27,6 +27,7 @@ module.exports = {
|
|||||||
$app: path.resolve("./fake/app"),
|
$app: path.resolve("./fake/app"),
|
||||||
"\\$lib": path.resolve(__dirname, "../src/lib/"),
|
"\\$lib": path.resolve(__dirname, "../src/lib/"),
|
||||||
$lib: path.resolve(__dirname, "../src/lib/"),
|
$lib: path.resolve(__dirname, "../src/lib/"),
|
||||||
|
"\\$app/env": path.resolve(__dirname, "../fake/app/env.js"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
1
fake/app/env.js
Normal file
1
fake/app/env.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
export const browser = true;
|
@ -5,15 +5,15 @@ import { createSelector } from "reselect";
|
|||||||
|
|
||||||
import ftl from "./ftl";
|
import ftl from "./ftl";
|
||||||
import engine, { calc_drive_reqs } from "./engine";
|
import engine, { calc_drive_reqs } from "./engine";
|
||||||
import weaponry from './weaponry';
|
import weaponry from "./weaponry";
|
||||||
import { calc_ftl_reqs } from "./ftl/rules";
|
import { calc_ftl_reqs } from "./ftl/rules";
|
||||||
import { calc_ship_req } from "./utils";
|
import { calc_ship_req } from "./utils";
|
||||||
import { candidate_ship_types } from './ship_types';
|
import { candidate_ship_types } from "./ship_types";
|
||||||
import structure from './structure';
|
import structure from "./structure";
|
||||||
import cargo from './cargo';
|
import cargo from "./cargo";
|
||||||
import streamlining from './streamlining';
|
import streamlining from "./streamlining";
|
||||||
import carrier from './carrier';
|
import carrier from "./carrier";
|
||||||
import { ceil } from './utils';
|
import { ceil } from "./utils";
|
||||||
|
|
||||||
const set_ship_mass = action("set_ship_mass", payload());
|
const set_ship_mass = action("set_ship_mass", payload());
|
||||||
const set_name = action("set_name", payload());
|
const set_name = action("set_name", payload());
|
||||||
@ -21,9 +21,9 @@ const set_name = action("set_name", payload());
|
|||||||
const set_ship_reqs = action("set_ship_reqs", payload());
|
const set_ship_reqs = action("set_ship_reqs", payload());
|
||||||
const set_hull = action("set_hull", payload());
|
const set_hull = action("set_hull", payload());
|
||||||
|
|
||||||
const set_ship_type = action('set_ship_type',payload());
|
const set_ship_type = action("set_ship_type", payload());
|
||||||
|
|
||||||
const reset = action('reset' );
|
const reset = action("reset");
|
||||||
|
|
||||||
const initial = {
|
const initial = {
|
||||||
general: {
|
general: {
|
||||||
@ -38,7 +38,7 @@ const initial = {
|
|||||||
|
|
||||||
const dux = new Updux({
|
const dux = new Updux({
|
||||||
subduxes: { ftl, engine, weaponry, structure, cargo, streamlining, carrier },
|
subduxes: { ftl, engine, weaponry, structure, cargo, streamlining, carrier },
|
||||||
initial
|
initial,
|
||||||
});
|
});
|
||||||
|
|
||||||
dux.addMutation(reset, () => () => initial);
|
dux.addMutation(reset, () => () => initial);
|
||||||
@ -53,8 +53,8 @@ dux.addMutation(set_hull, ({rating}) => (state) => {
|
|||||||
dux.addMutation(set_ship_mass, (mass) => u.updateIn("general", { mass }));
|
dux.addMutation(set_ship_mass, (mass) => u.updateIn("general", { mass }));
|
||||||
dux.addMutation(set_name, (name) => u.updateIn("general", { name }));
|
dux.addMutation(set_name, (name) => u.updateIn("general", { name }));
|
||||||
|
|
||||||
dux.addMutation( action('set_ship_class',payload() ),
|
dux.addMutation(action("set_ship_class", payload()), (ship_class) =>
|
||||||
ship_class => u.updateIn('general',{ship_class})
|
u.updateIn("general", { ship_class })
|
||||||
);
|
);
|
||||||
|
|
||||||
dux.addMutation(set_ship_reqs, ({ cost, mass: used_mass }) =>
|
dux.addMutation(set_ship_reqs, ({ cost, mass: used_mass }) =>
|
||||||
@ -68,13 +68,14 @@ dux.addSubscription((store) =>
|
|||||||
|
|
||||||
dux.addSubscription((store) =>
|
dux.addSubscription((store) =>
|
||||||
createSelector(
|
createSelector(
|
||||||
store => store.general.mass,
|
(store) => store.general.mass,
|
||||||
store => store.streamlining.type,
|
(store) => store.streamlining.type,
|
||||||
(ship_mass, streamlining) => {
|
(ship_mass, streamlining) => {
|
||||||
const mass = ceil( ship_mass * (
|
const mass = ceil(
|
||||||
streamlining === 'none' ? 0
|
(ship_mass *
|
||||||
: streamlining === 'partial' ? 5 : 10
|
(streamlining === "none" ? 0 : streamlining === "partial" ? 5 : 10)) /
|
||||||
) / 100 );
|
100
|
||||||
|
);
|
||||||
const cost = 2 * mass;
|
const cost = 2 * mass;
|
||||||
|
|
||||||
store.dispatch(dux.actions.set_streamlining_cost_mass({ cost, mass }));
|
store.dispatch(dux.actions.set_streamlining_cost_mass({ cost, mass }));
|
||||||
@ -84,9 +85,9 @@ dux.addSubscription((store) =>
|
|||||||
|
|
||||||
dux.addSubscription((store) =>
|
dux.addSubscription((store) =>
|
||||||
createSelector(
|
createSelector(
|
||||||
store => store.general.mass,
|
(store) => store.general.mass,
|
||||||
store => store.general.ship_type,
|
(store) => store.general.ship_type,
|
||||||
store => store.carrier.bays,
|
(store) => store.carrier.bays,
|
||||||
(mass, type, bays) => {
|
(mass, type, bays) => {
|
||||||
console.log({ bays });
|
console.log({ bays });
|
||||||
const candidates = candidate_ship_types(mass, bays > 0);
|
const candidates = candidate_ship_types(mass, bays > 0);
|
||||||
@ -96,17 +97,12 @@ dux.addSubscription((store) =>
|
|||||||
|
|
||||||
if (candidates.find(({ name }) => name === type)) return;
|
if (candidates.find(({ name }) => name === type)) return;
|
||||||
|
|
||||||
store.dispatch(
|
store.dispatch(store.actions.set_ship_type(candidates[0].name));
|
||||||
store.actions.set_ship_type(
|
|
||||||
candidates[0].name
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
dux.addMutation(set_ship_type, type => u.updateIn('general.ship_type',type) );
|
dux.addMutation(set_ship_type, (type) => u.updateIn("general.ship_type", type));
|
||||||
|
|
||||||
dux.addSubscription((store) =>
|
dux.addSubscription((store) =>
|
||||||
createSelector(
|
createSelector(
|
||||||
@ -116,23 +112,29 @@ dux.addSubscription((store) =>
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
dux.addSubscription( store => createSelector(
|
dux.addSubscription((store) =>
|
||||||
ship => ship.general.mass,
|
createSelector(
|
||||||
ship => ship.structure.screens.standard,
|
(ship) => ship.general.mass,
|
||||||
ship => ship.structure.screens.advanced,
|
(ship) => ship.structure.screens.standard,
|
||||||
|
(ship) => ship.structure.screens.advanced,
|
||||||
(mass, standard, advanced) => {
|
(mass, standard, advanced) => {
|
||||||
console.log({
|
console.log({
|
||||||
mass, standard, advanced
|
mass,
|
||||||
})
|
standard,
|
||||||
|
advanced,
|
||||||
|
});
|
||||||
const standard_mass = standard * Math.max(3, ceil(0.05 * mass));
|
const standard_mass = standard * Math.max(3, ceil(0.05 * mass));
|
||||||
const advanced_mass = advanced * Math.max(4, ceil(0.075 * mass));
|
const advanced_mass = advanced * Math.max(4, ceil(0.075 * mass));
|
||||||
|
|
||||||
store.dispatch( dux.actions.set_screens_reqs({
|
store.dispatch(
|
||||||
|
dux.actions.set_screens_reqs({
|
||||||
mass: standard_mass + advanced_mass,
|
mass: standard_mass + advanced_mass,
|
||||||
cost: 3 * standard_mass + 4 * advanced_mass
|
cost: 3 * standard_mass + 4 * advanced_mass,
|
||||||
}));
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
));
|
)
|
||||||
|
);
|
||||||
|
|
||||||
dux.addSubscription((store) =>
|
dux.addSubscription((store) =>
|
||||||
createSelector(
|
createSelector(
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
<label>{label}</label>
|
<label>{label}</label>
|
||||||
{/if}
|
{/if}
|
||||||
<slot>
|
<slot>
|
||||||
<input type="text" {placeholder} {value} on:change />
|
<input type="text" {placeholder} bind:value on:change />
|
||||||
</slot>
|
</slot>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -1,9 +1,4 @@
|
|||||||
<Meta
|
<Meta title="Identification" component={Identification} argTypes={{}} />
|
||||||
title="Identification"
|
|
||||||
component={Identification}
|
|
||||||
argTypes={{
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Story name="Primary" args={{}} />
|
<Story name="Primary" args={{}} />
|
||||||
|
|
||||||
@ -24,5 +19,4 @@
|
|||||||
setContext("ship", {
|
setContext("ship", {
|
||||||
dispatch: (type, detail) => action(type)(detail),
|
dispatch: (type, detail) => action(type)(detail),
|
||||||
});
|
});
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
@ -1,11 +1,7 @@
|
|||||||
<div>
|
<div>
|
||||||
<Field
|
<Field label="ship class" bind:value={shipClass} />
|
||||||
label="ship class"
|
|
||||||
value={shipClass}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Field label="ship type">
|
<Field label="ship type">
|
||||||
<select value={shipType}>
|
<select bind:value={shipType}>
|
||||||
{#each shipTypes as name (name)}
|
{#each shipTypes as name (name)}
|
||||||
<option>{name}</option>
|
<option>{name}</option>
|
||||||
{/each}
|
{/each}
|
||||||
@ -14,20 +10,25 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import Field from "$lib/components/Field/index.svelte";
|
import { getContext } from "svelte";
|
||||||
import { candidateShipTypes } from './shipTypes.js';
|
|
||||||
|
|
||||||
export let shipClass = '';
|
import Field from "$lib/components/Field/index.svelte";
|
||||||
export let shipType = '';
|
import { candidateShipTypes } from "./shipTypes.js";
|
||||||
|
|
||||||
|
export let shipClass = "";
|
||||||
|
export let shipType = "";
|
||||||
export let mass = 10;
|
export let mass = 10;
|
||||||
export let isCarrier = false;
|
export let isCarrier = false;
|
||||||
|
|
||||||
|
const ship = getContext("ship");
|
||||||
|
|
||||||
$: shipTypes = candidateShipTypes(mass, isCarrier).map(({ name }) => name);
|
$: shipTypes = candidateShipTypes(mass, isCarrier).map(({ name }) => name);
|
||||||
|
|
||||||
$: if( shipTypes.length >0 && !shipTypes.includes(shipType)) shipType= shipTypes[0];
|
$: if (shipTypes.length > 0 && !shipTypes.includes(shipType))
|
||||||
|
shipType = shipTypes[0];
|
||||||
|
|
||||||
|
|
||||||
|
$: ship.dispatch.setShipType(shipType);
|
||||||
|
$: ship.dispatch.setShipClass(shipClass);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
@ -21,5 +21,8 @@ const ship_types = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
export function candidateShipTypes(mass = 0, carrier = false) {
|
export function candidateShipTypes(mass = 0, carrier = false) {
|
||||||
return ship_types.filter((c) => carrier == !!c.carrier).filter((c) => c.mass[0] <= mass).filter((c) => c.mass[1] >= mass);
|
return ship_types
|
||||||
|
.filter((c) => carrier == !!c.carrier)
|
||||||
|
.filter((c) => c.mass[0] <= mass)
|
||||||
|
.filter((c) => c.mass[1] >= mass);
|
||||||
}
|
}
|
||||||
|
21
src/lib/components/ShipEdit/ShipEdit.stories.svelte
Normal file
21
src/lib/components/ShipEdit/ShipEdit.stories.svelte
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<Meta title="ShipEdit" component={ShipEdit} argTypes={{}} />
|
||||||
|
|
||||||
|
<Story name="Primary" args={{}} />
|
||||||
|
|
||||||
|
<Template let:args>
|
||||||
|
<div style="width: 50em">
|
||||||
|
<ShipEdit />
|
||||||
|
</div>
|
||||||
|
</Template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import { Meta, Template, Story } from "@storybook/addon-svelte-csf";
|
||||||
|
import { action } from "@storybook/addon-actions";
|
||||||
|
|
||||||
|
import { setContext } from "svelte";
|
||||||
|
|
||||||
|
import ShipEdit from "./index.svelte";
|
||||||
|
import shipStore from "$lib/store/ship.js";
|
||||||
|
|
||||||
|
setContext("ship", shipStore());
|
||||||
|
</script>
|
12
src/lib/components/ShipEdit/index.svelte
Normal file
12
src/lib/components/ShipEdit/index.svelte
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<Identification {...$shipState.identification} {...$shipState.reqs} />
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import { getContext } from "svelte";
|
||||||
|
|
||||||
|
import Identification from "./Identification/index.svelte";
|
||||||
|
|
||||||
|
const { state: shipState } = getContext("ship");
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
</style>
|
@ -1,49 +0,0 @@
|
|||||||
<div>
|
|
||||||
<Field
|
|
||||||
label="ship class"
|
|
||||||
value={general.ship_class}
|
|
||||||
on:change={change_class}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Field label="ship type">
|
|
||||||
<select value={ship_type} on:change={change_ship_type}>
|
|
||||||
{#each ship_types as type (type)}
|
|
||||||
<option>{type}</option>
|
|
||||||
{/each}
|
|
||||||
</select>
|
|
||||||
</Field>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
import { getContext } from "svelte";
|
|
||||||
|
|
||||||
import Field from "$lib/components/Field/index.svelte";
|
|
||||||
import { candidate_ship_types } from "../../dux/ship_types";
|
|
||||||
|
|
||||||
export let ship = getContext("ship");
|
|
||||||
let general;
|
|
||||||
$: general = $ship.general;
|
|
||||||
|
|
||||||
const change_class = (event) =>
|
|
||||||
ship.dispatch(ship.actions.set_ship_class(event.target.value));
|
|
||||||
|
|
||||||
let ship_type;
|
|
||||||
$: ship_type = $ship.general.ship_type;
|
|
||||||
|
|
||||||
const change_ship_type = ({ target: { value } }) =>
|
|
||||||
ship.dispatch.set_ship_type(value);
|
|
||||||
|
|
||||||
let ship_types;
|
|
||||||
$: ship_types = candidate_ship_types(
|
|
||||||
$ship.general.mass,
|
|
||||||
$ship.carrier.bays > 0
|
|
||||||
).map(({ name }) => name);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<style>
|
|
||||||
div {
|
|
||||||
display: flex;
|
|
||||||
align-items: end;
|
|
||||||
gap: 2em;
|
|
||||||
}
|
|
||||||
</style>
|
|
@ -1,7 +1,7 @@
|
|||||||
import { Updux } from "updux";
|
import { Updux } from "updux";
|
||||||
import u from 'updeep';
|
import u from "updeep";
|
||||||
|
|
||||||
import reqs from './reqs.js';
|
import reqs from "./reqs.js";
|
||||||
|
|
||||||
const dux = new Updux({
|
const dux = new Updux({
|
||||||
subduxes: { reqs },
|
subduxes: { reqs },
|
||||||
@ -12,11 +12,11 @@ const dux = new Updux({
|
|||||||
actions: {
|
actions: {
|
||||||
setEngine: null,
|
setEngine: null,
|
||||||
setEngineReqs: null,
|
setEngineReqs: null,
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
dux.addMutation('setEngine', changes => u(changes) );
|
dux.setMutation("setEngine", (changes) => u(changes));
|
||||||
dux.addMutation('setEngineReqs', reqs => u({reqs}) );
|
dux.setMutation("setEngineReqs", (reqs) => u({ reqs }));
|
||||||
|
|
||||||
export function calcDriveReqs(shipMass, rating, advanced = false) {
|
export function calcDriveReqs(shipMass, rating, advanced = false) {
|
||||||
const mass = Math.ceil(rating * 0.05 * shipMass);
|
const mass = Math.ceil(rating * 0.05 * shipMass);
|
||||||
|
20
src/lib/shipDux/identification.js
Normal file
20
src/lib/shipDux/identification.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { Updux } from "updux";
|
||||||
|
import u from "updeep";
|
||||||
|
|
||||||
|
const dux = new Updux({
|
||||||
|
actions: {
|
||||||
|
setShipType: null,
|
||||||
|
setShipClass: null,
|
||||||
|
},
|
||||||
|
initial: {
|
||||||
|
shipType: "",
|
||||||
|
shipClass: "",
|
||||||
|
isCarrier: false,
|
||||||
|
mass: 10,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
dux.setMutation("setShipType", (shipType) => u({ shipType }));
|
||||||
|
dux.setMutation("setShipClass", (shipClass) => u({ shipClass }));
|
||||||
|
|
||||||
|
export default dux;
|
@ -1,12 +1,17 @@
|
|||||||
import Updux from "updux";
|
import { Updux } from "updux";
|
||||||
|
|
||||||
import engine from './engine.js';
|
import engine from "./engine.js";
|
||||||
|
import identification from "./identification.js";
|
||||||
|
import reqs from "./reqs.js";
|
||||||
|
|
||||||
const dux = new Updux({
|
const dux = new Updux({
|
||||||
subduxes: {
|
subduxes: {
|
||||||
|
identification,
|
||||||
engine,
|
engine,
|
||||||
},
|
},
|
||||||
|
initial: {
|
||||||
|
reqs: { cost: 0, mass: 10 },
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export default dux;
|
export default dux;
|
||||||
|
@ -2,7 +2,8 @@ import {Updux} from "updux";
|
|||||||
|
|
||||||
const dux = new Updux({
|
const dux = new Updux({
|
||||||
initial: {
|
initial: {
|
||||||
cost: 0, mass: 0
|
cost: 0,
|
||||||
|
mass: 0,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
32
src/lib/store/ship.js
Normal file
32
src/lib/store/ship.js
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { browser } from "$app/env";
|
||||||
|
import { readable, get } from "svelte/store";
|
||||||
|
import { compose, applyMiddleware } from "redux";
|
||||||
|
|
||||||
|
import shipDux from "../shipDux/index.js";
|
||||||
|
|
||||||
|
let composeEnhancers = compose;
|
||||||
|
|
||||||
|
if (browser) {
|
||||||
|
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default () => {
|
||||||
|
const duxStore = shipDux.createStore(undefined, (mw) =>
|
||||||
|
composeEnhancers(applyMiddleware(mw))
|
||||||
|
);
|
||||||
|
|
||||||
|
let previous;
|
||||||
|
const state = readable(duxStore.getState(), (set) => {
|
||||||
|
duxStore.subscribe(() => {
|
||||||
|
if (previous === duxStore.getState()) return;
|
||||||
|
previous = duxStore.getState();
|
||||||
|
console.log("Setting!", previous);
|
||||||
|
set(previous);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
dispatch: duxStore.dispatch,
|
||||||
|
state,
|
||||||
|
};
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user