Example #1
0
        /// <summary>
        ///     Saves the specified room identifier.
        /// </summary>
        /// <param name="queryChunk"></param>
        /// <param name="id">Auto increment</param>
        internal void Save(QueryChunk queryChunk, uint id)
        {
            if (IsSaved)
                return;

            queryChunk.AddQuery("INSERT INTO users_chatlogs (user_id, room_id, timestamp, message) VALUES (@user" + id + ", @room, @time" + id + ", @message" + id + ")");
            queryChunk.AddParameter("user" + id, UserId);
            queryChunk.AddParameter("time" + id, Azure.DateTimeToUnix(TimeStamp));
            queryChunk.AddParameter("message" + id, Message);
        }
        /// <summary>
        ///     Saves the furniture.
        /// </summary>
        /// <param name="dbClient">The database client.</param>
        /// <param name="session">The session.</param>
        public void SaveFurniture(IQueryAdapter dbClient, GameClient session = null)
        {
            try
            {
                if (!_updatedItems.Any() && !_removedItems.Any() &&
                    _room.GetRoomUserManager().PetCount <= 0)
                    return;

                var queryChunk = new QueryChunk();
                var queryChunk2 = new QueryChunk();

                foreach (var itemId in _removedItems)
                {
                    queryChunk.AddQuery("UPDATE items_rooms SET room_id='0', x='0', y='0', z='0', rot='0' WHERE id = " +
                                        itemId);
                }

                foreach (var roomItem in _updatedItems.Select(GetItem).Where(roomItem => roomItem != null))
                {
                    if (roomItem.GetBaseItem() != null && roomItem.GetBaseItem().IsGroupItem)
                    {
                        try
                        {
                            var gD = roomItem.GroupData.Split(';');
                            roomItem.ExtraData = roomItem.ExtraData + ";" + gD[1] + ";" +
                                                 gD[2] + ";" + gD[3];
                        }
                        catch
                        {
                            roomItem.ExtraData = string.Empty;
                        }
                    }

                    if (roomItem.RoomId == 0) continue;

                    if (roomItem.GetBaseItem().Name.Contains("wallpaper_single") ||
                        roomItem.GetBaseItem().Name.Contains("floor_single") ||
                        roomItem.GetBaseItem().Name.Contains("landscape_single"))
                    {
                        queryChunk.AddQuery("DELETE FROM items_rooms WHERE id = " + roomItem.Id + " LIMIT 1");
                        continue;
                    }

                    var query = "UPDATE items_rooms SET room_id = " + roomItem.RoomId;
                    if (!string.IsNullOrEmpty(roomItem.ExtraData))
                    {
                        query += ", extra_data = @extraData" + roomItem.Id;
                        queryChunk2.AddParameter("extraData" + roomItem.Id, roomItem.ExtraData);
                    }

                    if (roomItem.IsFloorItem)
                    {
                        query +=
                            $", x={roomItem.X}, y={roomItem.Y}, z='{roomItem.Z.ToString(CultureInfo.InvariantCulture).Replace(',', '.')}', rot={roomItem.Rot}";
                    }
                    else
                    {
                        query += ", wall_pos = @wallPos" + roomItem.Id;
                        queryChunk2.AddParameter("wallPos" + roomItem.Id, roomItem.WallCoord);
                    }

                    query += " WHERE id = " + roomItem.Id;
                    queryChunk2.AddQuery(query);
                }

                _room.GetRoomUserManager().AppendPetsUpdateString(dbClient);

                if (session != null) session.GetHabbo().GetInventoryComponent().RunDbUpdate();

                _updatedItems.Clear();
                _removedItems.Clear();
                queryChunk.Execute(dbClient);
                queryChunk2.Execute(dbClient);
                queryChunk.Dispose();
                queryChunk2.Dispose();
                queryChunk = null;
                queryChunk2 = null;
            }
            catch (Exception ex)
            {
                Logging.LogCriticalException("Error during saving furniture for room " + _room.RoomId + ". Stack: " + ex);
            }
        }
        /// <summary>
        ///     Runs the database update.
        /// </summary>
        internal void RunDbUpdate()
        {
            try
            {
                if (_mRemovedItems.Count <= 0 && _mAddedItems.Count <= 0 && _inventoryPets.Count <= 0)
                    return;

                var queryChunk = new QueryChunk();

                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 (var queryReactor = Azure.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 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 = ", Azure.DateTimeToUnix(current.LastHealth), ", untilgrown_stamp = ", Azure.DateTimeToUnix(current.UntilGrown), " WHERE id = ", current.PetId));
                    }

                    current.DbState = DatabaseUpdateState.Updated;
                }

                using (var queryreactor2 = Azure.GetDatabaseManager().GetQueryReactor())
                    queryChunk.Execute(queryreactor2);
            }
            catch (Exception ex)
            {
                Logging.LogCacheError($"FATAL ERROR DURING USER INVENTORY DB UPDATE: {ex}");
            }
        }
Example #4
0
        /// <summary>
        /// Appends the pets update string.
        /// </summary>
        /// <param name="dbClient">The database client.</param>
        internal void AppendPetsUpdateString(IQueryAdapter dbClient)
        {
            var queryChunk = new QueryChunk("INSERT INTO bots (id,user_id,room_id,name,x,y,z) VALUES ");
            var queryChunk2 =
                new QueryChunk(
                    "INSERT INTO pets_data (type,race,color,experience,energy,createstamp,nutrition,respect) VALUES ");
            var queryChunk3 = new QueryChunk();
            var list = new List<uint>();
            foreach (var current in GetPets().Where(current => !list.Contains(current.PetId)))
            {
                list.Add(current.PetId);
                switch (current.DbState)
                {
                    case DatabaseUpdateState.NeedsInsert:
                        queryChunk.AddParameter(string.Format("{0}name", current.PetId), current.Name);
                        queryChunk2.AddParameter(string.Format("{0}race", current.PetId), current.Race);
                        queryChunk2.AddParameter(string.Format("{0}color", current.PetId), current.Color);
                        queryChunk.AddQuery(string.Concat("(", current.PetId, ",", current.OwnerId, ",", current.RoomId, ",@", current.PetId, "name,", current.X, ",", current.Y, ",", current.Z, ")"));
                        queryChunk2.AddQuery(string.Concat("(", current.Type, ",@", current.PetId, "race,@", current.PetId, "color,0,100,'", current.CreationStamp, "',0,0)"));
                        break;

                    case DatabaseUpdateState.NeedsUpdate:
                        queryChunk3.AddParameter(string.Format("{0}name", current.PetId), current.Name);
                        queryChunk3.AddParameter(string.Format("{0}race", current.PetId), current.Race);
                        queryChunk3.AddParameter(string.Format("{0}color", current.PetId), current.Color);
                        queryChunk3.AddQuery(string.Concat("UPDATE bots SET room_id = ", current.RoomId, ", name = @", current.PetId, "name, x = ", current.X, ", Y = ", current.Y, ", Z = ", current.Z, " WHERE id = ", current.PetId));
                        queryChunk3.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, "' WHERE id = ", current.PetId));
                        break;
                }
                current.DbState = DatabaseUpdateState.Updated;
            }
            queryChunk.Execute(dbClient);
            queryChunk3.Execute(dbClient);
            queryChunk.Dispose();
            queryChunk3.Dispose();
            queryChunk = null;
            queryChunk3 = null;
        }