Exemplo n.º 1
0
        /// <summary>
        ///     Runs the database update.
        /// </summary>
        internal void RunDbUpdate()
        {
            try
            {
                if (_mRemovedItems.Count <= 0 && _mAddedItems.Count <= 0 && _inventoryPets.Count <= 0)
                {
                    return;
                }

                DatabaseQueryChunk queryChunk = new DatabaseQueryChunk();

                if (_mAddedItems.Count > 0)
                {
                    foreach (UserItem userItem in _mAddedItems.Values)
                    {
                        queryChunk.AddQuery($"UPDATE items_rooms SET user_id='{UserId}', room_id='0' WHERE id='{userItem.Id}'");
                    }

                    _mAddedItems.Clear();
                }

                if (_mRemovedItems.Count > 0)
                {
                    try
                    {
                        foreach (UserItem userItem2 in _mRemovedItems.Values)
                        {
                            using (IQueryAdapter queryReactor = Yupi.GetDatabaseManager().GetQueryReactor())
                                GetClient().GetHabbo().CurrentRoom.GetRoomItemHandler().SaveFurniture(queryReactor);

                            if (SongDisks.Contains(userItem2.Id))
                            {
                                SongDisks.Remove(userItem2.Id);
                            }
                        }
                    }
                    catch
                    {
                        // ignored
                    }

                    _mRemovedItems.Clear();
                }

                foreach (Pet current in _inventoryPets.Values)
                {
                    if (current.DbState == DatabaseUpdateState.NeedsUpdate)
                    {
                        queryChunk.AddParameter($"{current.PetId}name", current.Name);
                        queryChunk.AddParameter($"{current.PetId}race", current.Race);
                        queryChunk.AddParameter($"{current.PetId}color", current.Color);

                        queryChunk.AddQuery(string.Concat("UPDATE bots_data SET room_id = ", current.RoomId, ", name = @", current.PetId, "name, x = ", current.X, ", Y = ", current.Y, ", Z = ", current.Z, " WHERE id = ", current.PetId));

                        queryChunk.AddQuery(string.Concat("UPDATE pets_data SET race = @", current.PetId, "race, color = @", current.PetId, "color, type = ", current.Type, ", experience = ", current.Experience, ", energy = ", current.Energy, ", nutrition = ", current.Nutrition, ", respect = ", current.Respect, ", createstamp = '", current.CreationStamp, "', lasthealth_stamp = ", Yupi.DateTimeToUnix(current.LastHealth), ", untilgrown_stamp = ", Yupi.DateTimeToUnix(current.UntilGrown), " WHERE id = ", current.PetId));
                    }

                    current.DbState = DatabaseUpdateState.Updated;
                }

                using (IQueryAdapter queryreactor2 = Yupi.GetDatabaseManager().GetQueryReactor())
                    queryChunk.Execute(queryreactor2);
            }
            catch (Exception ex)
            {
                ServerLogManager.LogCacheError($"FATAL ERROR DURING USER INVENTORY DB UPDATE: {ex}");
            }
        }