BuyLPI.BuyLPI.OnFrame C# (CSharp) Method

OnFrame() private static method

private static OnFrame ( object sender, EventArgs eventArgs ) : void
sender object
eventArgs System.EventArgs
return void
        private static void OnFrame(object sender, EventArgs eventArgs)
        {
            // New frame, invalidate old cache
            Cache.Instance.InvalidateCache();

            Cache.Instance.LastFrame = DateTime.UtcNow;

            // Only pulse state changes every 1.5s
            if (DateTime.UtcNow.Subtract(_lastPulse).TotalMilliseconds < 300)
                return;

            _lastPulse = DateTime.UtcNow;

            // Session is not ready yet, do not continue
            if (!Cache.Instance.DirectEve.Session.IsReady)
                return;

            if (Cache.Instance.DirectEve.Session.IsReady)
                Cache.Instance.LastSessionIsReady = DateTime.UtcNow;

            // We are not in space or station, don't do shit yet!
            if (!Cache.Instance.InSpace && !Cache.Instance.InStation)
            {
                Cache.Instance.NextInSpaceorInStation = DateTime.UtcNow.AddSeconds(12);
                Cache.Instance.LastSessionChange = DateTime.UtcNow;
                return;
            }

            if (DateTime.UtcNow < Cache.Instance.NextInSpaceorInStation)
                return;

            if (Cache.Instance.SessionState != "Quitting")
            {
                // Update settings (settings only load if character name changed)
                if (!Settings.Instance.DefaultSettingsLoaded)
                {
                    Settings.Instance.LoadSettings();
                }
            }

            // Start _cleanup.ProcessState
            // Description: Closes Windows, and eventually other things considered 'cleanup' useful to more than just Questor(Missions) but also Anomalies, Mining, etc
            //
            _cleanup.ProcessState();

            // Done
            // Cleanup State: ProcessState

            if (DateTime.UtcNow > _done)
                return;

            // Wait for the next action
            if (_nextAction >= DateTime.UtcNow)
            {
                return;
            }

            if (Cache.Instance.ItemHangar == null) return;

            if (Cache.Instance.LPStore == null)
            {
                _nextAction = DateTime.UtcNow.AddMilliseconds(WaitMillis);
                 return;
            }

            // Wait for the amount of LP to change
            if (_lastLoyaltyPoints == Cache.Instance.LPStore.LoyaltyPoints)
                return;

            // Do not expect it to be 0 (probably means its reloading)
            if (Cache.Instance.LPStore.LoyaltyPoints == 0)
            {
                if (_loyaltyPointTimeout < DateTime.UtcNow)
                {
                    Logging.Log("BuyLPI", "It seems we have no loyalty points left", Logging.White);
                    _done = DateTime.UtcNow;
                    return;
                }
                return;
            }

            _lastLoyaltyPoints = Cache.Instance.LPStore.LoyaltyPoints;

            // Find the offer
            DirectLoyaltyPointOffer offer = Cache.Instance.LPStore.Offers.FirstOrDefault(o => o.TypeId.ToString(CultureInfo.InvariantCulture) == _type || String.Compare(o.TypeName, _type, StringComparison.OrdinalIgnoreCase) == 0);
            if (offer == null)
            {
                Logging.Log("BuyLPI", " Can't find offer with type name/id: [" + _type + "]", Logging.White);
                _done = DateTime.UtcNow;
                return;
            }

            // Check LP
            if (_lastLoyaltyPoints < offer.LoyaltyPointCost)
            {
                Logging.Log("BuyLPI", "Not enough loyalty points left: you have [" + _lastLoyaltyPoints + "] and you need [" + offer.LoyaltyPointCost + "]", Logging.White);
                _done = DateTime.UtcNow;
                return;
            }

            // Check ISK
            if (Cache.Instance.DirectEve.Me.Wealth < offer.IskCost)
            {
                Logging.Log("BuyLPI", "Not enough ISK left: you have [" + Math.Round(Cache.Instance.DirectEve.Me.Wealth, 0) + "] and you need [" + offer.IskCost + "]", Logging.White);
                _done = DateTime.UtcNow;
                return;
            }

            // Check items
            foreach (DirectLoyaltyPointOfferRequiredItem requiredItem in offer.RequiredItems)
            {
                DirectItem item = Cache.Instance.ItemHangar.Items.FirstOrDefault(i => i.TypeId == requiredItem.TypeId);
                if (item == null || item.Quantity < requiredItem.Quantity)
                {
                    Logging.Log("BuyLPI", "Missing [" + requiredItem.Quantity + "] x [" +
                                                    requiredItem.TypeName + "]", Logging.White);
                    _done = DateTime.UtcNow;
                    return;
                }
            }

            // All passed, accept offer
            if (_quantity != null)
                if (_totalQuantityOfOrders != null)
                    Logging.Log("BuyLPI", "Accepting " + offer.TypeName + " [ " + _quantity.Value + " ] of [ " + _totalQuantityOfOrders.Value + " ] orders and will cost another [" + Math.Round(((offer.IskCost * _quantity.Value) / (double)1000000), 2) + "mil isk]", Logging.White);
            offer.AcceptOfferFromWindow();

            // Set next action + loyalty point timeout
            _nextAction = DateTime.UtcNow.AddMilliseconds(WaitMillis);
            _loyaltyPointTimeout = DateTime.UtcNow.AddSeconds(25);

            if (_quantity.HasValue)
            {
                _quantity = _quantity.Value - 1;
                if (_quantity.Value <= 0)
                {
                    Logging.Log("BuyLPI", "Quantity limit reached", Logging.White);
                    _done = DateTime.UtcNow;
                    return;
                }
            }
        }