tf2-currency
Version:
Set of tools that manage operations with tf2 currency.
189 lines (188 loc) • 6.8 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.isClassicCurrency = exports.isWeaponizedCurrency = exports.w = exports.pluralizeKeys = exports.c = exports.compareTo = exports.isSmallerOrEqual = exports.isBiggerOrEqual = exports.isSmaller = exports.isBigger = exports.isEqual = exports.fromKeysToCurrency = exports.fixCurrency = exports.fixMetal = exports.toRefinedFromWeapons = exports.toRefined = exports.toScrap = exports.toWeapons = exports.round = void 0;
var currency_class_1 = require("./currency.class");
var currency_error_1 = require("./currency.error");
/**
* Rounds a number
* @param n number we are rounding
* @param d number of decimals we round to
* @returns rounded number
*/
function round(n, d) {
if (d === void 0) { d = 2; }
var factor = Math.pow(10, d);
return Math.round(n * factor) / factor;
}
exports.round = round;
/**
* Convert refined to weapon value.
* Works the same as toScrap, but is result is multiplied by 2.
*
* @param value in scrap
* @returns value in weapons
*/
function toWeapons(value) {
var metal = Math.floor(value);
var scrapInMetal = round(value - metal);
var scrap = metal * 9 + (scrapInMetal * 100) / 11;
return Math.round(scrap * 2);
}
exports.toWeapons = toWeapons;
/**
* Converts refined to scrap.
* @param value in refined
* @return value in scrap
*/
function toScrap(value) {
var metal = Math.floor(value);
var scrapInMetal = round(value - metal);
var scrap = metal * 9 + (scrapInMetal * 100) / 11;
return Math.round(scrap * 2) / 2;
}
exports.toScrap = toScrap;
function toRefined(value) {
var isNegative = value < 0;
var metal = (isNegative ? -1 : 1) * Math.floor(Math.abs(value) / 9);
var remainingMetal = value - metal * 9;
var rounding = (isNegative ? remainingMetal < -5 : remainingMetal < 5)
? Math.floor
: Math.ceil;
var scrap = rounding(remainingMetal * 11) / 100;
return round(metal + scrap);
}
exports.toRefined = toRefined;
function toRefinedFromWeapons(value) {
var isNegative = value < 0;
var metal = (isNegative ? -1 : 1) * Math.floor(Math.abs(value) / 18);
var remainingMetalInScrap = (value - metal * 18) / 2;
var roundingCondition = isNegative
? remainingMetalInScrap < -5
: remainingMetalInScrap < 5;
var roundMethod;
if (roundingCondition) {
roundMethod = Math.floor;
}
else {
roundMethod = Math.ceil;
}
var scrap = roundMethod(remainingMetalInScrap * 11) / 100;
return round(metal + scrap);
}
exports.toRefinedFromWeapons = toRefinedFromWeapons;
function fixMetal(metal) {
return toRefined(toScrap(metal));
}
exports.fixMetal = fixMetal;
function fixCurrency(currency) {
return {
keys: currency.keys || 0,
metal: fixMetal(currency.metal || 0),
};
}
exports.fixCurrency = fixCurrency;
function fromKeysToCurrency(value, conversion) {
if (conversion === void 0) { conversion = 0; }
var isNegative = value < 0;
var keys = isNegative ? Math.ceil(value) : Math.floor(value);
var metalInKeys = round(value - keys);
if (metalInKeys && !conversion) {
throw new currency_error_1.CurrencyError('Conversion value is required when metal is present.');
}
var conversionInScrap = toScrap(conversion);
var scrap = Math.round(metalInKeys * conversionInScrap);
var metal = toRefined(scrap);
return {
keys: keys,
metal: metal,
};
}
exports.fromKeysToCurrency = fromKeysToCurrency;
function isEqual(currencyA, currencyB) {
return (currencyA.keys === currencyB.keys && currencyA.metal === currencyB.metal);
}
exports.isEqual = isEqual;
function isBigger(currencyA, currencyB) {
return (currencyA.keys > currencyB.keys ||
(currencyA.keys === currencyB.keys && currencyA.metal > currencyB.metal));
}
exports.isBigger = isBigger;
function isSmaller(currencyA, currencyB) {
return (currencyA.keys < currencyB.keys ||
(currencyA.keys === currencyB.keys && currencyA.metal < currencyB.metal));
}
exports.isSmaller = isSmaller;
function isBiggerOrEqual(currencyA, currencyB) {
return (currencyA.keys > currencyB.keys ||
(currencyA.keys === currencyB.keys && currencyA.metal >= currencyB.metal));
}
exports.isBiggerOrEqual = isBiggerOrEqual;
function isSmallerOrEqual(currencyA, currencyB) {
return (currencyA.keys < currencyB.keys ||
(currencyA.keys === currencyB.keys && currencyA.metal <= currencyB.metal));
}
exports.isSmallerOrEqual = isSmallerOrEqual;
function compareTo(currencyA, currencyB) {
if (isBigger(currencyA, currencyB)) {
return 1;
}
if (isSmaller(currencyA, currencyB)) {
return -1;
}
return 0;
}
exports.compareTo = compareTo;
function c(currency) {
return new currency_class_1.Currency(currency);
}
exports.c = c;
function pluralizeKeys(value) {
if (value === 0) {
return '0 keys';
}
return value + " " + (value < 0 ? (value < -1 ? 'keys' : 'key') : value > 1 ? 'keys' : 'key');
}
exports.pluralizeKeys = pluralizeKeys;
exports.w = {
isEqual: function (currencyA, currencyB) {
return (currencyA.keys === currencyB.keys &&
currencyA.metalInWeapons === currencyB.metalInWeapons);
},
isBigger: function (currencyA, currencyB) {
return (currencyA.keys > currencyB.keys ||
(currencyA.keys === currencyB.keys &&
currencyA.metalInWeapons > currencyB.metalInWeapons));
},
isSmaller: function (currencyA, currencyB) {
return (currencyA.keys < currencyB.keys ||
(currencyA.keys === currencyB.keys &&
currencyA.metalInWeapons < currencyB.metalInWeapons));
},
isBiggerOrEqual: function (currencyA, currencyB) {
return (currencyA.keys > currencyB.keys ||
(currencyA.keys === currencyB.keys &&
currencyA.metalInWeapons >= currencyB.metalInWeapons));
},
isSmallerOrEqual: function (currencyA, currencyB) {
return (currencyA.keys < currencyB.keys ||
(currencyA.keys === currencyB.keys &&
currencyA.metalInWeapons <= currencyB.metalInWeapons));
},
compareTo: function (currencyA, currencyB) {
if (exports.w.isBigger(currencyA, currencyB)) {
return 1;
}
if (exports.w.isSmaller(currencyA, currencyB)) {
return -1;
}
return 0;
},
};
function isWeaponizedCurrency(currency) {
return Object.prototype.hasOwnProperty.call(currency, 'metalInWeapons');
}
exports.isWeaponizedCurrency = isWeaponizedCurrency;
function isClassicCurrency(currency) {
return Object.prototype.hasOwnProperty.call(currency, 'metal');
}
exports.isClassicCurrency = isClassicCurrency;
;