Esempio n. 1
0
        /// <summary>
        /// Starts the actual trade-polling thread.
        /// </summary>
        public void StartTradeThread(Trade trade)
        {
            // initialize data to use in thread
            tradeStartTime = DateTime.Now;
            lastOtherActionTime = DateTime.Now;
            lastTimeoutMessage = DateTime.Now.AddSeconds(-1000);

            var pollThread = new Thread (() =>
                {
                    IsTradeThreadRunning = true;

                    DebugPrint ("Trade thread starting.");

                    // main thread loop for polling
                    try
                    {
                        while(IsTradeThreadRunning)
                        {
                            bool action = trade.Poll();

                            if(action)
                                lastOtherActionTime = DateTime.Now;

                            if (trade.HasTradeEnded || CheckTradeTimeout(trade))
                            {
                                IsTradeThreadRunning = false;
                                break;
                            }

                            Thread.Sleep(TradePollingInterval);
                        }
                    }
                    catch(Exception ex)
                    {
                        // TODO: find a new way to do this w/o the trade events
                        //if (OnError != null)
                        //    OnError("Error Polling Trade: " + e);

                        // ok then we should stop polling...
                        IsTradeThreadRunning = false;
                        DebugPrint("[TRADEMANAGER] general error caught: " + ex);
                        trade.FireOnErrorEvent("Unknown error occurred: " + ex.ToString());
                    }
                    finally
                    {
                        DebugPrint("Trade thread shutting down.");
                        try
                        {
                            try //Yikes, that's a lot of nested 'try's.  Is there some way to clean this up?
                            {
                                if(trade.HasTradeCompletedOk)
                                    trade.FireOnSuccessEvent();
                                else if(trade.IsTradeAwaitingConfirmation)
                                    trade.FireOnAwaitingConfirmation();
                            }
                            finally
                            {
                                //Make sure OnClose is always fired after OnSuccess, even if OnSuccess throws an exception
                                //(which it NEVER should, but...)
                                trade.FireOnCloseEvent();
                            }
                        }
                        catch(Exception ex)
                        {
                            trade.FireOnErrorEvent("Unknown error occurred DURING CLEANUP(!?): " + ex.ToString());
                        }
                    }
                });

            pollThread.Start();
        }
