Пример #1
0
        public virtual void HandleSellSequenceOnCargo(IMyCubeBlock cargoBlock, Item item, Item credit)
        {
            MyDefinitionId creditDefinition = credit.Definition;

            double         sellPrice      = item.SellPrice.GetStockPrice(item.CargoRatio);
            MyDefinitionId itemDefinition = item.Definition;

            double maximumItemsPerTransfer = Math.Round(item.CargoSize * 0.01f, 0); //1% of max

            if (maximumItemsPerTransfer < 1f)
            {
                maximumItemsPerTransfer = 1f;
            }

            double creditsInCargo = InventoryApi.CountItemsInventory(cargoBlock, creditDefinition);

            if (creditsInCargo <= 1f)
            {
                return;
            }

            double sellCount = Math.Floor(creditsInCargo / sellPrice);

            if (sellCount < 1f)
            {
                return;
            }

            if (sellCount > maximumItemsPerTransfer)
            {
                sellCount = maximumItemsPerTransfer;
            }
            if (sellCount > item.CurrentCargo)
            {
                sellCount = item.CurrentCargo;
            }

            double paymentAmount = Math.Ceiling(sellPrice * sellCount);

            try
            {
                double payedCredits = InventoryApi.RemoveFromInventory(cargoBlock, creditDefinition, paymentAmount);

                if (payedCredits <= 0)
                {
                    return;
                }

                item.CurrentCargo -= sellCount;
                double selledItems = InventoryApi.AddToInventory(cargoBlock, itemDefinition, sellCount);

                if (selledItems <= 0)
                {
                    // rollback if nothing given
                    item.CurrentCargo += sellCount;
                    InventoryApi.AddToInventory(cargoBlock, creditDefinition, paymentAmount);
                    return;
                }

                if (IsCreditLimitationEnabled(credit))
                {
                    credit.CurrentCargo += payedCredits;
                }
            }
            catch (UnknownItemException)
            {
                //MyAPIGateway.Utilities.ShowMessage("Error", "Wrong item: " + exception.Message);
            }
        }
Пример #2
0
        public virtual void HandlePurchaseSequenceOnCargo(IMyCubeBlock cargoBlock, Item item, Item credits)
        {
            MyDefinitionId creditDefinition = credits.Definition;

            double availableCount = InventoryApi.CountItemsInventory(cargoBlock, item.Definition);

            if (availableCount <= 0)
            {
                return;
            }

            double itemCount = availableCount;
            double maximumItemsPerTransfer = Math.Round(item.CargoSize * 0.01f);
            Price  pricing                = item.PurchasePrice;
            double purchasePrice          = pricing.GetStockPrice(item.CargoRatio);
            double minimumItemPerTransfer = Math.Ceiling(1f / purchasePrice);

            if (maximumItemsPerTransfer < minimumItemPerTransfer)
            {
                maximumItemsPerTransfer = minimumItemPerTransfer;
            }

            if (itemCount > maximumItemsPerTransfer)
            {
                itemCount = maximumItemsPerTransfer;
            }

            if (itemCount + item.CurrentCargo > item.CargoSize)
            {
                itemCount = item.CargoSize - item.CurrentCargo;
            }

            itemCount = Math.Round(itemCount);

            if (itemCount < minimumItemPerTransfer)
            {
                if (minimumItemPerTransfer > availableCount)
                {
                    return;
                }

                itemCount = minimumItemPerTransfer;
            }

            double paymentAmount = Math.Ceiling(purchasePrice * itemCount);

            if (IsCreditLimitationEnabled(credits) && paymentAmount > credits.CurrentCargo)
            {
                paymentAmount = Math.Floor(credits.CurrentCargo);
            }

            itemCount = Math.Round(paymentAmount / purchasePrice);

            var removedItemsCount =
                Math.Floor(InventoryApi.RemoveFromInventory(cargoBlock, item.Definition, itemCount));

            if (removedItemsCount <= 0)
            {
                return;
            }

            // could less items removed as expected
            if (removedItemsCount < itemCount)
            {
                paymentAmount = Math.Floor(purchasePrice * removedItemsCount);
            }

            item.CurrentCargo += removedItemsCount;
            try
            {
                double givenCredits = InventoryApi.AddToInventory(cargoBlock, creditDefinition, paymentAmount);

                if (givenCredits <= 0)
                {
                    // rollback
                    item.CurrentCargo -= removedItemsCount;
                    InventoryApi.AddToInventory(cargoBlock, item.Definition, removedItemsCount);
                    return;
                }

                if (IsCreditLimitationEnabled(credits))
                {
                    credits.CurrentCargo -= givenCredits;
                }
            }
            catch (UnknownItemException)
            {
                //MyAPIGateway.Utilities.ShowMessage("Error", "Wrong item: " + exception.Message);
            }
        }