Esempio n. 1
0
        public bool IsTraderInterestedIn(ItemStack stack)
        {
            ItemSlotTrade     tradeSlot = GetBuyingConditionsSlot(stack);
            ResolvedTradeItem tradeItem = tradeSlot?.TradeItem;

            if (tradeItem == null)
            {
                return(false);
            }

            if (tradeItem.Stock == 0)
            {
                PerformNotifySlot(GetSlotId(tradeSlot));
            }

            return(tradeItem.Stock > 0);
        }
Esempio n. 2
0
        public int GetTotalCost()
        {
            int totalCost = 0;

            for (int i = 0; i < 4; i++)
            {
                ItemSlotTrade     buySlot   = GetBuyingCartSlot(i);
                ResolvedTradeItem tradeitem = buySlot.TradeItem;

                if (tradeitem != null)
                {
                    int cnt = buySlot.StackSize / tradeitem.Stack.StackSize;
                    totalCost += tradeitem.Price * cnt;
                }
            }

            return(totalCost);
        }
Esempio n. 3
0
        public override void ToTreeAttributes(ITreeAttribute tree)
        {
            SlotsToTreeAttributes(slots, tree);

            TreeAttribute tradeItemTree = new TreeAttribute();

            for (int i = 0; i < slots.Length; i++)
            {
                if (slots[i].Itemstack == null || !(slots[i] is ItemSlotTrade))
                {
                    continue;
                }
                TreeAttribute     subtree   = new TreeAttribute();
                ResolvedTradeItem tradeitem = (slots[i] as ItemSlotTrade).TradeItem;
                tradeitem?.ToTreeAttributes(subtree);
                tradeItemTree[i + ""] = subtree;
            }

            tree["tradeItems"] = tradeItemTree;
        }
Esempio n. 4
0
        public bool HasTraderEnoughDemand(IPlayer player)
        {
            Dictionary <int, int> Stocks = new Dictionary <int, int>();

            for (int i = 0; i < 4; i++)
            {
                ItemSlot slot = GetSellingCartSlot(i);
                if (slot.Itemstack == null)
                {
                    continue;
                }

                ItemSlotTrade     tradeSlot = GetBuyingConditionsSlot(slot.Itemstack);
                ResolvedTradeItem tradeItem = tradeSlot?.TradeItem;

                if (tradeItem == null)
                {
                    player.InventoryManager.NotifySlot(player, slot);
                    return(false);
                }

                int tradeslotid = GetSlotId(tradeSlot);
                int stock;
                if (!Stocks.TryGetValue(tradeslotid, out stock))
                {
                    stock = tradeItem.Stock;
                }

                Stocks[tradeslotid] = stock - slot.Itemstack.StackSize / tradeItem.Stack.StackSize;

                if (Stocks[tradeslotid] < 0)
                {
                    player.InventoryManager.NotifySlot(player, tradeSlot);
                    player.InventoryManager.NotifySlot(player, slot);
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 5
0
        public int GetTotalGain()
        {
            int totalGain = 0;

            for (int i = 0; i < 4; i++)
            {
                ItemSlotSurvival sellSlot = GetSellingCartSlot(i);

                if (sellSlot.Itemstack == null)
                {
                    continue;
                }

                ResolvedTradeItem tradeitem = GetBuyingConditionsSlot(sellSlot.Itemstack)?.TradeItem;

                if (tradeitem != null)
                {
                    int cnt = sellSlot.StackSize / tradeitem.Stack.StackSize;
                    totalGain += tradeitem.Price * cnt;
                }
            }

            return(totalGain);
        }
Esempio n. 6
0
        internal EnumTransactionResult TryBuySell(IPlayer buyingPlayer)
        {
            if (!HasPlayerEnoughAssets(buyingPlayer))
            {
                return(EnumTransactionResult.PlayerNotEnoughAssets);
            }
            if (!HasTraderEnoughAssets())
            {
                return(EnumTransactionResult.TraderNotEnoughAssets);
            }

            if (!HasTraderEnoughStock(buyingPlayer))
            {
                return(EnumTransactionResult.TraderNotEnoughSupplyOrDemand);
            }
            if (!HasTraderEnoughDemand(buyingPlayer))
            {
                return(EnumTransactionResult.TraderNotEnoughSupplyOrDemand);
            }

            if (Api.Side == EnumAppSide.Client)
            {
                for (int i = 0; i < 4; i++)
                {
                    GetBuyingCartSlot(i).Itemstack = null;
                }
                return(EnumTransactionResult.Success);
            }

            // Take care of the money first
            if (!HandleMoneyTransaction(buyingPlayer))
            {
                return(EnumTransactionResult.Failure);
            }

            // Now hand over buying cart contents
            for (int i = 0; i < 4; i++)
            {
                ItemSlotTrade slot = GetBuyingCartSlot(i);
                if (slot.Itemstack == null)
                {
                    continue;
                }

                GiveOrDrop(buyingPlayer, slot.Itemstack);

                slot.TradeItem.Stock -= slot.Itemstack.StackSize / slot.TradeItem.Stack.StackSize;
                slot.Itemstack        = null;
                slot.MarkDirty();
            }

            // And delete selling cart contents
            for (int i = 0; i < 4; i++)
            {
                ItemSlot slot = GetSellingCartSlot(i);
                if (slot.Itemstack == null)
                {
                    continue;
                }

                ResolvedTradeItem tradeItem = GetBuyingConditionsSlot(slot.Itemstack).TradeItem;
                if (tradeItem == null)
                {
                    continue;
                }

                int q = slot.Itemstack.StackSize / tradeItem.Stack.StackSize;

                tradeItem.Stock -= q;
                slot.TakeOut(q * tradeItem.Stack.StackSize);
                slot.MarkDirty();
            }

            return(EnumTransactionResult.Success);
        }
Esempio n. 7
0
 public void SetTradeItem(ResolvedTradeItem tradeItem)
 {
     this.TradeItem = tradeItem;
     this.itemstack = tradeItem.Stack;
 }