forked from doctype/steam
-
Notifications
You must be signed in to change notification settings - Fork 2
/
inventory.go
226 lines (187 loc) · 5.83 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
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 uint32 `json:"appid"`
ContextID uint64 `json:"contextid"`
AssetID uint64 `json:"id,string,omitempty"`
ClassID uint64 `json:"classid,string,omitempty"`
InstanceID uint64 `json:"instanceid,string,omitempty"`
Amount uint64 `json:"amount,string"`
Desc *EconItemDesc `json:"-"` /* May be nil */
}
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 resp != nil {
defer resp.Body.Close()
}
if err != nil {
return false, 0, err
}
type Asset struct {
AppID uint32 `json:"appid"`
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 Response struct {
Assets []Asset `json:"assets"`
Descriptions []*EconItemDesc `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
}
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 {
var desc *EconItemDesc
key := fmt.Sprintf("%d_%d", asset.ClassID, asset.InstanceID)
if d, ok := descriptions[key]; ok {
desc = response.Descriptions[d]
}
item := InventoryItem{
AppID: asset.AppID,
ContextID: asset.ContextID,
AssetID: asset.AssetID,
ClassID: asset.ClassID,
InstanceID: asset.InstanceID,
Amount: asset.Amount,
Desc: desc,
}
add := true
for _, filter := range filters {
add = filter(&item)
if !add {
break
}
}
if add {
*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
}