Ejemplo n.º 1
0
        public uint PutItemsOnTrade(int SQLID, uint quantity, bool fromInventory)
        {
            //totalCalculated = false;
            itemTraded = true;
            bool itemFound = false;
            uint reservedAmount = TheMySqlManager.ReservedAmount(SQLID);
            uint reservedAmountForUser = TheMySqlManager.ReservedAmountForUser(SQLID, username);
            Inventory.inventory_item MyInventoryItem = new Inventory.inventory_item();
            Storage.StorageItem MyStorageItem = new Storage.StorageItem();
            int totalOnHand = 0;
            int itemIndex = 0;
            uint position = 0;
            if (fromInventory)
            {
                for (int i = 0; i < InventorySnapshop.Count; i++)
                {
                    MyInventoryItem = (Inventory.inventory_item)InventorySnapshop[i];
                    if (MyInventoryItem.SqlID == SQLID && MyInventoryItem.pos < 36)
                    {
                        itemFound = true;
                        totalOnHand += (int)MyInventoryItem.quantity;
                        itemIndex = i;
                        position = MyInventoryItem.pos;
                    }
                }
            }
            else
            {
                for (int i = 0; i < StorageSnapshot.Count; i++)
                {
                    MyStorageItem = (Storage.StorageItem)StorageSnapshot[i];
                    if (MyStorageItem.knownItemsID == SQLID && itemFound == false)
                    {
                        itemFound = true;
                        totalOnHand = (int)MyStorageItem.quantity;
                        itemIndex = i;
                        position = MyStorageItem.pos;
                    }
                }
            }

            totalOnHand = totalOnHand - (int)reservedAmount + (int)reservedAmountForUser;
            if (totalOnHand < 0)
            {
                totalOnHand = 0;
            }
            //Console.WriteLine("SQLID: " + SQLID);
            //Console.WriteLine("Position: " + position);
            //Console.WriteLine("Quantity: " + quantity);
            //Console.WriteLine("reservedAmount: " + reservedAmount);
            //Console.WriteLine("reservedAmountForUser: "******"Item Found: " + itemFound);
            //Console.WriteLine("Total On Hand: " + totalOnHand);
            //Console.WriteLine("Item index: " + itemIndex);
            uint amountToTrade = 0;
            if (itemFound && totalOnHand > 0)
            {
                if (totalOnHand > quantity)
                {
                    amountToTrade = quantity;
                }
                else
                {
                    amountToTrade = (uint)totalOnHand;
                }
                uint quantityLeftOver = (uint)totalOnHand - amountToTrade;
                //Console.WriteLine("Quantity LeftOver: " + quantityLeftOver);
                if (fromInventory)
                {
                    MyInventoryItem = (Inventory.inventory_item)InventorySnapshop[itemIndex];

            if (MyInventoryItem.is_stackable)
            {
                        MyInventoryItem.quantity = quantityLeftOver;
                        InventorySnapshop[itemIndex] = MyInventoryItem;
            }
            else
            {
            uint toClear = amountToTrade;
            for (int i = 0; i < InventorySnapshop.Count; i++)
                    {
                            MyInventoryItem = (Inventory.inventory_item)InventorySnapshop[i];
                            if (MyInventoryItem.SqlID == SQLID && MyInventoryItem.pos < 36 && MyInventoryItem.quantity > 0)
                            {
                    MyInventoryItem.quantity = 0;
                    InventorySnapshop[i] = MyInventoryItem;
                    toClear--;
                            }
                if (toClear == 0) break;
                    }

            }
                }
                else //from storage
                {
                    MyStorageItem = (Storage.StorageItem)StorageSnapshot[itemIndex];
                    MyStorageItem.quantity = quantityLeftOver;
                    StorageSnapshot[itemIndex] = MyStorageItem;
                }
                if (position > 255)
                {
                    TheTCPWrapper.Send(CommandCreator.PUT_OBJECT_ON_TRADE((ushort)position, amountToTrade));
                }
                else
                {
                    TheTCPWrapper.Send(CommandCreator.PUT_OBJECT_ON_TRADE((byte)position, amountToTrade, fromInventory));
                }
            }

            Console.WriteLine("Amount to trade: " + amountToTrade);
            return amountToTrade;
        }
Ejemplo n.º 2
0
 private void STORAGE_ITEMS(byte[] data)
 {
     if (TheInventory.GettingInventoryItems == true || makingWithdraw || withdrawMade || makingDeposit || depositMade || storageOpen)
     {
         if (storageOpen && (!depositMade && !withdrawMade && !makingDeposit && !makingWithdraw))
         {
             storageOpen = false;
         }
         return;
     }
     uint item_count = data[3]; // should be in this byte but isn't yet :P
     UInt16 data_length = System.BitConverter.ToUInt16(data,1);
     data_length += 2;
     uint category_num = data[4];
     UInt16 pos;
     int imageid;
     uint quantity;
     Storage.StorageItem MyStorageItem = new Storage.StorageItem();
     // so, we're calculating the number of items with the size of the packet
     Console.WriteLine("Storage data length: " + data_length);
     uint remainder = (uint)((data_length - 5) % Inventory.sizeOfPacket);
     Console.WriteLine("Remainder: " + remainder);
     if (Inventory.sizeOfPacket == 8 && remainder > 0)
     {
         Inventory.sizeOfPacket = 10;
     }
     item_count = (uint)((data_length - 5) / Inventory.sizeOfPacket);
     //check to see if the above devides evenly, if not, it's possible that inventory was empty and it's really 10 and not 8
     //should be able to figure that out here and set it to the right value...
     if (item_count > 0)
     {
         itemsFoundInStorage = true;
         for (int i = 0; i < item_count; i++)
         {
             imageid = System.BitConverter.ToUInt16(data, i * Inventory.sizeOfPacket + 5);
             quantity = System.BitConverter.ToUInt32(data, i * Inventory.sizeOfPacket + 5 + 2);
             pos = System.BitConverter.ToUInt16(data, i * Inventory.sizeOfPacket + 5 + 6);
             MyStorageItem.imageid = imageid;
             MyStorageItem.pos = pos;
             MyStorageItem.quantity = quantity;
             MyStorageItem.name = "";
             MyStorageItem.category_num = (int)category_num;
             TheStorage.AddItem(MyStorageItem);
             TheTCPWrapper.Send(CommandCreator.LOOK_AT_STORAGE_ITEM(pos));
         }
     }
     categoryCount++;
     if (categoryCount == total_categories && !itemsFoundInStorage)
     {
         TheTCPWrapper.Send(CommandCreator.SEND_PM(username, "Storage should be open now! (although it appears to be empty)"));
         storageOpen = true;
         openingStorage = false;
     }
     return;
 }