コード例 #1
0
ファイル: Trade.cs プロジェクト: windykiss/SteamBot
        /// <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();
                }
            }

            StatusObj status = GetStatus();

            if (status == null)
            {
                throw new TradeException("The web command to get the trade status failed.");
            }

            // I've noticed this when the trade is cancelled.
            if (status.trade_status == 3)
            {
                if (OnError != null)
                {
                    OnError("Trade was cancelled by other user.");
                }

                OtherUserCancelled = true;
                return(otherDidSomething);
            }

            if (status.events != null)
            {
                foreach (TradeEvent trdEvent in status.events)
                {
                    if (!eventList.Contains(trdEvent))
                    {
                        eventList.Add(trdEvent);//add event to processed list, as we are taking care of this event now
                        bool isBot = trdEvent.steamid == MySteamId.ConvertToUInt64().ToString();

                        /*
                         *
                         * Trade Action ID's
                         *
                         * 0 = Add item (itemid = "assetid")
                         * 1 = remove item (itemid = "assetid")
                         * 2 = Toggle ready
                         * 3 = Toggle not ready
                         * 4
                         * 5
                         * 6
                         * 7 = Chat (message = "text")
                         *
                         */
                        ulong itemID;

                        switch ((TradeEventType)trdEvent.action)
                        {
                        case TradeEventType.ItemAdded:
                            itemID = (ulong)trdEvent.assetid;

                            if (isBot)
                            {
                                steamMyOfferedItems.Add(itemID);
                                ValidateSteamItemChanged(itemID, true);
                                Inventory.Item item       = MyInventory.GetItem(itemID);
                                Schema.Item    schemaItem = CurrentSchema.GetItem(item.Defindex);
                            }
                            else
                            {
                                OtherOfferedItems.Add(itemID);
                                Inventory.Item item       = OtherInventory.GetItem(itemID);
                                Schema.Item    schemaItem = CurrentSchema.GetItem(item.Defindex);
                                OnUserAddItem(schemaItem, item);
                            }

                            break;

                        case TradeEventType.ItemRemoved:
                            itemID = (ulong)trdEvent.assetid;

                            if (isBot)
                            {
                                steamMyOfferedItems.Remove(itemID);
                                ValidateSteamItemChanged(itemID, false);
                                Inventory.Item item       = MyInventory.GetItem(itemID);
                                Schema.Item    schemaItem = CurrentSchema.GetItem(item.Defindex);
                            }
                            else
                            {
                                OtherOfferedItems.Remove(itemID);
                                Inventory.Item item       = OtherInventory.GetItem(itemID);
                                Schema.Item    schemaItem = CurrentSchema.GetItem(item.Defindex);
                                OnUserRemoveItem(schemaItem, item);
                            }
                            break;

                        case TradeEventType.UserSetReady:
                            if (!isBot)
                            {
                                otherIsReady = true;
                                OnUserSetReady(true);
                            }
                            break;

                        case TradeEventType.UserSetUnReady:
                            if (!isBot)
                            {
                                otherIsReady = false;
                                OnUserSetReady(false);
                            }
                            break;

                        case TradeEventType.UserAccept:
                            if (!isBot)
                            {
                                OnUserAccept();
                            }
                            break;

                        case TradeEventType.UserChat:
                            if (!isBot)
                            {
                                OnMessage(trdEvent.text);
                            }
                            break;

                        default:
                            // Todo: add an OnWarning or similar event
                            if (OnError != null)
                            {
                                OnError("Unknown Event ID: " + trdEvent.action);
                            }
                            break;
                        }

                        if (!isBot)
                        {
                            otherDidSomething = true;
                        }
                    } //if (!eventList.Contains(trdEvent))
                }     // foreach (TradeEvent trdEvent in status.events)
            }         //if (status.events != null)

            // Update Local Variables
            if (status.them != null)
            {
                otherIsReady = status.them.ready == 1 ? true : false;
                meIsReady    = status.me.ready == 1 ? true : false;
            }

            // Update version
            if (status.newversion)
            {
                Version = status.version;
            }

            if (status.logpos != 0)
            {
                LogPos = status.logpos;
            }

            return(otherDidSomething);
        }
