Beispiel #1
0
        public static StorageData Init <T>(T value, StorageAction action = StorageAction.Add) where T : ISubstance, new()
        {
            var sd = new StorageData()
            {
                Action = action
            };

            sd.SetData(value);
            return(sd);
        }
Beispiel #2
0
        public void Handle(Packet iPacket)
        {
            StorageAction action = (StorageAction)iPacket.ReadByte();

            switch (action)
            {
            case StorageAction.Withdraw:
            {
                ItemConstants.ItemType type = (ItemConstants.ItemType)iPacket.ReadByte();
                byte slot = iPacket.ReadByte();

                Item item = this.Items[slot];

                if (item == null)
                {
                    return;
                }

                this.Items.Remove(item);
                item.Delete();

                item.IsStored = false;

                this.Parent.Items.Add(item, forceGetSlot: true);

                List <Item> itemsByType = new List <Item>();

                foreach (Item loopItem in this.Items)
                {
                    if (loopItem.Type == item.Type)
                    {
                        itemsByType.Add(loopItem);
                    }
                }

                using (Packet oPacket = new Packet(ServerOperationCode.Storage))
                {
                    oPacket
                    .WriteByte(13)
                    .WriteByte(this.Slots)
                    .WriteShort((short)(2 << (byte)item.Type))
                    .WriteShort()
                    .WriteInt()
                    .WriteByte((byte)itemsByType.Count);

                    foreach (Item loopItem in itemsByType)
                    {
                        oPacket.WriteBytes(loopItem.ToByteArray(true, true));
                    }

                    this.Parent.Client.Send(oPacket);
                }
            }
            break;

            case StorageAction.Deposit:
            {
                short slot     = iPacket.ReadShort();
                int   itemID   = iPacket.ReadInt();
                short quantity = iPacket.ReadShort();

                Item item = this.Parent.Items[itemID, slot];

                if (this.IsFull)
                {
                    using (Packet oPacket = new Packet(ServerOperationCode.Storage))
                    {
                        oPacket.WriteByte(17);

                        this.Parent.Client.Send(oPacket);
                    }

                    return;
                }

                if (this.Parent.Meso <= this.Npc.StorageCost)
                {
                    this.Parent.Notify("You don't have enough meso to store the item.", NoticeType.Popup);         // TOOD: Is there a packet for this?

                    return;
                }

                this.Parent.Meso -= this.Npc.StorageCost;

                this.Parent.Items.Remove(item, true);

                item.Parent   = this.Parent.Items;       // NOTE: This is needed because when we remove the item is sets parent to none.
                item.Slot     = (short)this.Items.Count;
                item.IsStored = true;

                this.Items.Add(item);

                List <Item> itemsByType = new List <Item>();

                foreach (Item loopItem in this.Items)
                {
                    if (loopItem.Type == item.Type)
                    {
                        itemsByType.Add(loopItem);
                    }
                }

                using (Packet oPacket = new Packet(ServerOperationCode.Storage))
                {
                    oPacket
                    .WriteByte(13)
                    .WriteByte(this.Slots)
                    .WriteShort((short)(2 << (byte)item.Type))
                    .WriteShort()
                    .WriteInt()
                    .WriteByte((byte)itemsByType.Count);

                    foreach (Item loopItem in itemsByType)
                    {
                        oPacket.WriteBytes(loopItem.ToByteArray(true, true));
                    }

                    this.Parent.Client.Send(oPacket);
                }
            }
            break;

            case StorageAction.ModifyMeso:
            {
                int meso = iPacket.ReadInt();

                if (meso > 0)         // NOTE: Withdraw meso.
                {
                    // TODO: Meso checks.
                }
                else         // NOTE: Deposit meso.
                {
                    // TODO: Meso checks.
                }

                this.Meso        -= meso;
                this.Parent.Meso += meso;

                using (Packet oPacket = new Packet(ServerOperationCode.Storage))
                {
                    oPacket
                    .WriteByte(19)
                    .WriteByte(this.Slots)
                    .WriteShort(2)
                    .WriteShort()
                    .WriteInt()
                    .WriteInt(this.Meso);

                    this.Parent.Client.Send(oPacket);
                }
            }
            break;

            case StorageAction.Leave:
            {
                this.Save();
            }
            break;
            }
        }
