void ProcessCurrentTrade() { if (CurrentTrade == null) return; CurrentTrade.Update(); TradeEvent @event = null; while (CurrentTrade.Events.TryDequeue(out @event)) { //Logger.WriteLine("Got event: " + evt.type); HandleTradeEvent(CurrentTrade, @event); } if (CurrentTrade.Status != ETradeTransactionStatus.InProgress) CurrentTrade = null; }
void HandleTradeEvent(TradeSession trade, TradeEvent @event) { switch (@event.type) { case ETradeEventType.Initialized: HandleTradeInitialized(trade, @event); break; case ETradeEventType.Message: HandleTradeMessage(trade, @event); break; case ETradeEventType.ItemAdded: HandleTradeItemAdded(trade, @event); break; case ETradeEventType.ItemRemoved: var itemRemoved = trade.ForeignInventory.LookupItem(@event.item); Logger.WriteLine("Item removed: " + itemRemoved.details.name); break; case ETradeEventType.Confirmed: HandleTradeConfirmed(trade, @event); break; case ETradeEventType.InventoryLoaded: Logger.WriteLine("Inventory loaded: " + @event.ToString()); break; case ETradeEventType.ForeignInventoryLoaded: Logger.WriteLine("Foreign Inventory loaded: " + @event.ToString()); break; case ETradeEventType.Ready: Logger.WriteLine("Trader is ready to trade."); break; case ETradeEventType.Unready: Logger.WriteLine("Trader is not ready to trade."); break; case ETradeEventType.Finished: var s = string.Format("Trade is finished: " + trade.Status); if (trade.Status == ETradeTransactionStatus.Finished) s += string.Format(" id = {0}", @event.tradeId); Logger.WriteLine(s); break; default: Logger.WriteLine("Unhandled trade event: " + @event); break; } }
void HandleTradeItemsCommand(string msg, TradeSession trade) { trade.SendChatMessage("Not implemented yet!"); return; var sb = new StringBuilder(); sb.AppendLine("Items:"); foreach (var item in trade.ItemsToReceive) { sb.AppendLine(string.Format("{0} | {1}", item.id, item.details.name)); } trade.SendChatMessage(sb.ToString()); }
void HandleTradeCancelCommand(string msg, TradeSession trade) { trade.CancelTrade(); }
void HandleTradeConfirmCommand(string msg, TradeSession trade) { trade.Confirm(); }
void HandleTradeRemoveCommand(string msg, TradeSession trade) { string[] args = msg.Split(' '); if (args.Length < 2) { trade.SendChatMessage("Invalid arguments."); return; } var pattern = string.Format(".*{0}.*", msg.Substring(msg.IndexOf(" ")).Trim()); if (string.IsNullOrWhiteSpace(pattern)) return; UpdateBackpack(); var assets = GetAssetsMatchingPattern(pattern); foreach (var asset in assets) { var item = GetItemFromDefIndex(asset.DefIndex); Logger.WriteLine("Removing from trade: {0}", item.Name); trade.RemoveItem(asset.Id); } }
void HandleTradeHelpCommand(string msg, TradeSession trade) { OutputCommandsHelp<TradeCommand>("Trade", (s) => trade.SendChatMessage(s)); }
void HandleTradeReadyCommand(string msg, TradeSession trade) { trade.ToggleReady(); }
void HandleTradeTextCommand(TradeSession trade, TradeEvent @event) { SendChatDelegate sendDelegate = (sender, entry, text) => { trade.SendChatMessage(text); }; TradeCommand cmd; if (!HandleCommandCommon( @event.message, @event.sender, sendDelegate, out cmd)) return; var messageMap = new Dictionary<TradeCommand, Action<string, TradeSession>> { { TradeCommand.Help, HandleTradeHelpCommand }, { TradeCommand.Ready, HandleTradeReadyCommand }, { TradeCommand.Add, HandleTradeAddCommand }, { TradeCommand.Remove, HandleTradeRemoveCommand }, { TradeCommand.Confirm, HandleTradeConfirmCommand }, { TradeCommand.Items, HandleTradeItemsCommand }, { TradeCommand.Cancel, HandleTradeCancelCommand }, }; Action<string, TradeSession> func; if (!messageMap.TryGetValue(cmd, out func)) { Logger.WriteLine("Unhandled trade command: {0}", cmd); return; } func(@event.message, trade); }
void HandleTradeInitialized(TradeSession trade, TradeEvent @event) { if (trade.Inventory != null) return; foreach (var ctx in @event.inventoryApps) { Logger.WriteLine("Loading inventory: " + ctx.name); trade.LoadInventory(SteamUser.SteamID, ctx.appid, ctx.contexts[0].id); return; } }
void HandleTradeMessage(TradeSession trade, TradeEvent @event) { Logger.WriteLine("{0} says in trade: {1}", SteamFriends.GetFriendPersonaName(trade.OtherSteamId), @event.message); HandleTradeTextCommand(trade, @event); }
void HandleTradeItemAdded(TradeSession trade, TradeEvent @event) { if (@event.item.appid != TF2App) { Logger.WriteLine("Ignored item: not TF2 item..."); return; } var asset = trade.ForeignInventory.LookupItem(@event.item); var item = GetItemFromAsset(asset); if (item == null) { Logger.WriteLine("Unknown item added: " + @event.item); return; } Logger.WriteLine("Item added: " + item.ItemName); }
void HandleTradeConfirmed(TradeSession trade, TradeEvent @event) { Logger.WriteLine("Trade confirmed."); trade.Confirm(); }