Exemple #1
0
        void FinishTrade(ETradeTransactionStatus status,
                         Action <TradeEvent> eventCallback = null)
        {
            CancelTimer();
            Status = status;

            var @event = new TradeEvent();

            @event.type = ETradeEventType.Finished;

            if (eventCallback != null)
            {
                eventCallback(@event);
            }

            Events.Enqueue(@event);
        }
Exemple #2
0
        void ProcessInitialParams(IDictionary <string, object> Params)
        {
            if (Params.ContainsKey("strInventoryLoadURL"))
            {
                InventoryLoadURL = Params["strInventoryLoadURL"] as string;
                InventoryLoadURL = InventoryLoadURL.Trim(new char[] { '\'' });
                //Console.WriteLine("InventoryLoadURL: " + InventoryLoadURL);
            }

            if (Params.ContainsKey("sessionID"))
            {
                string SessionId = Params["sessionID"] as string;
                SessionId = SessionId.Trim(new char[] { '\'', '"' });
                if (SessionId != LoginData.SessionId)
                {
                    throw new Steam2Exception("Session ids from web differ.");
                }
            }

            if (!Params.ContainsKey("rgAppContextData"))
            {
                return;
            }

            string data = Params["rgAppContextData"] as string;
            var    ctxs = new JsonReader().Read(data) as IDictionary <string, object>;

            InventoryApps = new List <TradeInventoryApp>();

            foreach (var kvp in ctxs)
            {
                var ctx = kvp.Value as IDictionary <string, object>;
                var app = Inventory.ProcessInventoryApp(ctx);
                InventoryApps.Add(app);
            }

            var evt = new TradeEvent();

            evt.type          = ETradeEventType.Initialized;
            evt.inventoryApps = InventoryApps;

            Events.Enqueue(evt);
        }
Exemple #3
0
        void ProcessForeignInventory(IAsyncResult res)
        {
            var webRequest = res.AsyncState as HttpWebRequest;

            string status;

            try
            {
                using (var response = webRequest.EndGetResponse(res))
                    using (Stream stream = response.GetResponseStream())
                        status = stream.ReadAll();
            }
            catch (WebException ex)
            {
                Console.WriteLine("Web Exception: " + ex);
                return;
            }

            var data = new JsonReader().Read(status) as IDictionary <string, object>;

            var success = data["success"] as bool?;

            if (success.HasValue && success.Value == false)
            {
                var error = data["error"] as string;
                if (error != null)
                {
                    Console.WriteLine("Could not get foreign inventory: " + error);
                }
                return;
            }

            ForeignInventory = new Inventory();
            ForeignInventory.TryParse(data);

            var evt = new TradeEvent();

            evt.type = ETradeEventType.ForeignInventoryLoaded;
            Events.Enqueue(evt);
        }
Exemple #4
0
        private void FireOnUserAddItem(TradeEvent tradeEvent)
        {
            ulong itemID = tradeEvent.assetid;

            if (OtherInventory != null)
            {
                Inventory.Item item = OtherInventory.GetItem(itemID);
                Schema.Item schemaItem = CurrentSchema.GetItem(item.Defindex);
                OnUserAddItem(schemaItem, item);
            }
            else
            {
                var schemaItem = GetItemFromPrivateBp(tradeEvent, itemID);
                OnUserAddItem(schemaItem, null);
                // todo: figure out what to send in with Inventory item.....
            }
        }
