Skip to content

Commit

Permalink
Opened supply pack value (#794)
Browse files Browse the repository at this point in the history
* Checkpoint

* checkpoint

* Small refactors

* itemID request fix

* function name change

* Changelog

* Remove unuseful method

* Added API Data check

* Solved PR comments

* A few changes.

- There is no need to use flexbox.
- Additional padding at the bottom is already added by Torn.

- `loadListeners` parameter should not be `undefined`. It stores the
settings keys whose value changes should reload the feature.
- Sort the `SUPPLY_PACK_ITEMS` values.
- Remove `<br>` as there is padding already.
- Remove additional styling for when a drug pack is opened.
- Add `tt` to ID attribute.
- In order to indicate that the value is added by TornTools, message
is changed.

* Merge `master`, changelog & Prettier changes.

---------

Co-authored-by: Sashank999 <bandi.rao999@gmail.com>
c0nradLC and Sashank999 authored Jun 23, 2024
1 parent 9656832 commit cd597d2
Showing 6 changed files with 106 additions and 4 deletions.
10 changes: 8 additions & 2 deletions extension/changelog.json
Original file line number Diff line number Diff line change
@@ -3,7 +3,12 @@
"version": { "major": 7, "minor": 2, "build": 4 },
"title": "Beta",
"date": false,
"logs": { "features": [], "fixes": [{ "message": "Fixing racing features.", "contributor": "TheFoxMan" }], "changes": [], "removed": [] }
"logs": {
"features": [{ "message": "Show total value of an opened supply pack.", "contributor": "Conrado" }],
"fixes": [{ "message": "Fixing racing features.", "contributor": "TheFoxMan" }],
"changes": [],
"removed": []
}
},
{
"version": { "major": 7, "minor": 2, "build": 3 },
@@ -52,7 +57,8 @@
],
"changes": [
{ "message": "Migrated to manifest version 3.", "contributor": "TheFoxMan" },
{ "message": "Validate new key level in popup before using.", "contributor": "TheFoxMan" }
{ "message": "Validate new key level in popup before using.", "contributor": "TheFoxMan" },
{ "message": "Added a text that indicates the total value of the opened items from a Supply Pack.", "contributor": "Conrado" }
]
}
},
6 changes: 4 additions & 2 deletions extension/manifest.json
Original file line number Diff line number Diff line change
@@ -486,7 +486,8 @@
"scripts/features/alcohol-nerve/ttAlcoholNerve.css",
"scripts/features/candy-happy/ttCandyHappy.css",
"scripts/features/energy-warning/ttEnergyWarning.css",
"scripts/features/medical-life/ttMedicalLife.css"
"scripts/features/medical-life/ttMedicalLife.css",
"scripts/features/opened-supply-pack-value/ttOpenedSupplyPackValue.css"
],
"js": [
"vendor/drag-drop-touch/DragDropTouch.js",
@@ -504,7 +505,8 @@
"scripts/features/candy-happy/ttCandyHappy.js",
"scripts/features/no-confirm/ttItemNoConfirm.js",
"scripts/features/energy-warning/ttEnergyWarning.js",
"scripts/features/medical-life/ttMedicalLife.js"
"scripts/features/medical-life/ttMedicalLife.js",
"scripts/features/opened-supply-pack-value/ttOpenedSupplyPackValue.js"
],
"run_at": "document_end"
},
4 changes: 4 additions & 0 deletions extension/pages/settings/settings.html
Original file line number Diff line number Diff line change
@@ -1059,6 +1059,10 @@ <h2>
<input id="items-medicalLife" type="checkbox" />
<label for="items-medicalLife">Show life information when consuming medical items.</label>
</div>
<div class="option">
<input id="items-openedSupplyPackValue" type="checkbox" />
<label for="items-openedSupplyPackValue">Show the total value of items obtained when opening supply pack.</label>
</div>

<div class="header">Museum Sets</div>
<div class="option">
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.tt-opened-supply-pack-value-text {
display: block;
padding-top: 0.4em;
color: #82c91e;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"use strict";

(async () => {
const page = getPage();

const feature = featureManager.registerFeature(
"Opened supply pack total value",
"items",
() => settings.pages.items.openedSupplyPackValue,
addListener,
undefined,
removeTotalValueElement,
{ storage: ["settings.pages.items.openedSupplyPackValue"] },
() => {
if (!hasAPIData()) return "No API access.";
}
);

const SUPPLY_PACK_ITEMS = [
364, 365, 370, 588, 815, 817, 818, 1035, 1057, 1078, 1079, 1080, 1081, 1082, 1083, 1112, 1113, 1114, 1115, 1116, 1117, 1118, 1119, 1120, 1121, 1122,
1293, 1298,
];

function addListener() {
if (page !== "item") return;

let reqXID = 0;
let itemID = 0;

addXHRListener(async ({ detail: { xhr, json } }) => {
if (!feature.enabled()) return;

const params = new URLSearchParams(xhr.requestBody);
if (params.get("action") !== "use" && params.get("step") !== "useItem") return;

itemID = params.get("id")?.getNumber() ?? itemID;
if (isXIDRequestSupplyPack(itemID)) {
reqXID = (await requireElement(`[data-item="${itemID}"] .pack-open-msg input[type="hidden"]`)).value;
}

if (params.get("XID") === reqXID || isDrugPackUseRequest(params)) {
const totalOpenedValue = json?.items?.itemAppear?.reduce(
(totalValue, item) =>
(totalValue += item.isMoney ? item.moneyGain.substring(1).getNumber() : torndata.items[item.ID].market_value * item.qty),
0
);

await showTotalValue(totalOpenedValue, itemID);
}
});
}

async function showTotalValue(totalOpenedValue, itemID) {
await sleep(0.1 * TO_MILLIS.SECONDS);
const greenMsg = await requireElement(`[data-item="${itemID}"] .cont-wrap form p`);

removeTotalValueElement();

const openedValueTextElement = document.newElement({
id: "ttOpenedValueText",
type: "strong",
class: "tt-opened-supply-pack-value-text",
text: `TornTools total value: ${formatNumber(totalOpenedValue, { currency: true })}`,
});

greenMsg.insertAdjacentElement("beforeend", openedValueTextElement);
}

function isXIDRequestSupplyPack(itemID) {
return SUPPLY_PACK_ITEMS.includes(itemID) && !isDrugPack(itemID);
}

function isDrugPack(itemID) {
return itemID === 370;
}

function isDrugPackUseRequest(params) {
return params.get("item")?.getNumber() === 370 || params.get("itemID")?.getNumber() === 370;
}

function removeTotalValueElement() {
document.getElementById("ttOpenedValueText")?.remove();
}
})();
1 change: 1 addition & 0 deletions extension/scripts/global/globalData.js
Original file line number Diff line number Diff line change
@@ -494,6 +494,7 @@ const DEFAULT_STORAGE = {
candyHappyGains: new DefaultSetting({ type: "boolean", defaultValue: true }),
energyWarning: new DefaultSetting({ type: "boolean", defaultValue: true }),
medicalLife: new DefaultSetting({ type: "boolean", defaultValue: true }),
openedSupplyPackValue: new DefaultSetting({ type: "boolean", defaultValue: true }),
},
crimes: {
quickCrimes: new DefaultSetting({ type: "boolean", defaultValue: true }),

0 comments on commit cd597d2

Please sign in to comment.