-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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>
1 parent
9656832
commit cd597d2
Showing
6 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
extension/scripts/features/opened-supply-pack-value/ttOpenedSupplyPackValue.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
84 changes: 84 additions & 0 deletions
84
extension/scripts/features/opened-supply-pack-value/ttOpenedSupplyPackValue.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters