1+ import { decodeLink , CEconItemPreviewDataBlock } from '@csfloat/cs2-inspect-serializer' ;
12import { SimpleHandler } from './main' ;
23import { RequestType } from './types' ;
4+ import { gSchemaFetcher } from '../../services/schema_fetcher' ;
5+ import type { ItemSchema } from '../../types/schema' ;
36
47interface Sticker {
58 slot : number ;
69 stickerId : number ;
7- codename ?: string ;
8- material ?: string ;
910 name ?: string ;
1011 wear ?: number ;
1112}
@@ -25,12 +26,7 @@ export interface ItemInfo {
2526 paintseed : number ;
2627 inventory : number ;
2728 origin : number ;
28- s : string ;
29- a : string ;
30- d : string ;
31- m : string ;
3229 floatvalue : number ;
33- imageurl : string ;
3430 min : number ;
3531 max : number ;
3632 weapon_type ?: string ;
@@ -56,16 +52,125 @@ export interface FetchInspectInfoResponse {
5652
5753export const FetchInspectInfo = new SimpleHandler < FetchInspectInfoRequest , FetchInspectInfoResponse > (
5854 RequestType . FETCH_INSPECT_INFO ,
59- ( req ) => {
60- const apiUrl = `https://api.csfloat.com/?url=${ req . link } &minimal=true${ req . listPrice ? '&listPrice=' + req . listPrice : '' } ` ;
61- return fetch ( apiUrl ) . then ( ( resp ) => {
62- return resp . json ( ) . then ( ( json : FetchInspectInfoResponse ) => {
63- if ( resp . ok ) {
64- return json ;
65- } else {
66- throw Error ( json . error ) ;
67- }
68- } ) as Promise < FetchInspectInfoResponse > ;
69- } ) ;
55+ async ( req ) => {
56+ let decoded : CEconItemPreviewDataBlock ;
57+ try {
58+ decoded = decodeLink ( req . link ) ;
59+ } catch ( error ) {
60+ throw new Error ( 'Failed to decode inspect link' ) ;
61+ }
62+
63+ const defindex = decoded . defindex ?? 0 ;
64+ const paintindex = decoded . paintindex ?? 0 ;
65+ const floatvalue = decoded . paintwear ?? 0 ;
66+
67+ let min = 0 ;
68+ let max = 1 ;
69+ let weaponType : string | undefined ;
70+ let itemName : string | undefined ;
71+ let rarityName : string | undefined ;
72+ let stickers : Sticker [ ] = [ ] ;
73+ let keychains : Keychain [ ] = [ ] ;
74+
75+ try {
76+ const schema = await gSchemaFetcher . getSchema ( ) ;
77+ const weapon = schema . weapons [ defindex ] ;
78+ const paint = getSchemaPaint ( weapon , paintindex ) ;
79+
80+ weaponType = weapon ?. name ;
81+ rarityName = schema . rarities . find ( ( rarity ) => rarity . value === ( paint ?. rarity ?? decoded . rarity ) ) ?. name ;
82+
83+ if ( paint ) {
84+ itemName = paint . name ;
85+ min = paint . min ;
86+ max = paint . max ;
87+ }
88+
89+ stickers = decoded . stickers . map ( ( sticker ) => {
90+ const schemaSticker = schema . stickers [ sticker . stickerId ?. toString ( ) ?? '' ] ;
91+ return {
92+ slot : sticker . slot ?? 0 ,
93+ stickerId : sticker . stickerId ?? 0 ,
94+ wear : sticker . wear ,
95+ name : schemaSticker ?. market_hash_name ,
96+ } ;
97+ } ) ;
98+
99+ keychains = decoded . keychains . map ( ( keychain ) => {
100+ const schemaKeychain = schema . keychains [ keychain . stickerId ?. toString ( ) ?? '' ] ;
101+ return {
102+ slot : keychain . slot ?? 0 ,
103+ stickerId : keychain . stickerId ?? 0 ,
104+ wear : keychain . wear ,
105+ pattern : keychain . pattern ?? 0 ,
106+ name : schemaKeychain ?. market_hash_name ,
107+ } ;
108+ } ) ;
109+ } catch ( error ) {
110+ console . error ( 'Failed to fetch schema item metadata:' , error ) ;
111+ }
112+
113+ return {
114+ iteminfo : {
115+ stickers,
116+ keychains,
117+ itemid : decoded . itemid ?. toString ( ) ?? '' ,
118+ defindex,
119+ paintindex,
120+ rarity : decoded . rarity ?? 0 ,
121+ quality : decoded . quality ?? 0 ,
122+ paintseed : decoded . paintseed ?? 0 ,
123+ inventory : decoded . inventory ?? 0 ,
124+ origin : decoded . origin ?? 0 ,
125+ floatvalue,
126+ min,
127+ max,
128+ weapon_type : weaponType ,
129+ item_name : itemName ,
130+ rarity_name : rarityName ,
131+ wear_name : getWearName ( floatvalue ) ,
132+ } ,
133+ } ;
70134 }
71135) ;
136+
137+ function getSchemaPaint ( weapon : ItemSchema . RawWeapon | undefined , paintIndex : number ) : ItemSchema . RawPaint | undefined {
138+ if ( ! weapon ) {
139+ return undefined ;
140+ }
141+
142+ if ( weapon . paints [ paintIndex ] !== undefined ) {
143+ return weapon . paints [ paintIndex ] ;
144+ }
145+
146+ if ( weapon . paints ?. [ '0' ] !== undefined ) {
147+ return weapon . paints [ '0' ] ;
148+ }
149+
150+ const availablePaintIndexes = Object . keys ( weapon . paints || { } ) ;
151+ if ( availablePaintIndexes . length === 1 ) {
152+ return weapon . paints [ availablePaintIndexes [ 0 ] ] ;
153+ }
154+ }
155+
156+ function getWearName ( floatvalue : number ) : string | undefined {
157+ if ( floatvalue <= 0.07 ) {
158+ return 'Factory New' ;
159+ }
160+
161+ if ( floatvalue <= 0.15 ) {
162+ return 'Minimal Wear' ;
163+ }
164+
165+ if ( floatvalue <= 0.38 ) {
166+ return 'Field-Tested' ;
167+ }
168+
169+ if ( floatvalue <= 0.45 ) {
170+ return 'Well-Worn' ;
171+ }
172+
173+ if ( floatvalue <= 1 ) {
174+ return 'Battle-Scarred' ;
175+ }
176+ }
0 commit comments