From 16161441a1fb9d1bd5970fd19c3cf3bdfd0cf99a Mon Sep 17 00:00:00 2001 From: Sashank999 Date: Sun, 28 Jul 2024 11:40:43 +0530 Subject: [PATCH] Total Portfolio Value is now live and accurate... ...unless stocks transactions are made, in which case the profit will be accurate after the API data update (takes 30 seconds). --- extension/changelog.json | 2 +- extension/pages/settings/settings.html | 7 +++++ .../ttTotalPortfolioValue.js | 31 ++++++++++++++++--- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/extension/changelog.json b/extension/changelog.json index 9b7f4f38d..c9bb575ea 100644 --- a/extension/changelog.json +++ b/extension/changelog.json @@ -10,7 +10,7 @@ { "message": "Add Vanguard armor set to Auction House Filter.", "contributor": "TheFoxMan" }, { "message": "Fix Employee Effectiveness Highlight after training an employee.", "contributor": "TheFoxMan" } ], - "changes": [], + "changes": [{ "message": "Total Portfolio Value is now live and accurate unless stocks transactions are made.", "contributor": "TheFoxMan" }], "removed": [] } }, diff --git a/extension/pages/settings/settings.html b/extension/pages/settings/settings.html index c334d14f0..0244aa3cd 100644 --- a/extension/pages/settings/settings.html +++ b/extension/pages/settings/settings.html @@ -591,6 +591,9 @@

+
+ The popup shows the profit calculated purely from API data which makes it inaccurate by default of 5 minutes. +
@@ -1389,6 +1392,10 @@

+
+ The current stock price is live (from the stocks page) but the information of stocks you hold is fetched from the Torn API. This + may make the profit shown inaccurate for 30 seconds if you bought or sold stocks (the API data updates after 30 seconds). +
Hide Stocks
diff --git a/extension/scripts/features/total-portfolio-value/ttTotalPortfolioValue.js b/extension/scripts/features/total-portfolio-value/ttTotalPortfolioValue.js index 8d5f9e89e..a85b417f8 100644 --- a/extension/scripts/features/total-portfolio-value/ttTotalPortfolioValue.js +++ b/extension/scripts/features/total-portfolio-value/ttTotalPortfolioValue.js @@ -1,7 +1,7 @@ "use strict"; (async () => { - featureManager.registerFeature( + const feature = featureManager.registerFeature( "Total Portfolio Value and Profit", "stocks", () => settings.pages.stocks.valueAndProfit, @@ -9,7 +9,7 @@ addProfitAndValue, removeProfitAndValue, { - storage: ["settings.pages.stocks.valueAndProfit"], + storage: ["settings.pages.stocks.valueAndProfit", "userdata.stocks"], }, async () => { await checkDevice(); @@ -18,18 +18,33 @@ async function addProfitAndValue() { await requireElement("#stockmarketroot [class*='stock___']"); + + calculateAndShowProfits(); + + const observer = new MutationObserver(async (mutations) => { + if (!feature.enabled()) return; + + await sleep(0.5); + calculateAndShowProfits(); + }); + observer.observe(document.find("#priceTab"), { attributeOldValue: true }); + } + + function calculateAndShowProfits() { + removeProfitAndValue(); + const totalValue = [...document.findAll("[class*='stockOwned__'] [class*='value__']")].map((x) => x.textContent.getNumber()).totalSum(); + const stockPrices = getStockPrices(); const profits = [...document.findAll("#stockmarketroot [class*='stockMarket__'] > ul[id]")] .map((x) => { const stockID = x.id; - const data = stockdata[stockID]; const userStockData = userdata.stocks[stockID]; if (!userStockData) return 0; const boughtTotal = Object.values(userStockData.transactions).reduce((prev, trans) => prev + trans.bought_price * trans.shares, 0); const boughtPrice = boughtTotal / userStockData.total_shares; - return Math.floor((data.current_price - boughtPrice) * userStockData.total_shares); + return Math.floor((stockPrices[stockID] - boughtPrice) * userStockData.total_shares); }) .totalSum(); @@ -53,6 +68,14 @@ if (mobile) document.find("#stockmarketroot [class*='topSection__']").classList.add("tt-total-stock-value-wrap"); } + function getStockPrices() { + const data = {}; + document.findAll("[class*='stockMarket__'] > ul[id]").forEach((stock) => { + data[stock.id] = parseFloat(stock.find("#priceTab > :first-child").textContent); + }); + return data; + } + function removeProfitAndValue() { const ttTotalStockValue = document.find("#stockmarketroot .tt-total-stock-value"); if (ttTotalStockValue) ttTotalStockValue.remove();