forked from doctype/steam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.go
274 lines (232 loc) · 8.03 KB
/
inventory.go
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
package steam
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/url"
"regexp"
"strconv"
)
const (
InventoryEndpoint = "http://steamcommunity.com/inventory/%d/%d/%d?"
)
type ItemTag struct {
Category string `json:"category"`
InternalName string `json:"internal_name"`
LocalizedCategoryName string `json:"localized_category_name"`
LocalizedTagName string `json:"localized_tag_name"`
}
// Due to the JSON being string, etc... we cannot re-use EconItem
// Also, "assetid" is included as "id" not as assetid.
type InventoryItem struct {
AppID uint64 `json:"appid"`
ContextID uint64 `json:"contextid"`
AssetID uint64 `json:"assetid"`
ClassID uint64 `json:"classid"`
InstanceID uint64 `json:"instanceid"`
Amount uint64 `json:"amount"`
Tradable bool `json:"tradable"`
Currency int `json:"currency"`
IconURL string `json:"standard"`
IconURLLarge string `json:"large"`
Name string `json:"text"`
NameColor string `json:"color"`
MarketName string `json:"market_name"`
MarketHashName string `json:"market_hash"`
Commodity bool `json:"commodity"`
Type string `json:"type"`
Marketable bool `json:"marketable"`
Restrictions int `json:"restrictions"`
Tags []ItemTag `json:"tags"`
}
type InventoryContext struct {
ID uint64 `json:"id,string"` /* Apparently context id needs at least 64 bits... */
AssetCount uint32 `json:"asset_count"`
Name string `json:"name"`
}
type InventoryAppStats struct {
AppID uint64 `json:"appid"`
Name string `json:"name"`
AssetCount uint32 `json:"asset_count"`
Icon string `json:"icon"`
Link string `json:"link"`
InventoryLogo string `json:"inventory_logo"`
TradePermissions string `json:"trade_permissions"`
Contexts map[string]*InventoryContext `json:"rgContexts"`
}
var inventoryContextRegexp = regexp.MustCompile("var g_rgAppContextData = (.*?);")
func (session *Session) fetchInventory(
sid SteamID,
appID, contextID, startAssetID uint64,
filters *[]Filter,
items *[]InventoryItem,
) (hasMore bool, lastAssetID uint64, err error) {
params := url.Values{
"l": {session.language},
}
if startAssetID != 0 {
params.Set("start_assetid", strconv.FormatUint(startAssetID, 10))
params.Set("count", "75")
} else {
params.Set("count", "250")
}
resp, err := session.client.Get(fmt.Sprintf(InventoryEndpoint, sid, appID, contextID) + params.Encode())
if err != nil {
return false, 0, err
}
type Asset struct {
AppID uint64 `json:"appid,string"`
ContextID uint64 `json:"contextid,string"`
AssetID uint64 `json:"assetid,string"`
ClassID uint64 `json:"classid,string"`
InstanceID uint64 `json:"instanceid,string"`
Amount uint64 `json:"amount,string"`
}
type DescriptionsPart struct {
Type string `json:"type"`
Value string `json:"value"`
Color string `json:"color"`
}
type Description struct {
AppID uint64 `json:"appid"`
ClassID uint64 `json:"classid,string"`
InstanceID uint64 `json:"instanceid,string"`
Currency int `json:"currency"`
BackgroundColor string `json:"background_color"`
IconURL string `json:"icon_url"`
IconURLLarge string `json:"icon_url_large"`
Descriptions []DescriptionsPart `json:"descriptions"`
Tradable int `json:"tradable"`
Name string `json:"name"`
NameColor string `json:"name_color"`
Type string `json:"type"`
MarketName string `json:"market_name"`
MarketHashName string `json:"market_hash_name"`
Commodity int `json:"commodity"`
MarketTradableRestriction int `json:"market_tradable_restriction"`
Marketable int `json:"marketable"`
Tags []ItemTag `json:"tags"`
}
type Response struct {
Assets []Asset `json:"assets"`
Descriptions []Description `json:"descriptions"`
Success int `json:"success"`
HasMore int `json:"more_items"`
LastAssetID string `json:"last_assetid"`
TotalInventoryCount int `json:"total_inventory_count"`
ErrorMsg string `json:"error"`
}
var response Response
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil {
return false, 0, err
}
resp.Body.Close()
if response.Success == 0 {
if len(response.ErrorMsg) != 0 {
return false, 0, errors.New(response.ErrorMsg)
}
return false, 0, nil // empty inventory
}
// Fill in descriptions map, where key
// is "<CLASS_ID>_<INSTANCE_ID>" pattern, and
// value is position on asset description in
// response.Descriptions array
//
// We need it for fast asset's description
// searching in future
descriptions := make(map[string]int)
for i, desc := range response.Descriptions {
key := fmt.Sprintf("%d_%d", desc.ClassID, desc.InstanceID)
descriptions[key] = i
}
for _, asset := range response.Assets {
key := fmt.Sprintf("%d_%d", asset.ClassID, asset.InstanceID)
desc := &response.Descriptions[descriptions[key]]
item := InventoryItem{
AppID: asset.AppID,
ContextID: asset.ContextID,
AssetID: asset.AssetID,
ClassID: asset.ClassID,
InstanceID: asset.InstanceID,
Amount: asset.Amount,
Tradable: desc.Tradable != 0,
Currency: desc.Currency,
IconURL: desc.IconURL,
IconURLLarge: desc.IconURLLarge,
Name: desc.Name,
NameColor: desc.NameColor,
MarketName: desc.MarketName,
MarketHashName: desc.MarketHashName,
Commodity: desc.Commodity != 0,
Type: desc.Type,
Marketable: desc.Marketable != 0,
Restrictions: desc.MarketTradableRestriction,
Tags: desc.Tags,
}
st := true
for _, filter := range *filters {
if !filter(&item) {
st = false
break
}
}
if !st {
continue
}
*items = append(*items, item)
}
hasMore = response.HasMore != 0
if !hasMore {
return hasMore, 0, nil
}
lastAssetID, err = strconv.ParseUint(response.LastAssetID, 10, 64)
if err != nil {
return hasMore, 0, err
}
return hasMore, lastAssetID, nil
}
func (session *Session) GetInventory(sid SteamID, appID, contextID uint64, tradableOnly bool) ([]InventoryItem, error) {
filters := []Filter{}
if tradableOnly {
filters = append(filters, IsTradable(tradableOnly))
}
return session.GetFilterableInventory(sid, appID, contextID, filters)
}
func (session *Session) GetFilterableInventory(sid SteamID, appID, contextID uint64, filters []Filter) ([]InventoryItem, error) {
items := []InventoryItem{}
startAssetID := uint64(0)
for {
hasMore, lastAssetID, err := session.fetchInventory(sid, appID, contextID, startAssetID, &filters, &items)
if err != nil {
return nil, err
}
if !hasMore {
break
}
startAssetID = lastAssetID
}
return items, nil
}
func (session *Session) GetInventoryAppStats(sid SteamID) (map[string]InventoryAppStats, error) {
resp, err := session.client.Get("https://steamcommunity.com/profiles/" + sid.ToString() + "/inventory")
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
m := inventoryContextRegexp.FindSubmatch(body)
if m == nil || len(m) != 2 {
return nil, err
}
inven := map[string]InventoryAppStats{}
if err = json.Unmarshal(m[1], &inven); err != nil {
return nil, err
}
return inven, nil
}