diff --git a/src/app/components/CostSection.jsx b/src/app/components/CostSection.jsx index 70123d1d..46dd9252 100644 --- a/src/app/components/CostSection.jsx +++ b/src/app/components/CostSection.jsx @@ -2,7 +2,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import cn from 'classnames'; import Persist from '../stores/Persist'; -import Ship from '../shipyard/Ship'; +import { Factory, Ship } from 'ed-forge'; import { Insurance } from '../shipyard/Constants'; import TranslatedComponent from './TranslatedComponent'; import { ShoppingIcon } from './SvgIcons'; @@ -28,13 +28,15 @@ export default class CostSection extends TranslatedComponent { super(props); autoBind(this); + const { ship, buildName } = props; + this.shipType = ship.getShipType(); this.state = { - retrofitName: this._defaultRetrofitName(props.ship, props.buildName), + retrofitName: Persist.hasBuild(ship.getShipType(), buildName) ? buildName : null, shipDiscount: Persist.getShipDiscount(), moduleDiscount: Persist.getModuleDiscount(), insurance: Insurance[Persist.getInsurance()], tab: Persist.getCostTab(), - buildOptions: Persist.getBuildsNamesFor(props.ship.getShipType()), + buildOptions: Persist.getBuildsNamesFor(ship.getShipType()), predicate: 'cr', desc: true, excluded: {}, @@ -43,36 +45,15 @@ export default class CostSection extends TranslatedComponent { /** * Create a ship instance to base/reference retrofit changes from - * @param {string} ship Ship - * @param {string} name Build name - * @param {Ship} retrofitShip Existing retrofit ship * @return {Ship} Retrofit ship */ - _buildRetrofitShip(ship, name, retrofitShip) { - // TODO: once ships have been persisted, this can be fixed - return ship; - // let data = Ships[shipId]; // Retrieve the basic ship properties, slots and defaults - - // if (!retrofitShip) { // Don't create a new instance unless needed - // retrofitShip = new Ship(shipId, data.properties, data.slots); // Create a new Ship for retrofit comparison - // } - - // if (Persist.hasBuild(shipId, name)) { - // retrofitShip.buildFrom(Persist.getBuild(shipId, name)); // Populate modules from existing build - // } else { - // retrofitShip.buildWith(data.defaults); // Populate with default components - // } - // return retrofitShip; - } - - /** - * Get the default retrofit build name if it exists - * @param {string} ship Ship - * @param {string} name Build name - * @return {string} Build name or null - */ - _defaultRetrofitName(ship, name) { - return Persist.hasBuild(ship.getShipType(), name) ? name : null; + _buildRetrofitShip() { + const { retrofitName } = this.state; + if (Persist.hasBuild(this.shipType, retrofitName)) { + return new Ship(Persist.getBuild(this.shipType, retrofitName)); + } else { + return Factory.newShip(this.shipType); + } } /** @@ -220,8 +201,8 @@ export default class CostSection extends TranslatedComponent { */ _retrofitInfo() { const { ship } = this.props; - const { desc, moduleDiscount, predicate, retrofitName } = this.state; - const retrofitShip = this._buildRetrofitShip(ship, retrofitName); + const { desc, moduleDiscount, predicate, retrofitName, excluded } = this.state; + const retrofitShip = this._buildRetrofitShip(); const currentModules = ship.getModules(); const oldModules = retrofitShip.getModules(); @@ -229,37 +210,38 @@ export default class CostSection extends TranslatedComponent { const sellModules = differenceBy(oldModules, currentModules, (m) => m.getItem()); let modules = []; - for (let m of buyModules) { - const key = `buy_${m.getSlot()}`; + let totalCost = 0; + const addModule = (m, costFactor) => { + const key = `${m.getItem()}@${m.getSlot()}`; + const cost = costFactor * m.readMeta('cost') * (1 - moduleDiscount); modules.push({ - key, - cost: m.readMeta('cost') * (1 - moduleDiscount), - buyRating: m.getClassRating(), - buyItem: m.readMeta('type'), + key, cost, + rating: m.getClassRating(), + item: m.readMeta('type'), }); + if (!excluded[key]) { + totalCost += cost; + } + }; + for (let m of buyModules) { + addModule(m, 1); } for (let m of sellModules) { - const key = `sell_${m.getSlot()}`; - modules.push({ - key, - cost: -1 * m.readMeta('cost') * moduleDiscount, - sellRating: m.getClassRating(), - sellItem: m.readMeta('type'), - }); + addModule(m, -1); } let _sortF = undefined; switch (predicate) { - case 'cr': _sortF = (o) => o.cost; + case 'cr': _sortF = (o) => o.cost; break; case 'm': - default: _sortF = (o) => o.buyItem || o.sellItem; + default: _sortF = (o) => o.item; break; }; modules = sortBy(modules, _sortF); if (desc) { reverse(modules); } - return modules; + return [totalCost, modules]; } /** @@ -267,46 +249,18 @@ export default class CostSection extends TranslatedComponent { * @return {React.Component} Tab contents */ _retrofitTab() { - let { excluded, moduleDiscount, retrofitName } = this.state; + let { buildOptions, excluded, moduleDiscount, retrofitName } = this.state; const { termtip, tooltip } = this.context; let { translate, formats, units } = this.context.language; let int = formats.int; - let options = []; - - for (let opt of this.state.buildOptions) { - options.push(); - } - - const retrofitInfo = this._retrofitInfo(); - const retrofitTotal = 0; - let rows = []; - for (let i of retrofitInfo) { - const disabled = excluded[i.key]; - rows.push( - this._toggleExcluded(i.key)}> - {i.buyRating} - {translate(i.buyItem)} - {i.sellRating} - {translate(i.sellItem)} - - {int(i.cost)}{units.CR} - - - ); - retrofitTotal += disabled ? 0 : i.cost; - } - if (!rows.length) { - rows = {translate('PHRASE_NO_RETROCH')}; - } + const [retrofitTotal, retrofitInfo] = this._retrofitInfo(); return
- - + - {rows} + {retrofitInfo.length ? + retrofitInfo.map((info) => ( + this._toggleExcluded(info.key)}> + + + + + )) : ( + + )} - + - +
this._sortBy('m')}>{translate('sell')} this._sortBy('m')}>{translate('buy')} this._sortBy('m')}>{translate('module')} this._sortBy('cr')}> {translate('net cost')} {moduleDiscount ? {`[${translate('modules')} -${formats.pct(moduleDiscount)}]`} : null} @@ -314,20 +268,33 @@ export default class CostSection extends TranslatedComponent {
{info.rating}{translate(info.item)} + {int(info.cost)}{units.CR} +
{translate('PHRASE_NO_RETROCH')}
{translate('cost')}{translate('cost')} 0 ? 'warning' : 'secondary-disabled')} style={{ borderBottom:'none' }}> {int(retrofitTotal)}{units.CR}
{translate('retrofit from')}{translate('retrofit from')}