Ejemplo n.º 1
0
        internal static UserData GetUserData(string sessionTicket, string ip, out byte errorCode)
        {
            DataRow dUserInfo;
            DataRow dUserStats;

            DataTable dAchievements;
            DataTable dFavouriteRooms;
            DataTable dIgnores;
            DataTable dTags;
            DataRow dSubscriptions;
            DataTable dBadges;
            DataTable dInventory;
            DataTable dEffects;
            DataTable dFriends;
            DataTable dRequests;
            DataTable dRooms;
            DataTable dPets;
            DataTable dQuests;
            DataTable dSongs;
            DataTable dGroups = null;
            DataTable dBots;
            DataTable dRelations;
            UInt32 userID;

            using (IQueryAdapter dbClient = SilverwaveEnvironment.GetDatabaseManager().getQueryreactor())
            {
                if (SilverwaveEnvironment.useSSO)
                {
                    dbClient.setQuery("SELECT * " +
                                        "FROM users " +
                                        "WHERE auth_ticket = @sso " +
                                        "AND ip_last= @ipaddress ");
                }
                else
                {
                    dbClient.setQuery("SELECT * " +
                                        "FROM users " +
                                        "WHERE auth_ticket = @sso ");
                }

                dbClient.addParameter("sso", sessionTicket);
                dbClient.addParameter("ipaddress", ip);
                dUserInfo = dbClient.getRow();

                if (dUserInfo == null)
                {
                    errorCode = 1;

                    throw new UserDataNotFoundException(string.Format("No user found with ip {0} and sso {1}. Use SSO: {2} ", ip, sessionTicket, SilverwaveEnvironment.useSSO.ToString()));
                }

                userID = Convert.ToUInt32(dUserInfo["id"]);
                dbClient.runFastQuery("UPDATE users SET online='1' WHERE id=" + userID + " LIMIT 1");

                //SilverwaveEnvironment.GetGame().GetClientManager().LogClonesOut(Convert.ToUInt32(userID));

                if (SilverwaveEnvironment.GetGame().GetClientManager().GetClientByUserID(userID) != null)
                {
                    errorCode = 2;
                    SilverwaveEnvironment.GetGame().GetClientManager().GetClientByUserID(userID).Disconnect();
                    return null;
                }

                dbClient.setQuery("SELECT * FROM user_achievement WHERE userid = " + userID);
                dAchievements = dbClient.getTable();

                dbClient.setQuery("SELECT COUNT(*) FROM user_stats WHERE id=" + userID);
                int count = int.Parse(dbClient.getString());
                if (count == 0)
                    dbClient.runFastQuery("INSERT INTO user_stats (id) VALUES (" + userID + ");");

                /*if (SilverwaveStaticSettings.FASTFOOD_ENABLED)
                {
                    dbClient.setQuery("SELECT COUNT(*) FROM fastfood_data WHERE id=" + userID);
                    count = int.Parse(dbClient.getString());
                    if (count == 0)
                    {
                        dbClient.runFastQuery("INSERT INTO fastfood_data VALUES (" + userID + ", '', 6, 6, 6, 0);");
                    }
                }*/

                dbClient.setQuery("SELECT * FROM user_stats WHERE id=" + userID + " LIMIT 1");
                dUserStats = dbClient.getRow();

                dbClient.setQuery("SELECT room_id FROM user_favorites WHERE user_id = " + userID);
                dFavouriteRooms = dbClient.getTable();

                dbClient.setQuery("SELECT ignore_id FROM user_ignores WHERE user_id = " + userID);
                dIgnores = dbClient.getTable();

                dbClient.setQuery("SELECT tag FROM user_tags WHERE user_id = " + userID);
                dTags = dbClient.getTable();

                dbClient.setQuery("SELECT * FROM user_subscriptions WHERE user_id = " + userID + " AND timestamp_expire > UNIX_TIMESTAMP() ORDER BY subscription_id DESC LIMIT 1");
                dSubscriptions = dbClient.getRow();

                dbClient.setQuery("SELECT * FROM user_badges WHERE user_id = " + userID);
                dBadges = dbClient.getTable();

                dbClient.setQuery("SELECT `items`.* , COALESCE(`items_groups`.`group_id`, 0) AS group_id FROM `items` LEFT OUTER JOIN `items_groups` ON `items`.`id` = `items_groups`.`id` WHERE room_id='' AND user_id=" + userID + " LIMIT 8000");
                dInventory = dbClient.getTable();

                dbClient.setQuery("SELECT * FROM user_effects WHERE user_id =  " + userID);
                dEffects = dbClient.getTable();

                dbClient.setQuery("SELECT users.id,users.username,users.motto,users.look,users.last_online,users.hide_inroom,users.hide_online " +
                                        "FROM users " +
                                        "JOIN messenger_friendships " +
                                        "ON users.id = messenger_friendships.user_one_id " +
                                        "WHERE messenger_friendships.user_two_id = " + userID + " " +
                                        "UNION ALL " +
                                        "SELECT users.id,users.username,users.motto,users.look,users.last_online,users.hide_inroom,users.hide_online " +
                                        "FROM users " +
                                        "JOIN messenger_friendships " +
                                        "ON users.id = messenger_friendships.user_two_id " +
                                        "WHERE messenger_friendships.user_one_id = " + userID);
                dFriends = dbClient.getTable();

                dbClient.setQuery("SELECT * FROM user_stats WHERE id=" + userID + " LIMIT 1");
                dUserStats = dbClient.getRow();

                dbClient.setQuery("SELECT messenger_requests.from_id,messenger_requests.to_id,users.username " +
                                        "FROM users " +
                                        "JOIN messenger_requests " +
                                        "ON users.id = messenger_requests.from_id " +
                                        "WHERE messenger_requests.to_id = " + userID);
                dRequests = dbClient.getTable();

                dbClient.setQuery("SELECT * FROM rooms WHERE owner = @name LIMIT 150");
                dbClient.addParameter("name", (string)dUserInfo["username"]);
                dRooms = dbClient.getTable();

                dbClient.setQuery("SELECT * FROM bots WHERE user_id = " + userID + " AND room_id = 0 AND ai_type='pet'");
                dPets = dbClient.getTable();

                dbClient.setQuery("SELECT * FROM user_quests WHERE user_id = " + userID + "");
                dQuests = dbClient.getTable();

                dbClient.setQuery("SELECT * FROM bots WHERE user_id=" + userID + " AND room_id=0 AND ai_type='generic'");
                dBots = dbClient.getTable();
                dbClient.setQuery("SELECT group_id,rank FROM group_memberships WHERE user_id=" + userID);
                dGroups = dbClient.getTable();

                dbClient.setQuery("SELECT item_id, song_id FROM user_items_songs WHERE user_id = " + userID);
                dSongs = dbClient.getTable();

                dbClient.setQuery("UPDATE user_info SET login_timestamp = '" + SilverwaveEnvironment.GetUnixTimestamp() + "' WHERE user_id = " + userID + " ; ");
                dbClient.addParameter("ip", ip);
                dbClient.runQuery();

                dbClient.setQuery("SELECT * FROM user_relationships WHERE user_id=@id");
                dbClient.addParameter("id", userID);
                dRelations = dbClient.getTable();

                    dbClient.runFastQuery("UPDATE users SET online='1' WHERE id=" + userID + " LIMIT 1");
            }

            Dictionary<string, UserAchievement> achievements = new Dictionary<string, UserAchievement>();

            string achievementGroup;
            int achievementLevel;
            int achievementProgress;
            foreach (DataRow dRow in dAchievements.Rows)
            {
                achievementGroup = (string)dRow["group"];
                achievementLevel = (int)dRow["level"];
                achievementProgress = (int)dRow["progress"];

                UserAchievement achievement = new UserAchievement(achievementGroup, achievementLevel, achievementProgress);
                achievements.Add(achievementGroup, achievement);
            }

            List<uint> favouritedRooms = new List<uint>();

            uint favoritedRoomID;
            foreach (DataRow dRow in dFavouriteRooms.Rows)
            {
                favoritedRoomID = Convert.ToUInt32(dRow["room_id"]);
                favouritedRooms.Add(favoritedRoomID);
            }

            List<uint> ignores = new List<uint>();

            uint ignoredUserID;
            foreach (DataRow dRow in dIgnores.Rows)
            {
                ignoredUserID = Convert.ToUInt32(dRow["ignore_id"]);
                ignores.Add(ignoredUserID);
            }

            List<string> tags = new List<string>();

            string tag;
            foreach (DataRow dRow in dTags.Rows)
            {
                tag = (string)dRow["tag"].ToString().Replace(" ", "");
                tags.Add(tag);
            }

            Subscription Sub = null;

            if(dSubscriptions != null) {
                Sub = new Subscription((int)dSubscriptions["subscription_id"], (int)dSubscriptions["timestamp_activated"], (int)dSubscriptions["timestamp_expire"]);
            }

            Dictionary<uint, RoomBot> bots = new Dictionary<uint, RoomBot>();
            RoomBot bot;
            foreach (DataRow dRow in dBots.Rows)
            {
                bot = BotManager.GenerateBotFromRow(dRow);
                bots.Add(bot.BotId, bot);
            }

            List<Badge> badges = new List<Badge>();

            string badgeID;
            int slotID;
            foreach (DataRow dRow in dBadges.Rows)
            {
                badgeID = (string)dRow["badge_id"];
                slotID = (int)dRow["badge_slot"];
                badges.Add(new Badge(badgeID, slotID));
            }

            List<UserItem> inventory = new List<UserItem>();

            uint itemID;
            uint baseItem;
            string extraData;
            uint group;
            foreach (DataRow dRow in dInventory.Rows)
            {
                itemID = Convert.ToUInt32(dRow[0]);
                baseItem = Convert.ToUInt32(dRow[3]);
                if (!DBNull.Value.Equals(dRow[4]))
                    extraData = (string)dRow[4];
                else
                    extraData = string.Empty;

                group = Convert.ToUInt32(dRow[10]);

                inventory.Add(new UserItem(itemID, baseItem, extraData, group));
            }

            List<AvatarEffect> effects = new List<AvatarEffect>();

            int effectID;
            int duration;
            bool isActivated;
            double activatedTimeStamp;
            foreach (DataRow dRow in dEffects.Rows)
            {
                effectID = (int)dRow["effect_id"];
                duration = (int)dRow["total_duration"];
                isActivated = SilverwaveEnvironment.EnumToBool((string)dRow["is_activated"]);
                activatedTimeStamp = (double)dRow["activated_stamp"];

                effects.Add(new AvatarEffect(effectID, duration, isActivated, activatedTimeStamp));
            }

            Dictionary<uint, MessengerBuddy> friends = new Dictionary<uint, MessengerBuddy>();

            string username = (string)dUserInfo["username"];

            UInt32 friendID;
            string friendName;
            string friendLook;
            string friendMotto;
            int friendLastOnline;
            bool friendHideRoom;
            bool friendHideOnline;
            int FriendCount = dFriends.Rows.Count - 700;
            if (FriendCount > 0)
            {
                using (IQueryAdapter dbClient = SilverwaveEnvironment.GetDatabaseManager().getQueryreactor())
                {
                    dbClient.runFastQuery("DELETE FROM messenger_friendships WHERE user_one_id=" + userID + " OR user_two_id=" + userID + " LIMIT " + FriendCount);
                    dbClient.setQuery("SELECT users.id,users.username,users.motto,users.look,users.last_online,users.hide_inroom,users.hide_online " +
                                        "FROM users " +
                                        "JOIN messenger_friendships " +
                                        "ON users.id = messenger_friendships.user_one_id " +
                                        "WHERE messenger_friendships.user_two_id = " + userID + " " +
                                        "UNION ALL " +
                                        "SELECT users.id,users.username,users.motto,users.look,users.last_online,users.hide_inroom,users.hide_online " +
                                        "FROM users " +
                                        "JOIN messenger_friendships " +
                                        "ON users.id = messenger_friendships.user_two_id " +
                                        "WHERE messenger_friendships.user_one_id = " + userID);
                dFriends = dbClient.getTable();
                }
            }
            foreach (DataRow dRow in dFriends.Rows)
            {
                friendID = Convert.ToUInt32(dRow["id"]);
                friendName = (string)dRow["username"];
                friendLook = (string)dRow["look"];
                friendMotto = (string)dRow["motto"];
                friendLastOnline = Convert.ToInt32(dRow["last_online"]);
                friendHideOnline = SilverwaveEnvironment.EnumToBool(dRow["hide_online"].ToString());
                friendHideRoom = SilverwaveEnvironment.EnumToBool(dRow["hide_inroom"].ToString());

                if (friendID == userID)
                    continue;

                if (!friends.ContainsKey(friendID))
                    friends.Add(friendID, new MessengerBuddy(friendID, friendName, friendLook, friendMotto, friendLastOnline, friendHideOnline, friendHideRoom));
            }

            Dictionary<uint, MessengerRequest> requests = new Dictionary<uint, MessengerRequest>();

            uint receiverID;
            uint senderID;
            string requestUsername;
            foreach (DataRow dRow in dRequests.Rows)
            {
                receiverID = Convert.ToUInt32(dRow["from_id"]);
                senderID = Convert.ToUInt32(dRow["to_id"]);

                requestUsername = (string)dRow["username"];

                if (receiverID != userID)
                {
                    if (!requests.ContainsKey(receiverID))
                        requests.Add(receiverID, new MessengerRequest(userID, receiverID, requestUsername));
                }
                else
                {
                    if (!requests.ContainsKey(senderID))
                        requests.Add(senderID, new MessengerRequest(userID, senderID, requestUsername));
                }
            }

            List<RoomData> rooms = new List<RoomData>();

            uint roomID;
            foreach (DataRow dRow in dRooms.Rows)
            {
                roomID = Convert.ToUInt32(dRow["id"]);
                rooms.Add(SilverwaveEnvironment.GetGame().GetRoomManager().FetchRoomData(roomID, dRow));
            }

            Dictionary<uint, Pet> pets = new Dictionary<uint, Pet>();

            Pet pet;
            foreach (DataRow dRow in dPets.Rows)
            {
                using (IQueryAdapter dbClient = SilverwaveEnvironment.GetDatabaseManager().getQueryreactor())
                {
                    dbClient.setQuery("SELECT * FROM bots_petdata WHERE id=" + dRow[0] + " LIMIT 1");
                    DataRow mRow = dbClient.getRow();
                    if (mRow == null)
                        continue;

                    pet = Catalog.GeneratePetFromRow(dRow, mRow);
                    pets.Add(pet.PetId, pet);
                }
            }

            Dictionary<uint, int> quests = new Dictionary<uint, int>();

            uint questId;
            int progress;
            foreach (DataRow dRow in dQuests.Rows)
            {
                questId = Convert.ToUInt32(dRow["quest_id"]);
                progress = (int)dRow["progress"];

                if (quests.ContainsKey(questId))
                    quests.Remove(questId);

                quests.Add(questId, progress);
            }
            List<GroupUser> Groups = new List<GroupUser>();
            foreach (DataRow Row in dGroups.Rows)
                Groups.Add(new GroupUser(userID, (uint)Row[0], Convert.ToInt16(Row[1])));
            Hashtable songs = new Hashtable();

            uint songItemID;
            int songID;
            foreach (DataRow dRow in dSongs.Rows)
            {
                songItemID = (uint)dRow[0];
                songID = (int)dRow[1];

                SongItem song = new SongItem(songItemID, songID, 4568423);
                songs.Add(songItemID, song);
            }

            Dictionary<int, Relationship> Relationships = new Dictionary<int, Relationship>();
            foreach (DataRow Row in dRelations.Rows)
            {
                Relationships.Add((int)Row[0], new Relationship((int)Row[0], (int)Row[2], Convert.ToInt32(Row[3].ToString())));
            }

            Habbo user = HabboFactory.GenerateHabbo(dUserInfo, dUserStats, Groups);

            dUserInfo = null;
            dAchievements = null;
            dFavouriteRooms = null;
            dIgnores = null;
            dTags = null;
            dSubscriptions = null;
            dBadges = null;
            dInventory = null;
            dEffects = null;
            dFriends = null;
            dRequests = null;
            dRooms = null;
            dPets = null;
            dBots = null;
            dRelations = null;

            errorCode = 0;
            return new UserData(userID, achievements, favouritedRooms, ignores, tags, Sub, badges, inventory, effects, friends, requests, rooms, pets, quests, songs, user, bots, Relationships);
        }
