From 95868b3e9f45fdf4928401d297d64c0821365727 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Tue, 9 May 2023 12:06:39 -0400 Subject: [PATCH] import ship --- src/lib/components/ShipEdit.svelte | 6 + .../components/ShipEdit/Identification.svelte | 1 + src/lib/store/ship.ts | 156 +++++++++--------- src/routes/(editor)/+layout.svelte | 18 +- src/routes/(editor)/import/+page.svelte | 102 ++++++++++++ vite.config.js | 16 +- 6 files changed, 214 insertions(+), 85 deletions(-) create mode 100644 src/routes/(editor)/import/+page.svelte diff --git a/src/lib/components/ShipEdit.svelte b/src/lib/components/ShipEdit.svelte index f221b9f..be9ee72 100644 --- a/src/lib/components/ShipEdit.svelte +++ b/src/lib/components/ShipEdit.svelte @@ -8,6 +8,8 @@ {/if} diff --git a/src/lib/components/ShipEdit/Identification.svelte b/src/lib/components/ShipEdit/Identification.svelte index a697149..3e6785a 100644 --- a/src/lib/components/ShipEdit/Identification.svelte +++ b/src/lib/components/ShipEdit/Identification.svelte @@ -28,6 +28,7 @@ import Field from "$lib/components/Field.svelte"; import { candidateShipTypes } from "./Identification/shipTypes.js"; import ShipCost from "./Identification/ShipCost.svelte"; + import shipDux from "$lib/store/ship"; export let shipClass = ""; export let shipType = ""; diff --git a/src/lib/store/ship.ts b/src/lib/store/ship.ts index 969f5cb..b7ba86c 100644 --- a/src/lib/store/ship.ts +++ b/src/lib/store/ship.ts @@ -19,117 +19,121 @@ import { adfcDux } from "./ship/weaponry/adfc"; import { weaponsDux } from "./ship/weaponry/weapons"; if (typeof process !== "undefined") { - process.env.UPDEEP_MODE = "dangerously_never_freeze"; + process.env.UPDEEP_MODE = "dangerously_never_freeze"; } const structure = new Updux({ - initialState: {}, - subduxes: { - streamlining, - cargo: cargoDux, - hull: hullDux, - screens: screensDux, - armor: armorDux, - carrier: carrierDux, - }, + initialState: {}, + subduxes: { + streamlining, + cargo: cargoDux, + hull: hullDux, + screens: screensDux, + armor: armorDux, + carrier: carrierDux, + }, }); const propulsion = new Updux({ - initialState: {}, - subduxes: { - ftl, - drive, - }, + initialState: {}, + subduxes: { + ftl, + drive, + }, }); const weaponry = new Updux({ - initialState: {}, - subduxes: { - adfc: adfcDux, - firecons: fireconsDux, - weapons: weaponsDux, - }, + initialState: {}, + subduxes: { + adfc: adfcDux, + firecons: fireconsDux, + weapons: weaponsDux, + }, }); const restore = createPayloadAction("restore"); +const importShip = + createPayloadAction("importShip"); const shipDux = new Updux({ - actions: { - restore, - }, - initialState: { - schemaVersion: "1", - }, - subduxes: { - identification, - structure, - propulsion, - carrier: carrierDux, - weaponry, - }, + actions: { + restore, + importShip, + }, + initialState: { + schemaVersion: "1", + }, + subduxes: { + identification, + structure, + propulsion, + carrier: carrierDux, + weaponry, + }, }); shipDux.addMutation(restore, (state) => () => state); +shipDux.addMutation(importShip, (state) => () => state); shipDux.addReaction((api) => { - return createSelector( - api.selectors.getFtlType, - api.selectors.getShipMass, - (type, mass) => api.dispatch.setFtlReqs(calcFtlReqs(type, mass)) - ); + return createSelector( + api.selectors.getFtlType, + api.selectors.getShipMass, + (type, mass) => api.dispatch.setFtlReqs(calcFtlReqs(type, mass)) + ); }); shipDux.addReaction((api) => { - const setShipReqs = memoize((cost, usedMass) => - api.dispatch.setShipReqs({ cost, usedMass }) - ); + const setShipReqs = memoize((cost, usedMass) => + api.dispatch.setShipReqs({ cost, usedMass }) + ); - return (state) => { - let cost = 0; - let mass = 0; + return (state) => { + let cost = 0; + let mass = 0; - let subsystems = R.values(R.omit(state, ["identification"])); + let subsystems = R.values(R.omit(state, ["identification"])); - while (subsystems.length > 0) { - const subsystem = subsystems.shift(); - if (typeof subsystem !== "object") continue; + while (subsystems.length > 0) { + const subsystem = subsystems.shift(); + if (typeof subsystem !== "object") continue; - if (subsystem.reqs) { - cost += subsystem.reqs.cost ?? 0; - mass += subsystem.reqs.mass ?? 0; - } + if (subsystem.reqs) { + cost += subsystem.reqs.cost ?? 0; + mass += subsystem.reqs.mass ?? 0; + } - subsystems.push(...Object.values(subsystem)); - } + subsystems.push(...Object.values(subsystem)); + } - if (Number.isNaN(cost)) { - console.log(state.weaponry.weapons); - throw new Error(); - } + if (Number.isNaN(cost)) { + console.log(state.weaponry.weapons); + throw new Error(); + } - setShipReqs(cost, mass); - }; + setShipReqs(cost, mass); + }; }); shipDux.addReaction((api) => - createSelector( - api.selectors.getShipMass, - (state) => state.propulsion.drive.rating, - (state) => state.propulsion.drive.advanced, - (mass, rating, advanced) => - api.dispatch.setDriveReqs(calcDriveReqs(mass, rating, advanced)) - ) + createSelector( + api.selectors.getShipMass, + (state) => state.propulsion.drive.rating, + (state) => state.propulsion.drive.advanced, + (mass, rating, advanced) => + api.dispatch.setDriveReqs(calcDriveReqs(mass, rating, advanced)) + ) ); shipDux.addReaction((api) => - createSelector( - // (state) => state, - api.selectors.getShipMass, - api.selectors.getStreamlining, - (mass, type) => { - api.dispatch.setStreamliningReqs(calcStreamliningReqs(type, mass)); - } - ) + createSelector( + // (state) => state, + api.selectors.getShipMass, + api.selectors.getStreamlining, + (mass, type) => { + api.dispatch.setStreamliningReqs(calcStreamliningReqs(type, mass)); + } + ) ); shipDux.addReaction(screensReqsReaction); diff --git a/src/routes/(editor)/+layout.svelte b/src/routes/(editor)/+layout.svelte index c7c339a..5970352 100644 --- a/src/routes/(editor)/+layout.svelte +++ b/src/routes/(editor)/+layout.svelte @@ -8,7 +8,7 @@ print print - + output export @@ -16,9 +16,25 @@ input import + + restart_alt + reset + + + diff --git a/vite.config.js b/vite.config.js index 1bb5711..4f5b830 100644 --- a/vite.config.js +++ b/vite.config.js @@ -4,14 +4,14 @@ import packageJson from "./package.json"; /** @type {import('vite').UserConfig} */ const config = { - plugins: [sveltekit()], - publicDir: "./static", - ssr: {}, - optimizeDeps: {}, - define: { - "import.meta.env.PACKAGE_VERSION": JSON.stringify(packageJson.version), - "import.meta.env.HOMEPAGE": JSON.stringify(packageJson.homepage), - }, + plugins: [sveltekit()], + // publicDir: "./static", + ssr: {}, + optimizeDeps: {}, + define: { + "import.meta.env.PACKAGE_VERSION": JSON.stringify(packageJson.version), + "import.meta.env.HOMEPAGE": JSON.stringify(packageJson.homepage), + }, }; export default config;