diff --git a/.storybook/main.js b/.storybook/main.js index 1a51bb1..9fa12a9 100644 --- a/.storybook/main.js +++ b/.storybook/main.js @@ -2,6 +2,7 @@ const path = require("path"); const preprocess = require("svelte-preprocess"); module.exports = { + // core: { builder: "storybook-builder-vite" }, staticDirs: ["../static", "../pictures"], stories: [ "../src/**/*.stories.mdx", @@ -9,13 +10,20 @@ module.exports = { "../src/**/stories.svelte", ], addons: [ - "@storybook/addon-links", "@storybook/addon-essentials", "@storybook/addon-svelte-csf", ], framework: "@storybook/svelte", svelteOptions: { preprocess: preprocess(), + }, + async viteFinal(config, { configType }) { + // customize the Vite config here + config.resolve.alias.$lib = path.resolve(__dirname, "../src/lib/"); + config.resolve.alias.$app = path.resolve(__dirname, "../fake/app/"); + + // return the customized config + return config; }, webpackFinal: async (config) => { return { diff --git a/package.json b/package.json index e40e34c..422941b 100644 --- a/package.json +++ b/package.json @@ -24,14 +24,13 @@ "eslint-plugin-svelte3": "^3.4.1", "prettier": "~2.5.1", "prettier-plugin-svelte": "^2.6.0", + "storybook-builder-vite": "0.1.17", "svelte": "^3.46.4", "vite": "^2.7.0" }, "type": "module", "dependencies": { - "@storybook/addon-actions": "^6.4.19", "@storybook/addon-essentials": "^6.4.19", - "@storybook/addon-links": "6.4.19", "@storybook/addon-svelte-csf": "^1.1.0", "@storybook/svelte": "^6.4.19", "@sveltejs/adapter-node": "^1.0.0-next.0", diff --git a/src/dux/index.js b/src/dux/index.js index 9d83cb5..2c234ca 100644 --- a/src/dux/index.js +++ b/src/dux/index.js @@ -66,22 +66,6 @@ dux.addSubscription((store) => createSelector(calc_ship_req, (reqs) => store.dispatch(set_ship_reqs(reqs))) ); -dux.addSubscription((store) => - createSelector( - (store) => store.general.mass, - (store) => store.streamlining.type, - (ship_mass, streamlining) => { - const mass = ceil( - (ship_mass * - (streamlining === "none" ? 0 : streamlining === "partial" ? 5 : 10)) / - 100 - ); - const cost = 2 * mass; - - store.dispatch(dux.actions.set_streamlining_cost_mass({ cost, mass })); - } - ) -); dux.addSubscription((store) => createSelector( @@ -145,18 +129,6 @@ dux.addSubscription((store) => ) ); -const calc_firecons_reqs = (nbr) => ({ - cost: 4 * nbr, - mass: nbr, -}); - -const set_firecons = action("set_firecons", payload()); -dux.addMutation(set_firecons, (nbr) => - u.updateIn("weaponry.firecons", { - nbr, - ...calc_firecons_reqs(nbr), - }) -); export default dux.asDux; diff --git a/src/dux/weaponry/adfc/index.js b/src/dux/weaponry/adfc/index.js deleted file mode 100644 index 485557a..0000000 --- a/src/dux/weaponry/adfc/index.js +++ /dev/null @@ -1,17 +0,0 @@ -import Updux from "updux"; -import { action, payload } from "ts-action"; -import u from "@yanick/updeep"; - -const dux = new Updux({ - initial: { - rating: 0, - cost: 0, - mass: 0, - }, -}) - -dux.addMutation( action( 'set_adfc', payload() ), rating => - u.update({ rating, mass: 2 * rating, cost: 8 * rating }) -); - -export default dux.asDux; diff --git a/src/dux/weapons/rules.js b/src/dux/weapons/rules.js deleted file mode 100644 index 77d5c4d..0000000 --- a/src/dux/weapons/rules.js +++ /dev/null @@ -1,74 +0,0 @@ - -export function weapon_cost_mass(weapon){ - let cost = 0; - let mass = 0; - - if( weapon.weapon_type === 'beam' ) { - return beam_cost_mass(weapon); - } - - if( weapon.weapon_type == 'submunition' ) { - return { mass: 1, cost: 3 }; - } - - if( weapon.weapon_type === 'pds' ) { - return { mass: 1, cost: 3 }; - } - - if( weapon.weapon_type === 'scattergun' ) { - return { mass: 1, cost: 4 }; - } - - if( weapon.weapon_type === 'needle' ) { - return { mass: 2, cost: 6 }; - } - - return { cost, mass }; -} - -const is_broadside = arcs => { - if( arcs.length !== 4 ) return false; - - // that'd be A or F - return !arcs.some( a => a.length === 1 ); -} - -function beam_cost_mass({weapon_class, arcs}) { - let mass; - if( weapon_class === 1 ) { - mass = 1; - } - - if( weapon_class == 2 ) { - mass = 2 + (arcs.length > 3 ? 1 : 0); - } - - - if( weapon_class == 3 ) { - mass = 4; - - if( is_broadside(arcs) ) { - mass += 2; - } - else { - mass += arcs.length - 1; - } - } - - if( weapon_class == 4 ) { - mass = 8; - - if( is_broadside(arcs) ) { - mass += 4; - } - else { - mass += 2*(arcs.length - 1); - } - } - - return { - mass, cost: 3 * mass - } - - -} diff --git a/src/lib/components/ShipEdit/Carrier/Carrier.stories.svelte b/src/lib/components/ShipEdit/Carrier/Carrier.stories.svelte new file mode 100644 index 0000000..b17359f --- /dev/null +++ b/src/lib/components/ShipEdit/Carrier/Carrier.stories.svelte @@ -0,0 +1,33 @@ + + + + + + + diff --git a/src/lib/components/Carrier/Squadron/index.svelte b/src/lib/components/ShipEdit/Carrier/Squadron.svelte similarity index 52% rename from src/lib/components/Carrier/Squadron/index.svelte rename to src/lib/components/ShipEdit/Carrier/Squadron.svelte index 7eb17ac..6f46f46 100644 --- a/src/lib/components/Carrier/Squadron/index.svelte +++ b/src/lib/components/ShipEdit/Carrier/Squadron.svelte @@ -1,4 +1,4 @@ - + + + + + {#each squadrons as squadron,id (id)} + + {/each} + + + + + diff --git a/src/lib/components/ShipEdit/Identification/Identification.stories.svelte b/src/lib/components/ShipEdit/Identification/Identification.stories.svelte index 67ecb68..a3d01ab 100644 --- a/src/lib/components/ShipEdit/Identification/Identification.stories.svelte +++ b/src/lib/components/ShipEdit/Identification/Identification.stories.svelte @@ -1,4 +1,4 @@ - + diff --git a/src/lib/components/ShipEdit/Identification/shipTypes.js b/src/lib/components/ShipEdit/Identification/shipTypes.js index be32552..19ad819 100644 --- a/src/lib/components/ShipEdit/Identification/shipTypes.js +++ b/src/lib/components/ShipEdit/Identification/shipTypes.js @@ -1,5 +1,5 @@ const ship_types = [ - { name: "Scout", mass: [4, 10], abbrev: "SC" }, + { name: "Scout", mass: [0, 10], abbrev: "SC" }, { name: "Courier", mass: [4, 10], abbrev: "SC" }, { name: "Corvette", mass: [8, 16], abbrev: "CT" }, { name: "Frigate", mass: [14, 28], abbrev: "FF" }, @@ -14,7 +14,7 @@ const ship_types = [ { name: "Heavy Battleship", mass: [120, 160], abbrev: "BDN" }, { name: "Dreadnought", mass: [140, 180], abbrev: "DN" }, { name: "Superdreadnought", mass: [160, 300], abbrev: "SDN" }, - { name: "Escort Carrier", mass: [60, 140], abbrev: "CVE", carrier: true }, + { name: "Escort Carrier", mass: [0, 140], abbrev: "CVE", carrier: true }, { name: "Light Carrier", mass: [120, 180], abbrev: "CVL", carrier: true }, { name: "Heavy Carrier", mass: [160, 300], abbrev: "CVH", carrier: true }, { name: "Attack Carrier", mass: [150, 300], abbrev: "CVA", carrier: true }, diff --git a/src/lib/components/ShipEdit/Propulsion/Drive/index.svelte b/src/lib/components/ShipEdit/Propulsion/Drive/index.svelte index 9af3248..a092b2d 100644 --- a/src/lib/components/ShipEdit/Propulsion/Drive/index.svelte +++ b/src/lib/components/ShipEdit/Propulsion/Drive/index.svelte @@ -19,7 +19,6 @@ const ship = getContext("ship"); - console.log( ship.dispatch ) $: ship.dispatch.setDrive({ rating, advanced }); diff --git a/src/lib/components/ShipEdit/Structure/Armor.svelte b/src/lib/components/ShipEdit/Structure/Armor.svelte new file mode 100644 index 0000000..de581f2 --- /dev/null +++ b/src/lib/components/ShipEdit/Structure/Armor.svelte @@ -0,0 +1,41 @@ + +
+
+ + + +
+ +
+ {#each layers as rating,i (i)} + + {/each} +
+
+
+ + + + diff --git a/src/lib/components/ShipEdit/Structure/Armor/Layer.svelte b/src/lib/components/ShipEdit/Structure/Armor/Layer.svelte new file mode 100644 index 0000000..b6dab9e --- /dev/null +++ b/src/lib/components/ShipEdit/Structure/Armor/Layer.svelte @@ -0,0 +1,22 @@ + + + + + + + diff --git a/src/lib/components/ShipEdit/Structure/Cargo.svelte b/src/lib/components/ShipEdit/Structure/Cargo.svelte new file mode 100644 index 0000000..6bb00eb --- /dev/null +++ b/src/lib/components/ShipEdit/Structure/Cargo.svelte @@ -0,0 +1,25 @@ + + + + + + + + + diff --git a/src/lib/components/ShipEdit/Structure/Streamlining.svelte b/src/lib/components/ShipEdit/Structure/Streamlining.svelte new file mode 100644 index 0000000..f610be3 --- /dev/null +++ b/src/lib/components/ShipEdit/Structure/Streamlining.svelte @@ -0,0 +1,41 @@ + + +
+ + + +
+
+
+ + + + diff --git a/src/lib/components/ShipEdit/Structure/index.svelte b/src/lib/components/ShipEdit/Structure/index.svelte index 0bfd6bf..f6c583e 100644 --- a/src/lib/components/ShipEdit/Structure/index.svelte +++ b/src/lib/components/ShipEdit/Structure/index.svelte @@ -1,15 +1,24 @@
+ + +
diff --git a/src/lib/components/ShipEdit/Weaponry/ADFC.svelte b/src/lib/components/ShipEdit/Weaponry/ADFC.svelte new file mode 100644 index 0000000..93da8e6 --- /dev/null +++ b/src/lib/components/ShipEdit/Weaponry/ADFC.svelte @@ -0,0 +1,18 @@ + + + + + + + diff --git a/src/lib/components/ShipEdit/Weaponry/AddWeapon.svelte b/src/lib/components/ShipEdit/Weaponry/AddWeapon.svelte new file mode 100644 index 0000000..b84e43a --- /dev/null +++ b/src/lib/components/ShipEdit/Weaponry/AddWeapon.svelte @@ -0,0 +1,22 @@ + + + + + + + diff --git a/src/lib/components/ShipEdit/Weaponry/Firecons.svelte b/src/lib/components/ShipEdit/Weaponry/Firecons.svelte new file mode 100644 index 0000000..24ffc9c --- /dev/null +++ b/src/lib/components/ShipEdit/Weaponry/Firecons.svelte @@ -0,0 +1,21 @@ + + + + + + + + + diff --git a/src/lib/components/Weapons/Arc.svelte b/src/lib/components/ShipEdit/Weaponry/Weapon/Arc.svelte similarity index 100% rename from src/lib/components/Weapons/Arc.svelte rename to src/lib/components/ShipEdit/Weaponry/Weapon/Arc.svelte diff --git a/src/lib/components/Weapon/Arcs/index.svelte b/src/lib/components/ShipEdit/Weaponry/Weapon/Arcs.svelte similarity index 92% rename from src/lib/components/Weapon/Arcs/index.svelte rename to src/lib/components/ShipEdit/Weaponry/Weapon/Arcs.svelte index 682bc8a..6657c21 100644 --- a/src/lib/components/Weapon/Arcs/index.svelte +++ b/src/lib/components/ShipEdit/Weaponry/Weapon/Arcs.svelte @@ -12,7 +12,7 @@ diff --git a/src/lib/components/ShipEdit/Weaponry/Weapon/Needle.svelte b/src/lib/components/ShipEdit/Weaponry/Weapon/Needle.svelte new file mode 100644 index 0000000..cdb17fd --- /dev/null +++ b/src/lib/components/ShipEdit/Weaponry/Weapon/Needle.svelte @@ -0,0 +1,27 @@ + + + click_arc(detail)} /> + + + + diff --git a/src/lib/components/ShipEdit/Weaponry/Weapon/PDS.svelte b/src/lib/components/ShipEdit/Weaponry/Weapon/PDS.svelte new file mode 100644 index 0000000..6c9c9ef --- /dev/null +++ b/src/lib/components/ShipEdit/Weaponry/Weapon/PDS.svelte @@ -0,0 +1 @@ + diff --git a/src/lib/components/Weapon/Scattergun/index.svelte b/src/lib/components/ShipEdit/Weaponry/Weapon/Scattergun.svelte similarity index 100% rename from src/lib/components/Weapon/Scattergun/index.svelte rename to src/lib/components/ShipEdit/Weaponry/Weapon/Scattergun.svelte diff --git a/src/lib/components/ShipEdit/Weaponry/Weapon/Submunition.svelte b/src/lib/components/ShipEdit/Weaponry/Weapon/Submunition.svelte new file mode 100644 index 0000000..070b55a --- /dev/null +++ b/src/lib/components/ShipEdit/Weaponry/Weapon/Submunition.svelte @@ -0,0 +1,19 @@ + + + click_arc(detail)} /> + + diff --git a/src/lib/components/ShipEdit/Weaponry/Weapon/index.svelte b/src/lib/components/ShipEdit/Weaponry/Weapon/index.svelte new file mode 100644 index 0000000..dc8965a --- /dev/null +++ b/src/lib/components/ShipEdit/Weaponry/Weapon/index.svelte @@ -0,0 +1,121 @@ + +
+ + + +
+
+ + + + diff --git a/src/lib/components/ShipEdit/Weaponry/Weaponry.stories.svelte b/src/lib/components/ShipEdit/Weaponry/Weaponry.stories.svelte new file mode 100644 index 0000000..4072200 --- /dev/null +++ b/src/lib/components/ShipEdit/Weaponry/Weaponry.stories.svelte @@ -0,0 +1,34 @@ + + + + + + + diff --git a/src/lib/components/ShipEdit/Weaponry/index.svelte b/src/lib/components/ShipEdit/Weaponry/index.svelte new file mode 100644 index 0000000..d9b168b --- /dev/null +++ b/src/lib/components/ShipEdit/Weaponry/index.svelte @@ -0,0 +1,34 @@ +
+ + + + + + + {#each weapons as weapon (weapon.id)} + + {/each} + +
+ + + + diff --git a/src/lib/components/ShipEdit/index.svelte b/src/lib/components/ShipEdit/index.svelte index 89dec7a..9278810 100644 --- a/src/lib/components/ShipEdit/index.svelte +++ b/src/lib/components/ShipEdit/index.svelte @@ -8,6 +8,10 @@ + + + + diff --git a/src/lib/components/Weapon/Needle/index.svelte b/src/lib/components/Weapon/Needle/index.svelte deleted file mode 100644 index dff20ee..0000000 --- a/src/lib/components/Weapon/Needle/index.svelte +++ /dev/null @@ -1,35 +0,0 @@ - - click_arc(detail)} /> - - - - diff --git a/src/lib/shipDux/carrier.js b/src/lib/shipDux/carrier.js new file mode 100644 index 0000000..6d8d145 --- /dev/null +++ b/src/lib/shipDux/carrier.js @@ -0,0 +1,76 @@ +import { Updux } from "updux"; +import u from "updeep"; +import _ from 'lodash'; + +import reqs from "./reqs.js"; + +const dux = new Updux({ + subduxes: { reqs }, + initial: { + bays: 0, + squadrons: [], + }, + actions: { + setCarrierBays: null, + setSquadronType: null, + } +}); + +dux.setMutation( 'setCarrierBays', bays => u({bays, reqs: + calcBaysReqs(bays), + squadrons: adjustSquadrons(bays), +}) ); + +dux.setMutation('setSquadronType', ({type, id}) => state => { + + return u.updateIn(['squadrons', id-1], { + type, + reqs: squadronReqs(type) + }, state ) + +} ); + +export const squadronTypes= [ + { type: "standard", cost: 3 }, + { type: "fast", cost: 4 }, + { type: "heavy", cost: 5 }, + { type: "interceptor", cost: 3 }, + { type: "attack", cost: 4 }, + { type: "long range", cost: 4 }, + { type: "torpedo", cost: 6 }, +]; + +function squadronReqs(type) { + return { mass: 6, cost: 6 * squadronTypes.find( s => s.type === type )?.cost } +} + + +const adjustSquadrons = bays => squadrons => { + if( squadrons.length > bays ) { + squadrons = squadrons.slice(0,bays); + } + + if( squadrons.length < bays ) { + squadrons = [ ...squadrons, ..._.times( + bays - squadrons.length, () => ({ + type: squadronTypes[0].type, + reqs: { + cost: 6 * squadronTypes[0].cost, + mass: 6, + }, + }) + )]; + } + + return squadrons; + +} + +function calcBaysReqs(bays) { + return { + mass: 9 * bays, + cost: 18 * bays, + } +} + +export default dux; diff --git a/src/lib/shipDux/identification.js b/src/lib/shipDux/identification.js index 50abc17..71e4085 100644 --- a/src/lib/shipDux/identification.js +++ b/src/lib/shipDux/identification.js @@ -1,10 +1,13 @@ import { Updux } from "updux"; import u from "updeep"; +import carrier from './carrier.js'; + const dux = new Updux({ actions: { setShipType: null, setShipClass: null, + setCarrierBays: carrier.actions.setCarrierBays, }, initial: { shipType: "", @@ -16,5 +19,8 @@ const dux = new Updux({ dux.setMutation("setShipType", (shipType) => u({ shipType })); dux.setMutation("setShipClass", (shipClass) => u({ shipClass })); +dux.setMutation('setCarrierBays', (bays) => u({ + isCarrier: bays > 0, +})) export default dux; diff --git a/src/lib/shipDux/index.js b/src/lib/shipDux/index.js index 4b74aab..bd20867 100644 --- a/src/lib/shipDux/index.js +++ b/src/lib/shipDux/index.js @@ -1,30 +1,59 @@ import { Updux } from "updux"; -import u from 'updeep'; +import u from "updeep"; import propulsion from "./propulsion/index.js"; import identification from "./identification.js"; -import { calculateDriveReqs } from './propulsion/drive.js'; -import { ftlReqsReaction } from './propulsion/ftl.js'; -import structure from './structure/index.js'; -import { screenReqsReaction, screensReqsReaction } from './structure/screens.js' +import { calculateDriveReqs } from "./propulsion/drive.js"; +import { ftlReqsReaction } from "./propulsion/ftl.js"; +import structure from "./structure/index.js"; +import carrier from "./carrier.js"; +import weaponry from "./weaponry/index.js"; +import { screensReqsReaction } from "./structure/screens.js"; const dux = new Updux({ - subduxes: { - identification, - propulsion, - structure - }, - initial: { - reqs: { cost: 0, mass: 10, usedMass: 0 }, - }, + subduxes: { + identification, + propulsion, + structure, + carrier, + weaponry, + }, + initial: { + reqs: { cost: 0, mass: 10, usedMass: 0 }, + }, actions: { - } + setShipReqs: null, + }, }); -dux.setMutation( 'setShipMass', mass => u({reqs: {mass}}) ); +dux.setMutation("setShipMass", (mass) => u({ reqs: { mass } })); +dux.setMutation('setShipReqs', reqs => u({reqs})); + +dux.addReaction(calculateDriveReqs); +dux.addReaction(ftlReqsReaction); +dux.addReaction(screensReqsReaction); + +dux.addReaction( (store) => (state) => { + let cost = 0; + let mass = 0; + + let subsystems = Object.values(state); + + while(subsystems.length>0) { + const subsystem = subsystems.shift(); + if( typeof subsystem !== 'object' ) continue; + + if( subsystem.reqs ) { + cost += subsystem.reqs.cost; + mass += subsystem.reqs.mass; + } + + subsystems.push( ...Object.values(subsystem)); + } + + store.dispatch.setShipReqs({cost,usedMass: mass}); + +}); -dux.addReaction( calculateDriveReqs ); -dux.addReaction( ftlReqsReaction ); -dux.addReaction( screenReqsReaction ); export default dux; diff --git a/src/lib/shipDux/structure/armor.js b/src/lib/shipDux/structure/armor.js new file mode 100644 index 0000000..9b43574 --- /dev/null +++ b/src/lib/shipDux/structure/armor.js @@ -0,0 +1,50 @@ +import { Updux } from "updux"; +import u from "updeep"; + +import reqs from "../reqs.js"; + +const dux = new Updux({ + subduxes: { + reqs, + }, + initial: { + layers: [], + }, + actions: { + setArmorLayers: null, + setArmorRating: null, + }, +}); +export default dux; + +dux.setMutation('setArmorRating', ({layer, rating}) => state => { + let layers = [ ...state.layers ].map( (v,k) => k === layer-1 ? rating : v ); + + return { layers, reqs: calcArmorReqs(layers) } +} ); + +dux.setMutation( 'setArmorLayers', nbrLayers => state => { + + let layers = [...state.layers]; + + if( nbrLayers < state.layers.length ) + layers = [ ...state.layers ].slice(0,nbrLayers); + + while( layers.length < nbrLayers ) { + layers.push(0); + } + + return { + layers, + reqs: calcArmorReqs(layers), + } +}); + +function calcArmorReqs(layers) { + const mass = 2* layers.reduce( (a,b) => a+ b,0 ); + const cost = 2* layers.map( (v,k) => v * (k+1) ).reduce( (a,b) => a+ b,0 ); + + return { + mass, cost + } +} diff --git a/src/lib/shipDux/structure/cargo.js b/src/lib/shipDux/structure/cargo.js new file mode 100644 index 0000000..204d94b --- /dev/null +++ b/src/lib/shipDux/structure/cargo.js @@ -0,0 +1,24 @@ +import { Updux } from "updux"; +import u from 'updeep'; +import { createSelector } from 'reselect'; + +import reqs from '../reqs.js'; + +const dux = new Updux({ + subduxes: { + reqs + }, + initial: { + space: 0, + }, + actions: { + setCargo: null, + } +}); +export default dux; + +dux.setMutation('setCargo', space => u({ + space, + reqs: { mass: space } +})); + diff --git a/src/lib/shipDux/structure/index.js b/src/lib/shipDux/structure/index.js index a2f8b4d..c5fe3cb 100644 --- a/src/lib/shipDux/structure/index.js +++ b/src/lib/shipDux/structure/index.js @@ -2,8 +2,12 @@ import { Updux } from 'updux'; import hull from './hull.js'; import screens from './screens.js'; +import cargo from './cargo.js'; +import armor from './armor.js'; +import streamlining from './streamlining.js'; const dux = new Updux({ - subduxes: { hull, screens } + subduxes: { hull, screens, cargo, streamlining, armor } }); export default dux; + diff --git a/src/lib/shipDux/structure/screens.js b/src/lib/shipDux/structure/screens.js index 49e3ed4..068e653 100644 --- a/src/lib/shipDux/structure/screens.js +++ b/src/lib/shipDux/structure/screens.js @@ -21,7 +21,7 @@ export default dux; dux.setMutation('setScreens', payload => u(payload)); dux.setMutation('setScreensReqs', reqs => u({reqs})); -export const screenReqsReaction = store => createSelector( +export const screensReqsReaction = store => createSelector( (ship) => ship.reqs.mass, (ship) => ship.structure.screens.standard, (ship) => ship.structure.screens.advanced, diff --git a/src/lib/shipDux/structure/streamlining.js b/src/lib/shipDux/structure/streamlining.js new file mode 100644 index 0000000..aca39e6 --- /dev/null +++ b/src/lib/shipDux/structure/streamlining.js @@ -0,0 +1,32 @@ +import { Updux } from "updux"; +import u from "updeep"; + +import reqs from "../reqs.js"; + +const dux = new Updux({ + subduxes: { + reqs, + }, + initial: { + type: "none", + }, + actions: { + setStreamlining: null, + }, +}); +export default dux; + +dux.setMutation("setStreamlining", ({ shipMass, type }) => + u({ + type, + reqs: calcStreamliningReqs({ shipMass, type }), + }) +); + +function calcStreamliningReqs({ shipMass, type }) { + const mass = Math.ceil( + (shipMass * (type === "none" ? 0 : type === "partial" ? 5 : 10)) / 100 + ); + + return { mass, cost: 2 * mass }; +} diff --git a/src/lib/shipDux/weaponry/index.js b/src/lib/shipDux/weaponry/index.js new file mode 100644 index 0000000..7cfbed7 --- /dev/null +++ b/src/lib/shipDux/weaponry/index.js @@ -0,0 +1,46 @@ +import { Updux } from "updux"; +import u from "updeep"; + +import weapons from './weapons.js'; + +const reqs = { cost: 0, mass: 0 }; + +const dux = new Updux({ + subduxes: { weapons }, + initial: { + firecons: { + stations: 0, + reqs, + }, + adfc: { rating: 0, reqs }, + }, + actions: { + setADFC: null, + setFirecons: null, + }, +}); + +dux.setMutation("setFirecons", (stations) => + u({ + firecons: { + stations, + reqs: { + cost: 4 * stations, + mass: stations, + }, + }, + }) +); +dux.setMutation("setADFC", (rating) => + u({ + adfc: { + rating, + reqs: { + cost: 8 * rating, + mass: 2 * rating, + }, + }, + }) +); + +export default dux; diff --git a/src/lib/shipDux/weaponry/weapons.js b/src/lib/shipDux/weaponry/weapons.js new file mode 100644 index 0000000..6aa6167 --- /dev/null +++ b/src/lib/shipDux/weaponry/weapons.js @@ -0,0 +1,122 @@ +import { Updux } from "updux"; +import u from "updeep"; + +const reqs = { cost: 0, mass: 0 }; + +export const weaponTypes = [ + { name: 'beam', type: 'beam', reqs: beam_cost_mass, initial: { + weaponClass: 1 + }}, + { name: 'submunition pack', type: 'submunition', reqs: { mass:1, cost:3 }, + initial: { arc: 'F' } + }, + { name: 'point defence system', type: 'pds', reqs: {mass:1,cost:3}, initial: {}}, + { name: 'scattergun', type: 'scattergun', reqs: { mass:1,cost:4 }, initial: {}}, + { name: 'needle weapon', type: 'needle', reqs: { mass: 2, cost: 6 }, + initial: { arc: 'F' }}, +]; + +const dux = new Updux({ + initial: [], + actions: { + addWeapon: null, + removeWeapon: null, + setWeapon: null, + }, +}); + +dux.setMutation('setWeapon', ({id,...rest}) => state => { +console.log(id,rest,state); + state = u.map( u.if( (w) => w.id === id, + weapon => { + return { + id, + ...rest, + reqs: weaponReqs(rest), + } + } ), state ); + console.log(state); + return state; + +} +); + +dux.setMutation('removeWeapon', id => state => [ + ...state.filter( (w) => w.id !== id ) +]); + +dux.setMutation('addWeapon', type => state => { + const initial = weaponTypes.find(w => w.type === type ).initial; + return [ + ...state, + { + id: state.length === 0 ? 1 : state[state.length -1]+1, + type, + reqs: weaponReqs({type,...initial}), + ...initial, + } + ] +}); + +function weaponReqs(weapon) { + + const {reqs} = weaponTypes.find( wt => wt.type === weapon.type ) ||{}; + + if(!reqs) return {}; + + if( typeof reqs === 'function' ) return reqs(weapon); + + return reqs; +} + +const isBroadside = arcs => { + if( arcs.length !== 4 ) return false; + + // that'd be A or F + return !arcs.some( a => a.length === 1 ); +} + +function beam_cost_mass({weaponClass, arcs}) { + console.log({weaponClass,arcs}) + let mass; + + if( weaponClass === 1 ) { + mass = 1; + } + + if( weaponClass === 2 ) { + mass = 2 + (arcs.length > 3 ? 1 : 0); + } + + + if( weaponClass == 3 ) { + mass = 4; + + if( isBroadside(arcs) ) { + mass += 2; + } + else { + mass += arcs.length - 1; + } + } + + if( weaponClass == 4 ) { + mass = 8; + + if( isBroadside(arcs) ) { + mass += 4; + } + else { + mass += 2*(arcs.length - 1); + } + } + + return { + mass, cost: 3 * mass + } + + +} + + +export default dux; diff --git a/src/lib/store/ship.js b/src/lib/store/ship.js index dcfbe80..2e507f1 100644 --- a/src/lib/store/ship.js +++ b/src/lib/store/ship.js @@ -1,5 +1,5 @@ import { browser } from "$app/env"; -import { readable, get } from "svelte/store"; +import { readable, get, derived } from "svelte/store"; import { compose, applyMiddleware } from "redux"; import shipDux from "../shipDux/index.js"; @@ -20,7 +20,6 @@ export default () => { duxStore.subscribe(() => { if (previous === duxStore.getState()) return; previous = duxStore.getState(); - console.log("Setting!", previous); set(previous); }); }); @@ -28,5 +27,6 @@ export default () => { return { dispatch: duxStore.dispatch, state, + shipMass: derived( state, state => state.reqs.mass ) }; };