Skip to content

Commit

Permalink
correctly matches items, fixes #129
Browse files Browse the repository at this point in the history
  • Loading branch information
gergelyszabo94 committed Mar 15, 2020
1 parent 824633d commit 7d1a2f4
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions extension/src/contentScripts/steam/tradeOffers.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const matchItemsWithDescriptions = (items) => {
instanceid: item.instanceid,
assetid: item.assetid,
position: item.position,
side: item.side,
dopplerInfo: (item.name.includes('Doppler') || item.name.includes('doppler')) ? getDopplerInfo(item.icon_url) : null,
exterior: getExteriorFromTags(item.tags),
iconURL: item.icon_url,
Expand Down Expand Up @@ -84,10 +85,14 @@ const getIDsFromElement = (element) => {
};
};

const getItemByIDs = (items, IDs) => {
// class and instance IDs are the only IDs that can be extracted from the page
// they are not enough to match every item exactly all the time
// side of the offer and position must be used to to uniquely match items
const findItem = (items, IDs, side, position) => {
if (IDs.instanceid !== null) {
return items.filter((item) => {
return item.classid === IDs.classid && item.instanceid === IDs.instanceid;
return item.classid === IDs.classid && item.instanceid === IDs.instanceid
&& item.position === position && item.side === side;
})[0];
}
return items.filter((item) => item.classid === IDs.classid)[0];
Expand All @@ -103,6 +108,8 @@ const extractItemsFromOffers = (offers) => {
...item,
owner: getProperStyleSteamIDFromOfferStyle(offer.accountid_other),
position: index,
inOffer: offer.tradeofferid,
side: 'your',
});
});
}
Expand All @@ -112,6 +119,8 @@ const extractItemsFromOffers = (offers) => {
...item,
owner: getProperStyleSteamIDFromOfferStyle(offer.accountid_other),
position: index,
inOffer: offer.tradeofferid,
side: 'their',
});
});
}
Expand All @@ -124,20 +133,29 @@ const extractItemsFromOffers = (offers) => {
const addItemInfo = (items) => {
let activeItemElements = [];
document.querySelectorAll('.tradeoffer').forEach((offerElement) => {
const offerItemsElement = offerElement.querySelector('.tradeoffer_items_ctn');

if (isOfferActive(offerElement)) {
const activeItemElementsInOffer = [...offerItemsElement.querySelectorAll('.trade_item')].map((item) => {
return item;
const primary = offerElement.querySelector('.tradeoffer_items.primary');
const secondary = offerElement.querySelector('.tradeoffer_items.secondary');
const theirSide = activePage === 'incoming_offers' ? primary : secondary;
const yourSide = activePage === 'incoming_offers' ? secondary : primary;

const yourSideItems = [...yourSide.querySelectorAll('.trade_item')].map((itemElement, index) => {
return { itemElement, side: 'your', position: index };
});

const theirSideItems = [...theirSide.querySelectorAll('.trade_item')].map((itemElement, index) => {
return { itemElement, side: 'their', position: index };
});

const activeItemElementsInOffer = yourSideItems.concat(theirSideItems);
activeItemElements = activeItemElements.concat(activeItemElementsInOffer);
}
});

chrome.storage.local.get(['colorfulItems', 'autoFloatOffer', 'showStickerPrice'], (result) => {
activeItemElements.forEach((itemElement) => {
activeItemElements.forEach(({ itemElement, side, position }) => {
if ((itemElement.getAttribute('data-processed') === null || itemElement.getAttribute('data-processed') === 'false') && isCSGOItemElement(itemElement)) {
const item = getItemByIDs(items, getIDsFromElement(itemElement));
const item = findItem(items, getIDsFromElement(itemElement), side, position);

if (item !== undefined) {
addDopplerPhase(itemElement, item.dopplerInfo);
Expand Down Expand Up @@ -179,7 +197,6 @@ const getOffersFromAPI = (type) => {
};

const addTotals = (offers, items) => {
console.log(offers);
chrome.storage.local.get('currency', (result) => {
let totalProfit = 0.0;
let activeOfferCount = 0;
Expand Down Expand Up @@ -583,8 +600,11 @@ if (activePage === 'incoming_offers' || activePage === 'sent_offers') {

const matchedItems = matchItemsWithDescriptions(itemsWithMoreInfo);

chrome.runtime.sendMessage({ addPricesAndFloatsToInventory: matchedItems }, (response) => {
const itemsWithAllInfo = response.addPricesAndFloatsToInventory;
chrome.runtime.sendMessage({
addPricesAndFloatsToInventory: matchedItems,
}, ({ addPricesAndFloatsToInventory }) => {
const itemsWithAllInfo = addPricesAndFloatsToInventory;

addItemInfo(itemsWithAllInfo);

if (activePage === 'incoming_offers') {
Expand Down

0 comments on commit 7d1a2f4

Please sign in to comment.