Exemple #5
0
        void ProcessEvent(int key, ExpandoObject obj)
        {
            //Console.WriteLine("Key " + key + " NextLogPos: " + (NextLogPos-1));

            if (key < Interlocked.Read(ref NextLogPos) - 1)
            {
                //Console.WriteLine("Ignoring...");
                return;
            }

            var evt = obj as IDictionary <string, object>;

            if (!evt.ContainsKey("action"))
            {
                return;
            }

            int action;

            if (!Int32.TryParse(evt["action"] as string, out action))
            {
                return;
            }

            string steamid = String.Empty;

            if (evt.ContainsKey("steamid"))
            {
                steamid = evt["steamid"] as string;
            }

            var steamId = new SteamID();

            steamId.SetFromUInt64(UInt64.Parse(steamid));

            if (steamId == OwnSteamId)
            {
                return;
            }

            var appid = evt["appid"] as int?;

            var tradeEvent = new TradeEvent();

            tradeEvent.sender = steamId;

            bool enqueue     = false;
            bool loadForeign = false;

            switch (action)
            {
            case 0: // Item added/remove
            case 1:
            {
                var item = new TradeItem();
                tradeEvent.type = (action == 0) ?
                                  ETradeEventType.ItemAdded : ETradeEventType.ItemRemoved;
                item.appid = appid.Value;
                int.TryParse(evt["assetid"] as string, out item.assetid);
                int.TryParse(evt["contextid"] as string, out item.contextid);
                tradeEvent.item = item;
                if (ForeignInventory == null)
                {
                    LoadForeignInventory(steamId, item.appid, item.contextid);
                    loadForeign = true;
                }
                enqueue = true;
                break;
            }

            case 2: // Ready/unready
            case 3:
            {
                tradeEvent.type = (action == 2) ?
                                  ETradeEventType.Ready : ETradeEventType.Unready;
                var timestamp = evt["timestamp"] as int?;
                tradeEvent.timestamp = (uint)timestamp.Value;
                enqueue = true;
                break;
            }

            case 4: // Confirmed
            {
                tradeEvent.type = ETradeEventType.Confirmed;
                var timestamp = evt["timestamp"] as int?;
                tradeEvent.timestamp = (uint)timestamp.Value;
                enqueue = true;
                break;
            }

            case 7: // Chat message
            {
                string text = String.Empty;
                if (evt.ContainsKey("text"))
                {
                    text = evt["text"] as string;
                }
                tradeEvent.type    = ETradeEventType.Message;
                tradeEvent.message = text;
                enqueue            = true;
                break;
            }

            default:
                Console.WriteLine("Invalid action: " + action);
                break;
            }

            if (loadForeign)
            {
                PendingEvents.Enqueue(tradeEvent);
            }
            else if (enqueue)
            {
                Events.Enqueue(tradeEvent);
            }

            ResetTimer();
        }
        void ProcessInitialParams(IDictionary<string, object> Params)
        {
            if (Params.ContainsKey("strInventoryLoadURL"))
            {
                InventoryLoadURL = Params["strInventoryLoadURL"] as string;
                InventoryLoadURL = InventoryLoadURL.Trim(new char[]{ '\'' });
                //Console.WriteLine("InventoryLoadURL: " + InventoryLoadURL);
            }

            if (Params.ContainsKey("sessionID"))
            {
                string SessionId = Params["sessionID"] as string;
                SessionId = SessionId.Trim(new char[]{ '\'', '"' });
                if (SessionId != LoginData.SessionId)
                    throw new Steam2Exception("Session ids from web differ.");
            }

            if (!Params.ContainsKey("rgAppContextData"))
                return;

            string data = Params["rgAppContextData"] as string;
            var ctxs = new JsonReader().Read(data) as IDictionary<string, object>;

            InventoryApps = new List<TradeInventoryApp>();

            foreach (var kvp in ctxs)
            {
                var ctx = kvp.Value as IDictionary<string, object>;
                var app = Inventory.ProcessInventoryApp(ctx);
                InventoryApps.Add(app);
            }

            var evt = new TradeEvent();
            evt.type = ETradeEventType.Initialized;
            evt.inventoryApps = InventoryApps;

            Events.Enqueue(evt);
        }
Exemple #7
0
        /// <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 = (ulong) tradeEvent.assetid;
            Inventory.Item item;

            if (OtherInventory != null)
            {
                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);
            }
        }
