-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtradeoffer.go
297 lines (250 loc) · 9.83 KB
/
tradeoffer.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package steamapi
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
// State represents the state of the tradeoffer, see constants
type State uint
const (
// ETradeOfferStateCreated /!\ non steam status, used to know the TO has been created
ETradeOfferStateCreated State = 0
// ETradeOfferStateInvalid Invalid
ETradeOfferStateInvalid State = 1
// ETradeOfferStateActive This trade offer has been sent, neither party has acted on it yet.
ETradeOfferStateActive State = 2
// ETradeOfferStateAccepted The trade offer was accepted by the recipient and items were exchanged.
ETradeOfferStateAccepted State = 3
// ETradeOfferStateCountered The recipient made a counter offer
ETradeOfferStateCountered State = 4
// ETradeOfferStateExpired The trade offer was not accepted before the expiration date
ETradeOfferStateExpired State = 5
// ETradeOfferStateCanceled The sender cancelled the offer
ETradeOfferStateCanceled State = 6
// ETradeOfferStateDeclined The recipient declined the offer
ETradeOfferStateDeclined State = 7
// ETradeOfferStateInvalidItems Some of the items in the offer are no longer available
// (indicated by the missing flag in the output)
ETradeOfferStateInvalidItems State = 8
// ETradeOfferStateCreatedNeedsConfirmation The offer hasn't been sent yet and is awaiting email/mobile confirmation. The offer is only visible to the sender.
ETradeOfferStateCreatedNeedsConfirmation State = 9
// ETradeOfferStateCanceledBySecondFactor Either party canceled the offer via email/mobile. The offer is visible to both parties, even if the sender canceled it before it was sent.
ETradeOfferStateCanceledBySecondFactor State = 10
// ETradeOfferStateInEscrow The trade has been placed on hold. The items involved in the trade have all been removed from both parties' inventories and will be automatically delivered in the future.
ETradeOfferStateInEscrow State = 11
)
// ConfirmationMethod different methods in which a trade offer can be confirmed.
type ConfirmationMethod int
const (
// ETradeOfferConfirmationMethodInvalid Invalid
ETradeOfferConfirmationMethodInvalid ConfirmationMethod = 0
// ETradeOfferConfirmationMethodEmail An email was sent with details on how to confirm the trade offer
ETradeOfferConfirmationMethodEmail ConfirmationMethod = 1
// ETradeOfferConfirmationMethodMobileApp The trade offer may be confirmed via the mobile app
ETradeOfferConfirmationMethodMobileApp ConfirmationMethod = 2
)
// CEconAsset represents an asset in steam web api
type CEconAsset struct {
AppID uint `json:",string"`
ContextID uint64 `json:",string"`
AssetID uint64 `json:",string"`
CurrencyID uint64 `json:",string"`
ClassID uint64 `json:",string"`
InstanceID uint64 `json:",string"`
Amount uint64 `json:",string"`
Missing bool
MarketHashName string
}
// CEconTradeOffer represent the to from the steam API
type CEconTradeOffer struct {
TradeOfferID uint64 `json:",string"`
OtherAccountID uint64 `json:"accountid_other"`
Message string
ExpirationTime uint32 `json:"expiration_time"`
State State `json:"trade_offer_state"`
ToGive []*CEconAsset `json:"items_to_give"`
ToReceive []*CEconAsset `json:"items_to_receive"`
IsOurs bool `json:"is_our_offer"`
TimeCreated uint32 `json:"time_created"`
TimeUpdated uint32 `json:"time_updated"`
FromRealTimeTrade bool `json:"from_real_time_trade"`
EscrowEndDate uint32 `json:"escrow_end_date"`
ConfirmationMethod ConfirmationMethod `json:"confirmation_method"`
TradeID uint64 `json:"tradeid,string"`
}
// CEconTradeOffers represent the list of different tradeoffers types
type CEconTradeOffers struct {
Sent []*CEconTradeOffer `json:"trade_offers_sent"`
Received []*CEconTradeOffer `json:"trade_offers_received"`
}
type ieconGetTradeOffersResponse struct {
Response struct {
CEconTradeOffers
}
}
// IEconGetTradeOffers retrieves a list of tradeoffers
func IEconGetTradeOffers(
apiKey string,
getSentOffers bool,
getReceivedOffers bool,
getDescriptions bool,
activeOnly bool,
historicalOnly bool,
timeHistoricalCutoff int64,
) (*CEconTradeOffers, error) {
querystring := url.Values{}
querystring.Add("key", apiKey)
querystring.Add("get_sent_offers", boolToStr(getSentOffers))
querystring.Add("get_received_offers", boolToStr(getReceivedOffers))
querystring.Add("get_descriptions", boolToStr(getDescriptions))
querystring.Add("language", "en")
querystring.Add("active_only", boolToStr(activeOnly))
querystring.Add("historical_only", boolToStr(historicalOnly))
querystring.Add("time_historical_cutoff", strconv.FormatInt(timeHistoricalCutoff, 10))
resp, err := http.Get(BaseSteamAPIURL + "/IEconService/GetTradeOffers/v0001?" + querystring.Encode())
if err != nil {
return nil, fmt.Errorf("tradeoffer IEconGetTradeOffers http.Get: error %v", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("tradeoffer IEconGetTradeOffers http.Get: http status %v", resp.Status)
}
defer resp.Body.Close()
tosResp := &ieconGetTradeOffersResponse{}
err = json.NewDecoder(resp.Body).Decode(tosResp)
if err != nil {
return nil, fmt.Errorf("tradeoffer IEconGetTradeOffers Decode: error %v", err)
}
return &tosResp.Response.CEconTradeOffers, nil
}
type ieconGetTradeOfferResponse struct {
Response struct {
Offer CEconTradeOffer
Descriptions []ItemDescription
}
}
// ItemDescription represents the details about the items unique w classid instanceid
type ItemDescription struct {
AppID uint `json:"appid"`
ClassID uint64 `json:"classid,string"`
InstanceID uint64 `json:"instanceid,string"`
MarketHashName string `json:"market_hash_name"`
IconURL string `json:"icon_url"`
NameColor string `json:"name_color"`
Name string `json:"name"`
}
func findMarketHashName(itemD []ItemDescription, appID uint, classID, instanceID uint64) string {
for _, description := range itemD {
if description.AppID == appID &&
description.ClassID == classID &&
description.InstanceID == instanceID {
return description.MarketHashName
}
}
return ""
}
// IEconGetTradeOffer retrieves details about a specific tradeoffer
func IEconGetTradeOffer(apiKey string, tradeOfferID uint64) (
*CEconTradeOffer, error,
) {
querystring := url.Values{}
querystring.Add("key", apiKey)
querystring.Add("format", "json")
querystring.Add("tradeofferid", strconv.FormatUint(tradeOfferID, 10))
querystring.Add("language", "en")
resp, err := http.Get(BaseSteamAPIURL + "/IEconService/GetTradeOffer/v1?" + querystring.Encode())
if err != nil {
return nil, fmt.Errorf("tradeoffer IEconGetTradeOffer http.Get: error %v", err)
}
if resp.StatusCode != http.StatusOK {
body, errBody := ioutil.ReadAll(resp.Body)
return nil,
fmt.Errorf("tradeoffer IEconGetTradeOffer: steam responded with a status %d with the message: %s (%v)",
resp.StatusCode,
body,
errBody,
)
}
defer resp.Body.Close()
toResp := ieconGetTradeOfferResponse{}
err = json.NewDecoder(resp.Body).Decode(&toResp)
if err != nil {
body, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("tradeoffer IEconGetTradeOffer Decode(%s): error %v", body, err)
}
// If the state is 0, it means there is a mistake
if toResp.Response.Offer.State == 0 {
body, errBody := ioutil.ReadAll(resp.Body)
return nil,
fmt.Errorf("tradeoffer IEconGetTradeOffer: steam responded with a status %d with the message: %s (%v)",
resp.StatusCode,
body,
errBody,
)
}
for giveIndex, asset := range toResp.Response.Offer.ToGive {
toResp.Response.Offer.ToGive[giveIndex].MarketHashName =
findMarketHashName(toResp.Response.Descriptions, asset.AppID, asset.ClassID, asset.InstanceID)
}
for receiveIndex, asset := range toResp.Response.Offer.ToReceive {
toResp.Response.Offer.ToReceive[receiveIndex].MarketHashName =
findMarketHashName(toResp.Response.Descriptions, asset.AppID, asset.ClassID, asset.InstanceID)
}
return &toResp.Response.Offer, nil
}
// IEconActionTradeOffer declines a TO created by someone else
func IEconActionTradeOffer(action string, apiKey string, tradeOfferID uint64) error {
if action != "Decline" && action != "Cancel" {
return fmt.Errorf("tradeoffer IEconActionTradeOffer doesn't support %v action", action)
}
querystring := url.Values{}
querystring.Add("key", apiKey)
querystring.Add("tradeofferid", strconv.FormatUint(tradeOfferID, 10))
resp, err := http.Get(
BaseSteamAPIURL + "/IEconService/" + action + "TradeOffer/v0001?" + querystring.Encode())
if err != nil {
return fmt.Errorf("tradeoffer IEconGetTradeOffer http.Get: error %v", err)
}
if resp.StatusCode != http.StatusOK {
body, errBody := ioutil.ReadAll(resp.Body)
return fmt.Errorf("tradeoffer IEcon%sTradeOffer: steam responded with a status %d with the message: %s (%v)",
action,
resp.StatusCode,
body,
errBody,
)
}
err = resp.Body.Close()
if err != nil {
return fmt.Errorf("tradeoffer IEcon%sTradeOffer resp.Body.Close(): error %v", action, err)
}
return nil
}
// IEconCancelTradeOffer declines a TO created by someone else
func IEconCancelTradeOffer(apiKey string, tradeOfferID uint64) error {
resp, err := http.PostForm(
BaseSteamAPIURL+"/IEconService/CancelTradeOffer/v1",
url.Values{"key": {apiKey}, "tradeofferid": {strconv.FormatUint(tradeOfferID, 10)}},
)
if err != nil {
return fmt.Errorf("tradeoffer IEconGetTradeOffer http.Get: error %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, errBody := ioutil.ReadAll(resp.Body)
return fmt.Errorf("tradeoffer IEconCancelTradeOffer: steam responded with a status %d with the message: %s (%v)",
resp.StatusCode,
body,
errBody,
)
}
return nil
}
func boolToStr(b bool) string {
if b {
return "1"
}
return "0"
}