From 77e652cd3450652f7e2a1f51399fd3cc81a657a2 Mon Sep 17 00:00:00 2001 From: Felix Linker Date: Sat, 8 Sep 2018 21:06:20 +0200 Subject: [PATCH] Allow for SE prefixes when formating module stats --- src/app/shipyard/Module.js | 36 ++++++++++++++++++++++++------ src/app/shipyard/StatsFormating.js | 32 ++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/app/shipyard/Module.js b/src/app/shipyard/Module.js index c68f7609..b6b64520 100755 --- a/src/app/shipyard/Module.js +++ b/src/app/shipyard/Module.js @@ -1,7 +1,7 @@ import * as ModuleUtils from './ModuleUtils'; import { Modifications } from 'coriolis-data/dist'; import React from 'react'; -import StatsFormating from './StatsFormating'; +import { STATS_FORMATING, SI_PREFIXES } from './StatsFormating'; /** * Module - active module in a ship's buildout @@ -45,8 +45,8 @@ export default class Module { let result = this.mods && this.mods[name] ? this.mods[name] : null; // Calculate the percentage change for a synthetic value - if (StatsFormating[name] && StatsFormating[name].synthetic) { - const statGetter = this[StatsFormating[name].synthetic]; + if (STATS_FORMATING[name] && STATS_FORMATING[name].synthetic) { + const statGetter = this[STATS_FORMATING[name].synthetic]; let unmodifiedStat = statGetter.call(this, false); let modifiedStat = statGetter.call(this, true); result = (modifiedStat / unmodifiedStat - 1) * 10000; @@ -201,12 +201,15 @@ export default class Module { * Creates a react element that pretty-prints the queried module value * @param {String} name The name of the value * @param {object} language Language object holding formats and util functions + * @param {String} [unit] If unit is given not the stat's default formating + * unit will be applied but the given one taking into + * account SI-prefixes such as kilo, milli, etc. * @param {Number} [val] If val is given, not the modules value but given * one will be formated * @returns {React.Component} The formated value as component */ - formatModifiedValue(name, language, val) { - const formatingOptions = StatsFormating[name]; + formatModifiedValue(name, language, unit, val) { + const formatingOptions = STATS_FORMATING[name]; if (val === undefined) { if (formatingOptions && formatingOptions.synthetic) { val = (this[formatingOptions.synthetic]).call(this, true); @@ -223,8 +226,27 @@ export default class Module { ); } - if (formatingOptions.format && language.formats[formatingOptions.format]) { - val = (language.formats[formatingOptions.format])(val); + let { format } = formatingOptions; + unit = unit || formatingOptions.unit; + let storedUnit = formatingOptions.storedUnit || formatingOptions.unit; + let factor = 1; + if (storedUnit && storedUnit !== unit) { + // Find out si prefix of storedUnit and unit as si prefixes can only take + // on charactere it suffices to compare the first character of each string + let prefixUnit = unit[0]; + let prefixStored = unit[0]; + if (unit.length > storedUnit.length) { + factor /= SI_PREFIXES[prefixUnit]; + } else if (storedUnit.length > unit.length) { + factor *= SI_PREFIXES[prefixStored]; + } else if (prefixUnit !== prefixStored) { + factor *= SI_PREFIXES[prefixStored]; + factor /= SI_PREFIXES[prefixUnit]; + } + } + + if (format && language.formats[format]) { + val = (language.formats[format])(val * factor); } return ( diff --git a/src/app/shipyard/StatsFormating.js b/src/app/shipyard/StatsFormating.js index 699e379f..8b088d8a 100644 --- a/src/app/shipyard/StatsFormating.js +++ b/src/app/shipyard/StatsFormating.js @@ -1,4 +1,28 @@ -export default { +export const SI_PREFIXES = { + 'Y': 1e+24, // Yotta + 'Z': 1e+21, // Zetta + 'E': 1e+18, // Peta + 'P': 1e+15, // Peta + 'T': 1e+12, // Tera + 'G': 1e+9, // Giga + 'M': 1e+6, // Mega + 'k': 1e+3, // Kilo + 'h': 1e+2, // Hekto + 'da': 1e+1, // Deka + '': 1, + 'd': 1e-1, // Dezi + 'c': 1e-2, // Zenti + 'm': 1e-3, // Milli + 'μ': 1e-6, // mikro not supported due to charset + 'n': 10e-9, // Nano + 'p': 1e-12, // Nano + 'f': 1e-15, // Femto + 'a': 1e-18, // Atto + 'z': 1e-21, // Zepto + 'y': 1e-24 // Yokto +}; + +export const STATS_FORMATING = { 'ammo': { 'format': 'int', }, 'boot': { 'format': 'int', 'unit': 'secs' }, 'brokenregen': { 'format': 'round1', 'unit': 'ps' }, @@ -17,8 +41,8 @@ export default { 'eps': { 'format': 'round', 'units': 'ps', 'synthetic': 'getEps' }, 'explres': { 'format': 'pct' }, 'facinglimit': { 'format': 'round1', 'unit': 'ang' }, - 'falloff': { 'format': 'round1', 'unit': 'm' }, - 'fallofffromrange': { 'format': 'round1', 'unit': 'm', 'synthetic': 'getFalloff' }, + 'falloff': { 'format': 'round1', 'unit': 'km', 'storedUnit': 'm' }, + 'fallofffromrange': { 'format': 'round1', 'unit': 'km', 'storedUnit': 'm', 'synthetic': 'getFalloff' }, 'hps': { 'format': 'round', 'units': 'ps', 'synthetic': 'getHps' }, 'hullboost': { 'format': 'pct1' }, 'hullreinforcement': { 'format': 'int' }, @@ -33,7 +57,7 @@ export default { 'piercing': { 'format': 'int' }, 'power': { 'format': 'round', 'unit': 'MW' }, 'protection': { 'format': 'pct' }, - 'range': { 'format': 'round1', 'unit': 'm' }, + 'range': { 'format': 'round1', 'unit': 'km', 'storedUnit': 'm' }, 'ranget': { 'format': 'round1', 'unit': 's' }, 'regen': { 'format': 'round1', 'unit': 'ps' }, 'reload': { 'format': 'int', 'unit': 's' },