Пример #1
0
        public Storage LoadStorage(Player player, StorageType type)
        {
            string SQL = "SELECT * FROM `inventory` WHERE "
                + "`playerid` = ?pid AND `storagetype` = ?type";
            MySqlCommand cmd = new MySqlCommand(SQL, InventoryDAOConnection);
            cmd.Parameters.AddWithValue("?pid", player.Id);
            cmd.Parameters.AddWithValue("?type", type.ToString());
            MySqlDataReader LoadStorageReader = cmd.ExecuteReader();

            var storage = new Storage { StorageType = StorageType.Inventory };
            if (LoadStorageReader.HasRows)
            {
                while (LoadStorageReader.Read())
                {
                    if (LoadStorageReader.GetInt32(2) != 20000000)
                    {
                        StorageItem item = new StorageItem()
                        {
                            ItemId = LoadStorageReader.GetInt32(2),
                            Amount = LoadStorageReader.GetInt32(3),
                            Color = LoadStorageReader.GetInt32(4),
                        };

                        storage.Items.Add(LoadStorageReader.GetInt32(5), item);
                    }
                    else
                    {
                        storage.Money = LoadStorageReader.GetInt32(3);
                    }
                }
            }
            LoadStorageReader.Close();

            return storage;
        }
Пример #2
0
        public void SaveStorage(Player player, Storage storage)
        {
            string cmdString = "DELETE FROM inventory WHERE PlayerId=?pid";
            MySqlCommand command = new MySqlCommand(cmdString, InventoryDAOConnection);
            command.Parameters.AddWithValue("?pid", player.Id);
            command.ExecuteNonQuery();

            storage.Items.Add(999, storage.MoneyToItem); // add temporary save money item

            if (storage.Items.Count > 0)
            {
                foreach (var item in storage.Items)
                    AddItem(player, storage.StorageType, item);
            }
            storage.Items.Remove(999); // remove temporary save money item
        }
Пример #3
0
        public List<int> GetFreeSlots(Storage storage)
        {
            var freeSlots = new List<int>();

            for (int i = (storage.StorageType == StorageType.Inventory ? 20 : 0);
                i <= storage.Size + (storage.StorageType == StorageType.Inventory ? 19 : 0);
                i++)
             if (!storage.Items.ContainsKey(i))
                 freeSlots.Add(i);

            return freeSlots;
        }
Пример #4
0
        public Storage LoadAccountStorage(GameAccount gameAccount)
        {
            string SQL = "SELECT * FROM `inventory` WHERE "
                + "`accountname` = ?accountname AND `storagetype` = ?type";
            MySqlCommand cmd = new MySqlCommand(SQL, InventoryDAOConnection);
            cmd.Parameters.AddWithValue("?accountname", gameAccount.Username);
            cmd.Parameters.AddWithValue("?type", StorageType.AccountWarehouse.ToString());
            MySqlDataReader LoadAccountStorageReader = cmd.ExecuteReader();

            var storage = new Storage { StorageType = StorageType.AccountWarehouse };
            if (LoadAccountStorageReader.HasRows)
            {
                while (LoadAccountStorageReader.Read())
                {
                    StorageItem item = new StorageItem()
                    {
                        ItemId = LoadAccountStorageReader.GetInt32(2),
                        Amount = LoadAccountStorageReader.GetInt32(3),
                        Color = LoadAccountStorageReader.GetInt32(4),
                    };
                    storage.Items.Add(LoadAccountStorageReader.GetInt32(5), item);
                }
            }
            LoadAccountStorageReader.Close();

            return storage;
        }
Пример #5
0
        public StorageItem GetItemBySlot(Storage storage, int slot)
        {
            lock (storage.ItemsLock)
            {
                if (!storage.Items.ContainsKey(slot))
                    return null;

                return storage.Items[slot];
            }
        }
