Skip to content

Commit

Permalink
fix: csmoney item detection
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums committed Dec 11, 2024
1 parent 97832ca commit 25bad94
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/contents/buffmarket_script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { activateHandler, initPriceMapping } from '~lib/handlers/eventhandler';
import { BigCurrency, SmallCurrency, getBuffCurrencyRate, getBuffGoodsInfo, getBuffMarketItem, getFirstBuffPageItem, getMarketID } from '~lib/handlers/mappinghandler';
import { MarketSource } from '~lib/util/globals';
import { getBuffPrice, handleSpecialStickerNames, isBuffBannedItem } from '~lib/util/helperfunctions';
import { getAllSettings, type IStorage } from '~lib/util/storage';
import { type IStorage, getAllSettings } from '~lib/util/storage';
import { generatePriceLine } from '~lib/util/uigeneration';

export const config: PlasmoCSConfig = {
Expand Down
43 changes: 27 additions & 16 deletions src/contents/csmoney_script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ import type { PlasmoCSConfig } from 'plasmo';

import type { CSMoney } from '~lib/@typings/CsmoneyTypes';
import type { DopplerPhase, ItemStyle } from '~lib/@typings/FloatTypes';
import { getFirstCSMoneyUserInventoryItem, getFirstCSMoneyBotInventoryItem, getFirstCSMoneyItem, getSpecificCSMoneyItem } from '~lib/handlers/cache/csmoney_cache';
import {
getFirstCSMoneyBotInventoryItem,
getFirstCSMoneyItem,
getFirstCSMoneyUserInventoryItem,
getSpecificCSMoneyItem,
isCSMoneyBotInventoryEmpty,
isCSMoneyUserInventoryEmpty,
} from '~lib/handlers/cache/csmoney_cache';
import { activateHandler, initPriceMapping } from '~lib/handlers/eventhandler';
import { getAndFetchCurrencyRate, getMarketID } from '~lib/handlers/mappinghandler';
import { MarketSource } from '~lib/util/globals';
import { handleSpecialStickerNames, getBuffPrice, BigUSDollar, USDollar, isBuffBannedItem, parsePrice } from '~lib/util/helperfunctions';
import { getAllSettings, type IStorage } from '~lib/util/storage';
import { BigUSDollar, USDollar, getBuffPrice, handleSpecialStickerNames, isBuffBannedItem, parsePrice } from '~lib/util/helperfunctions';
import { type IStorage, getAllSettings } from '~lib/util/storage';
import { generatePriceLine } from '~lib/util/uigeneration';

export const config: PlasmoCSConfig = {
Expand Down Expand Up @@ -114,12 +121,20 @@ async function adjustItem(container: Element, isPopout = false) {
return newItem;
}
};
const isInventoryEmpty = () => {
if (location.pathname === '/csgo/trade/') {
const isUserItem = !container.closest('#botInventory');
return isUserItem ? isCSMoneyUserInventoryEmpty() : isCSMoneyBotInventoryEmpty();
} else {
return !getFirstCSMoneyItem();
}
};
let apiItem = getApiItem();

let attempts = 0;
while (!apiItem && attempts++ < 5) {
while (!apiItem && attempts++ < 5 && isInventoryEmpty()) {
// wait for 1s and try again
console.log('[BetterFloat] No item found, waiting 1s and trying again...');
console.log('[BetterFloat] No item found, waiting 1s and trying again...', container);
await new Promise((resolve) => setTimeout(resolve, 1000));
apiItem = getApiItem();
}
Expand Down Expand Up @@ -184,7 +199,7 @@ async function getBuffItem(container: Element, item: CSMoney.Item) {
symbol: currencyItem?.symbol,
text: currencyItem?.text,
rate: currencyRate,
}
},
};
}

Expand All @@ -195,12 +210,7 @@ function getItemPrice(item: CSMoney.Item): number {
const invItem = item as CSMoney.InventoryItem;
const marketItem = item as CSMoney.MarketItem;

return invItem.sellPrice ??
invItem.recommendedPrice?.decreased ??
marketItem.pricing?.computed ??
invItem.botPrice ??
invItem.price ??
0;
return invItem.sellPrice ?? invItem.recommendedPrice?.decreased ?? marketItem.pricing?.computed ?? invItem.botPrice ?? invItem.price ?? 0;
}

function getHTMLPrice(container: Element) {
Expand Down Expand Up @@ -289,7 +299,7 @@ async function addBuffPrice(item: CSMoney.Item, container: Element, isPopout = f
priceClass: 'suggested-price',
addSpaceBetweenPrices: false,
showPrefix: false,
iconHeight: '15px'
iconHeight: '15px',
});

