This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 911
/
Trade.cs
779 lines (648 loc) · 25.6 KB
/
Trade.cs
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SteamKit2;
using SteamTrade.Exceptions;
using SteamTrade.TradeWebAPI;
namespace SteamTrade
{
public partial class Trade
{
#region Static Public data
public static Schema CurrentSchema = null;
#endregion
private const int WEB_REQUEST_MAX_RETRIES = 3;
private const int WEB_REQUEST_TIME_BETWEEN_RETRIES_MS = 600;
// list to store all trade events already processed
private readonly List<TradeEvent> eventList;
// current bot's sid
private readonly SteamID mySteamId;
private readonly Dictionary<int, ulong> myOfferedItems;
private readonly List<ulong> steamMyOfferedItems;
private readonly TradeSession session;
private readonly Task<Inventory> myInventoryTask;
private readonly Task<Inventory> otherInventoryTask;
internal Trade(SteamID me, SteamID other, string sessionId, string token, Task<Inventory> myInventoryTask, Task<Inventory> otherInventoryTask)
{
TradeStarted = false;
OtherIsReady = false;
MeIsReady = false;
mySteamId = me;
OtherSID = other;
session = new TradeSession(sessionId, token, other);
this.eventList = new List<TradeEvent>();
OtherOfferedItems = new List<ulong>();
myOfferedItems = new Dictionary<int, ulong>();
steamMyOfferedItems = new List<ulong>();
this.otherInventoryTask = otherInventoryTask;
this.myInventoryTask = myInventoryTask;
}
#region Public Properties
/// <summary>Gets the other user's steam ID.</summary>
public SteamID OtherSID { get; private set; }
/// <summary>
/// Gets the bot's Steam ID.
/// </summary>
public SteamID MySteamId
{
get { return mySteamId; }
}
/// <summary>
/// Gets the inventory of the other user.
/// </summary>
public Inventory OtherInventory
{
get
{
if(otherInventoryTask == null)
return null;
otherInventoryTask.Wait();
return otherInventoryTask.Result;
}
}
/// <summary>
/// Gets the private inventory of the other user.
/// </summary>
public ForeignInventory OtherPrivateInventory { get; private set; }
/// <summary>
/// Gets the inventory of the bot.
/// </summary>
public Inventory MyInventory
{
get
{
if(myInventoryTask == null)
return null;
myInventoryTask.Wait();
return myInventoryTask.Result;
}
}
/// <summary>
/// Gets the items the user has offered, by itemid.
/// </summary>
/// <value>
/// The other offered items.
/// </value>
public List<ulong> OtherOfferedItems { get; private set; }
/// <summary>
/// Gets a value indicating if the other user is ready to trade.
/// </summary>
public bool OtherIsReady { get; private set; }
/// <summary>
/// Gets a value indicating if the bot is ready to trade.
/// </summary>
public bool MeIsReady { get; private set; }
/// <summary>
/// Gets a value indicating if a trade has started.
/// </summary>
public bool TradeStarted { get; private set; }
/// <summary>
/// Gets a value indicating if the remote trading partner cancelled the trade.
/// </summary>
public bool OtherUserCancelled { get; private set; }
/// <summary>
/// Gets a value indicating whether the trade completed normally. This
/// is independent of other flags.
/// </summary>
public bool HasTradeCompletedOk { get; private set; }
#endregion
#region Public Events
public delegate void CloseHandler ();
public delegate void CompleteHandler ();
public delegate void ErrorHandler (string error);
public delegate void TimeoutHandler ();
public delegate void SuccessfulInit ();
public delegate void UserAddItemHandler (Schema.Item schemaItem,Inventory.Item inventoryItem);
public delegate void UserRemoveItemHandler (Schema.Item schemaItem,Inventory.Item inventoryItem);
public delegate void MessageHandler (string msg);
public delegate void UserSetReadyStateHandler (bool ready);
public delegate void UserAcceptHandler ();
/// <summary>
/// When the trade closes, this is called. It doesn't matter
/// whether or not it was a timeout or an error, this is called
/// to close the trade.
/// </summary>
public event CloseHandler OnClose;
/// <summary>
/// Called when the trade completes successfully.
/// </summary>
public event CompleteHandler OnSuccess;
/// <summary>
/// This is for handling errors that may occur, like inventories
/// not loading.
/// </summary>
public event ErrorHandler OnError;
/// <summary>
/// This occurs after Inventories have been loaded.
/// </summary>
public event SuccessfulInit OnAfterInit;
/// <summary>
/// This occurs when the other user adds an item to the trade.
/// </summary>
public event UserAddItemHandler OnUserAddItem;
/// <summary>
/// This occurs when the other user removes an item from the
/// trade.
/// </summary>
public event UserAddItemHandler OnUserRemoveItem;
/// <summary>
/// This occurs when the user sends a message to the bot over
/// trade.
/// </summary>
public event MessageHandler OnMessage;
/// <summary>
/// This occurs when the user sets their ready state to either
/// true or false.
/// </summary>
public event UserSetReadyStateHandler OnUserSetReady;
/// <summary>
/// This occurs when the user accepts the trade.
/// </summary>
public event UserAcceptHandler OnUserAccept;
#endregion
/// <summary>
/// Cancel the trade. This calls the OnClose handler, as well.
/// </summary>
public bool CancelTrade ()
{
return RetryWebRequest(session.CancelTradeWebCmd);
}
/// <summary>
/// Adds a specified TF2 item by its itemid.
/// If the item is not a TF2 item, use the AddItem(ulong itemid, int appid, long contextid) overload
/// </summary>
/// <returns><c>false</c> if the tf2 item was not found in the inventory.</returns>
public bool AddItem (ulong itemid)
{
if (MyInventory.GetItem(itemid) == null)
{
return false;
}
else
{
return AddItem(new TradeUserAssets(){assetid=itemid,appid=440,contextid=2});
}
}
public bool AddItem(ulong itemid, int appid, long contextid)
{
return AddItem(new TradeUserAssets(){assetid=itemid,appid=appid,contextid=contextid});
}
public bool AddItem(TradeUserAssets item)
{
var slot = NextTradeSlot();
bool success = RetryWebRequest(() => session.AddItemWebCmd(item.assetid, slot, item.appid, item.contextid));
if(success)
myOfferedItems[slot] = item.assetid;
return success;
}
/// <summary>
/// Adds a single item by its Defindex.
/// </summary>
/// <returns>
/// <c>true</c> if an item was found with the corresponding
/// defindex, <c>false</c> otherwise.
/// </returns>
public bool AddItemByDefindex (int defindex)
{
List<Inventory.Item> items = MyInventory.GetItemsByDefindex (defindex);
foreach (Inventory.Item item in items)
{
if (item != null && !myOfferedItems.ContainsValue(item.Id) && !item.IsNotTradeable)
{
return AddItem (item.Id);
}
}
return false;
}
/// <summary>
/// Adds an entire set of items by Defindex to each successive
/// slot in the trade.
/// </summary>
/// <param name="defindex">The defindex. (ex. 5022 = crates)</param>
/// <param name="numToAdd">The upper limit on amount of items to add. <c>0</c> to add all items.</param>
/// <returns>Number of items added.</returns>
public uint AddAllItemsByDefindex (int defindex, uint numToAdd = 0)
{
List<Inventory.Item> items = MyInventory.GetItemsByDefindex (defindex);
uint added = 0;
foreach (Inventory.Item item in items)
{
if (item != null && !myOfferedItems.ContainsValue(item.Id) && !item.IsNotTradeable)
{
bool success = AddItem (item.Id);
if (success)
added++;
if (numToAdd > 0 && added >= numToAdd)
return added;
}
}
return added;
}
public bool RemoveItem(TradeUserAssets item)
{
return RemoveItem(item.assetid, item.appid, item.contextid);
}
/// <summary>
/// Removes an item by its itemid.
/// </summary>
/// <returns><c>false</c> the item was not found in the trade.</returns>
public bool RemoveItem (ulong itemid, int appid = 440, long contextid = 2)
{
int? slot = GetItemSlot (itemid);
if (!slot.HasValue)
return false;
bool success = RetryWebRequest(() => session.RemoveItemWebCmd(itemid, slot.Value, appid, contextid));
if(success)
myOfferedItems.Remove (slot.Value);
return success;
}
/// <summary>
/// Removes an item with the given Defindex from the trade.
/// </summary>
/// <returns>
/// Returns <c>true</c> if it found a corresponding item; <c>false</c> otherwise.
/// </returns>
public bool RemoveItemByDefindex (int defindex)
{
foreach (ulong id in myOfferedItems.Values)
{
Inventory.Item item = MyInventory.GetItem (id);
if (item != null && item.Defindex == defindex)
{
return RemoveItem (item.Id);
}
}
return false;
}
/// <summary>
/// Removes an entire set of items by Defindex.
/// </summary>
/// <param name="defindex">The defindex. (ex. 5022 = crates)</param>
/// <param name="numToRemove">The upper limit on amount of items to remove. <c>0</c> to remove all items.</param>
/// <returns>Number of items removed.</returns>
public uint RemoveAllItemsByDefindex (int defindex, uint numToRemove = 0)
{
List<Inventory.Item> items = MyInventory.GetItemsByDefindex (defindex);
uint removed = 0;
foreach (Inventory.Item item in items)
{
if (item != null && myOfferedItems.ContainsValue (item.Id))
{
bool success = RemoveItem (item.Id);
if (success)
removed++;
if (numToRemove > 0 && removed >= numToRemove)
return removed;
}
}
return removed;
}
/// <summary>
/// Removes all offered items from the trade.
/// </summary>
/// <returns>Number of items removed.</returns>
public uint RemoveAllItems()
{
uint numRemoved = 0;
foreach(var id in myOfferedItems.Values.ToList())
{
Inventory.Item item = MyInventory.GetItem(id);
if(item != null)
{
bool wasRemoved = RemoveItem(item.Id);
if(wasRemoved)
numRemoved++;
}
}
return numRemoved;
}
/// <summary>
/// Sends a message to the user over the trade chat.
/// </summary>
public bool SendMessage (string msg)
{
return RetryWebRequest(() => session.SendMessageWebCmd(msg));
}
/// <summary>
/// Sets the bot to a ready status.
/// </summary>
public bool SetReady (bool ready)
{
//If the bot calls SetReady(false) and the call fails, we still want meIsReady to be
//set to false. Otherwise, if the call to SetReady() was a result of a callback
//from Trade.Poll() inside of the OnTradeAccept() handler, the OnTradeAccept()
//handler might think the bot is ready, when really it's not!
if(!ready)
MeIsReady = false;
// testing
ValidateLocalTradeItems ();
return RetryWebRequest(() => session.SetReadyWebCmd(ready));
}
/// <summary>
/// Accepts the trade from the user. Returns a deserialized
/// JSON object.
/// </summary>
public bool AcceptTrade ()
{
ValidateLocalTradeItems ();
return RetryWebRequest(session.AcceptTradeWebCmd);
}
/// <summary>
/// Calls the given function multiple times, until we get a non-null/non-false/non-zero result, or we've made at least
/// WEB_REQUEST_MAX_RETRIES attempts (with WEB_REQUEST_TIME_BETWEEN_RETRIES_MS between attempts)
/// </summary>
/// <returns>The result of the function if it succeeded, or default(T) (null/false/0) otherwise</returns>
private T RetryWebRequest<T>(Func<T> webEvent)
{
for (int i = 0; i < WEB_REQUEST_MAX_RETRIES; i++)
{
//Don't make any more requests if the trade has ended!
if (HasTradeCompletedOk || OtherUserCancelled)
return default(T);
try
{
T result = webEvent();
// if the web request returned some error.
if (!EqualityComparer<T>.Default.Equals(result, default(T)))
return result;
}
catch (Exception ex)
{
// TODO: log to SteamBot.Log but... see issue #394
// realistically we should not throw anymore
Console.WriteLine(ex);
}
if (i != WEB_REQUEST_MAX_RETRIES)
{
//This will cause the bot to stop responding while we wait between web requests. ...Is this really what we want?
Thread.Sleep(WEB_REQUEST_TIME_BETWEEN_RETRIES_MS);
}
}
return default(T);
}
/// <summary>
/// This updates the trade. This is called at an interval of a
/// default of 800ms, not including the execution time of the
/// method itself.
/// </summary>
/// <returns><c>true</c> if the other trade partner performed an action; otherwise <c>false</c>.</returns>
public bool Poll ()
{
bool otherDidSomething = false;
if (!TradeStarted)
{
TradeStarted = true;
// since there is no feedback to let us know that the trade
// is fully initialized we assume that it is when we start polling.
if (OnAfterInit != null)
OnAfterInit ();
}
TradeStatus status = RetryWebRequest(session.GetStatus);
if (status == null)
return false;
switch (status.trade_status)
{
// Nothing happened. i.e. trade hasn't closed yet.
case 0:
break;
// Successful trade
case 1:
HasTradeCompletedOk = true;
return false;
// All other known values (3, 4) correspond to trades closing.
default:
FireOnErrorEvent("Trade was closed by other user. Trade status: " + status.trade_status);
OtherUserCancelled = true;
return false;
}
if (status.newversion)
{
// handle item adding and removing
session.Version = status.version;
HandleTradeVersionChange(status);
return true;
}
else if (status.version > session.Version)
{
// oh crap! we missed a version update abort so we don't get
// scammed. if we could get what steam thinks what's in the
// trade then this wouldn't be an issue. but we can only get
// that when we see newversion == true
throw new TradeException("The trade version does not match. Aborting.");
}
var events = status.GetAllEvents();
foreach (var tradeEvent in events)
{
if (eventList.Contains(tradeEvent))
continue;
//add event to processed list, as we are taking care of this event now
eventList.Add(tradeEvent);
bool isBot = tradeEvent.steamid == MySteamId.ConvertToUInt64().ToString();
// dont process if this is something the bot did
if (isBot)
continue;
otherDidSomething = true;
switch ((TradeEventType) tradeEvent.action)
{
case TradeEventType.ItemAdded:
FireOnUserAddItem(tradeEvent);
break;
case TradeEventType.ItemRemoved:
FireOnUserRemoveItem(tradeEvent);
break;
case TradeEventType.UserSetReady:
OtherIsReady = true;
OnUserSetReady(true);
break;
case TradeEventType.UserSetUnReady:
OtherIsReady = false;
OnUserSetReady(false);
break;
case TradeEventType.UserAccept:
OnUserAccept();
break;
case TradeEventType.UserChat:
OnMessage(tradeEvent.text);
break;
default:
// Todo: add an OnWarning or similar event
FireOnErrorEvent("Unknown Event ID: " + tradeEvent.action);
break;
}
}
// Update Local Variables
if (status.them != null)
{
OtherIsReady = status.them.ready == 1;
MeIsReady = status.me.ready == 1;
}
if (status.logpos != 0)
{
session.LogPos = status.logpos;
}
return otherDidSomething;
}
private void HandleTradeVersionChange(TradeStatus status)
{
CopyNewAssets(OtherOfferedItems, status.them.GetAssets());
CopyNewAssets(steamMyOfferedItems, status.me.GetAssets());
}
private void CopyNewAssets(List<ulong> dest, IEnumerable<TradeUserAssets> assetList)
{
if (assetList == null)
return;
dest.Clear();
dest.AddRange(assetList.Select(asset => asset.assetid));
}
/// <summary>
/// Gets an item from a TradeEvent, and passes it into the UserHandler's implemented OnUserAddItem([...]) routine.
/// Passes in null items if something went wrong.
/// </summary>
/// <param name="tradeEvent">TradeEvent to get item from</param>
/// <returns></returns>
private void FireOnUserAddItem(TradeEvent tradeEvent)
{
ulong itemID = tradeEvent.assetid;
if (OtherInventory != null)
{
Inventory.Item item = OtherInventory.GetItem(itemID);
if (item != null)
{
Schema.Item schemaItem = CurrentSchema.GetItem(item.Defindex);
if (schemaItem == null)
{
Console.WriteLine("User added an unknown item to the trade.");
}
OnUserAddItem(schemaItem, item);
}
else
{
item = new Inventory.Item
{
Id=itemID,
AppId=tradeEvent.appid,
ContextId=tradeEvent.contextid
};
//Console.WriteLine("User added a non TF2 item to the trade.");
OnUserAddItem(null, item);
}
}
else
{
var schemaItem = GetItemFromPrivateBp(tradeEvent, itemID);
if (schemaItem == null)
{
Console.WriteLine("User added an unknown item to the trade.");
}
OnUserAddItem(schemaItem, null);
// todo: figure out what to send in with Inventory item.....
}
}
private Schema.Item GetItemFromPrivateBp(TradeEvent tradeEvent, ulong itemID)
{
if (OtherPrivateInventory == null)
{
// get the foreign inventory
var f = session.GetForiegnInventory(OtherSID, tradeEvent.contextid, tradeEvent.appid);
OtherPrivateInventory = new ForeignInventory(f);
}
ushort defindex = OtherPrivateInventory.GetDefIndex(itemID);
Schema.Item schemaItem = CurrentSchema.GetItem(defindex);
return schemaItem;
}
/// <summary>
/// Gets an item from a TradeEvent, and passes it into the UserHandler's implemented OnUserRemoveItem([...]) routine.
/// Passes in null items if something went wrong.
/// </summary>
/// <param name="tradeEvent">TradeEvent to get item from</param>
/// <returns></returns>
private void FireOnUserRemoveItem(TradeEvent tradeEvent)
{
ulong itemID = tradeEvent.assetid;
if (OtherInventory != null)
{
Inventory.Item item = OtherInventory.GetItem(itemID);
if (item != null)
{
Schema.Item schemaItem = CurrentSchema.GetItem(item.Defindex);
if (schemaItem == null)
{
// TODO: Add log (counldn't find item in CurrentSchema)
}
OnUserRemoveItem(schemaItem, item);
}
else
{
// TODO: Log this (Couldn't find item in user's inventory can't find item in CurrentSchema
item = new Inventory.Item()
{
Id = itemID,
AppId = tradeEvent.appid,
ContextId = tradeEvent.contextid
};
OnUserRemoveItem(null, item);
}
}
else
{
var schemaItem = GetItemFromPrivateBp(tradeEvent, itemID);
if (schemaItem == null)
{
// TODO: Add log (counldn't find item in CurrentSchema)
}
OnUserRemoveItem(schemaItem, null);
}
}
internal void FireOnSuccessEvent()
{
var onSuccessEvent = OnSuccess;
if (onSuccessEvent != null)
onSuccessEvent();
}
internal void FireOnCloseEvent()
{
var onCloseEvent = OnClose;
if (onCloseEvent != null)
onCloseEvent();
}
internal void FireOnErrorEvent(string errorMessage)
{
var onErrorEvent = OnError;
if(onErrorEvent != null)
onErrorEvent(errorMessage);
}
private int NextTradeSlot()
{
int slot = 0;
while (myOfferedItems.ContainsKey (slot))
{
slot++;
}
return slot;
}
private int? GetItemSlot(ulong itemid)
{
foreach (int slot in myOfferedItems.Keys)
{
if (myOfferedItems [slot] == itemid)
{
return slot;
}
}
return null;
}
private void ValidateLocalTradeItems ()
{
if (myOfferedItems.Count != steamMyOfferedItems.Count)
{
throw new TradeException ("Error validating local copy of items in the trade: Count mismatch");
}
if (myOfferedItems.Values.Any(id => !steamMyOfferedItems.Contains(id)))
{
throw new TradeException ("Error validating local copy of items in the trade: Item was not in the Steam Copy.");
}
}
}
}