Beispiel #3
0
        public void CharStorageHandler(Packet inPacket)
        {
            StorageAction actionType = (StorageAction)inPacket.ReadByte();

            switch (actionType)
            {
            case StorageAction.WithdrawItem:
            {
                // Read packet data
                ItemConstants.ItemType itemType = (ItemConstants.ItemType)inPacket.ReadByte();
                byte itemSlot = inPacket.ReadByte();

                // First seek slot to find the item to withdraw in
                if (itemSlot < 0 || itemSlot > this.Slots)
                {
                    return;
                    // TODO: storage exceptions on wrongly received/read data from packet, CharacterStorageExceptions.WrongSlotRecieved
                }

                // Slot is valid thus seek an item on its position
                Item item = this.Items[itemSlot];
                if (item == null)
                {
                    return;
                    // TODO: CharacterStorageExceptions.ItemCouldNotBeFound
                }

                // Do i actually have free inventory slot to place withdrawn item in?
                if (this.Parent.Items.IsFull(item.Type))
                {
                    this.Parent.Client.Send(CharacterPackets.StorageErrorPacket(this.Parent, StoragePacketType.ErrorPlayerInventoryFull));
                    return;
                }

                // Even if i do, still i may not have enough mesos to demand this payed service :(
                int costToWithdraw = 1000;         // TODO: how is the cost actually calculated?
                if (this.Parent.Meso < costToWithdraw)
                {
                    this.Parent.Client.Send(CharacterPackets.StorageErrorPacket(this.Parent, StoragePacketType.ErrorNotEnoughMesos));
                    return;
                }

                if (this.Parent.Meso >= costToWithdraw)
                {
                    Maple.Meso.giveMesos(this.Parent, costToWithdraw); // Devour mesos

                    this.Items.Remove(item);                           // Remove item from storage
                    item.Delete();                                     // Delete item from Database
                    item.IsStored = false;                             // Set stored flag to false
                    this.Parent.Items.Add(item, forceGetSlot: true);   // Add item to char. items

                    // routine to get count of items of same type
                    List <Item> itemsByType = new List <Item>();
                    foreach (Item loopItem in this.Items)
                    {
                        if (loopItem.Type == item.Type)
                        {
                            itemsByType.Add(loopItem);
                        }
                    }

                    this.Parent.Client.Send(CharacterPackets.StorageRemoveItem(this.Parent, item, itemsByType));
                }
            }
            break;

            case StorageAction.DepositItem:
            {
                // Read packet data
                short slot     = inPacket.ReadShort();
                int   itemID   = inPacket.ReadInt();
                short quantity = inPacket.ReadShort();

                // TODO: slot validation, quantity validation
                // try to get item to deposit from slot in char. inventory
                Item item = this.Parent.Items[itemID, slot];

                // storage full cant deposit
                if (this.IsFull)
                {
                    this.Parent.Client.Send(CharacterPackets.StorageErrorPacket(this.Parent, StoragePacketType.ErrorStorageInventoryFull));
                    return;
                }

                // not enough mesos to pay for deposit
                if (this.Parent.Meso <= this.Npc.StorageCost)
                {
                    this.Parent.Client.Send(CharacterPackets.StorageErrorPacket(this.Parent, StoragePacketType.ErrorNotEnoughMesos));
                    return;
                }

                if (this.Parent.Meso >= this.Npc.StorageCost)
                {
                    this.Parent.Meso -= this.Npc.StorageCost; //devour mesos
                    this.Parent.Items.Remove(item, true);     // remove item form inventory

                    item.Parent   = this.Parent.Items;        // NOTE: This is needed because when we remove the item is sets parent to none.
                    item.Slot     = (short)this.Items.Count;  // slot in storage is maxCount of items in it
                    item.IsStored = true;                     // set flag on item

                    this.Items.Add(item);                     // add item to storage

                    // routine to get count of items of same type
                    List <Item> itemsByType = new List <Item>();
                    foreach (Item loopItem in this.Items)
                    {
                        if (loopItem.Type == item.Type)
                        {
                            itemsByType.Add(loopItem);
                        }
                    }

                    this.Parent.Client.Send(CharacterPackets.StorageAddItem(this.Parent, item, itemsByType));
                }
            }
            break;

            case StorageAction.ArrangeItem:
            {
                this.Parent.Client.Send(CharacterPackets.StorageArrangeItems(this.Parent));
            }
            break;

            case StorageAction.ChangeMesos:
            {
                int meso = inPacket.ReadInt();

                if (meso > 0)         // NOTE: Withdraw meso.
                {
                    // TODO: Meso checks.
                }
                else         // NOTE: Deposit meso.
                {
                    // TODO: Meso checks.
                }

                this.Meso        -= meso;
                this.Parent.Meso += meso;

                this.Parent.Client.Send(CharacterPackets.StorageChangeMesos(this.Parent));
            }
            break;

            case StorageAction.CloseStorage:
            {
                this.Save();
            }
            break;

            default: throw new ArgumentOutOfRangeException();
            }
        }