Пример #6
0
        public bool PlaceItemToOtherStorage(Player player, Player second, Storage from, int fromSlot, Storage to, int toSlot, int count)
        {
            if (to.IsFull())
                return false;

            if (count < 0)
                return false;

            if (!(player.Controller is DefaultController) || !(second.Controller is DefaultController))
                return false;

            if(player == second)
            {
                StorageItem item = null;

                if(to.StorageType == StorageType.CharacterWarehouse)
                {
                    if(toSlot == -1)
                        toSlot = to.GetFreeSlot(player.PlayerCurrentBankSection * 72);

                    if(toSlot == 0 && to.Items.ContainsKey(toSlot))
                        toSlot = to.GetFreeSlot(player.PlayerCurrentBankSection * 72);

                    if (toSlot == to.LastIdRanged(player.PlayerCurrentBankSection * 72, (player.PlayerCurrentBankSection + 1) * 72 - 1))
                        toSlot = to.GetFreeSlot(player.PlayerCurrentBankSection * 72);
                }
                else
                    if ((toSlot == 0 && to.Items.ContainsKey(toSlot)) || toSlot == -1)
                        toSlot = to.GetFreeSlot();

                if (to.Items.ContainsKey(toSlot))
                {
                    if (!PlaceItemToOtherStorage(player, player, to, toSlot, player.Inventory, player.Inventory.GetFreeSlot(), to.Items[toSlot].Amount))
                        return false;
                }

                lock (from.ItemsLock)
                {
                    if(from.Items.ContainsKey(fromSlot))
                    {
                        item = from.Items[fromSlot];

                        if(item.Amount < count)
                            return false;

                        if (item.Amount == count)
                            from.Items.Remove(fromSlot);
                        else
                        {
                            from.Items[fromSlot].Amount -= count;
                            item = new StorageItem { ItemId = item.ItemId, Amount = count };
                        }
                    }
                }

                if (item == null)
                    return false;

                if (!AddItem(player, to, item.ItemId, count, toSlot))
                    AddItem(player, from, item.ItemId, count, fromSlot);
            }
            return false;
        }
Пример #7
0
 public bool ContainsItem(Storage storage, int itemId, long counter)
 {
     lock (storage.ItemsLock)
     {
         for (int i = (storage.StorageType == StorageType.Inventory ? 20 : 0);
              i <= storage.Size + (storage.StorageType == StorageType.Inventory ? 19 : 0);
              i++)
             if (storage.Items.ContainsKey(i))
             {
                 if (storage.Items[i].ItemId == itemId && storage.Items[i].Amount >= counter)
                     return true;
             }
     }
     return false;
 }
Пример #8
0
        public bool AddItem(Player player, Storage storage, StorageItem item)
        {
            lock (storage.ItemsLock)
            {
                int maxStack = ItemTemplate.Factory(item.ItemId).MaxStack;
                int canStacked = 0;

                if (maxStack > 1)
                {
                    for (int i = 20; i < 20 + storage.Size; i++)
                    {
                        if (!storage.Items.ContainsKey(i)) continue;

                        if (storage.Items[i].ItemId == item.ItemId)
                        {
                            canStacked += maxStack - storage.Items[i].Amount;

                            if (canStacked >= item.Amount)
                                break;
                        }
                    }
                }

                if (canStacked < item.Amount && GetFreeSlots(storage).Count < 1)
                    return false;

                if (canStacked > 0)
                {
                    for (int i = 20; i < 20 + storage.Size; i++)
                    {
                        if (!storage.Items.ContainsKey(i)) continue;

                        if (storage.Items[i].ItemId == item.ItemId)
                        {
                            int put = maxStack - storage.Items[i].Amount;
                            if (item.Amount < put)
                                put = item.Amount;

                            storage.Items[i].Amount += put;
                            item.Amount -= put;

                            if (item.Amount <= 0)
                                break;
                        }
                    }
                }

                if (item.Amount > 0)
                    storage.Items.Add(storage.GetFreeSlot(), item);

                ShowPlayerStorage(player, storage.StorageType);
                return true;
            }
        }
Пример #9
0
        public bool AddMoneys(Player player, Storage storage, long counter)
        {
            if (counter < 0)
                return false;

            storage.Money += counter;
            ShowPlayerStorage(player, storage.StorageType);

            return true;
        }
Пример #10
0
        public bool RemoveMoney(Player player, Storage storage, long counter)
        {
            if (counter < 0)
                return false;

            if (storage.Money - counter >= 0)
                storage.Money -= counter;
            else
            {
                player.Inventory.Money = 0;
                Logger.WriteLine(LogState.Warn,"InventorService: Player {0} moneys can't be less than 0");
                ShowPlayerStorage(player, storage.StorageType);
                return false;
            }

            ShowPlayerStorage(player, storage.StorageType);
            return true;
        }
Пример #11
0
        public void ReplaceItem(Player player, Storage storage, int @from, int to, bool isForDress = false, bool showStorage = true)
        {
            lock (storage.ItemsLock)
            {
                if (!storage.Items.ContainsKey(@from))
                    return;

                if (from < 20 && GetFreeSlots(storage).Count == 0)
                    return;

                StorageItem item = storage.Items[@from];

                if (isForDress && storage.StorageType == StorageType.Inventory)
                {
                    if (to == 0)
                        to = GetDressSlot(item.ItemId);

                    if (to == 0)
                        return;
                }

                if (to != 0 && to < 20 && storage.StorageType == StorageType.Inventory)
                {
                    if (!CanDress(player, item, true))
                        return;

                    switch (GetDressSlot(item.ItemId))
                    {
                        case 6:
                            if (to != 6 && to != 7)
                                return;
                            break;
                        case 8:
                            if (to != 8 && to != 9)
                                return;
                            break;
                        default:
                            if (to != GetDressSlot(item.ItemId))
                                return;
                            break;
                    }
                }

                storage.Items.Remove(@from);

                if (storage.Items.ContainsKey(to))
                {
                    StorageItem item2 = storage.Items[to];
                    storage.Items.Remove(to);
                    storage.Items.Add(@from, item2);
                }

                if (to == 0)
                    to = storage.GetFreeSlot();

                storage.Items.Add(to, item);

                if (@from <= 20 || to <= 20)
                    Communication.Logic.CreatureLogic.UpdateCreatureStats(player);
            }

            if (showStorage)
            {
                ShowPlayerStorage(player, storage.StorageType);
                if(@from <= 20 || to <= 20)
                    new SpCharacterThings(player).Send(player.Connection);
            }
        }
Пример #12
0
        public bool RemoveItemById(Player player, Storage storage, int itemId, int counter)
        {
            //todo rework
            for (int i = 20; i <= player.Inventory.Size + 19; i++)
                if (player.Inventory.Items.ContainsKey(i))
                {
                    if (player.Inventory.Items[i].ItemId == itemId)
                    {
                        if (player.Inventory.Items[i].Amount <= counter)
                            player.Inventory.Items.Remove(i);
                        else
                            player.Inventory.Items[i].Amount -= counter;

                        ShowPlayerStorage(player, storage.StorageType);
                        return true;
                    }
                }
            return false;
        }
Пример #13
0
        public bool RemoveItem(Player player, Storage storage, int slot, int counter)
        {
            if (counter < 0)
                return false;

            slot += 20;

            lock (storage.ItemsLock)
            {
                if (!storage.Items.ContainsKey(slot) || storage.Items[slot].Amount < counter)
                    return false;

                if (storage.Items[slot].Amount == counter)
                    storage.Items.Remove(slot);
                else if (storage.Items[slot].Amount > counter)
                    storage.Items[slot].Amount -= counter;

                ShowPlayerStorage(player, storage.StorageType, false);
            }
            return true;
        }
Пример #14
0
        private void CheckLock(Storage storage)
        {
            if (!storage.Locked)
                return;

            storage.Locked = false;
            SystemMessages.TradeHasChangedPleaseCheckItAgain.Send(storage.Equals(Storage1) ? Player1 : Player2);
        }
Пример #15
0
 public StorageItem GetItemById(Storage storage, int id)
 {
     lock (storage.ItemsLock)
     {
         for (int i = (storage.StorageType == StorageType.Inventory ? 20 : 0);
              i <= storage.Size + (storage.StorageType == StorageType.Inventory ? 19 : 0);
              i++)
             if (storage.Items.ContainsKey(i))
             {
                 if (storage.Items[i].ItemId == id)
                     return storage.Items[i];
             }
     }
     return null;
 }
