Skip to content

Commit

Permalink
Feature/avgpersonalstat (#814)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ibixina authored Nov 8, 2024
1 parent 21002bd commit a0f72f1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
3 changes: 2 additions & 1 deletion extension/changelog.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
5 changes: 5 additions & 0 deletions extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
5 changes: 5 additions & 0 deletions extension/pages/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,11 @@ <h2>
</section>
<section name="profile">
<div class="header">Profile</div>
<div class="option">
<input id="profile-avgpersonalstats" type="checkbox" />
<label for="profile-avgpersonalstats">Calculate Average Personal Stats</label>
</div>

<div class="option">
<input id="profile-idBesideProfileName" type="checkbox" />
<label for="profile-idBesideProfileName">Reformat profile page headings as "USERNAME [ID]".</label>
Expand Down
Original file line number Diff line number Diff line change
@@ -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++;
}
}
}
})();
1 change: 1 addition & 0 deletions extension/scripts/global/globalData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down

0 comments on commit a0f72f1

Please sign in to comment.