Exemple #8
0
        private void FireOnUserRemoveItem(TradeEvent tradeEvent)
        {
            ulong itemID = (ulong)tradeEvent.assetid;

            // TODO: Add log
            // Customer removed item [itemID]

            if (OtherInventory != null)
            {
                Inventory.Item item = OtherInventory.GetItem(itemID);
                if (null != item)
                {
                    Schema.Item schemaItem = CurrentSchema.GetItem(item.Defindex);
                    if (null == schemaItem)
                    {

                        // TODO: Add log
                        // Could not find item in schema.
                    }

                    OnUserRemoveItem(schemaItem, item);
                    return;
                }
                else
                {
                    // TODO: Add log
                    // Could not find item in customer's inventory.
                    // Cannot look for item in schema.

                    OnUserAddItem(null, item);
                    return;
                }
            }
            else
            {
                // TODO: Add log
                // Customer inventory is private.

                var schemaItem = GetItemFromPrivateBp(tradeEvent, itemID);
                if (null == schemaItem)
                {
                    // TODO: Add log
                    // Could not find item in schema.
                }

                OnUserRemoveItem(schemaItem, null);
                return;
            }
        }
        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();
 }
Exemple #13
0
        void ProcessEvent(int key, ExpandoObject obj)
        {
            //Console.WriteLine("Key " + key + " NextLogPos: " + (NextLogPos-1));

            if (key < Interlocked.Read(ref NextLogPos) - 1)
            {
                //Console.WriteLine("Ignoring...");
                return;
            }

            var evt = obj as IDictionary<string, object>;

            if (!evt.ContainsKey("action"))
                return;

            int action;
            if (!Int32.TryParse(evt["action"] as string, out action))
                return;

            string steamid = String.Empty;
            if (evt.ContainsKey("steamid"))
                steamid = evt["steamid"] as string;

            var steamId = new SteamID();
            steamId.SetFromUInt64(UInt64.Parse(steamid));

            if (steamId == OwnSteamId)
                return;

            var appid = evt["appid"] as int?;

            var tradeEvent = new TradeEvent();
            tradeEvent.sender = steamId;

            bool enqueue = false;
            bool loadForeign = false;

            switch (action)
            {
            case 0: // Item added/remove
            case 1:
            {
                var item = new TradeItem();
                tradeEvent.type = (action == 0) ?
                    ETradeEventType.ItemAdded : ETradeEventType.ItemRemoved;
                item.appid = appid.Value;
                int.TryParse(evt["assetid"] as string, out item.assetid);
                int.TryParse(evt["contextid"] as string, out item.contextid);
                tradeEvent.item = item;
                if (ForeignInventory == null)
                {
                    LoadForeignInventory(steamId, item.appid, item.contextid);
                    loadForeign = true;
                }
                enqueue = true;
                break;
            }
            case 2: // Ready/unready
            case 3:
            {
                tradeEvent.type = (action == 2) ?
                    ETradeEventType.Ready : ETradeEventType.Unready;
                var timestamp = evt["timestamp"] as int?;
                tradeEvent.timestamp = (uint)timestamp.Value;
                enqueue = true;
                break;
            }
            case 4: // Confirmed
            {
                tradeEvent.type = ETradeEventType.Confirmed;
                var timestamp = evt["timestamp"] as int?;
                tradeEvent.timestamp = (uint)timestamp.Value;
                enqueue = true;
                break;
            }
            case 7: // Chat message
            {
                string text = String.Empty;
                if (evt.ContainsKey("text"))
                    text = evt["text"] as string;
                tradeEvent.type = ETradeEventType.Message;
                tradeEvent.message = text;
                enqueue = true;
                break;
            }
            default:
                Console.WriteLine("Invalid action: " + action);
                break;
            }

            if (loadForeign)
                PendingEvents.Enqueue(tradeEvent);
            else if (enqueue)
                Events.Enqueue(tradeEvent);

            ResetTimer();
        }
Exemple #14
0
        void ProcessForeignInventory(IAsyncResult res)
        {
            var webRequest = res.AsyncState as HttpWebRequest;

            string status;
            try
            {
                using (var response = webRequest.EndGetResponse(res))
                using (Stream stream = response.GetResponseStream())
                    status = stream.ReadAll();
            }
            catch (WebException ex)
            {
                Console.WriteLine("Web Exception: " + ex);
                return;
            }

            var data = new JsonReader().Read(status) as IDictionary<string, object>;

            var success = data["success"] as bool?;
            if (success.HasValue && success.Value == false)
            {
                var error = data["error"] as string;
                if (error != null)
                    Console.WriteLine("Could not get foreign inventory: " + error);
                return;
            }

            ForeignInventory = new Inventory();
            ForeignInventory.TryParse(data);

            var evt = new TradeEvent();
            evt.type = ETradeEventType.ForeignInventoryLoaded;
            Events.Enqueue(evt);
        }