Пример #16
0
        public override void Release()
        {
            ObservedCreature = null;

            Inventory.Release();
            Inventory = null;

            PlayerData = null;

            try
            {
                Visible.Stop();
                Visible.Release();
            }
            // ReSharper disable EmptyGeneralCatchClause
            catch
            // ReSharper restore EmptyGeneralCatchClause
            {
                //Already stoped
            }
            Visible = null;

            Skills = null;

            Quests = null;

            if (CurrentDialog != null)
                CurrentDialog.Release();

            if (Pet != null)
                Pet.Release();
            Pet = null;

            MarkedCreatures = null;

            TeleportLoadMapEvent = null;

            try
            {
                SystemWindowsTimer.Stop();
                SystemWindowsTimer.Dispose();
            }
                // ReSharper disable EmptyGeneralCatchClause
            catch
                // ReSharper restore EmptyGeneralCatchClause
            {
            }
            SystemWindowsTimer = null;

            BlockersInChat = null;

            Friends = null;

            base.Release();
        }
Пример #17
0
        public bool AddItem(Player player, Storage storage, int itemId, int count, int slot = -1)
        {
            if (count < 0)
                return false;

            if (slot > (storage.StorageType == StorageType.Inventory ? storage.Size + 20 : storage.Size))
                return false;

            if ((storage.StorageType == StorageType.Inventory && slot < 20 && slot != -1))
                return false;

            if (slot < -1)
                return false;

            lock (storage.ItemsLock)
            {

                int stackSize = ItemTemplate.Factory(itemId).MaxStack;

                if (stackSize == 1 && storage.IsFull())
                {
                    SystemMessages.InventoryIsFull.Send(player);
                    return false;
                }

                if (stackSize == 1)
                {
                    if (slot == -1)
                        storage.Items.Add(storage.GetFreeSlot(), new StorageItem {ItemId = itemId, Amount = count});
                    else if (storage.Items.ContainsKey(slot))
                    {
                        SystemMessages.YouCantPutItemInInventory(ItemTemplate.Factory(itemId).Name).Send(player);
                        return false;
                    }
                    else
                        storage.Items.Add(slot, new StorageItem {ItemId = itemId, Amount = count});
                }
                else
                {
                    if (slot != -1)
                    {
                        // Certain slot + Stackable
                        if (storage.Items.ContainsKey(slot))
                        {
                            SystemMessages.YouCantPutItemInInventory(ItemTemplate.Factory(itemId).Name).Send(player);
                            return false;
                        }

                        storage.Items.Add(slot, new StorageItem { ItemId = itemId, Amount = count });
                    }
                    else
                    {
                        #region Any slot + Stackable
                        Dictionary<int, StorageItem> itemsById = storage.GetItemsById(itemId);

                        int canBeAdded =
                            itemsById.Values.Where(storageItem => storageItem.Amount < stackSize).Sum(
                                storageItem => stackSize - storageItem.Amount);

                        if (canBeAdded >= count)
                        {
                            foreach (var storageItem in itemsById.Values)
                            {
                                int added = Math.Min(stackSize - storageItem.Amount, count);
                                storageItem.Amount += added;
                                count -= added;
                                if (count == 0)
                                    break;
                            }
                        }
                        else
                        {
                            if (storage.IsFull() || count > GetFreeSlots(storage).Count*stackSize)
                            {
                                SystemMessages.InventoryIsFull.Send(player);
                                return false;
                            }

                            foreach (var storageItem in itemsById.Values)
                            {
                                int added = Math.Min(stackSize - storageItem.Amount, count);
                                storageItem.Amount += added;
                                count -= added;
                            }
                            while (count > 0)
                            {
                                int added = Math.Min(stackSize, count);
                                StorageItem item = new StorageItem {ItemId = itemId, Amount = added};
                                storage.Items.Add(storage.GetFreeSlot(), item);
                                count -= added;
                            }
                        }
                        #endregion
                    }
                }

                ShowPlayerStorage(player, storage.StorageType);
                return true;
            }
        }