@@ -2,6 +2,8 @@ import {decodeLink, CEconItemPreviewDataBlock} from '@csfloat/cs2-inspect-serial
22import { SimpleHandler } from './main' ;
33import { RequestType } from './types' ;
44import { gSchemaFetcher } from '../../services/schema_fetcher' ;
5+ import { gThresholdFetcher } from '../../services/threshold_fetcher' ;
6+ import { gRankBatcher } from '../../services/rank_batcher' ;
57import type { ItemSchema } from '../../types/schema' ;
68
79interface Sticker {
@@ -42,95 +44,144 @@ export interface ItemInfo {
4244
4345export interface FetchInspectInfoRequest {
4446 link : string ;
45- listPrice ?: number ;
47+ asset_id : string ;
4648}
4749
4850export interface FetchInspectInfoResponse {
4951 iteminfo : ItemInfo ;
5052 error ?: string ;
5153}
5254
55+ async function processInspectItem ( req : FetchInspectInfoRequest , schema : ItemSchema . Response ) : Promise < ItemInfo > {
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 weapon = schema . weapons [ defindex ] ;
77+ const paint = getSchemaPaint ( weapon , paintindex ) ;
78+
79+ weaponType = weapon ?. name ;
80+ rarityName = schema . rarities . find ( ( rarity ) => rarity . value === ( paint ?. rarity ?? decoded . rarity ) ) ?. name ;
81+
82+ if ( paint ) {
83+ itemName = paint . name ;
84+ min = paint . min ;
85+ max = paint . max ;
86+ }
87+
88+ stickers = decoded . stickers . map ( ( sticker ) => {
89+ const schemaSticker = schema . stickers [ sticker . stickerId ?. toString ( ) ?? '' ] ;
90+ return {
91+ slot : sticker . slot ?? 0 ,
92+ stickerId : sticker . stickerId ?? 0 ,
93+ wear : sticker . wear ,
94+ name : schemaSticker ?. market_hash_name ,
95+ } ;
96+ } ) ;
97+
98+ keychains = decoded . keychains . map ( ( keychain ) => {
99+ const schemaKeychain = schema . keychains [ keychain . stickerId ?. toString ( ) ?? '' ] ;
100+ return {
101+ slot : keychain . slot ?? 0 ,
102+ stickerId : keychain . stickerId ?? 0 ,
103+ wear : keychain . wear ,
104+ pattern : keychain . pattern ?? 0 ,
105+ name : schemaKeychain ?. market_hash_name ,
106+ } ;
107+ } ) ;
108+ } catch ( error ) {
109+ console . error ( 'Failed to fetch schema item metadata:' , error ) ;
110+ }
111+
112+ const iteminfo : ItemInfo = {
113+ stickers,
114+ keychains,
115+ itemid : decoded . itemid ?. toString ( ) ?? '' ,
116+ defindex,
117+ paintindex,
118+ rarity : decoded . rarity ?? 0 ,
119+ quality : decoded . quality ?? 0 ,
120+ paintseed : decoded . paintseed ?? 0 ,
121+ inventory : decoded . inventory ?? 0 ,
122+ origin : decoded . origin ?? 0 ,
123+ floatvalue,
124+ min,
125+ max,
126+ weapon_type : weaponType ,
127+ item_name : itemName ,
128+ rarity_name : rarityName ,
129+ wear_name : getWearName ( floatvalue ) ,
130+ } ;
131+
132+ try {
133+ if ( decoded . itemid != null && decoded . paintwear != null ) {
134+ const stattrak = decoded . killeaterscoretype !== undefined ;
135+ const souvenir = decoded . quality === 12 ;
136+
137+ if ( await gThresholdFetcher . qualifiesForRankCheck ( defindex , paintindex , stattrak , souvenir , floatvalue ) ) {
138+ const rankResult = await gRankBatcher . check ( req . link , decoded . itemid . toString ( ) ) ;
139+ if ( rankResult ) {
140+ iteminfo . low_rank = rankResult . low_rank ;
141+ iteminfo . high_rank = rankResult . high_rank ;
142+ }
143+ }
144+ }
145+ } catch ( e ) {
146+ console . error ( 'Failed to check rank:' , e ) ;
147+ }
148+
149+ return iteminfo ;
150+ }
151+
53152export const FetchInspectInfo = new SimpleHandler < FetchInspectInfoRequest , FetchInspectInfoResponse > (
54153 RequestType . FETCH_INSPECT_INFO ,
55154 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- }
155+ const schema = await gSchemaFetcher . getSchema ( ) ;
156+ return { iteminfo : await processInspectItem ( req , schema ) } ;
157+ }
158+ ) ;
62159
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- }
160+ export interface FetchInspectInfoBatchRequest {
161+ requests : FetchInspectInfoRequest [ ] ;
162+ }
88163
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- }
164+ export interface FetchInspectInfoBatchResponse {
165+ results : Record < string , FetchInspectInfoResponse > ;
166+ }
112167
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- } ;
168+ export const FetchInspectInfoBatch = new SimpleHandler < FetchInspectInfoBatchRequest , FetchInspectInfoBatchResponse > (
169+ RequestType . FETCH_INSPECT_INFO_BATCH ,
170+ async ( req ) => {
171+ const schema = await gSchemaFetcher . getSchema ( ) ;
172+
173+ const results : Record < string , FetchInspectInfoResponse > = { } ;
174+ await Promise . all (
175+ req . requests . map ( async ( itemReq ) => {
176+ try {
177+ results [ itemReq . asset_id ] = { iteminfo : await processInspectItem ( itemReq , schema ) } ;
178+ } catch ( e ) {
179+ results [ itemReq . asset_id ] = { iteminfo : { } as ItemInfo , error : ( e as any ) . toString ( ) } ;
180+ }
181+ } )
182+ ) ;
183+
184+ return { results} ;
134185 }
135186) ;
136187
0 commit comments