tf2-currency
Version:
Set of tools that manage operations with tf2 currency.
196 lines (195 loc) • 8.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Currency = void 0;
var currency_helper_1 = require("./currency.helper");
var currency_error_1 = require("./currency.error");
var Currency = /** @class */ (function () {
function Currency(currency) {
if (currency === void 0) { currency = {}; }
this.keys = currency.keys || 0;
if ((0, currency_helper_1.isWeaponizedCurrency)(currency)) {
this.metalInWeapons = currency.metalInWeapons || 0;
}
else if ((0, currency_helper_1.isClassicCurrency)(currency)) {
this.metalInWeapons = (0, currency_helper_1.toWeapons)((0, currency_helper_1.fixMetal)(currency.metal || 0));
}
else {
this.metalInWeapons = 0;
}
}
Object.defineProperty(Currency.prototype, "metal", {
get: function () {
return (0, currency_helper_1.toRefinedFromWeapons)(this.metalInWeapons);
},
enumerable: false,
configurable: true
});
Currency.fromScrap = function (scrap, conversion) {
if (conversion === void 0) { conversion = 0; }
var conversionInScrap = (0, currency_helper_1.toScrap)(conversion);
var roundMethod = scrap < 0 ? Math.ceil : Math.floor;
var keys = conversionInScrap ? roundMethod(scrap / conversionInScrap) : 0;
var metalInScrap = scrap - keys * conversionInScrap;
var metal = (0, currency_helper_1.toRefined)(metalInScrap);
return new Currency({
keys: keys,
metal: metal,
});
};
Currency.fromWeapons = function (weapons, conversion) {
if (conversion === void 0) { conversion = 0; }
var conversionInWeapons = (0, currency_helper_1.toWeapons)(conversion);
var roundMethod = weapons < 0 ? Math.ceil : Math.floor;
var keys = conversionInWeapons
? roundMethod(weapons / conversionInWeapons)
: 0;
var metalInWeapons = weapons - keys * conversionInWeapons;
return new Currency({ keys: keys, metalInWeapons: metalInWeapons });
};
Currency.fromKeys = function (value, conversion) {
if (conversion === void 0) { conversion = 0; }
return new Currency((0, currency_helper_1.fromKeysToCurrency)(value, conversion));
};
Currency.prototype.clone = function () {
return new Currency(this);
};
Currency.prototype.isEmpty = function () {
return this.keys === 0 && this.metal === 0;
};
Currency.prototype.toWeapons = function (conversion) {
if (conversion === void 0) { conversion = 0; }
if (this.keys && !conversion) {
throw new currency_error_1.CurrencyError('Conversion value is required when keys are present.');
}
var conversionInWeapons = (0, currency_helper_1.toWeapons)(conversion);
var keysInWeapons = this.keys * conversionInWeapons;
return keysInWeapons + this.metalInWeapons;
};
Currency.prototype.toScrap = function (conversion) {
if (conversion === void 0) { conversion = 0; }
return this.toWeapons(conversion) / 2;
};
Currency.prototype.toKeys = function (conversion) {
if (conversion === void 0) { conversion = 0; }
if (this.metal && !conversion) {
throw new currency_error_1.CurrencyError('Conversion value is required when metal is present.');
}
var conversionInScrap = (0, currency_helper_1.toScrap)(conversion);
var metalInScrap = (0, currency_helper_1.toScrap)(this.metal);
var metalInKeys = conversion
? (0, currency_helper_1.round)(metalInScrap / conversionInScrap)
: 0;
return this.keys + metalInKeys;
};
Currency.prototype.toString = function () {
if (!this.keys && !this.metalInWeapons) {
return '0 keys, 0 metal';
}
var currency = '';
if (this.keys) {
currency += (0, currency_helper_1.pluralizeKeys)(this.keys);
}
if (this.metalInWeapons) {
if (currency) {
currency += ', ';
}
currency += this.metal + " metal";
}
return currency;
};
Currency.prototype.toJSON = function () {
return {
keys: this.keys,
metal: this.metal,
};
};
/**
* Returns a JSON representation of the currency with weapons instead of metal.
* @returns JSON representation with keys and weapons.
*/
Currency.prototype.toWeaponizedJSON = function () {
return {
keys: this.keys,
metalInWeapons: this.metalInWeapons,
};
};
Currency.prototype.addWeapons = function (value, conversion) {
if (conversion === void 0) { conversion = 0; }
var currentScrapValue = this.toWeapons(conversion);
var total = currentScrapValue + value;
var currency = Currency.fromWeapons(total, conversion);
this.keys = currency.keys;
this.metalInWeapons = currency.metalInWeapons;
return this;
};
Currency.prototype.addScrap = function (value, conversion) {
if (conversion === void 0) { conversion = 0; }
return this.addWeapons(value * 2, conversion);
};
Currency.prototype.addMetal = function (value, conversion) {
return this.addWeapons((0, currency_helper_1.toWeapons)(value), conversion);
};
Currency.prototype.addKeys = function (value, conversion) {
return this.addCurrency((0, currency_helper_1.fromKeysToCurrency)(value, conversion), conversion);
};
Currency.prototype.addCurrency = function (currency, conversion) {
return this.addWeapons(new Currency(currency).toWeapons(conversion), conversion);
};
Currency.prototype.removeWeapons = function (value, conversion) {
if (conversion === void 0) { conversion = 0; }
return this.addWeapons(-value, conversion);
};
Currency.prototype.removeScrap = function (value, conversion) {
return this.addScrap(-value, conversion);
};
Currency.prototype.removeMetal = function (value, conversion) {
return this.addMetal(-value, conversion);
};
Currency.prototype.removeKeys = function (value, conversion) {
return this.addKeys(-value, conversion);
};
Currency.prototype.removeCurrency = function (currency, conversion) {
return this.addCurrency({
keys: -currency.keys,
metal: -currency.metal,
}, conversion);
};
Currency.prototype.isEqual = function (currency) {
return (0, currency_helper_1.isEqual)(this, currency);
};
Currency.prototype.isBigger = function (currency) {
return (0, currency_helper_1.isBigger)(this, currency);
};
Currency.prototype.isSmaller = function (currency) {
return (0, currency_helper_1.isSmaller)(this, currency);
};
Currency.prototype.isBiggerOrEqual = function (currency) {
return (0, currency_helper_1.isBiggerOrEqual)(this, currency);
};
Currency.prototype.isSmallerOrEqual = function (currency) {
return (0, currency_helper_1.isSmallerOrEqual)(this, currency);
};
Currency.prototype.compareTo = function (value) {
return (0, currency_helper_1.compareTo)(this, value);
};
Currency.prototype.wIsEqual = function (currency) {
return currency_helper_1.w.isEqual(this, currency);
};
Currency.prototype.wIsBigger = function (currency) {
return currency_helper_1.w.isBigger(this, currency);
};
Currency.prototype.wIsSmaller = function (currency) {
return currency_helper_1.w.isSmaller(this, currency);
};
Currency.prototype.wIsBiggerOrEqual = function (currency) {
return currency_helper_1.w.isBiggerOrEqual(this, currency);
};
Currency.prototype.wIsSmallerOrEqual = function (currency) {
return currency_helper_1.w.isSmallerOrEqual(this, currency);
};
Currency.prototype.wCompareTo = function (value) {
return currency_helper_1.w.compareTo(this, value);
};
return Currency;
}());
exports.Currency = Currency;