Esempio n. 2
0
        private bool CheckTradeTimeout(Trade trade)
        {
            // User has accepted the trade. Disregard time out.
            if (trade.OtherUserAccepted)
                return false;

            var now = DateTime.Now;

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

            DebugPrint (String.Format ("{0} {1}", actionTimeout, untilActionTimeout));

            DateTime tradeTimeout = tradeStartTime.AddSeconds (MaxTradeTimeSec);
            int untilTradeTimeout = (int)Math.Round ((tradeTimeout - now).TotalSeconds);

            double secsSinceLastTimeoutMessage = (now - lastTimeoutMessage).TotalSeconds;

            if (untilActionTimeout <= 0 || untilTradeTimeout <= 0)
            {
                DebugPrint ("timed out...");

                if (OnTimeout != null)
                {
                    OnTimeout (this, null);
                }

                trade.CancelTrade ();

                return true;
            }
            else if (untilActionTimeout <= 20 && secsSinceLastTimeoutMessage >= 10)
            {
                try
                {
                    trade.SendMessage("Are You AFK? The trade will be canceled in " + untilActionTimeout + " seconds if you don't do something.");
                }
                catch { }
                lastTimeoutMessage = now;
            }
            return false;
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a trade object and returns it for use. 
        /// Call <see cref="InitializeTrade"/> before using this method.
        /// </summary>
        /// <returns>
        /// The trade object to use to interact with the Steam trade.
        /// </returns>
        /// <param name='me'>
        /// The <see cref="SteamID"/> of the bot.
        /// </param>
        /// <param name='other'>
        /// The <see cref="SteamID"/> of the other trade partner.
        /// </param>
        /// <remarks>
        /// If the needed inventories are <c>null</c> then they will be fetched.
        /// </remarks>
        public Trade CreateTrade(SteamID  me, SteamID other)
        {
            if (otherInventoryTask == null || myInventoryTask == null)
                InitializeTrade (me, other);

            var t = new Trade (me, other, SteamWeb, myInventoryTask, otherInventoryTask);

            t.OnClose += delegate
                {
                    IsTradeThreadRunning = false;
                };

            return t;
        }
Esempio n. 4
0
        void CheckTradeTimeout(Trade trade)
        {
            var now = DateTime.Now;

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

            DebugPrint(String.Format("{0} {1}", actionTimeout, untilActionTimeout));

            DateTime tradeTimeout = tradeStartTime.AddSeconds(MaxTradeTimeSec);
            int untilTradeTimeout = (int)Math.Round((tradeTimeout - now).TotalSeconds);

            if (untilActionTimeout <= 0 || untilTradeTimeout <= 0)
            {
                // stop thread
                IsTradeThreadRunning = false;

                DebugPrint("timed out...");

                if (OnTimeout != null)
                {
                    OnTimeout(this, null);
                }

                trade.CancelTrade();
            }
            else if (untilActionTimeout <= 20 && untilActionTimeout % 10 == 0)
            {
                trade.SendMessage("Are You AFK? The trade will be canceled in " + untilActionTimeout + " seconds if you don't do something.");
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Creates a trade object, starts the trade, and returns it for use. 
        /// Call <see cref="FetchInventories"/> before using this method.
        /// </summary>
        /// <returns>
        /// The trade object to use to interact with the Steam trade.
        /// </returns>
        /// <param name='me'>
        /// The <see cref="SteamID"/> of the bot.
        /// </param>
        /// <param name='other'>
        /// The <see cref="SteamID"/> of the other trade partner.
        /// </param>
        /// <remarks>
        /// If the needed inventories are <c>null</c> then they will be fetched.
        /// </remarks>
        public Trade StartTrade(SteamID me, SteamID other)
        {
            if (OtherInventory == null || MyInventory == null)
                InitializeTrade(me, other);

            var t = new Trade(me, other, sessionId, token, MyInventory, OtherInventory);

            t.OnClose += delegate
            {
                IsTradeThreadRunning = false;

                if (OnTradeEnded != null)
                    OnTradeEnded(this, null);
            };

            trade = t;

            StartTradeThread();

            return t;
        }
Esempio n. 6
0
        /// <summary>
        /// Creates a trade object and returns it for use. 
        /// Call <see cref="InitializeTrade"/> before using this method.
        /// </summary>
        /// <returns>
        /// The trade object to use to interact with the Steam trade.
        /// </returns>
        /// <param name='me'>
        /// The <see cref="SteamID"/> of the bot.
        /// </param>
        /// <param name='other'>
        /// The <see cref="SteamID"/> of the other trade partner.
        /// </param>
        /// <remarks>
        /// If the needed inventories are <c>null</c> then they will be fetched.
        /// </remarks>
        public Trade CreateTrade(SteamID me, SteamID other)
        {
            if (myInventory == null || otherInventory == null)
                InitializeTrade(me, other);

            var t = new Trade(me, other, sessionId, token, myInventory, otherInventory);

            t.OnClose += delegate
            {
                IsTradeThreadRunning = false;
            };

            return t;
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a trade object and returns it for use. 
        /// Call <see cref="InitializeTrade"/> before using this method.
        /// </summary>
        /// <returns>
        /// The trade object to use to interact with the Steam trade.
        /// </returns>
        /// <param name='me'>
        /// The <see cref="SteamID"/> of the bot.
        /// </param>
        /// <param name='other'>
        /// The <see cref="SteamID"/> of the other trade partner.
        /// </param>
        /// <param name="otherUserName">The trade partner's username</param>
        /// <remarks>
        /// If the needed inventories are <c>null</c> then they will be fetched.
        /// </remarks>
        public Trade CreateTrade(SteamID  me, SteamID other, string otherUserName)
        {
            if (otherInventoryTask == null || myInventoryTask == null)
                InitializeTrade (me, other);

            var t = new Trade (me, other, otherUserName, sessionId, token, myInventoryTask, otherInventoryTask);

            t.OnClose += delegate
            {
                IsTradeThreadRunning = false;
            };

            return t;
        }