Skip to content

Commit 8519b08

Browse files
authored
Merge pull request #273 from csfloat/fix/inventory-selected-commodity
Fix: Selected Item Info for Commodities (fixes #271)
2 parents 9de3382 + e3b0600 commit 8519b08

File tree

2 files changed

+77
-41
lines changed

2 files changed

+77
-41
lines changed

src/lib/components/inventory/selected_item_info.ts

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import {FloatElement} from '../custom';
22
import {CustomElement, InjectAfter, InjectionMode} from '../injectors';
3-
import {html, css, TemplateResult, HTMLTemplateResult, nothing} from 'lit';
3+
import {html, css, TemplateResult, HTMLTemplateResult} from 'lit';
44
import {state} from 'lit/decorators.js';
55
import {InventoryAsset} from '../../types/steam';
66
import {gFloatFetcher} from '../../services/float_fetcher';
77
import {ItemInfo} from '../../bridge/handlers/fetch_inspect_info';
8-
import {formatSeed, getFadePercentage, isSkin, renderClickableRank, floor, isCharm} from '../../utils/skin';
8+
import {
9+
formatSeed,
10+
getFadePercentage,
11+
isSkin,
12+
renderClickableRank,
13+
floor,
14+
isCharm,
15+
isSellableOnCSFloat,
16+
} from '../../utils/skin';
917
import {Observe} from '../../utils/observers';
1018
import {FetchStallResponse} from '../../bridge/handlers/fetch_stall';
1119
import {gStallFetcher} from '../../services/stall_fetcher';
@@ -86,33 +94,40 @@ export class SelectedItemInfo extends FloatElement {
8694
return html`<div>Loading...</div>`;
8795
}
8896

89-
if (!this.itemInfo || !this.asset?.description) {
97+
if (!this.asset?.description) {
9098
return html``;
9199
}
92100

93-
if (isSkin(this.asset.description)) {
94-
const fadePercentage = this.asset && getFadePercentage(this.asset.description, this.itemInfo);
95-
96-
return html`
97-
<div class="container">
98-
<div>Float: ${this.itemInfo.floatvalue.toFixed(14)} ${renderClickableRank(this.itemInfo)}</div>
99-
<div>Paint Seed: ${formatSeed(this.itemInfo)}</div>
100-
${fadePercentage !== undefined ? html`<div>Fade: ${floor(fadePercentage, 5)}%</div>` : nothing}
101-
${this.renderListOnCSFloat()} ${this.renderFloatMarketListing()}
102-
</div>
103-
`;
104-
} else if (isCharm(this.asset.description)) {
105-
return html`
106-
<div class="container">
107-
<div>
108-
Pattern:
109-
#${this.itemInfo.keychains?.length > 0 ? this.itemInfo.keychains[0].pattern : 'Unknown'}
110-
</div>
111-
</div>
112-
`;
113-
} else {
101+
const containerChildren: TemplateResult[] = [];
102+
103+
if (isSkin(this.asset.description) && this.itemInfo) {
104+
containerChildren.push(
105+
html`<div>Float: ${this.itemInfo.floatvalue.toFixed(14)} ${renderClickableRank(this.itemInfo)}</div>`
106+
);
107+
108+
containerChildren.push(html`<div>Paint Seed: ${formatSeed(this.itemInfo)}</div>`);
109+
110+
const fadePercentage = getFadePercentage(this.asset.description, this.itemInfo);
111+
if (fadePercentage !== undefined) {
112+
containerChildren.push(html`<div>Fade: ${floor(fadePercentage, 5)}%</div>`);
113+
}
114+
} else if (isCharm(this.asset.description) && this.itemInfo) {
115+
containerChildren.push(
116+
html`<div>
117+
Pattern: #${this.itemInfo.keychains?.length > 0 ? this.itemInfo.keychains[0].pattern : 'Unknown'}
118+
</div>`
119+
);
120+
}
121+
122+
if (isSellableOnCSFloat(this.asset.description)) {
123+
containerChildren.push(html`${this.renderListOnCSFloat()} ${this.renderFloatMarketListing()}`);
124+
}
125+
126+
if (containerChildren.length === 0) {
114127
return html``;
115128
}
129+
130+
return html` <div class="container">${containerChildren}</div> `;
116131
}
117132

118133
renderFloatMarketListing(): TemplateResult<1> {
@@ -146,7 +161,7 @@ export class SelectedItemInfo extends FloatElement {
146161

147162
return html`
148163
<div class="market-btn-container">
149-
<a class="market-btn" href="https://csfloat.com" target="_blank">
164+
<a class="market-btn" href="https://csfloat.com/sell" target="_blank">
150165
<span>List on </span>
151166
<img src="https://csfloat.com/assets/n_full_logo.png" height="21" style="margin-left: 5px;" />
152167
</a>
@@ -160,21 +175,19 @@ export class SelectedItemInfo extends FloatElement {
160175

161176
if (!this.asset) return;
162177

163-
if (!isSkin(this.asset.description) && !isCharm(this.asset.description)) return;
178+
// Guarantees a re-render for items without inspect links
179+
this.loading = true;
164180

165-
// Commodities won't have inspect links
166-
if (!this.inspectLink) return;
167-
168-
try {
169-
this.loading = true;
170-
this.itemInfo = await gFloatFetcher.fetch({
171-
link: this.inspectLink,
172-
});
173-
} catch (e: any) {
174-
console.error(`Failed to fetch float for ${this.asset.assetid}: ${e.toString()}`);
175-
} finally {
176-
this.loading = false;
181+
if (this.inspectLink && (isSkin(this.asset.description) || isCharm(this.asset.description))) {
182+
try {
183+
this.itemInfo = await gFloatFetcher.fetch({
184+
link: this.inspectLink,
185+
});
186+
} catch (e: any) {
187+
console.error(`Failed to fetch float for ${this.asset.assetid}: ${e.toString()}`);
188+
}
177189
}
190+
this.loading = false;
178191
}
179192

180193
connectedCallback() {

src/lib/utils/skin.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ export function renderClickableRank(info: ItemInfo): TemplateResult<1> {
104104
</a>`;
105105
}
106106

107+
export function isSellableOnCSFloat(asset: rgAsset): boolean {
108+
return isSkin(asset) || isCharm(asset) || isAgent(asset) || isSticker(asset) || isPatch(asset) || isCase(asset);
109+
}
110+
107111
export function isSkin(asset: rgAsset): boolean {
108112
return asset.tags
109113
? asset.tags.some((a) => a.category === 'Weapon' || (a.category === 'Type' && a.internal_name === 'Type_Hands'))
@@ -113,16 +117,35 @@ export function isSkin(asset: rgAsset): boolean {
113117
}
114118

115119
export function isCharm(asset: rgAsset): boolean {
116-
if (asset.market_hash_name.startsWith('Charm')) {
117-
// Tags aren't available on SCM items, so use a MHN heuristic instead
120+
return isAbstractType(asset, 'Charm', 'CSGO_Type_Charm');
121+
}
122+
123+
export function isAgent(asset: rgAsset): boolean {
124+
return isAbstractType(asset, 'Agent', 'Type_CustomPlayer');
125+
}
126+
127+
export function isSticker(asset: rgAsset): boolean {
128+
return isAbstractType(asset, 'Sticker', 'CSGO_Tool_Sticker');
129+
}
130+
131+
export function isPatch(asset: rgAsset): boolean {
132+
return isAbstractType(asset, 'Patch', 'CSGO_Type_Patch');
133+
}
134+
135+
export function isCase(asset: rgAsset): boolean {
136+
return isAbstractType(asset, 'Container', 'CSGO_Type_WeaponCase');
137+
}
138+
139+
function isAbstractType(asset: rgAsset, type: string, internalName: string): boolean {
140+
if (asset.type.endsWith(type)) {
118141
return true;
119142
}
120143

121144
if (!asset.tags) {
122145
return false;
123146
}
124147

125-
return asset.tags.some((e) => e.category === 'Type' && e.internal_name === 'CSGO_Tool_Keychain');
148+
return asset.tags.some((e) => e.category === 'Type' && e.internal_name === internalName);
126149
}
127150

128151
export function getFadeCalculatorAndSupportedWeapon(

0 commit comments

Comments
 (0)