Skip to content

Commit

Permalink
Fix Popup Market Search for Torn v2 API.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sashank999 committed Nov 1, 2024
1 parent 35c1b07 commit 457ca62
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 37 deletions.
3 changes: 2 additions & 1 deletion extension/changelog.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
{ "message": "Display when Live Networth was last updated after 1 hour global cache.", "contributor": "Kwack" },
{ "message": "Fix stats estimates on the attack page not showing up.", "contributor": "DeKleineKobini" },
{ "message": "Fix weapon experience on the attack page not showing up.", "contributor": "DeKleineKobini" },
{ "message": "Fix fair fight modifier on the attack page not showing up.", "contributor": "DeKleineKobini" }
{ "message": "Fix fair fight modifier on the attack page not showing up.", "contributor": "DeKleineKobini" },
{ "message": "Fix Popup Market Search for Torn v2 API.", "contributor": "TheFoxMan" }
],
"changes": [
{ "message": "Adapt cheap item highlights to work with the item market overhaul.", "contributor": "DeKleineKobini" },
Expand Down
87 changes: 52 additions & 35 deletions extension/pages/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,50 +740,49 @@ async function setupMarketSearch() {
const viewItem = document.find("#market #item-information");
viewItem.find(".market").classList.add("tt-hidden");

const item = torndata.items[id];
viewItem.find(".circulation").textContent = formatNumber(item.circulation);
viewItem.find(".value").textContent = `$${formatNumber(item.market_value)}`;
viewItem.find(".name").textContent = item.name;
viewItem.find(".name").href = `https://www.torn.com/imarket.php#/p=shop&step=shop&type=&searchname=${item.name}`;
viewItem.find(".image").src = item.image;

viewItem.classList.remove("tt-hidden");

showLoadingPlaceholder(viewItem.find(".market").parentElement, true);

if (ttCache.hasValue("livePrice", id)) {
handleMarket(ttCache.get("livePrice", id));
showLoadingPlaceholder(viewItem.find(".market").parentElement, false);
} else {
fetchData("torn", { section: "market", id, selections: ["bazaar", "itemmarket"] })
fetchData("tornv2", { section: "market", id, selections: ["bazaar", "itemmarket"] })
.then((result) => {
handleMarket(result);

ttCache.set({ [id]: result }, TO_MILLIS.SECONDS * 30, "livePrice");
})
.catch((error) => {
document.find(".error").classList.remove("tt-hidden");
document.find(".error").textContent = error.error;
});
document.find(".error").textContent = error.message;
}).finally(() => showLoadingPlaceholder(viewItem.find(".market").parentElement, false));
}

const item = torndata.items[id];
viewItem.find(".circulation").textContent = formatNumber(item.circulation);
viewItem.find(".value").textContent = `$${formatNumber(item.market_value)}`;
viewItem.find(".name").textContent = item.name;
viewItem.find(".name").href = `https://www.torn.com/imarket.php#/p=shop&step=shop&type=&searchname=${item.name}`;
viewItem.find(".image").src = item.image;

viewItem.classList.remove("tt-hidden");

function handleMarket(result) {
const list = viewItem.find(".market");
list.innerHTML = "";

let found = false;

for (const type of Object.keys(result)) {
let text;
if (type === "itemmarket") text = "Item Market";
else text = capitalizeText(type);

const wrap = document.newElement({ type: "div" });

wrap.appendChild(document.newElement({ type: "h4", text }));

if (result[type]) {
found = true;

for (const item of result[type].slice(0, 3)) {
wrap.appendChild(
if (!isSellable(id) && !result.bazaar && !result.itemmarket.listings.length) {
list.classList.add("untradable");
list.innerHTML = "Item is not sellable!";
} else {
// Bazaar listings.
const bazaarWrap = document.newElement({ type: "div" });
bazaarWrap.appendChild(document.newElement({ type: "h4", text: "Bazaar" }));
if (result.bazaar) {
for (const item of result.bazaar.slice(0, 3)) {
bazaarWrap.appendChild(
document.newElement({
type: "div",
class: "price",
Expand All @@ -792,21 +791,39 @@ async function setupMarketSearch() {
);
}
} else {
wrap.appendChild(
bazaarWrap.appendChild(
document.newElement({
type: "div",
class: "price no-price",
text: "No price found.",
text: "No listings found.",
})
);
}

list.appendChild(wrap);
}

if (!isSellable(id) && !found) {
list.classList.add("untradable");
list.innerHTML = "Item is not sellable!";
list.appendChild(bazaarWrap);

// Item market listings.
const itemMarketWrap = document.newElement({ type: "div" });
itemMarketWrap.appendChild(document.newElement({ type: "h4", text: "Item Market" }));
if (result.itemmarket?.listings?.length) {
for (const item of result.itemmarket.listings.slice(0, 3)) {
itemMarketWrap.appendChild(
document.newElement({
type: "div",
class: "price",
text: `${item.amount}x | $${formatNumber(item.price)}`,
})
);
}
} else {
itemMarketWrap.appendChild(
document.newElement({
type: "div",
class: "price no-price",
text: "No listings found.",
})
);
}
list.appendChild(itemMarketWrap);
}
viewItem.find(".market").classList.remove("tt-hidden");
}
Expand Down
2 changes: 1 addition & 1 deletion extension/scripts/global/functions/torn.js
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ function isSellable(id) {
1009, // Halloween Basket
1010, // Halloween Basket
1011, // Halloween Basket
].includes(item.id)
].includes(item.id || parseInt(id))
);
}

Expand Down

0 comments on commit 457ca62

Please sign in to comment.