Ejemplo n.º 1
0
        public static bool BuySkill(int skillID)
        {
            if (!Cache.Instance.InStation)
            {
                return(false);
            }
            if (DateTime.UtcNow < _nextSkillTrainingAction)
            {
                if (Settings.Instance.DebugSkillTraining)
                {
                    Logging.Log("SkillPlan.buySkill:", "Next Skill Training Action is set to continue in [" + Math.Round(_nextSkillTrainingAction.Subtract(DateTime.UtcNow).TotalSeconds, 0) + "] seconds", Logging.White);
                }
                return(false);
            }

            if (buyingSkill == true && buyingSkillTypeID != 0)
            {
                buyingIterator++;

                if (buyingIterator > 20)
                {
                    Logging.Log("buySkill", "buying iterator < 20 with SkillID" + skillID, Logging.White);
                    buyingSkill       = false;
                    buyingSkillTypeID = 0;
                    buyingIterator    = 0;
                    return(true);
                }
                // only buy if we do not have it already in our itemhangar
                if (DoWeHaveThisSkillAlreadyInOurItemHangar(skillID))
                {
                    Logging.Log("buySkill", "We already purchased this skill" + skillID, Logging.White);
                    buyingSkill       = false;
                    buyingSkillTypeID = 0;
                    buyingIterator    = 0;
                    return(true);
                }

                DirectMarketWindow marketWindow = Cache.Instance.DirectEve.Windows.OfType <DirectMarketWindow>().FirstOrDefault();
                if (Cache.Instance.DirectEve.HasSupportInstances())
                {
                    if (marketWindow == null)
                    {
                        _nextSkillTrainingAction = DateTime.UtcNow.AddSeconds(10);

                        Logging.Log("buySkill", "Opening market window", Logging.White);

                        Cache.Instance.DirectEve.ExecuteCommand(DirectCmd.OpenMarket);
                        return(false);
                    }
                    if (!marketWindow.IsReady)
                    {
                        _nextSkillTrainingAction = DateTime.UtcNow.AddSeconds(3);
                        return(false);
                    }

                    if (marketWindow.DetailTypeId != skillID)
                    {
                        // No, load the right order
                        marketWindow.LoadTypeId(skillID);
                        Logging.Log("buySkill", "Loading market with right typeid ", Logging.White);
                        _nextSkillTrainingAction = DateTime.UtcNow.AddSeconds(6);
                        return(false);
                    }

                    // Get the median sell price
                    InvType type     = Cache.Instance.InvTypesById[skillID];
                    double? maxPrice = type.MedianSell * 10;
                    Logging.Log("buySkill", "maxPrice " + maxPrice.ToString(), Logging.White);

                    // Do we have orders?
                    IEnumerable <DirectOrder> orders = marketWindow.SellOrders.Where(o => o.StationId == Cache.Instance.DirectEve.Session.StationId && o.Price < maxPrice).ToList();
                    if (orders.Any())
                    {
                        DirectOrder order = orders.OrderBy(o => o.Price).FirstOrDefault();
                        if (order != null)
                        {
                            order.Buy(1, DirectOrderRange.Station);
                        }
                        Logging.Log("buySkill", "Buying skill with typeid & waiting 20 seconds ( to ensure we don't buy the skills twice ) " + skillID, Logging.White);
                        buyingSkill       = false;
                        buyingSkillTypeID = 0;
                        buyingIterator    = 0;
                        // Wait for the order to go through
                        _nextRetrieveCharactersheetInfoAction = DateTime.MinValue;                         // ensure we get the character sheet update
                        _nextSkillTrainingAction = DateTime.UtcNow.AddSeconds(20);
                        return(true);
                    }
                    else
                    {
                        Logging.Log("buySkill", "No skill could be found with median price ", Logging.White);
                        buyingSkill       = false;
                        buyingSkillTypeID = 0;
                        buyingIterator    = 0;
                        return(false);
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        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;
            }
        }
Ejemplo n.º 3
0
        public void ProcessState()
        {
            if (!Cache.Instance.InStation)
            {
                return;
            }

            if (Cache.Instance.InSpace)
            {
                return;
            }

            if (DateTime.UtcNow < Cache.Instance.LastInSpace.AddSeconds(20)) // we wait 20 seconds after we last thought we were in space before trying to do anything in station
            {
                return;
            }

            DirectMarketWindow marketWindow = Cache.Instance.Windows.OfType <DirectMarketWindow>().FirstOrDefault();

            switch (_States.CurrentBuyState)
            {
            case BuyState.Idle:
            case BuyState.Done:
                break;

            case BuyState.Begin:

                // Close the market window if there is one
                if (marketWindow != null)
                {
                    marketWindow.Close();
                }
                _States.CurrentBuyState = BuyState.OpenMarket;
                break;

            case BuyState.OpenMarket:
                // Close the market window if there is one
                //if (marketWindow != null)
                //    marketWindow.Close();

                if (marketWindow == null)
                {
                    Cache.Instance.DirectEve.ExecuteCommand(DirectCmd.OpenMarket);
                    break;
                }

                if (!marketWindow.IsReady)
                {
                    break;
                }

                Logging.Log("Buy", "Opening Market", Logging.White);
                _States.CurrentBuyState = BuyState.LoadItem;

                break;

            case BuyState.LoadItem:

                _lastAction = DateTime.UtcNow;

                if (marketWindow != null && marketWindow.DetailTypeId != Item)
                {
                    marketWindow.LoadTypeId(Item);
                    if (useOrders)
                    {
                        _States.CurrentBuyState = BuyState.CreateOrder;
                    }
                    else
                    {
                        _States.CurrentBuyState = BuyState.BuyItem;
                    }

                    break;
                }

                break;

            case BuyState.CreateOrder:

                if (DateTime.UtcNow.Subtract(_lastAction).TotalSeconds < 5)
                {
                    break;
                }

                _lastAction = DateTime.UtcNow;

                if (marketWindow != null)
                {
                    IEnumerable <DirectOrder> orders = marketWindow.BuyOrders.Where(o => o.StationId == Cache.Instance.DirectEve.Session.StationId);

                    DirectOrder order = orders.OrderByDescending(o => o.Price).FirstOrDefault();
                    if (order != null)
                    {
                        double price = order.Price + 0.01;
                        if (Cache.Instance.DirectEve.Session.StationId != null)
                        {
                            Cache.Instance.DirectEve.Buy((int)Cache.Instance.DirectEve.Session.StationId, Item, price, Unit, DirectOrderRange.Station, 1, 30);
                        }
                    }
                    useOrders = false;
                    _States.CurrentBuyState = BuyState.Done;
                }

                break;

            case BuyState.BuyItem:

                if (DateTime.UtcNow.Subtract(_lastAction).TotalSeconds < 5)
                {
                    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)
                    {
                        // Calculate how much we still need
                        if (order.VolumeEntered >= Unit)
                        {
                            order.Buy(Unit, DirectOrderRange.Station);
                            _States.CurrentBuyState = BuyState.WaitForItems;
                        }
                        else
                        {
                            order.Buy(Unit, DirectOrderRange.Station);
                            Unit = Unit - order.VolumeEntered;
                            Logging.Log("Buy", "Missing " + Convert.ToString(Unit) + " units", Logging.White);
                            _returnBuy = true;
                            _States.CurrentBuyState = BuyState.WaitForItems;
                        }
                    }
                }

                break;

            case BuyState.WaitForItems:
                // Wait 5 seconds after moving
                if (DateTime.UtcNow.Subtract(_lastAction).TotalSeconds < 5)
                {
                    break;
                }

                // Close the market window if there is one
                if (marketWindow != null)
                {
                    marketWindow.Close();
                }

                if (_returnBuy)
                {
                    Logging.Log("Buy", "Return Buy", Logging.White);
                    _returnBuy = false;
                    _States.CurrentBuyState = BuyState.OpenMarket;
                    break;
                }

                Logging.Log("Buy", "Done", Logging.White);
                _States.CurrentBuyState = BuyState.Done;

                break;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Check if we have kernite in station
        /// </summary>
        /// <returns></returns>
        public StorylineState PreAcceptMission(Storyline storyline)
        {
            DirectEve directEve = Cache.Instance.DirectEve;

            if (_nextAction > DateTime.UtcNow)
            {
                return(StorylineState.PreAcceptMission);
            }

            // the ore and ore quantity can be stored in the characters settings xml this is to facility mission levels other than 4.
            //The defaults are for level 4 so it will not break for those people that do not include these in their settings file
            //  Level 1         <MaterialsForWarOreID>1230</MaterialsForWarOreID>
            //                  <MaterialsForWarOreQty>999</MaterialsForWarOreQty>
            //  Level 4         <MaterialsForWarOreID>20</MaterialsForWarOreID>
            //                  <MaterialsForWarOreQty>8000</MaterialsForWarOreQty>

            int oreid       = Settings.Instance.MaterialsForWarOreID;  //1230;
            int orequantity = Settings.Instance.MaterialsForWarOreQty; //999

            // Open the item hangar
            if (Cache.Instance.ItemHangar == null)
            {
                return(StorylineState.PreAcceptMission);
            }

            //if (Cache.Instance.ItemHangar.Window == null)
            //{
            //    Logging.Log("MaterialsForWar", "PreAcceptMission: ItemHangar is null", Logging.Orange);
            //    if (!Cache.Instance.ReadyItemsHangar("MaterialsForWarPreparation")) return StorylineState.PreAcceptMission;
            //    return StorylineState.PreAcceptMission;
            //}

            // Is there a market window?
            DirectMarketWindow marketWindow = directEve.Windows.OfType <DirectMarketWindow>().FirstOrDefault();

            // Do we have the ore we need in the Item Hangar?.

            if (Cache.Instance.ItemHangar.Items.Where(i => i.TypeId == oreid).Sum(i => i.Quantity) >= orequantity)
            {
                DirectItem thisOreInhangar = Cache.Instance.ItemHangar.Items.FirstOrDefault(i => i.TypeId == oreid);
                if (thisOreInhangar != null)
                {
                    Logging.Log("MaterialsForWarPreparation", "We have [" + Cache.Instance.ItemHangar.Items.Where(i => i.TypeId == oreid).Sum(i => i.Quantity).ToString(CultureInfo.InvariantCulture) + "] " + thisOreInhangar.TypeName + " in the item hangar accepting mission", Logging.White);
                }

                // Close the market window if there is one
                if (marketWindow != null)
                {
                    marketWindow.Close();
                }

                return(StorylineState.AcceptMission);
            }

            if (Cache.Instance.CurrentShipsCargo == null)
            {
                return(StorylineState.PreAcceptMission);
            }

            if (Cache.Instance.CurrentShipsCargo.Items.Where(i => i.TypeId == oreid).Sum(i => i.Quantity) >= orequantity)
            {
                DirectItem thisOreInhangar = Cache.Instance.CurrentShipsCargo.Items.FirstOrDefault(i => i.TypeId == oreid);
                if (thisOreInhangar != null)
                {
                    Logging.Log("MaterialsForWarPreparation", "We have [" + Cache.Instance.CurrentShipsCargo.Items.Where(i => i.TypeId == oreid).Sum(i => i.Quantity).ToString(CultureInfo.InvariantCulture) + "] " + thisOreInhangar.TypeName + " in the CargoHold accepting mission", Logging.White);
                }

                // Close the market window if there is one
                if (marketWindow != null)
                {
                    marketWindow.Close();
                }

                return(StorylineState.AcceptMission);
            }

            if (Cache.Instance.DirectEve.HasSupportInstances())
            {
                // We do not have enough ore, open the market window
                if (marketWindow == null)
                {
                    _nextAction = DateTime.UtcNow.AddSeconds(10);

                    Logging.Log("MaterialsForWarPreparation", "Opening market window", Logging.White);

                    directEve.ExecuteCommand(DirectCmd.OpenMarket);
                    return(StorylineState.PreAcceptMission);
                }

                // Wait for the window to become ready (this includes loading the ore info)
                if (!marketWindow.IsReady)
                {
                    return(StorylineState.PreAcceptMission);
                }

                // Are we currently viewing ore orders?
                if (marketWindow.DetailTypeId != oreid)
                {
                    // No, load the ore orders
                    marketWindow.LoadTypeId(oreid);

                    Logging.Log("MaterialsForWarPreparation", "Loading market window", Logging.White);

                    _nextAction = DateTime.UtcNow.AddSeconds(5);
                    return(StorylineState.PreAcceptMission);
                }

                // Get the median sell price
                InvType type     = Cache.Instance.InvTypesById[20];
                double? maxPrice = type.MedianSell * 4;

                // Do we have orders that sell enough ore for the mission?
                IEnumerable <DirectOrder> orders = marketWindow.SellOrders.Where(o => o.StationId == directEve.Session.StationId && o.Price < maxPrice).ToList();
                if (!orders.Any() || orders.Sum(o => o.VolumeRemaining) < orequantity)
                {
                    Logging.Log("MaterialsForWarPreparation", "Not enough (reasonably priced) ore available! Blacklisting agent for this Questor session!", Logging.Orange);

                    // Close the market window
                    marketWindow.Close();

                    // No, black list the agent in this Questor session (note we will never decline storylines!)
                    return(StorylineState.BlacklistAgent);
                }

                // How much ore do we still need?
                int neededQuantity = orequantity - Cache.Instance.ItemHangar.Items.Where(i => i.TypeId == oreid).Sum(i => i.Quantity);
                if (neededQuantity > 0)
                {
                    // Get the first order
                    DirectOrder order = orders.OrderBy(o => o.Price).FirstOrDefault();
                    if (order != null)
                    {
                        // Calculate how much ore we still need
                        int remaining = Math.Min(neededQuantity, order.VolumeRemaining);
                        order.Buy(remaining, DirectOrderRange.Station);

                        Logging.Log("MaterialsForWarPreparation", "Buying [" + remaining + "] ore", Logging.White);

                        // Wait for the order to go through
                        _nextAction = DateTime.UtcNow.AddSeconds(10);
                    }
                }
                return(StorylineState.PreAcceptMission);
            }

            Logging.Log("MaterialsForWarPreparation", "No DirectEVE Instances Available: free version detected. Buy/Sell support not available. Blacklisting agent for this Questor session!", Logging.Orange);

            // Close the market window
            if (marketWindow != null)
            {
                marketWindow.Close();
            }
            // No, black list the agent in this Questor session (note we will never decline storylines!)
            return(StorylineState.BlacklistAgent);
        }
Ejemplo n.º 5
0
        public void Process()
        {
            if (!Status.Instance.InStation)
            {
                return;
            }

            if (Status.Instance.InSpace)
            {
                return;
            }

            DirectMarketWindow marketWindow = Cache.Instance.DirectEve.Windows.OfType <DirectMarketWindow>().FirstOrDefault();

            switch (_state)
            {
            case State.Idle:
                break;

            case State.Done:
                _done = true;

                if (OnBuyItemFinished != null)
                {
                    OnBuyItemFinished(_typeId, _createOrder);
                }
                break;

            case State.Begin:

                // Close the market window if there is one
                if (marketWindow != null)
                {
                    marketWindow.Close();
                }
                _state = State.OpenMarket;
                break;

            case State.OpenMarket:

                if (marketWindow == null)
                {
                    _lastAction = DateTime.UtcNow;

                    Cache.Instance.DirectEve.ExecuteCommand(DirectCmd.OpenMarket);
                    break;
                }

                if (!marketWindow.IsReady)
                {
                    Logging.Log("BuyItem:Process", "Market window is not ready", Logging.White);
                    break;
                }

                Logging.Log("BuyItem:Process", "Opening Market", Logging.White);
                _state = State.LoadItem;

                break;

            case State.LoadItem:

                if (DateTime.UtcNow.Subtract(_lastAction).TotalSeconds < 2)
                {
                    break;
                }

                _lastAction = DateTime.UtcNow;

                if (marketWindow != null)
                {
                    Logging.Log("BuyItem:Process", "Load orders for TypeId - " + _typeId.ToString(), Logging.White);

                    if (marketWindow.DetailTypeId != _typeId)
                    {
                        if (marketWindow.LoadTypeId(_typeId))
                        {
                            if (_createOrder == true)
                            {
                                _state = State.CreateOrder;
                            }
                            else
                            {
                                _state = State.BuyItem;
                            }
                        }
                    }

                    break;
                }
                else
                {
                    Logging.Log("BuyItem:Process", "Market Window is not open, going back to open market state", Logging.White);

                    _state = State.OpenMarket;
                }

                break;

            case State.CreateOrder:

                if (DateTime.UtcNow.Subtract(_lastAction).TotalSeconds < 5)
                {
                    break;
                }

                _lastAction = DateTime.UtcNow;

                if (marketWindow != null)
                {
                    if (!marketWindow.IsReady)
                    {
                        Logging.Log("BuyItem:Process", "Market window is not ready", Logging.White);
                        break;
                    }

                    if (marketWindow.DetailTypeId != _typeId)
                    {
                        _state = State.LoadItem;
                        break;
                    }

                    List <DirectOrder> buyOrders  = marketWindow.BuyOrders.Where(o => o.StationId == Cache.Instance.DirectEve.Session.StationId).ToList();
                    List <DirectOrder> sellOrders = marketWindow.SellOrders.Where(o => o.StationId == Cache.Instance.DirectEve.Session.StationId).ToList();

                    DirectOrder highestBuyOrder = buyOrders.OrderByDescending(o => o.Price).FirstOrDefault();
                    DirectOrder lowestSellOrder = sellOrders.OrderBy(o => o.Price).FirstOrDefault();
                    if (highestBuyOrder != null)
                    {
                        bool createBuyOrder = (lowestSellOrder == null);

                        if (lowestSellOrder != null)
                        {
                            double profit    = lowestSellOrder.Price - highestBuyOrder.Price;
                            double tax       = lowestSellOrder.Price * .01 + highestBuyOrder.Price * 0.015;
                            double profitPct = lowestSellOrder.Price / highestBuyOrder.Price;

                            if ((profit < 10000000 && profitPct > 1.50) || (profit >= 10000000 && tax < profit * 0.25))
                            {
                                createBuyOrder = true;
                            }
                        }

                        if (createBuyOrder == true)
                        {
                            double price = double.Parse((decimal.Parse(highestBuyOrder.Price.ToString()) + 0.01m).ToString());
                            if (Cache.Instance.DirectEve.Session.StationId != null)
                            {
                                Cache.Instance.DirectEve.Buy((int)Cache.Instance.DirectEve.Session.StationId, _typeId, price, _volume, DirectOrderRange.Station, 1, 90);
                            }
                        }
                    }
                    _state = State.Done;
                }

                break;

            case State.BuyItem:

                if (DateTime.UtcNow.Subtract(_lastAction).TotalSeconds < 5)
                {
                    break;
                }

                if (marketWindow != null)
                {
                    if (!marketWindow.IsReady)
                    {
                        Logging.Log("BuyItem:Process", "Market window is not ready", Logging.White);
                        break;
                    }

                    if (marketWindow.DetailTypeId != _typeId)
                    {
                        _state = State.LoadItem;
                        break;
                    }

                    List <DirectOrder> orders = marketWindow.SellOrders.Where(o => o.StationId == Cache.Instance.DirectEve.Session.StationId).ToList();

                    DirectOrder order = orders.OrderBy(o => o.Price).FirstOrDefault();
                    if (order != null)
                    {
                        // Calculate how much we still need
                        if (order.VolumeEntered >= _volume)
                        {
                            order.Buy(_volume, DirectOrderRange.Station);
                            _state = State.WaitForItems;
                        }
                        else
                        {
                            order.Buy(_volume, DirectOrderRange.Station);
                            _volume = _volume - order.VolumeEntered;
                            Logging.Log("BuyItem:Process", "Missing " + Convert.ToString(_volume) + " units", Logging.White);
                            _returnBuy = true;
                            _state     = State.WaitForItems;
                        }
                    }
                }

                break;

            case State.WaitForItems:
                // Wait 5 seconds after moving
                if (DateTime.UtcNow.Subtract(_lastAction).TotalSeconds < 5)
                {
                    break;
                }

                // Close the market window if there is one
                if (marketWindow != null)
                {
                    marketWindow.Close();
                }

                if (_returnBuy)
                {
                    Logging.Log("BuyItem:Process", "Return Buy", Logging.White);
                    _returnBuy = false;
                    _state     = State.OpenMarket;
                    break;
                }

                Logging.Log("BuyItem:Process", "Done", Logging.White);
                _state = State.Done;

                break;
            }
        }