public void AddNewStoredLoot(Loot loot, Player player)
        {
            // Saves the money and item loot associated with an openable item to the DB
            if (loot.IsLooted()) // no money and no loot
            {
                return;
            }

            if (_lootItemStorage.ContainsKey(loot.containerID.GetCounter()))
            {
                Log.outError(LogFilter.Misc, $"Trying to store item loot by player: {player.GetGUID()} for container id: {loot.containerID.GetCounter()} that is already in storage!");
                return;
            }

            StoredLootContainer container = new StoredLootContainer(loot.containerID.GetCounter());

            SQLTransaction trans = new SQLTransaction();

            if (loot.gold != 0)
            {
                container.AddMoney(loot.gold, trans);
            }

            PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.DEL_ITEMCONTAINER_ITEMS);

            stmt.AddValue(0, loot.containerID.GetCounter());
            trans.Append(stmt);

            foreach (LootItem li in loot.items)
            {
                // Conditions are not checked when loot is generated, it is checked when loot is sent to a player.
                // For items that are lootable, loot is saved to the DB immediately, that means that loot can be
                // saved to the DB that the player never should have gotten. This check prevents that, so that only
                // items that the player should get in loot are in the DB.
                // IE: Horde items are not saved to the DB for Ally players.
                if (!li.AllowedForPlayer(player))
                {
                    continue;
                }

                // Don't save currency tokens
                ItemTemplate itemTemplate = Global.ObjectMgr.GetItemTemplate(li.itemid);
                if (itemTemplate == null || itemTemplate.IsCurrencyToken())
                {
                    continue;
                }

                container.AddLootItem(li, trans);
            }

            DB.Characters.CommitTransaction(trans);

            _lootItemStorage.TryAdd(loot.containerID.GetCounter(), container);
        }
Beispiel #2
0
        public void LoadStorageFromDB()
        {
            uint oldMSTime = Time.GetMSTime();

            _lootItemStorage.Clear();
            uint count = 0;

            PreparedStatement stmt   = DB.Characters.GetPreparedStatement(CharStatements.SEL_ITEMCONTAINER_ITEMS);
            SQLResult         result = DB.Characters.Query(stmt);

            if (!result.IsEmpty())
            {
                do
                {
                    ulong key = result.Read <ulong>(0);
                    var   itr = _lootItemStorage.LookupByKey(key);
                    if (!_lootItemStorage.ContainsKey(key))
                    {
                        _lootItemStorage[key] = new StoredLootContainer(key);
                    }

                    StoredLootContainer storedContainer = _lootItemStorage[key];

                    LootItem lootItem = new();
                    lootItem.itemid            = result.Read <uint>(1);
                    lootItem.count             = result.Read <byte>(2);
                    lootItem.follow_loot_rules = result.Read <bool>(3);
                    lootItem.freeforall        = result.Read <bool>(4);
                    lootItem.is_blocked        = result.Read <bool>(5);
                    lootItem.is_counted        = result.Read <bool>(6);
                    lootItem.is_underthreshold = result.Read <bool>(7);
                    lootItem.needs_quest       = result.Read <bool>(8);
                    lootItem.randomBonusListId = result.Read <uint>(9);
                    lootItem.context           = (ItemContext)result.Read <byte>(10);
                    StringArray bonusLists = new(result.Read <string>(11), ' ');

                    foreach (string str in bonusLists)
                    {
                        lootItem.BonusListIDs.Add(uint.Parse(str));
                    }

                    storedContainer.AddLootItem(lootItem, null);

                    ++count;
                } while (result.NextRow());

                Log.outInfo(LogFilter.ServerLoading, $"Loaded {count} stored item loots in {Time.GetMSTimeDiffToNow(oldMSTime)} ms");
            }
            else
            {
                Log.outInfo(LogFilter.ServerLoading, "Loaded 0 stored item loots");
            }

            stmt   = DB.Characters.GetPreparedStatement(CharStatements.SEL_ITEMCONTAINER_MONEY);
            result = DB.Characters.Query(stmt);
            if (!result.IsEmpty())
            {
                count = 0;
                do
                {
                    ulong key = result.Read <ulong>(0);
                    if (!_lootItemStorage.ContainsKey(key))
                    {
                        _lootItemStorage.TryAdd(key, new StoredLootContainer(key));
                    }

                    StoredLootContainer storedContainer = _lootItemStorage[key];
                    storedContainer.AddMoney(result.Read <uint>(1), null);

                    ++count;
                } while (result.NextRow());

                Log.outInfo(LogFilter.ServerLoading, $"Loaded {count} stored item money in {Time.GetMSTimeDiffToNow(oldMSTime)} ms");
            }
            else
            {
                Log.outInfo(LogFilter.ServerLoading, "Loaded 0 stored item money");
            }
        }