Ejemplo n.º 2
0
        /*internal void BadgeDescs(GameClient Session)
        {
            ServerMessage Response = new ServerMessage(Outgoing.AchBadgeDescs);
            Response.AppendInt32(Achievements.Values.ToList().Count);

            foreach (Achievement Ach in Achievements.Values)
            {
                Response.AppendString(Ach.GroupName.Replace("ACH_", "")); // The badge without ACH_

                Response.AppendInt32(Ach.Levels.Count); // Amount of levels

                for (int i = 1; i < Ach.Levels.Count + 1; i++)
                {
                    AchievementLevel TargetLevelData = Ach.Levels[i];
                    Response.AppendInt32(i); // the level
                    Response.AppendInt32(TargetLevelData.Requirement); // the goal
                }
            }

            Response.AppendInt32(0); // dunno?
            Session.SendMessage(Response);
        }*/
        internal bool ProgressUserAchievement(GameClient Session, string AchievementGroup, int ProgressAmount, bool FromZero = false)
        {
            if (!Achievements.ContainsKey(AchievementGroup) || Session == null)
            {
                return false;
            }

            Achievement AchievementData = null;

            AchievementData = Achievements[AchievementGroup];

            UserAchievement UserData = Session.GetHabbo().GetAchievementData(AchievementGroup);

            if (UserData == null)
            {
                UserData = new UserAchievement(AchievementGroup, 0, 0);
                Session.GetHabbo().Achievements.Add(AchievementGroup, UserData);
            }

            int TotalLevels = AchievementData.Levels.Count;

            if (UserData != null && UserData.Level == TotalLevels)
            {
                return false; // done, no more.
            }

            int TargetLevel = (UserData != null ? UserData.Level + 1 : 1);

            if (TargetLevel > TotalLevels)
            {
                TargetLevel = TotalLevels;
            }

            AchievementLevel TargetLevelData = AchievementData.Levels[TargetLevel];
            int NewProgress=0;
            if (FromZero)
                NewProgress = ProgressAmount;
            else
                NewProgress = (UserData != null ? UserData.Progress + ProgressAmount : ProgressAmount);
            int NewLevel = (UserData != null ? UserData.Level : 0);
            int NewTarget = NewLevel + 1;

            if (NewTarget > TotalLevels)
            {
                NewTarget = TotalLevels;
            }

            if (NewProgress >= TargetLevelData.Requirement)
            {
                NewLevel++;
                NewTarget++;

                int ProgressRemainder = NewProgress - TargetLevelData.Requirement;

                NewProgress = 0;

                if (TargetLevel == 1)
                    Session.GetHabbo().GetBadgeComponent().GiveBadge(AchievementGroup + TargetLevel, true, Session);
                else
                {
                    Session.GetHabbo().GetBadgeComponent().RemoveBadge(Convert.ToString(AchievementGroup + (TargetLevel - 1)));
                    Session.GetHabbo().GetBadgeComponent().GiveBadge(AchievementGroup + TargetLevel, true, Session);
                }

                if (NewTarget > TotalLevels)
                {
                    NewTarget = TotalLevels;
                }

                Session.GetHabbo().ActivityPoints += TargetLevelData.RewardPixels;
                Session.GetHabbo().NotifyNewPixels(TargetLevelData.RewardPixels);
                Session.GetHabbo().UpdateActivityPointsBalance();

                Session.SendMessage(AchievementUnlockedComposer.Compose(AchievementData, TargetLevel, TargetLevelData.RewardPoints,
                    TargetLevelData.RewardPixels));

                using (IQueryAdapter dbClient = SilverwaveEnvironment.GetDatabaseManager().getQueryreactor())
                {
                        dbClient.setQuery("REPLACE INTO user_achievement VALUES (" + Session.GetHabbo().Id + ", @group, " + NewLevel + ", " + NewProgress + ")");
                    dbClient.addParameter("group", AchievementGroup);
                    dbClient.runQuery();
                }

                UserData.Level = NewLevel;
                UserData.Progress = NewProgress;

                Session.GetHabbo().AchievementPoints += TargetLevelData.RewardPoints;
                Session.GetHabbo().NotifyNewPixels(TargetLevelData.RewardPixels);
                Session.GetHabbo().ActivityPoints += TargetLevelData.RewardPixels;
                Session.GetHabbo().UpdateActivityPointsBalance();
                Session.SendMessage(AchievementScoreUpdateComposer.Compose(Session.GetHabbo().AchievementPoints));

                AchievementLevel NewLevelData = AchievementData.Levels[NewTarget];
                Session.SendMessage(AchievementProgressComposer.Compose(AchievementData, NewTarget, NewLevelData,
                    TotalLevels, Session.GetHabbo().GetAchievementData(AchievementGroup)));

                return true;
            }
            else
            {
                UserData.Level = NewLevel;
                UserData.Progress = NewProgress;
                using (IQueryAdapter dbClient = SilverwaveEnvironment.GetDatabaseManager().getQueryreactor())
                {
                    dbClient.setQuery("REPLACE INTO user_achievement VALUES (" + Session.GetHabbo().Id + ", @group, " + NewLevel + ", " + NewProgress + ")");
                    dbClient.addParameter("group", AchievementGroup);
                    dbClient.runQuery();
                }

                Session.SendMessage(AchievementProgressComposer.Compose(AchievementData, TargetLevel, TargetLevelData,
                TotalLevels, Session.GetHabbo().GetAchievementData(AchievementGroup)));
            }

            return false;
        }