From 6f26408de7298e711731dc9bacfc7c8020e31908 Mon Sep 17 00:00:00 2001 From: danielyxie Date: Tue, 20 Nov 2018 21:43:15 -0800 Subject: [PATCH] Fixed bug with numeraljs displaying NaN for very small numbers. Cleaned up stock market argument validation code --- src/Constants.ts | 3 ++- src/Location.js | 2 +- src/StockMarket.js | 39 +++++++++++++++++++++------------------ src/ui/numeralFormat.js | 2 ++ 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/Constants.ts b/src/Constants.ts index 995e25c72..e1d324ad4 100644 --- a/src/Constants.ts +++ b/src/Constants.ts @@ -523,8 +523,9 @@ export let CONSTANTS: IMap = { * 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: '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: Stock Market UI should no longer show 'NaN' profit immediately after buying a stock ` } diff --git a/src/Location.js b/src/Location.js index 4e7629827..786e67cdf 100644 --- a/src/Location.js +++ b/src/Location.js @@ -264,7 +264,7 @@ function displayLocationContent() { } work.addEventListener("click", function() { - if (currPos.isPartTimeJob()) { + if (currPos.isPartTimeJob() || currPos.isSoftwareConsultantJob() || currPos.isBusinessConsultantJob()) { Player.startWorkPartTime(); } else { Player.startWork(); diff --git a/src/StockMarket.js b/src/StockMarket.js index 23b2fed0a..acba39d55 100755 --- a/src/StockMarket.js +++ b/src/StockMarket.js @@ -403,12 +403,13 @@ function stockMarketCycle() { //Returns true if successful, false otherwise 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"); return false; } - shares = Math.round(shares); - if (shares == 0) {return false;} var totalPrice = stock.price * shares; if (Player.money.lt(totalPrice + CONSTANTS.StockMarketCommission)) { @@ -420,7 +421,7 @@ function buyStock(stock, shares) { var origTotal = stock.playerShares * stock.playerAvgPx; Player.loseMoney(totalPrice + CONSTANTS.StockMarketCommission); var newTotal = origTotal + totalPrice; - stock.playerShares += shares; + stock.playerShares = Math.round(stock.playerShares + shares); stock.playerAvgPx = newTotal / stock.playerShares; updateStockPlayerPosition(stock); 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;} var gains = stock.price * shares - CONSTANTS.StockMarketCommission; Player.gainMoney(gains); - stock.playerShares -= shares; + stock.playerShares = Math.round(stock.playerShares - shares); if (stock.playerShares == 0) { stock.playerAvgPx = 0; } @@ -455,7 +456,11 @@ function sellStock(stock, shares) { //Returns true if successful and false otherwise function shortStock(stock, shares, workerScript=null) { 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) { workerScript.scriptRef.log("ERROR: shortStock() failed because of invalid arguments."); } else { @@ -464,8 +469,6 @@ function shortStock(stock, shares, workerScript=null) { } return false; } - shares = Math.round(shares); - if (shares === 0) {return false;} var totalPrice = stock.price * shares; if (Player.money.lt(totalPrice + CONSTANTS.StockMarketCommission)) { @@ -484,7 +487,7 @@ function shortStock(stock, shares, workerScript=null) { var origTotal = stock.playerShortShares * stock.playerAvgShortPx; Player.loseMoney(totalPrice + CONSTANTS.StockMarketCommission); var newTotal = origTotal + totalPrice; - stock.playerShortShares += shares; + stock.playerShortShares = Math.round(stock.playerShortShares + shares); stock.playerAvgShortPx = newTotal / stock.playerShortShares; updateStockPlayerPosition(stock); if (tixApi) { @@ -526,7 +529,7 @@ function sellShort(stock, shares, workerScript=null) { Player.scriptProdSinceLastAug += profit; } - stock.playerShortShares -= shares; + stock.playerShortShares = Math.round(stock.playerShortShares - shares); if (stock.playerShortShares === 0) { stock.playerAvgShortPx = 0; } @@ -1370,15 +1373,15 @@ function updateStockPlayerPosition(stock) { } //Calculate returns - var totalCost = stock.playerShares * stock.playerAvgPx, - gains = (stock.price - stock.playerAvgPx) * stock.playerShares, - percentageGains = gains / totalCost; - if (isNaN(percentageGains)) {percentageGains = 0;} + const totalCost = stock.playerShares * stock.playerAvgPx; + let gains = (stock.price - stock.playerAvgPx) * stock.playerShares; + let percentageGains = gains / totalCost; + if (isNaN(percentageGains)) { percentageGains = 0; } - var shortTotalCost = stock.playerShortShares * stock.playerAvgShortPx, - shortGains = (stock.playerAvgShortPx - stock.price) * stock.playerShortShares, - shortPercentageGains = shortGains/ shortTotalCost; - if (isNaN(shortPercentageGains)) {shortPercentageGains = 0;} + const shortTotalCost = stock.playerShortShares * stock.playerAvgShortPx; + let shortGains = (stock.playerAvgShortPx - stock.price) * stock.playerShortShares; + let shortPercentageGains = shortGains/ shortTotalCost; + if (isNaN(shortPercentageGains)) { shortPercentageGains = 0; } stock.posTxtEl.innerHTML = "

Long Position: " + diff --git a/src/ui/numeralFormat.js b/src/ui/numeralFormat.js index 9d4a79b90..3c32bddcf 100644 --- a/src/ui/numeralFormat.js +++ b/src/ui/numeralFormat.js @@ -32,6 +32,8 @@ class NumeralFormatter { } 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); } }