コード例 #2
0
ファイル: Trade.cs プロジェクト: norgalyn/SteamBot
        /// <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();
                }
            }

            StatusObj status = GetStatus();

            if (status == null)
            {
                throw new TradeException("The web command to get the trade status failed.");
            }

            // I've noticed this when the trade is cancelled.
            if (status.trade_status == 3)
            {
                if (OnError != null)
                {
                    OnError("Trade was cancelled by other user.");
                }

                OtherUserCancelled = true;
                return(otherDidSomething);
            }

            if (status.events != null && numEvents != status.events.Length)
            {
                int numLoops = status.events.Length - numEvents;
                numEvents = status.events.Length;

                for (int i = numLoops; i > 0; i--)
                {
                    int EventID;

                    if (numLoops == 1)
                    {
                        EventID = numEvents - 1;
                    }
                    else
                    {
                        EventID = numEvents - i;
                    }

                    bool isBot = status.events [EventID].steamid == MySteamId.ConvertToUInt64().ToString();

                    /*
                     *
                     * Trade Action ID's
                     *
                     * 0 = Add item (itemid = "assetid")
                     * 1 = remove item (itemid = "assetid")
                     * 2 = Toggle ready
                     * 3 = Toggle not ready
                     * 4
                     * 5
                     * 6
                     * 7 = Chat (message = "text")
                     *
                     */
                    ulong itemID;

                    switch (status.events [EventID].action)
                    {
                    case 0:
                        itemID = (ulong)status.events [EventID].assetid;

                        if (isBot)
                        {
                            steamMyOfferedItems.Add(itemID);
                            ValidateSteamItemChanged(itemID, true);
                        }
                        else
                        {
                            OtherOfferedItems.Add(itemID);
                            Inventory.Item item       = OtherInventory.GetItem(itemID);
                            Schema.Item    schemaItem = CurrentSchema.GetItem(item.Defindex);
                            OnUserAddItem(schemaItem, item);
                        }

                        break;

                    case 1:
                        itemID = (ulong)status.events [EventID].assetid;

                        if (isBot)
                        {
                            steamMyOfferedItems.Remove(itemID);
                            ValidateSteamItemChanged(itemID, false);
                        }
                        else
                        {
                            OtherOfferedItems.Remove(itemID);
                            Inventory.Item item       = OtherInventory.GetItem(itemID);
                            Schema.Item    schemaItem = CurrentSchema.GetItem(item.Defindex);
                            OnUserRemoveItem(schemaItem, item);
                        }

                        break;

                    case 2:
                        if (!isBot)
                        {
                            otherIsReady = true;
                            OnUserSetReady(true);
                        }
                        break;

                    case 3:
                        if (!isBot)
                        {
                            otherIsReady = false;
                            OnUserSetReady(false);
                        }
                        break;

                    case 4:
                        if (!isBot)
                        {
                            OnUserAccept();
                        }
                        break;

                    case 7:
                        if (!isBot)
                        {
                            OnMessage(status.events [EventID].text);
                        }
                        break;

                    default:
                        // Todo: add an OnWarning or similar event
                        if (OnError != null)
                        {
                            OnError("Unkown Event ID: " + status.events [EventID].action);
                        }
                        break;
                    }

                    if (!isBot)
                    {
                        otherDidSomething = true;
                    }
                }
            }

            // Update Local Variables
            if (status.them != null)
            {
                otherIsReady = status.them.ready == 1 ? true : false;
                meIsReady    = status.me.ready == 1 ? true : false;
            }

            // Update version
            if (status.newversion)
            {
                Version = status.version;
            }

            if (status.logpos != 0)
            {
                LogPos = status.logpos;
            }

            return(otherDidSomething);
        }
