1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Steam Economy features.
//!
//! This module provides access to Steam's economy system including:
//! - Trade URL management
//! - Asset class information lookup
//! - Emoticon lists
//! - Profile item management (backgrounds, avatar frames, etc.)
use std::collections::HashMap;
use steamid::SteamID;
use crate::{error::SteamError, SteamClient};
/// Asset class information for an item.
#[derive(Debug, Clone)]
pub struct AssetClassInfo {
/// App ID
pub appid: i32,
/// Class ID
pub classid: u64,
/// Instance ID
pub instanceid: u64,
/// Display name
pub name: String,
/// Market hash name for trading
pub market_hash_name: Option<String>,
/// Market display name
pub market_name: Option<String>,
/// Name color (hex)
pub name_color: Option<String>,
/// Background color (hex)
pub background_color: Option<String>,
/// Item type description
pub item_type: Option<String>,
/// Icon URL (append to Steam CDN base URL)
pub icon_url: Option<String>,
/// Large icon URL
pub icon_url_large: Option<String>,
/// Is item tradable
pub tradable: bool,
/// Is item marketable
pub marketable: bool,
/// Is item a commodity (stackable on market)
pub commodity: bool,
}
/// A class to look up asset info for.
#[derive(Debug, Clone)]
pub struct AssetClass {
/// Class ID of the asset
pub classid: u64,
/// Instance ID (optional, defaults to 0)
pub instanceid: Option<u64>,
}
/// Trade URL information.
#[derive(Debug, Clone)]
pub struct TradeUrl {
/// The trade offer access token
pub token: String,
/// Full trade URL
pub url: String,
}
/// Emoticon information.
#[derive(Debug, Clone)]
pub struct Emoticon {
/// Emoticon name (e.g., "steamhappy")
pub name: String,
/// Number of times used
pub use_count: u32,
/// Timestamp last used
pub time_last_used: Option<u32>,
/// Timestamp received
pub time_received: Option<u32>,
/// App ID that granted this emoticon
pub appid: Option<u32>,
}
/// Profile item information.
#[derive(Debug, Clone)]
pub struct ProfileItem {
/// Community item ID
pub communityitemid: u64,
/// Large image URL
pub image_large: Option<String>,
/// Small image URL
pub image_small: Option<String>,
/// Item name
pub name: Option<String>,
/// Item title
pub item_title: Option<String>,
/// Item description
pub item_description: Option<String>,
/// App ID that granted this item
pub appid: Option<u32>,
/// Item type
pub item_type: Option<u32>,
/// Item class
pub item_class: Option<u32>,
/// Movie WebM URL
pub movie_webm: Option<String>,
/// Movie MP4 URL
pub movie_mp4: Option<String>,
}
/// Owned profile items organized by category.
#[derive(Debug, Clone, Default)]
pub struct OwnedProfileItems {
/// Profile backgrounds
pub profile_backgrounds: Vec<ProfileItem>,
/// Mini profile backgrounds
pub mini_profile_backgrounds: Vec<ProfileItem>,
/// Avatar frames
pub avatar_frames: Vec<ProfileItem>,
/// Animated avatars
pub animated_avatars: Vec<ProfileItem>,
/// Profile modifiers
pub profile_modifiers: Vec<ProfileItem>,
}
/// Equipped profile items.
#[derive(Debug, Clone, Default)]
pub struct EquippedProfileItems {
/// Profile background
pub profile_background: Option<ProfileItem>,
/// Mini profile background
pub mini_profile_background: Option<ProfileItem>,
/// Avatar frame
pub avatar_frame: Option<ProfileItem>,
/// Animated avatar
pub animated_avatar: Option<ProfileItem>,
/// Profile modifier
pub profile_modifier: Option<ProfileItem>,
}
const STEAM_CDN_BASE: &str = "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/";
impl SteamClient {
/// Get asset class information for items in a specific app.
///
/// This fetches detailed information about items based on their class IDs.
///
/// # Arguments
///
/// * `language` - Language code for descriptions (e.g., "english",
/// "german")
/// * `appid` - App ID the items belong to
/// * `classes` - List of asset classes to look up
///
/// # Example
///
/// ```rust,ignore
/// let classes = vec![
/// AssetClass { classid: 123456789, instanceid: None },
/// AssetClass { classid: 987654321, instanceid: Some(0) },
/// ];
/// let info = client.get_asset_class_info("english", 730, classes).await?;
/// for (classid, item) in info {
/// tracing::info!("{}: {}", classid, item.name);
/// }
/// ```
pub async fn get_asset_class_info(&mut self, language: &str, appid: u32, classes: Vec<AssetClass>) -> Result<HashMap<u64, AssetClassInfo>, SteamError> {
if !self.is_logged_in() {
return Err(SteamError::NotLoggedOn);
}
let request_classes: Vec<_> = classes.iter().map(|c| steam_protos::c_econ_get_asset_class_info_request::Class { classid: Some(c.classid), instanceid: c.instanceid }).collect();
let request = steam_protos::CEconGetAssetClassInfoRequest { language: Some(language.to_string()), appid: Some(appid), classes: request_classes };
let response: steam_protos::CEconGetAssetClassInfoResponse = self.send_unified_request_and_wait("Econ.GetAssetClassInfo#1", &request).await?;
let mut result = HashMap::new();
for description in response.descriptions {
if let Some(classid) = description.classid {
result.insert(
classid,
AssetClassInfo {
appid: description.appid.unwrap_or(0),
classid,
instanceid: description.instanceid.unwrap_or(0),
name: description.name.unwrap_or_default(),
market_hash_name: description.market_hash_name,
market_name: description.market_name,
name_color: description.name_color,
background_color: description.background_color,
item_type: description.r#type,
icon_url: description.icon_url.map(|s| format!("{}{}", STEAM_CDN_BASE, s)),
icon_url_large: description.icon_url_large.map(|s| format!("{}{}", STEAM_CDN_BASE, s)),
tradable: description.tradable.unwrap_or(false),
marketable: description.marketable.unwrap_or(false),
commodity: description.commodity.unwrap_or(false),
},
);
}
}
Ok(result)
}
/// Get your account's trade URL.
///
/// The trade URL can be shared with others to initiate trade offers.
///
/// # Example
///
/// ```rust,ignore
/// let trade_url = client.get_trade_url().await?;
/// tracing::info!("Trade URL: {}", trade_url.url);
/// tracing::info!("Token: {}", trade_url.token);
/// ```
pub async fn get_trade_url(&mut self) -> Result<TradeUrl, SteamError> {
if !self.is_logged_in() {
return Err(SteamError::NotLoggedOn);
}
let request = steam_protos::CEconGetTradeOfferAccessTokenRequest { generate_new_token: Some(false) };
// Send and wait for response
let response: steam_protos::CEconGetTradeOfferAccessTokenResponse = self.send_unified_request_and_wait("Econ.GetTradeOfferAccessToken#1", &request).await?;
let token = response.trade_offer_access_token.unwrap_or_default();
let account_id = self.steam_id.map(|id| id.account_id).unwrap_or(0);
Ok(TradeUrl {
token: token.clone(),
url: format!("https://steamcommunity.com/tradeoffer/new/?partner={}&token={}", account_id, token),
})
}
/// Generate a new trade URL for your account.
///
/// This invalidates the old trade URL and creates a new one.
///
/// # Example
///
/// ```rust,ignore
/// let new_url = client.change_trade_url().await?;
/// tracing::info!("New trade URL: {}", new_url.url);
/// ```
pub async fn change_trade_url(&mut self) -> Result<TradeUrl, SteamError> {
if !self.is_logged_in() {
return Err(SteamError::NotLoggedOn);
}
let request = steam_protos::CEconGetTradeOfferAccessTokenRequest { generate_new_token: Some(true) };
// Send and wait for response
let response: steam_protos::CEconGetTradeOfferAccessTokenResponse = self.send_unified_request_and_wait("Econ.GetTradeOfferAccessToken#1", &request).await?;
let token = response.trade_offer_access_token.unwrap_or_default();
let account_id = self.steam_id.map(|id| id.account_id).unwrap_or(0);
Ok(TradeUrl {
token: token.clone(),
url: format!("https://steamcommunity.com/tradeoffer/new/?partner={}&token={}", account_id, token),
})
}
/// Get the list of emoticons your account can use.
///
/// # Example
///
/// ```rust,ignore
/// let emoticons = client.get_emoticon_list().await?;
/// for (name, emoticon) in emoticons {
/// tracing::info!(":{}: - used {} times", name, emoticon.use_count);
/// }
/// ```
pub async fn get_emoticon_list(&mut self) -> Result<HashMap<String, Emoticon>, SteamError> {
if !self.is_logged_in() {
return Err(SteamError::NotLoggedOn);
}
let request = steam_protos::CPlayerGetEmoticonListRequest {};
// Send and wait for response
let response: steam_protos::CPlayerGetEmoticonListResponse = self.send_unified_request_and_wait("Player.GetEmoticonList#1", &request).await?;
let mut emoticons = HashMap::new();
for emoticon in response.emoticons {
if let Some(name) = emoticon.name {
let name_clone = name.clone();
emoticons.insert(
name_clone.clone(),
Emoticon {
name: name_clone,
use_count: emoticon.use_count.unwrap_or(0),
time_last_used: emoticon.time_last_used,
time_received: emoticon.time_received,
appid: emoticon.appid,
},
);
}
}
Ok(emoticons)
}
/// Get a listing of profile items you own.
///
/// This returns all profile customization items you own, organized by
/// category.
///
/// # Arguments
///
/// * `language` - Language code for item descriptions (defaults to
/// "english")
///
/// # Example
///
/// ```rust,ignore
/// let items = client.get_owned_profile_items(Some("english")).await?;
/// tracing::info!("You own {} profile backgrounds", items.profile_backgrounds.len());
/// tracing::info!("You own {} avatar frames", items.avatar_frames.len());
/// ```
pub async fn get_owned_profile_items(&mut self, language: Option<&str>) -> Result<OwnedProfileItems, SteamError> {
if !self.is_logged_in() {
return Err(SteamError::NotLoggedOn);
}
let request = steam_protos::CPlayerGetProfileItemsOwnedRequest { language: Some(language.unwrap_or("english").to_string()) };
// Send and wait for response
let response: steam_protos::CPlayerGetProfileItemsOwnedResponse = self.send_unified_request_and_wait("Player.GetProfileItemsOwned#1", &request).await?;
Ok(OwnedProfileItems {
profile_backgrounds: response.profile_backgrounds.iter().filter_map(process_profile_item).collect(),
mini_profile_backgrounds: response.mini_profile_backgrounds.iter().filter_map(process_profile_item).collect(),
avatar_frames: response.avatar_frames.iter().filter_map(process_profile_item).collect(),
animated_avatars: response.animated_avatars.iter().filter_map(process_profile_item).collect(),
profile_modifiers: response.profile_modifiers.iter().filter_map(process_profile_item).collect(),
})
}
/// Get a user's equipped profile items.
///
/// This returns the profile customization items currently equipped by a
/// user.
///
/// # Arguments
///
/// * `steam_id` - The Steam ID of the user to look up
/// * `language` - Language code for item descriptions (defaults to
/// "english")
///
/// # Example
///
/// ```rust,ignore
/// let items = client.get_equipped_profile_items(friend_id, Some("english")).await?;
/// if let Some(bg) = items.profile_background {
/// tracing::info!("Profile background: {}", bg.name.unwrap_or_default());
/// }
/// ```
pub async fn get_equipped_profile_items(&mut self, steam_id: SteamID, language: Option<&str>) -> Result<EquippedProfileItems, SteamError> {
if !self.is_logged_in() {
return Err(SteamError::NotLoggedOn);
}
let request = steam_protos::CPlayerGetProfileItemsEquippedRequest { steamid: Some(steam_id.steam_id64()), language: Some(language.unwrap_or("english").to_string()) };
// Send and wait for response
let response: steam_protos::CPlayerGetProfileItemsEquippedResponse = self.send_unified_request_and_wait("Player.GetProfileItemsEquipped#1", &request).await?;
Ok(EquippedProfileItems {
profile_background: response.profile_background.as_ref().and_then(process_profile_item),
mini_profile_background: response.mini_profile_background.as_ref().and_then(process_profile_item),
avatar_frame: response.avatar_frame.as_ref().and_then(process_profile_item),
animated_avatar: response.animated_avatar.as_ref().and_then(process_profile_item),
profile_modifier: response.profile_modifier.as_ref().and_then(process_profile_item),
})
}
/// Set your current profile background.
///
/// # Arguments
///
/// * `background_asset_id` - The community item ID of the background to set
///
/// # Example
///
/// ```rust,ignore
/// // Get owned items first
/// let items = client.get_owned_profile_items(None).await?;
/// if let Some(bg) = items.profile_backgrounds.first() {
/// client.set_profile_background(bg.communityitemid).await?;
/// }
/// ```
pub async fn set_profile_background(&mut self, background_asset_id: u64) -> Result<(), SteamError> {
if !self.is_logged_in() {
return Err(SteamError::NotLoggedOn);
}
let request = steam_protos::CPlayerSetProfileBackgroundRequest { communityitemid: Some(background_asset_id) };
self.send_service_method("Player.SetProfileBackground#1", &request).await
}
}
/// Helper to process profile item URLs by prepending the Steam CDN base.
///
/// This function will be used when job-based response handling is implemented
/// to convert protobuf profile items to the public ProfileItem type.
fn process_profile_item(item: &steam_protos::CPlayerProfileItem) -> Option<ProfileItem> {
// Check if item has any data
item.communityitemid?;
Some(ProfileItem {
communityitemid: item.communityitemid.unwrap_or(0),
image_large: item.image_large.as_ref().map(|s| format!("{}{}", STEAM_CDN_BASE, s)),
image_small: item.image_small.as_ref().map(|s| format!("{}{}", STEAM_CDN_BASE, s)),
name: item.name.clone(),
item_title: item.item_title.clone(),
item_description: item.item_description.clone(),
appid: item.appid,
item_type: item.item_type,
item_class: item.item_class,
movie_webm: item.movie_webm.as_ref().map(|s| format!("{}{}", STEAM_CDN_BASE, s)),
movie_mp4: item.movie_mp4.as_ref().map(|s| format!("{}{}", STEAM_CDN_BASE, s)),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_trade_url_format() {
let url = TradeUrl {
token: "abc123".to_string(),
url: "https://steamcommunity.com/tradeoffer/new/?partner=12345&token=abc123".to_string(),
};
assert!(url.url.contains("partner=12345"));
assert!(url.url.contains("token=abc123"));
}
}