if (footerContainer) {
Expand Down Expand Up @@ -341,8 +351,9 @@ async function addBuffPrice(item: CSMoney.Item, container: Element, isPopout = f

const buffPriceHTML = `<div class="sale-tag betterfloat-sale-tag" style="background-color: ${background}; color: ${color}; padding: 1px 3px; border-radius: 4px;${
isPopout ? ' margin-left: 10px;' : ''
}" data-betterfloat="${difference}"><span>${difference.isPos() ? '+' : '-'}${CurrencyFormatter.format(absDifference.toNumber())
}</span><span>(${percentage.gt(150) ? percentage.toFixed(0) : percentage.toFixed(2)}%)</span></div>`;
}" data-betterfloat="${difference}"><span>${difference.isPos() ? '+' : '-'}${CurrencyFormatter.format(
absDifference.toNumber()
)}</span><span>(${percentage.gt(150) ? percentage.toFixed(0) : percentage.toFixed(2)}%)</span></div>`;
priceContainer?.setAttribute('style', 'display: flex; gap: 5px; align-items: center;');

priceContainer?.insertAdjacentHTML('beforeend', buffPriceHTML);
Expand Down
38 changes: 19 additions & 19 deletions src/css/csmoney_styles.css
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
.betterfloat-buffprice {
margin-left: 2px;
padding-top: 1px;
font-size: 13px;
font-style: normal;
font-weight: 500;
line-height: 20px;
letter-spacing: .02em;
font-size: 13px;
font-style: normal;
font-weight: 500;
line-height: 20px;
letter-spacing: .02em;
}

.betterfloat-sale-tag {
font-size: 11px;
font-style: normal;
font-weight: 525;
line-height: 17px;
letter-spacing: -.005em;
display: flex;
flex-direction: column;
font-size: 11px;
font-style: normal;
font-weight: 525;
line-height: 17px;
letter-spacing: -.005em;
display: flex;
flex-direction: column;
}

.betterfloat-sale-tag span {
vertical-align: middle;
vertical-align: middle;
}

.betterfloat-buff-a {
width: 100%;
display: inline-flex;
align-items: center;
flex-direction: row;
justify-content: center;
width: 100%;
display: inline-flex;
align-items: center;
flex-direction: row;
/* justify-content: center; */
}

.betterfloat-buff-tooltip {
Expand Down Expand Up @@ -57,4 +57,4 @@
visibility: visible;
opacity: 1;
transition: visibility 0s, opacity 0.2s linear;
}
}
27 changes: 21 additions & 6 deletions src/lib/handlers/cache/csmoney_cache.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { CSMoney } from "~lib/@typings/CsmoneyTypes";
import type { CSMoney } from '~lib/@typings/CsmoneyTypes';

// csmoney: cached items from api
let csmoneyItems: CSMoney.Item[] = [];
const csmoneyItemMapping: { [itemId: number]: CSMoney.Item } = {};
let csmoneyUserInventory: CSMoney.InventoryItem[] = [];
let csmoneyBotInventory: CSMoney.InventoryItem[] = [];


export function cacheCSMoneyItems(data: CSMoney.Item[]) {
if (!csmoneyItems) {
csmoneyItems = [];
Expand All @@ -18,25 +17,41 @@ export function cacheCSMoneyItems(data: CSMoney.Item[]) {
data.forEach((item) => {
csmoneyItemMapping[item.id] = item;
});
csmoneyItems = data;
csmoneyItems = Object.assign([], data);
}

export function cacheCSMoneyUserInventory(data: CSMoney.InventoryItem[]) {
csmoneyUserInventory = data;
if (csmoneyUserInventory.length > 0) {
csmoneyUserInventory.push(...data);
} else {
csmoneyUserInventory = Object.assign(csmoneyUserInventory, data);
}
}

export function cacheCSMoneyBotInventory(data: CSMoney.InventoryItem[]) {
csmoneyBotInventory = data;
if (csmoneyBotInventory.length > 0) {
csmoneyBotInventory.push(...data);
} else {
csmoneyBotInventory = Object.assign(csmoneyBotInventory, data);
}
}

export function getFirstCSMoneyUserInventoryItem() {
return csmoneyUserInventory?.shift();
}

export function isCSMoneyUserInventoryEmpty() {
return csmoneyUserInventory.length === 0;
}

export function getFirstCSMoneyBotInventoryItem() {
return csmoneyBotInventory?.shift();
}

export function isCSMoneyBotInventoryEmpty() {
return csmoneyBotInventory.length === 0;
}

export function getFirstCSMoneyItem() {
if (!csmoneyItems) {
csmoneyItems = [];
Expand All @@ -52,4 +67,4 @@ export function getFirstCSMoneyItem() {

export function getSpecificCSMoneyItem(itemId: number) {
return csmoneyItemMapping[itemId];
}
}
6 changes: 3 additions & 3 deletions src/lib/handlers/eventhandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { handleListed, handleSold } from '~lib/helpers/websockethandler';

import { sendToBackground } from '@plasmohq/messaging';
import type { BuffMarket } from '~lib/@typings/BuffmarketTypes';
import type { CSMoney } from '~lib/@typings/CsmoneyTypes';
import { adjustOfferBubbles } from '~lib/helpers/csfloat_helpers';
import { addTotalInventoryPrice } from '~lib/helpers/skinport_helpers';
import { MarketSource } from '~lib/util/globals';
Expand All @@ -21,12 +22,11 @@ import {
cacheCSFPopupItem,
cacheCSFSimilarItems,
} from './cache/csfloat_cache';
import { cacheCSMoneyBotInventory, cacheCSMoneyItems, cacheCSMoneyUserInventory } from './cache/csmoney_cache';
import { cacheSkbInventory, cacheSkbItems, cacheSkinbidCurrencyRates, cacheSkinbidUserCurrency } from './cache/skinbid_cache';
import { cacheSkinportCurrencyRates, cacheSpItems, cacheSpMinOrderPrice, cacheSpPopupInventoryItem, cacheSpPopupItem } from './cache/skinport_cache';
import { cacheBuffCurrencyRate, cacheBuffGoodsInfos, cacheBuffMarketItems, cacheBuffPageItems, cacheBuffUserId, loadMapping } from './mappinghandler';
import { urlHandler } from './urlhandler';
import type { CSMoney } from '~lib/@typings/CsmoneyTypes';
import { cacheCSMoneyItems, cacheCSMoneyUserInventory, cacheCSMoneyBotInventory } from './cache/csmoney_cache';

type StallData = {
data: CSFloat.ListingData[];
Expand Down Expand Up @@ -284,4 +284,4 @@ function processCSMoneyEvent(eventData: EventData<unknown>) {
} else if (eventData.url.includes('5.0/load_bots_inventory/730')) {
cacheCSMoneyBotInventory((eventData.data as CSMoney.UserInventoryResponse).items);
}
}
}
2 changes: 1 addition & 1 deletion src/lib/util/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function openIntercept() {
(<XMLHttpRequest>this).addEventListener('load', (e) => {
const target = <XMLHttpRequest>e.currentTarget;
if (!target.responseURL.includes(location.hostname)) {
console.debug('[BetterFloat] Ignoring HTTP request to: ' + target.responseURL);
// console.debug('[BetterFloat] Ignoring HTTP request to: ' + target.responseURL);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/popup.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import '~style.css';
import buffmarketLogo from 'data-base64:~/../assets/buffmarket.ico';
import csfloatLogo from 'data-base64:~/../assets/csfloat.png';
import csmoneyLogo from 'data-base64:~/../assets/csmoney.ico';
import betterfloatLogo from 'data-base64:~/../assets/icon.png';
import lisskinsLogo from 'data-base64:~/../assets/lisskins.svg';
import skinportLogo from 'data-base64:~/../assets/skinport.ico';
import csmoneyLogo from 'data-base64:~/../assets/csmoney.ico';
import { useEffect } from 'react';
import { IcRoundWarning, MdiGithub, SkillIconsDiscord, SkinBidIcon, SolarDocumentTextLinear, SolarInfoSquareLinear } from '~lib/components/Icons';
import { Badge, Button, Tabs, TabsList, TabsTrigger } from '~lib/components/Shadcn';
Expand All @@ -14,12 +14,12 @@ import { About } from '~lib/pages/About';
import { BuffMarketSettings } from '~lib/pages/Buffmarket';
import { Changelogs } from '~lib/pages/Changelog';
import { CSFloatSettings } from '~lib/pages/Csfloat';
import { CSMoneySettings } from '~lib/pages/Csmoney';
import { LisSkinsSettings } from '~lib/pages/Lisskins';
import { SkinbidSettings } from '~lib/pages/Skinbid';
import { SkinportSettings } from '~lib/pages/Skinport';
import { DISCORD_URL, GITHUB_URL, WEBSITE_URL } from '~lib/util/globals';
import { DEFAULT_SETTINGS } from '~lib/util/storage';
import { CSMoneySettings } from '~lib/pages/Csmoney';

export default function IndexPopup() {
const hostpermissions = chrome.runtime.getManifest().host_permissions as string[];
Expand Down

0 comments on commit 25bad94

Please sign in to comment.