Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public void Process()
        {
            if (!Status.Instance.InStation)
            {
                return;
            }

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

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

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

            case State.Done:
                _done = true;

                if (OnSellItemFinished != null)
                {
                    OnSellItemFinished(_item, _sold);
                }
                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("SellItem:Process", "Market window is not ready", Logging.White);
                    break;
                }

                Logging.Log("SellItem: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("SellItem:Process", "Load orders for TypeId - " + _item.TypeId.ToString(), Logging.White);

                    if (marketWindow.DetailTypeId != _item.TypeId)
                    {
                        if (marketWindow.LoadTypeId(_item.TypeId))
                        {
                            _state = State.GetPrice;
                        }
                    }

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

                    _state = State.OpenMarket;
                }

                break;

            case State.GetPrice:
                if (DateTime.UtcNow.Subtract(_lastAction).TotalSeconds < 2)
                {
                    break;
                }

                _lastAction = DateTime.UtcNow;

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

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

                    List <DirectOrder> sellOrders     = marketWindow.SellOrders.OrderBy(o => o.Price).Where(o => o.StationId == Cache.Instance.DirectEve.Session.StationId).ToList();
                    List <DirectOrder> buyOrders      = marketWindow.BuyOrders.OrderByDescending(o => o.Price).Where(o => o.StationId == Cache.Instance.DirectEve.Session.StationId).ToList();
                    DirectOrder        firstSellOrder = sellOrders.FirstOrDefault();
                    DirectOrder        firstBuyOrder  = buyOrders.FirstOrDefault();

                    if (firstSellOrder != null)
                    {
                        sellOrders.Remove(firstSellOrder);
                        DirectOrder secondSellOrder = sellOrders.FirstOrDefault();

                        if (secondSellOrder != null)
                        {
                            decimal priceDifference    = decimal.Parse(secondSellOrder.Price.ToString()) - decimal.Parse(firstSellOrder.Price.ToString());
                            decimal priceDifferencePct = priceDifference / decimal.Parse(firstSellOrder.Price.ToString());
                            if (priceDifferencePct > 0.05m || priceDifference > 5000000)
                            {
                                // Check if the first buy order is close enough to the sell order that we no longer want to sell, otherwise the jump between the two sell orders doesn't matter.
                                // If there is no first buy order then create the order anyway
                                if (firstBuyOrder != null && firstSellOrder.Price / firstBuyOrder.Price < 1.5)
                                {
                                    Logging.Log("SellItem:Process", "No sale, price difference between the first two orders is too high Pct - " + priceDifferencePct + " Diff - " + priceDifference, Logging.White);
                                    _state = State.Done;
                                    break;
                                }
                            }
                        }

                        _price = double.Parse((decimal.Parse(firstSellOrder.Price.ToString()) - 0.01m).ToString());
                        _state = State.OpenSellWindow;

                        Logging.Log("SellItem:Process", "Lowest sell price, Name - " + _item.Name + " Price - " + _price, Logging.White);
                    }
                    else
                    {
                        Logging.Log("SellItem:Process", "No current sell orders, can't create lowest order for Name - " + _item.Name, Logging.White);
                        _state = State.Done;
                    }
                }
                else
                {
                    Logging.Log("SellItem:Process", "MarketWindow is not open, going back to open market state", Logging.White);

                    _state = State.OpenMarket;
                }

                break;

            case State.OpenSellWindow:

                _sellWindowNotReadyCount = 0;

                if (Cache.Instance.DirectEve.OpenSellItems(_item) == true)
                {
                    Logging.Log("SellItem:Process", "Opening sell window for Name - " + _item.Name, Logging.White);
                    if (_createOrder)
                    {
                        _state = State.SetPrice;
                    }
                    else
                    {
                        _state = State.SellItem;
                    }
                }
                else
                {
                    Logging.Log("SellItem:Process", "Failed to open sell window for Name - " + _item.Name, Logging.White);
                    _state = State.Done;
                }

                break;

            case State.SetPrice:
                if (DateTime.UtcNow.Subtract(_lastAction).TotalSeconds < 1)
                {
                    break;
                }

                _lastAction = DateTime.UtcNow;

                if (sellWindow != null)
                {
                    if (!sellWindow.IsReady)
                    {
                        _sellWindowNotReadyCount++;
                        Logging.Log("SellItem:Process", "Sell window is not ready", Logging.White);

                        if (_sellWindowNotReadyCount >= 5)
                        {
                            Logging.Log("SellItem:Process", "Something is wrong trying to sell this item, quitting the sell", Logging.White);
                            _state = State.Done;
                        }

                        break;
                    }

                    sellWindow.SetPrice(_item.ItemId, _price);

                    Logging.Log("SellItem:Process", "Sell window setting price to " + _price, Logging.White);
                    _state = State.SellItem;
                }
                else
                {
                    Logging.Log("SellItem:Process", "Sell Window is not open going back to open sell window state", Logging.White);
                    _state = State.OpenSellWindow;
                }

                break;

            case State.SellItem:
                if (sellWindow != null)
                {
                    if (!sellWindow.IsReady)
                    {
                        _sellWindowNotReadyCount++;
                        Logging.Log("SellItem:Process", "Sell window is not ready", Logging.White);

                        if (_sellWindowNotReadyCount >= 5)
                        {
                            _state = State.OpenSellWindow;
                        }

                        break;
                    }

                    sellWindow.SellItems();

                    Logging.Log("SellItem:Process", "Selling item Name - " + _item.Name, Logging.White);

                    _sold = true;

                    _state = State.Done;
                }
                else
                {
                    Logging.Log("SellItem:Process", "Sell Window is not open going back to open sell window state", Logging.White);
                    _state = State.OpenSellWindow;
                }

                break;
            }
        }
Exemplo 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;
            }
        }
Exemplo n.º 4
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;
            }
        }