コード例 #3
0
        /// <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>
        public void Poll()
        {
            if (!TradeStarted)
            {
                tradeStarted        = true;
                tradeStartTime      = DateTime.Now;
                lastOtherActionTime = DateTime.Now;
            }

            StatusObj status = GetStatus();

            if (status == null)
            {
                throw new TradeException("The web command to get the trade status failed.");
            }

            // I've noticed this when the trade is cancelled.
            if (status.trade_status == 3)
            {
                if (OnError != null)
                {
                    OnError("Trade was cancelled by other user.");
                }

                OtherUserCancelled = true;
                return;
            }

            if (status.events != null && numEvents != status.events.Length)
            {
                int numLoops = status.events.Length - numEvents;
                numEvents = status.events.Length;

                for (int i = numLoops; i > 0; i--)
                {
                    int EventID;

                    if (numLoops == 1)
                    {
                        EventID = numEvents - 1;
                    }
                    else
                    {
                        EventID = numEvents - i;
                    }

                    bool isBot = status.events [EventID].steamid == MySteamId.ConvertToUInt64().ToString();

                    /*
                     *
                     * Trade Action ID's
                     *
                     * 0 = Add item (itemid = "assetid")
                     * 1 = remove item (itemid = "assetid")
                     * 2 = Toggle ready
                     * 3 = Toggle not ready
                     * 4
                     * 5
                     * 6
                     * 7 = Chat (message = "text")
                     *
                     */
                    ulong itemID;

                    switch (status.events [EventID].action)
                    {
                    case 0:
                        itemID = (ulong)status.events [EventID].assetid;

                        if (isBot)
                        {
                            steamMyOfferedItems.Add(itemID);
                            ValidateSteamItemChanged(itemID, true);
                        }
                        else
                        {
                            OtherOfferedItems.Add(itemID);
                            Inventory.Item item       = OtherInventory.GetItem(itemID);
                            Schema.Item    schemaItem = CurrentSchema.GetItem(item.Defindex);
                            OnUserAddItem(schemaItem, item);
                        }

                        break;

                    case 1:
                        itemID = (ulong)status.events [EventID].assetid;

                        if (isBot)
                        {
                            steamMyOfferedItems.Remove(itemID);
                            ValidateSteamItemChanged(itemID, false);
                        }
                        else
                        {
                            OtherOfferedItems.Remove(itemID);
                            Inventory.Item item       = OtherInventory.GetItem(itemID);
                            Schema.Item    schemaItem = CurrentSchema.GetItem(item.Defindex);
                            OnUserRemoveItem(schemaItem, item);
                        }

                        break;

                    case 2:
                        if (!isBot)
                        {
                            otherIsReady = true;
                            OnUserSetReady(true);
                        }
                        break;

                    case 3:
                        if (!isBot)
                        {
                            otherIsReady = false;
                            OnUserSetReady(false);
                        }
                        break;

                    case 4:
                        if (!isBot)
                        {
                            OnUserAccept();
                        }
                        break;

                    case 7:
                        if (!isBot)
                        {
                            OnMessage(status.events [EventID].text);
                        }
                        break;

                    default:
                        // Todo: add an OnWarning or similar event
                        if (OnError != null)
                        {
                            OnError("Unkown Event ID: " + status.events [EventID].action);
                        }
                        break;
                    }

                    if (!isBot)
                    {
                        lastOtherActionTime = DateTime.Now;
                    }
                }
            }
            else
            {
                // check if the user is AFK
                var now = DateTime.Now;

                DateTime actionTimeout      = lastOtherActionTime.AddSeconds(MaximumActionGap);
                int      untilActionTimeout = (int)Math.Round((actionTimeout - now).TotalSeconds);

                DateTime tradeTimeout      = TradeStartTime.AddSeconds(MaximumTradeTime);
                int      untilTradeTimeout = (int)Math.Round((tradeTimeout - now).TotalSeconds);

                if (untilActionTimeout <= 0 || untilTradeTimeout <= 0)
                {
                    if (OnTimeout != null)
                    {
                        OnTimeout();
                    }
                    CancelTrade();
                }
                else if (untilActionTimeout <= 15 && untilActionTimeout % 5 == 0)
                {
                    SendMessageWebCmd("Are You AFK? The trade will be canceled in " + untilActionTimeout + " seconds if you don't do something.");
                }
            }

            // Update Local Variables
            if (status.them != null)
            {
                otherIsReady = status.them.ready == 1 ? true : false;
                meIsReady    = status.me.ready == 1 ? true : false;
            }

            // Update version
            if (status.newversion)
            {
                Version = status.version;
            }

            if (status.logpos != 0)
            {
                LogPos = status.logpos;
            }
        }