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; } } }
public void ProcessState() { if (DateTime.UtcNow.Subtract(_lastAction).TotalSeconds < 1) { return; } _lastAction = DateTime.UtcNow; if (Cache.Instance.ItemHangar == null) { return; } DirectMarketWindow marketWindow = Cache.Instance.Windows.OfType <DirectMarketWindow>().FirstOrDefault(); switch (_States.CurrentBuyLPIState) { case BuyLPIState.Idle: case BuyLPIState.Done: break; case BuyLPIState.Begin: /* * if(marketWindow != null) * marketWindow.Close(); * * if(lpstore != null) * lpstore.Close();*/ _States.CurrentBuyLPIState = BuyLPIState.ReadyItemhangar; break; case BuyLPIState.ReadyItemhangar: if (Cache.Instance.ItemHangar == null) { return; } if (!Cache.Instance.OpenShipsHangar("BuyLPI")) { return; } _States.CurrentBuyLPIState = BuyLPIState.OpenLpStore; break; case BuyLPIState.OpenLpStore: if (Cache.Instance.LPStore == null) { return; } _States.CurrentBuyLPIState = BuyLPIState.FindOffer; break; case BuyLPIState.FindOffer: if (Cache.Instance.LPStore != null) { BuyLPI._offer = Cache.Instance.LPStore.Offers.FirstOrDefault(o => o.TypeId == Item); // Wait for the amount of LP to change if (_lastLoyaltyPoints == Cache.Instance.LPStore.LoyaltyPoints) { break; } // 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); _States.CurrentBuyLPIState = BuyLPIState.Done; break; } break; } _lastLoyaltyPoints = Cache.Instance.LPStore.LoyaltyPoints; // Find the offer if (_offer == null) { Logging.Log("BuyLPI", "Can't find offer with type name/id: " + Item + "!", Logging.White); _States.CurrentBuyLPIState = BuyLPIState.Done; break; } _States.CurrentBuyLPIState = BuyLPIState.CheckPetition; } _States.CurrentBuyLPIState = BuyLPIState.OpenLpStore; break; case BuyLPIState.CheckPetition: if (Cache.Instance.LPStore != null) { // Check LP if (_lastLoyaltyPoints < _offer.LoyaltyPointCost) { Logging.Log("BuyLPI", "Not enough loyalty points left", Logging.White); _States.CurrentBuyLPIState = BuyLPIState.Done; break; } // Check ISK if (Cache.Instance.DirectEve.Me.Wealth < _offer.IskCost) { Logging.Log("BuyLPI", "Not enough ISK left", Logging.White); _States.CurrentBuyLPIState = BuyLPIState.Done; break; } // Check items foreach (DirectLoyaltyPointOfferRequiredItem requiredItem in _offer.RequiredItems) { DirectItem ship = Cache.Instance.ShipHangar.Items.FirstOrDefault(i => i.TypeId == requiredItem.TypeId); DirectItem item = Cache.Instance.ItemHangar.Items.FirstOrDefault(i => i.TypeId == requiredItem.TypeId); if (item == null || item.Quantity < requiredItem.Quantity) { if (ship == null || ship.Quantity < requiredItem.Quantity) { Logging.Log("BuyLPI", "Missing [" + requiredItem.Quantity + "] x [" + requiredItem.TypeName + "]", Logging.White); //if(!_form.chkBuyItems.Checked) //{ // Logging.Log("BuyLPI","Done, do not buy item"); // States.CurrentBuyLPIState = BuyLPIState.Done; // break; //} Logging.Log("BuyLPI", "Are buying the item [" + requiredItem.TypeName + "]", Logging.White); _requiredUnit = Convert.ToInt32(requiredItem.Quantity); _requiredItemId = requiredItem.TypeId; _States.CurrentBuyLPIState = BuyLPIState.OpenMarket; return; } } _States.CurrentBuyLPIState = BuyLPIState.AcceptOffer; } _States.CurrentBuyLPIState = BuyLPIState.OpenLpStore; } break; case BuyLPIState.OpenMarket: if (marketWindow == null) { Cache.Instance.DirectEve.ExecuteCommand(DirectCmd.OpenMarket); break; } if (!marketWindow.IsReady) { break; } _States.CurrentBuyLPIState = BuyLPIState.BuyItems; break; case BuyLPIState.BuyItems: Logging.Log("BuyLPI", "Opening Market", Logging.White); if (marketWindow != null && marketWindow.DetailTypeId != _requiredItemId) { marketWindow.LoadTypeId(_requiredItemId); break; } if (marketWindow != null) { IEnumerable <DirectOrder> orders = marketWindow.SellOrders.Where(o => o.StationId == Cache.Instance.DirectEve.Session.StationId); DirectOrder order = orders.OrderBy(o => o.Price).FirstOrDefault(); if (order == null) { Logging.Log("BuyLPI", "No orders", Logging.White); _States.CurrentBuyLPIState = BuyLPIState.Done; break; } order.Buy(_requiredUnit, DirectOrderRange.Station); } Logging.Log("BuyLPI", "Buy Item", Logging.White); _States.CurrentBuyLPIState = BuyLPIState.CheckPetition; break; case BuyLPIState.AcceptOffer: if (Cache.Instance.LPStore != null) { DirectLoyaltyPointOffer offer2 = Cache.Instance.LPStore.Offers.FirstOrDefault(o => o.TypeId == Item); if (offer2 != null) { Logging.Log("BuyLPI", "Accepting [" + offer2.TypeName + "]", Logging.White); offer2.AcceptOfferFromWindow(); } } _States.CurrentBuyLPIState = BuyLPIState.Quantity; break; case BuyLPIState.Quantity: _loyaltyPointTimeout = DateTime.UtcNow.AddSeconds(1); Unit = Unit - 1; if (Unit <= 0) { Logging.Log("BuyLPI", "Quantity limit reached", Logging.White); _States.CurrentBuyLPIState = BuyLPIState.Done; break; } _States.CurrentBuyLPIState = BuyLPIState.Begin; break; } }