Exemple #15
0
        void ProcessInventory(IAsyncResult res)
        {
            var webRequest = res.AsyncState as HttpWebRequest;

            string status;
            try
            {
                using (var response = webRequest.EndGetResponse(res))
                using (Stream stream = response.GetResponseStream())
                    status = stream.ReadAll();
            }
            catch (WebException ex)
            {
                Console.WriteLine("Web Exception: " + ex);
                return;
            }

            var data = new JsonReader().Read(status) as IDictionary<string, object>;
            
            var success = data["success"] as bool?;
            if (success.HasValue && success.Value == false)
                return;

            Inventory = new Inventory();
            Inventory.TryParse(data);

            var @event = new TradeEvent();
            @event.type = ETradeEventType.InventoryLoaded;
            Events.Enqueue(@event);
        }
Exemple #16
0
        private void FireOnUserRemoveItem(TradeEvent tradeEvent)
        {
            ulong itemID = (ulong) tradeEvent.assetid;

            if (OtherInventory != null)
            {
                Inventory.Item item = OtherInventory.GetItem(itemID);
                Schema.Item schemaItem = CurrentSchema.GetItem(item.Defindex);
                OnUserRemoveItem(schemaItem, item);
            }
            else
            {
                var schemaItem = GetItemFromPrivateBp(tradeEvent, itemID);
                OnUserRemoveItem(schemaItem, null);
            }
        }
Exemple #17
0
        /// <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)
        {
            // TODO: Add log
            // Customer removed item

            ulong itemID = tradeEvent.assetid;

            if (null != OtherInventory)
            {
                Inventory.Item item = OtherInventory.GetItem(itemID);
                if (null != item)
                {
                    Schema.Item schemaItem = CurrentSchema.GetItem(item.Defindex);
                    if (null == schemaItem)
                    {
                        // TODO: Log this
                        // Could not find item in schema.
                    }

                    OnUserAddItem(schemaItem, item);
                    return;
                }
                else
                {
                    // TODO: Log this
                    // Could not find item in customer's inventory."
                    // Cannot look for item in schema.

                    OnUserAddItem(null, item);
                    return;
                }
            }
            else
            {
                // Customer inventory is private.

                var schemaItem = GetItemFromPrivateBp(tradeEvent, itemID);
                if (null == schemaItem)
                {
                    // TODO: Log this
                    // Could not find item in schema.
                }

                OnUserAddItem(schemaItem, null);
                // todo: figure out what to send in with Inventory item.....

                return;
            }
        }
        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);
        }
Exemple #19
0
        /// <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;
            Inventory.Item item;

            if (OtherInventory != null)
            {
                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.....
            }
        }
 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;
     }
 }
Exemple #21
0
        private Schema.Item GetItemFromPrivateBp(TradeEvent tradeEvent, ulong itemID)
        {
            if (OtherPrivateInventory == null)
            {
                // get the foreign inventory
                var f = session.GetForiegnInventory(OtherSID, tradeEvent.contextid,tradeEvent.contextid);
                OtherPrivateInventory = new ForeignInventory(f);
            }

            ushort defindex = OtherPrivateInventory.GetDefIndex(itemID);

            Schema.Item schemaItem = CurrentSchema.GetItem(defindex);
            return schemaItem;
        }
Exemple #22
0
        void FinishTrade(ETradeTransactionStatus status,
            Action<TradeEvent> eventCallback = null)
        {
            CancelTimer();
            Status = status;

            var @event = new TradeEvent();
            @event.type = ETradeEventType.Finished;

            if (eventCallback != null)
                eventCallback(@event);

            Events.Enqueue(@event);
        }