Fixed bug with numeraljs displaying NaN for very small numbers. Cleaned up stock market argument validation code

This commit is contained in:
danielyxie 2018-11-20 21:43:15 -08:00
parent 66d50a7ae4
commit 6f26408de7
4 changed files with 26 additions and 20 deletions

@ -523,8 +523,9 @@ export let CONSTANTS: IMap<any> = {
* Bug Fix: In Bladeburner, you can no longer start a BlackOp through the Netscript API if it has already been completed * Bug Fix: In Bladeburner, you can no longer start a BlackOp through the Netscript API if it has already been completed
* Bug Fix: In Bladeburner, fixed a bug which caused the configured 'automate' actions to occasionally be switched to other actions * Bug Fix: In Bladeburner, fixed a bug which caused the configured 'automate' actions to occasionally be switched to other actions
* Bug Fix: 'Return to World' button at locations no longer accumulates event listeners * Bug Fix: 'Return to World' button at locations no longer accumulates event listeners
* Bug Fix: Working & taking classes now continuously add/subtract money during the action, instead of doing it only at completion * Bug Fix: Working & taking classes now continuously add/subtract money during the action, instead of doing it at completion
* Bug Fix: Top-right overview panel now displays negative money using '-' instead of '()' * Bug Fix: Top-right overview panel now displays negative money using '-' instead of '()'
* Bug Fix: Stock Market UI should no longer show 'NaN' profit immediately after buying a stock
` `
} }

