From b82bbf7cff5e9a4898be2df0f830da57e12bc2da Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Mon, 27 Jul 2020 13:42:19 -0400 Subject: [PATCH] add carrier --- .storybook/main.js | 1 + package.json | 1 + src/App.svelte | 2 + src/components/Carrier/Squadron/index.svelte | 38 +++++++++++++ src/components/Carrier/index.svelte | 37 +++++++++++++ src/components/Carrier/stories.js | 20 +++++++ src/components/Identification.svelte | 2 +- src/components/Streamlining/index.svelte | 1 - src/dux/carrier/index.js | 58 ++++++++++++++++++++ src/dux/carrier/squadron_types.js | 9 +++ src/dux/index.js | 11 +++- src/dux/ship_types.js | 2 +- 12 files changed, 176 insertions(+), 6 deletions(-) create mode 100644 src/components/Carrier/Squadron/index.svelte create mode 100644 src/components/Carrier/index.svelte create mode 100644 src/components/Carrier/stories.js create mode 100644 src/dux/carrier/index.js create mode 100644 src/dux/carrier/squadron_types.js diff --git a/.storybook/main.js b/.storybook/main.js index d8e5914..444611d 100644 --- a/.storybook/main.js +++ b/.storybook/main.js @@ -1,6 +1,7 @@ const path = require('path'); module.exports = { + addons: ['@storybook/addon-actions/register'], stories: [ '../src/**/*stories.js' ], webpackFinal: (config) => { config.resolve.alias['~'] = path.resolve(__dirname, '../src/'); diff --git a/package.json b/package.json index 24bcd4e..2c42349 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "@rollup/plugin-alias": "^3.1.1", "@rollup/plugin-commonjs": "^12.0.0", "@rollup/plugin-node-resolve": "^8.0.0", + "@storybook/addon-actions": "^5.3.19", "eslint": "7.4.0", "eslint-config-prettier": "6.11.0", "eslint-plugin-babel": "5.3.1", diff --git a/src/App.svelte b/src/App.svelte index a3a1d01..bfe5e8d 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -15,6 +15,7 @@ import Weapon from '~C/Weapon'; import Cargo from '~C/Cargo/index.svelte'; import Streamlining from '~C/Streamlining/index.svelte'; + import Carrier from '~C/Carrier'; const ship = shipStore(); @@ -93,6 +94,7 @@ + diff --git a/src/components/Carrier/Squadron/index.svelte b/src/components/Carrier/Squadron/index.svelte new file mode 100644 index 0000000..d0dd47a --- /dev/null +++ b/src/components/Carrier/Squadron/index.svelte @@ -0,0 +1,38 @@ + + + + + + + + + diff --git a/src/components/Carrier/index.svelte b/src/components/Carrier/index.svelte new file mode 100644 index 0000000..76032fa --- /dev/null +++ b/src/components/Carrier/index.svelte @@ -0,0 +1,37 @@ +
+ + + + + + + {#each squadrons as squad (squad.id)} + + {/each} + +
+ + + + diff --git a/src/components/Carrier/stories.js b/src/components/Carrier/stories.js new file mode 100644 index 0000000..db72155 --- /dev/null +++ b/src/components/Carrier/stories.js @@ -0,0 +1,20 @@ +import { action } from '@storybook/addon-actions'; + +import Carrier from './index.svelte'; + +export default { + title: 'Carrier', +}; + +export const basic = () => ({ + Component: Carrier, + props: { + bays: 3, + squadrons: [ + { id: 1, type: "standard", ftl: false, nbr_fighters: 6 }, + { id: 2, type: "fast", ftl: false, nbr_fighters: 6 }, + { id: 3, type: "none", ftl: false, nbr_fighters: 6 }, + ], + ship_change: action('ship_change'), + }, +}) diff --git a/src/components/Identification.svelte b/src/components/Identification.svelte index 5c860b9..b02b1ea 100644 --- a/src/components/Identification.svelte +++ b/src/components/Identification.svelte @@ -34,7 +34,7 @@ ship.dispatch.set_ship_type(value); let ship_types; - $: ship_types = candidate_ship_types($ship.general.mass,false).map( + $: ship_types = candidate_ship_types($ship.general.mass,$ship.carrier.bays>0).map( ({name}) => name ); diff --git a/src/components/Streamlining/index.svelte b/src/components/Streamlining/index.svelte index 63c87ff..3f386d5 100644 --- a/src/components/Streamlining/index.svelte +++ b/src/components/Streamlining/index.svelte @@ -19,7 +19,6 @@ import {getContext } from 'svelte'; - export let type = 'none'; export let cost = 0; export let mass = 0; diff --git a/src/dux/carrier/index.js b/src/dux/carrier/index.js new file mode 100644 index 0000000..22d8d2f --- /dev/null +++ b/src/dux/carrier/index.js @@ -0,0 +1,58 @@ +import Updux from "updux"; +import { action, payload } from "ts-action"; +import u from "updeep"; +import _ from 'lodash'; +import { createSelector } from "reselect"; + +import squadron_types from './squadron_types'; + +const uu = transform => state => transform(state)(state); + +const dux = new Updux({ + initial: { + bays: 0, + cost: 0, + mass: 0, + squadrons: [], + }, +}); + +const set_squadron = action('set_squadron',payload()); + +dux.addMutation(set_squadron, ({id,type}) => u({ squadrons: u.map( + u.if(_.matches({id}), u({type, cost: 6 * _.find(squadron_types,{type}).cost, mass: 6 })) +)})); + +const set_carrier_bays = action('set_carrier_bays', payload() ); + +dux.addMutation( set_carrier_bays, bays => state => { + state = u({ + bays, + mass: 1.5*6*bays, + cost: 3 * 1.5 * 6 * bays, + })(state); + + if( state.squadrons.length > bays ) { + state = u({ + squadrons: squadrons => squadrons.slice(0,bays) + }, state) + } + + if( state.squadrons.length < bays ) { + state = u({ + squadrons: squadrons => [ ...squadrons, ..._.times( + bays - state.squadrons.length, i => ({ + id: 1 + i + state.squadrons.length, + cost: 6 * squadron_types[0].cost, + mass: 6, + type: squadron_types[0].type, + }) + )] }, state) + } + + return state; + +}); + + +export default dux.asDux; diff --git a/src/dux/carrier/squadron_types.js b/src/dux/carrier/squadron_types.js new file mode 100644 index 0000000..dc3c4d3 --- /dev/null +++ b/src/dux/carrier/squadron_types.js @@ -0,0 +1,9 @@ +export default [ + { 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 }, +]; diff --git a/src/dux/index.js b/src/dux/index.js index 38945b5..7b1d336 100644 --- a/src/dux/index.js +++ b/src/dux/index.js @@ -12,6 +12,7 @@ import { candidate_ship_types } from './ship_types'; import structure from './structure'; import cargo from './cargo'; import streamlining from './streamlining'; +import carrier from './carrier'; import { ceil } from '~/dux/utils'; const set_ship_mass = action("set_ship_mass", payload()); @@ -36,7 +37,7 @@ const initial = { }; const dux = new Updux({ - subduxes: { ftl, engine, weaponry, structure, cargo, streamlining }, + subduxes: { ftl, engine, weaponry, structure, cargo, streamlining, carrier }, initial }); @@ -85,8 +86,12 @@ dux.addSubscription((store) => createSelector( store => store.general.mass, store => store.general.ship_type, - (mass,type) => { - const candidates = candidate_ship_types(mass,false); + store => store.carrier.bays, + (mass,type,bays) => { + console.log({bays}); + const candidates = candidate_ship_types(mass,bays > 0); + + console.log({candidates}); if( candidates.length === 0 ) return; if( candidates.find( ({name}) => name === type ) ) return; diff --git a/src/dux/ship_types.js b/src/dux/ship_types.js index 21eb107..f37b43b 100644 --- a/src/dux/ship_types.js +++ b/src/dux/ship_types.js @@ -21,6 +21,6 @@ const ship_types = [ ]; export function candidate_ship_types(mass = 0, carrier = false) { - console.log(mass); + console.log({carrier}); return ship_types.filter((c) => carrier == !!c.carrier).filter((c) => c.mass[0] <= mass).filter((c) => c.mass[1] >= mass); }