From a0f72f1ee84e380fdd5cb09b96240e45adfa62b4 Mon Sep 17 00:00:00 2001 From: ibixina Date: Thu, 7 Nov 2024 19:26:13 -0600 Subject: [PATCH] Feature/avgpersonalstat (#814) * updated the settings page for the new personal-stat-avg feature * implement avg personal stats * removed unnecessary code * changed lets to const, made it on by default, changed abbreviation in settings page * default set to false * updated the changelog to reflect the addition of the new feature * const * fixed typo * fix * changed variable type to const for unchanging varibales --- extension/changelog.json | 3 +- extension/manifest.json | 5 ++ extension/pages/settings/settings.html | 5 ++ .../avg-personal-stat.entry.js | 62 +++++++++++++++++++ extension/scripts/global/globalData.js | 1 + 5 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 extension/scripts/features/avg-personal-stat/avg-personal-stat.entry.js diff --git a/extension/changelog.json b/extension/changelog.json index 8a4eec704..52ca78392 100644 --- a/extension/changelog.json +++ b/extension/changelog.json @@ -17,7 +17,8 @@ { "message": "Adapt drug details to work with the item market overhaul.", "contributor": "DeKleineKobini" }, { "message": "Update some mission hints and tasks.", "contributor": "DeKleineKobini" }, { "message": "Detect sidebar conditions better for the OC time feature.", "contributor": "DeKleineKobini" }, - { "message": "Added a bail cost filter", "contributor": "nao" } + { "message": "Added a bail cost filter", "contributor": "nao" }, + {"message": "Added new feature to calculate the average in personal stats page, can be enabled from Profile page in Settings", "contributor": "nao"} ], "removed": [ { "message": "Remove the bazaar and item market redirect feature, due to the item market overhaul.", "contributor": "DeKleineKobini" }, diff --git a/extension/manifest.json b/extension/manifest.json index de6808adf..850ca0b8c 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -234,6 +234,11 @@ ], "run_at": "document_end" }, + { + "matches": ["https://www.torn.com/personalstats.php*"], + "js": ["scripts/features/avg-personal-stat/avg-personal-stat.entry.js"], + "run_at": "document_start" + }, { "matches": ["https://www.torn.com/bazaar.php*"], "css": ["scripts/features/bazaar-worth/ttBazaarWorth.css"], diff --git a/extension/pages/settings/settings.html b/extension/pages/settings/settings.html index 0bac6aa99..5d0dfe6d7 100644 --- a/extension/pages/settings/settings.html +++ b/extension/pages/settings/settings.html @@ -1303,6 +1303,11 @@

Profile
+
+ + +
+
diff --git a/extension/scripts/features/avg-personal-stat/avg-personal-stat.entry.js b/extension/scripts/features/avg-personal-stat/avg-personal-stat.entry.js new file mode 100644 index 000000000..23c7d2a37 --- /dev/null +++ b/extension/scripts/features/avg-personal-stat/avg-personal-stat.entry.js @@ -0,0 +1,62 @@ +"use strict"; + +(async () => { + if (!getPageStatus().access) return; + + featureManager.registerFeature( + "Average Personal Stat", + "personalstats", + () => settings.pages.profile.avgpersonalstats, + init, + init, + null, + null, + async () => { + await checkDevice(); + } + ); + + function init() { + addFetchListener((event) => { + const { + page, + json, + fetch: { body }, + } = event.detail; + if (page !== "personalstats") return; + if (body.includes("getGraphData")) { + const graphData = json; + calculateStatsAverage(graphData); + } + }); + } + + function calculateStatsAverage(graphData) { + for (const stat in graphData.data) { + const statData = graphData.data[stat]; + let userIndex = 2; + for (const user of statData) { + // Get Relevant Data + const uid = user.uid; + const userName = graphData.definitions[uid]; + const lowerTime = user.data[0].time; + const lowerVal = user.data[0].value; + const userDatalen = user.data.length; + const upperTime = user.data[userDatalen - 1].time; + const upperVal = user.data[userDatalen - 1].value; + + // Calcualte Average + const timeLength = (upperTime - lowerTime) / (60 * 60 * 24); + const difference = upperVal - lowerVal; + const avg = difference / timeLength; + const roundedAvg = avg.toFixed(2); // Rounds to 2 decimal places + const formattedAvg = roundedAvg.replace(/\B(?=(\d{3})+(?!\d))/g, ","); + + // Insert the data + const element = document.querySelectorAll("div[class^='titleItem']")[userIndex]; + element.textContent = `${userName} (${formattedAvg} per day)`; + userIndex++; + } + } + } +})(); diff --git a/extension/scripts/global/globalData.js b/extension/scripts/global/globalData.js index 2836e4d2d..25579c175 100644 --- a/extension/scripts/global/globalData.js +++ b/extension/scripts/global/globalData.js @@ -403,6 +403,7 @@ const DEFAULT_STORAGE = { stackingMode: new DefaultSetting({ type: "boolean", defaultValue: false }), }, profile: { + avgpersonalstats: new DefaultSetting({ type: "boolean", defaultValue: false }), statusIndicator: new DefaultSetting({ type: "boolean", defaultValue: true }), idBesideProfileName: new DefaultSetting({ type: "boolean", defaultValue: true }), notes: new DefaultSetting({ type: "boolean", defaultValue: true }),