@ -264,7 +264,7 @@ function displayLocationContent() {
} }
work.addEventListener("click", function() { work.addEventListener("click", function() {
if (currPos.isPartTimeJob()) { if (currPos.isPartTimeJob() || currPos.isSoftwareConsultantJob() || currPos.isBusinessConsultantJob()) {
Player.startWorkPartTime(); Player.startWorkPartTime();
} else { } else {
Player.startWork(); Player.startWork();

@ -403,12 +403,13 @@ function stockMarketCycle() {
//Returns true if successful, false otherwise //Returns true if successful, false otherwise
function buyStock(stock, shares) { function buyStock(stock, shares) {
if (stock == null || shares < 0 || isNaN(shares)) { // Validate arguments
shares = Math.round(shares);
if (shares == 0 || shares < 0) { return false; }
if (stock == null || isNaN(shares)) {
dialogBoxCreate("Failed to buy stock. This may be a bug, contact developer"); dialogBoxCreate("Failed to buy stock. This may be a bug, contact developer");
return false; return false;
} }
shares = Math.round(shares);
if (shares == 0) {return false;}
var totalPrice = stock.price * shares; var totalPrice = stock.price * shares;
if (Player.money.lt(totalPrice + CONSTANTS.StockMarketCommission)) { if (Player.money.lt(totalPrice + CONSTANTS.StockMarketCommission)) {
@ -420,7 +421,7 @@ function buyStock(stock, shares) {
var origTotal = stock.playerShares * stock.playerAvgPx; var origTotal = stock.playerShares * stock.playerAvgPx;
Player.loseMoney(totalPrice + CONSTANTS.StockMarketCommission); Player.loseMoney(totalPrice + CONSTANTS.StockMarketCommission);
var newTotal = origTotal + totalPrice; var newTotal = origTotal + totalPrice;
stock.playerShares += shares; stock.playerShares = Math.round(stock.playerShares + shares);
stock.playerAvgPx = newTotal / stock.playerShares; stock.playerAvgPx = newTotal / stock.playerShares;
updateStockPlayerPosition(stock); updateStockPlayerPosition(stock);
dialogBoxCreate("Bought " + numeralWrapper.format(shares, '0,0') + " shares of " + stock.symbol + " at " + dialogBoxCreate("Bought " + numeralWrapper.format(shares, '0,0') + " shares of " + stock.symbol + " at " +
@ -441,7 +442,7 @@ function sellStock(stock, shares) {
if (shares === 0) {return false;} if (shares === 0) {return false;}
var gains = stock.price * shares - CONSTANTS.StockMarketCommission; var gains = stock.price * shares - CONSTANTS.StockMarketCommission;
Player.gainMoney(gains); Player.gainMoney(gains);
stock.playerShares -= shares; stock.playerShares = Math.round(stock.playerShares - shares);
if (stock.playerShares == 0) { if (stock.playerShares == 0) {
stock.playerAvgPx = 0; stock.playerAvgPx = 0;
} }
@ -455,7 +456,11 @@ function sellStock(stock, shares) {
//Returns true if successful and false otherwise //Returns true if successful and false otherwise
function shortStock(stock, shares, workerScript=null) { function shortStock(stock, shares, workerScript=null) {
var tixApi = (workerScript instanceof WorkerScript); var tixApi = (workerScript instanceof WorkerScript);
if (stock == null || isNaN(shares) || shares < 0) {
// Validate arguments
shares = Math.round(shares);
if (shares === 0 || shares < 0) { return false; }
if (stock == null || isNaN(shares)) {
if (tixApi) { if (tixApi) {
workerScript.scriptRef.log("ERROR: shortStock() failed because of invalid arguments."); workerScript.scriptRef.log("ERROR: shortStock() failed because of invalid arguments.");
} else { } else {
@ -464,8 +469,6 @@ function shortStock(stock, shares, workerScript=null) {
} }
return false; return false;
} }
shares = Math.round(shares);
if (shares === 0) {return false;}
var totalPrice = stock.price * shares; var totalPrice = stock.price * shares;
if (Player.money.lt(totalPrice + CONSTANTS.StockMarketCommission)) { if (Player.money.lt(totalPrice + CONSTANTS.StockMarketCommission)) {
@ -484,7 +487,7 @@ function shortStock(stock, shares, workerScript=null) {
var origTotal = stock.playerShortShares * stock.playerAvgShortPx; var origTotal = stock.playerShortShares * stock.playerAvgShortPx;
Player.loseMoney(totalPrice + CONSTANTS.StockMarketCommission); Player.loseMoney(totalPrice + CONSTANTS.StockMarketCommission);
var newTotal = origTotal + totalPrice; var newTotal = origTotal + totalPrice;
stock.playerShortShares += shares; stock.playerShortShares = Math.round(stock.playerShortShares + shares);
stock.playerAvgShortPx = newTotal / stock.playerShortShares; stock.playerAvgShortPx = newTotal / stock.playerShortShares;
updateStockPlayerPosition(stock); updateStockPlayerPosition(stock);
if (tixApi) { if (tixApi) {
@ -526,7 +529,7 @@ function sellShort(stock, shares, workerScript=null) {
Player.scriptProdSinceLastAug += profit; Player.scriptProdSinceLastAug += profit;
} }
stock.playerShortShares -= shares; stock.playerShortShares = Math.round(stock.playerShortShares - shares);
if (stock.playerShortShares === 0) { if (stock.playerShortShares === 0) {
stock.playerAvgShortPx = 0; stock.playerAvgShortPx = 0;
} }
@ -1370,14 +1373,14 @@ function updateStockPlayerPosition(stock) {
} }
//Calculate returns //Calculate returns
var totalCost = stock.playerShares * stock.playerAvgPx, const totalCost = stock.playerShares * stock.playerAvgPx;
gains = (stock.price - stock.playerAvgPx) * stock.playerShares, let gains = (stock.price - stock.playerAvgPx) * stock.playerShares;
percentageGains = gains / totalCost; let percentageGains = gains / totalCost;
if (isNaN(percentageGains)) { percentageGains = 0; } if (isNaN(percentageGains)) { percentageGains = 0; }
var shortTotalCost = stock.playerShortShares * stock.playerAvgShortPx, const shortTotalCost = stock.playerShortShares * stock.playerAvgShortPx;
shortGains = (stock.playerAvgShortPx - stock.price) * stock.playerShortShares, let shortGains = (stock.playerAvgShortPx - stock.price) * stock.playerShortShares;
shortPercentageGains = shortGains/ shortTotalCost; let shortPercentageGains = shortGains/ shortTotalCost;
if (isNaN(shortPercentageGains)) { shortPercentageGains = 0; } if (isNaN(shortPercentageGains)) { shortPercentageGains = 0; }
stock.posTxtEl.innerHTML = stock.posTxtEl.innerHTML =

@ -32,6 +32,8 @@ class NumeralFormatter {
} }
format(n, format) { format(n, format) {
// numeraljs doesnt properly format numbers that are too big or too small
if (Math.abs(n) < 1e-6) { n = 0; }
return numeral(n).format(format); return numeral(n).format(format);
} }
} }