Example #1
0
        public static bool TryRedeemVoucher(SqlDatabaseClient MySqlClient, Session Session, string Code)
        {
            lock (mSyncRoot)
            {
                VoucherValueData ValueData = GetVoucherValue(Code);

                if (ValueData == null)
                {
                    return(false);
                }

                if (ValueData.ValueCredits > 0)
                {
                    Session.CharacterInfo.UpdateCreditsBalance(MySqlClient, ValueData.ValueCredits);
                    Session.SendData(CreditsBalanceComposer.Compose(Session.CharacterInfo.CreditsBalance));
                }

                if (ValueData.ValuePixels > 0)
                {
                    Session.CharacterInfo.UpdateActivityPointsBalance(MySqlClient, ValueData.ValuePixels);
                    Session.SendData(ActivityPointsBalanceComposer.Compose(Session.CharacterInfo.ActivityPointsBalance, ValueData.ValuePixels));
                }

                if (ValueData.ValueFurni.Count > 0)
                {
                    Dictionary <int, List <uint> > NotifyItems = new Dictionary <int, List <uint> >();

                    foreach (uint ItemId in ValueData.ValueFurni)
                    {
                        Item Item = ItemFactory.CreateItem(MySqlClient, ItemId, Session.CharacterId, string.Empty,
                                                           string.Empty, 0, false);

                        if (Item != null)
                        {
                            int NotifyTabId = Item.Definition.Type == ItemType.WallItem ? 2 : 1;

                            Session.InventoryCache.Add(Item);
                            Session.NewItemsCache.MarkNewItem(MySqlClient, NotifyTabId, Item.Id);

                            if (!NotifyItems.ContainsKey(NotifyTabId))
                            {
                                NotifyItems.Add(NotifyTabId, new List <uint>());
                            }

                            NotifyItems[NotifyTabId].Add(Item.Id);
                        }
                    }

                    if (NotifyItems.Count > 0)
                    {
                        Session.SendData(InventoryRefreshComposer.Compose());
                        Session.SendData(InventoryNewItemsComposer.Compose(new Dictionary <int, List <uint> >(NotifyItems)));
                    }
                }

                MarkVoucherUsed(Code);
                return(true);
            }
        }
        private static void ProcessThread(object state)
        {
            Dictionary <uint, Session> Sessions = SessionManager.Sessions;

            if (Sessions.Count > 0)
            {
                using (SqlDatabaseClient MySqlClient = SqlDatabaseManager.GetClient())
                {
                    foreach (Session Session in Sessions.Values)
                    {
                        if (!Session.Authenticated || Session.CharacterInfo.TimeSinceLastActivityPointsUpdate <= Interval)
                        {
                            continue;
                        }

                        Session.CharacterInfo.UpdateActivityPointsBalance(MySqlClient, Amount);
                        Session.SendData(ActivityPointsBalanceComposer.Compose(Session.CharacterInfo.ActivityPointsBalance, Amount));
                        Session.CharacterInfo.SetLastActivityPointsUpdate(MySqlClient);
                    }
                }
            }
        }
Example #3
0
        private static void ProcessThread()
        {
            try
            {
                int Interval = (int)ConfigManager.GetValue("activitypoints.interval");
                int Amount   = (int)ConfigManager.GetValue("activitypoints.amount");

                Thread.Sleep(60000);

                while (Program.Alive)
                {
                    Dictionary <uint, Session> Sessions = SessionManager.Sessions;

                    if (Sessions.Count > 0)
                    {
                        using (SqlDatabaseClient MySqlClient = SqlDatabaseManager.GetClient())
                        {
                            foreach (Session Session in Sessions.Values)
                            {
                                if (!Session.Authenticated || Session.CharacterInfo.TimeSinceLastActivityPointsUpdate <= Interval)
                                {
                                    continue;
                                }

                                Session.CharacterInfo.UpdateActivityPointsBalance(MySqlClient, Amount);
                                Session.SendData(ActivityPointsBalanceComposer.Compose(Session.CharacterInfo.ActivityPointsBalance, Amount));
                                Session.CharacterInfo.SetLastActivityPointsUpdate(MySqlClient);
                            }
                        }
                    }

                    Thread.Sleep((Interval / 5) * 1000);
                }
            }
            catch (ThreadAbortException) { }
        }
        public static void HandlePurchase(SqlDatabaseClient MySqlClient, Session Session, CatalogItem Item, string ItemFlags)
        {
            lock (mPurchaseSyncRoot)
            {
                string Color           = "ffffff";
                int    TotalCreditCost = Item.CostCredits;
                int    TotalApCost     = Item.CostActivityPoints;

                if (Session.CharacterInfo.CreditsBalance < TotalCreditCost || Session.CharacterInfo.ActivityPointsBalance
                    < TotalApCost)
                {
                    return;
                }

                string[] PetData = null;

                if (Item.PresetFlags.Length > 0)
                {
                    ItemFlags = Item.PresetFlags;
                }
                else
                {
                    switch (Item.Definition.Behavior)
                    {
                    case ItemBehavior.Pet:

                        PetData = ItemFlags.Split('\n');
                        if (PetData.Length != 3)
                        {
                            return;
                        }

                        string Name = PetData[0];
                        Color = PetData[2];

                        int Race = 0;
                        int.TryParse(PetData[1], out Race);

                        bool RaceOk = false;

                        List <PetRaceData> Races = PetDataManager.GetRaceDataForType(Item.Definition.BehaviorData);

                        foreach (PetRaceData RaceData in Races)
                        {
                            if (RaceData.Data1 == Race)
                            {
                                RaceOk = true;
                                break;
                            }
                        }

                        /// if (PetName.VerifyPetName(Name) != PetNameError.NameOk || Color.ToLower() != "ffffff" || !RaceOk)
                        if (PetName.VerifyPetName(Name) != PetNameError.NameOk || !RaceOk)     // WHY COLOR???
                        {
                            return;
                        }

                        break;

                    case ItemBehavior.PrizeTrophy:

                        if (ItemFlags.Length > 255)
                        {
                            ItemFlags = ItemFlags.Substring(0, 255);
                        }

                        ItemFlags = Session.CharacterInfo.Username + Convert.ToChar(9) + DateTime.Now.Day + "-" +
                                    DateTime.Now.Month + "-" + DateTime.Now.Year + Convert.ToChar(9) +
                                    UserInputFilter.FilterString(ItemFlags.Trim(), true);
                        break;

                    default:

                        ItemFlags = string.Empty;
                        break;
                    }
                }

                if (TotalCreditCost > 0)
                {
                    Session.CharacterInfo.UpdateCreditsBalance(MySqlClient, -TotalCreditCost);
                    Session.SendData(CreditsBalanceComposer.Compose(Session.CharacterInfo.CreditsBalance));
                }

                if (TotalApCost > 0)
                {
                    Session.CharacterInfo.UpdateActivityPointsBalance(MySqlClient, -TotalApCost);
                    Session.SendData(ActivityPointsBalanceComposer.Compose(Session.CharacterInfo.ActivityPointsBalance, -TotalApCost));
                }

                Dictionary <int, List <uint> > NewItems = new Dictionary <int, List <uint> >();

                for (int i = 0; i < Item.Amount; i++)
                {
                    switch (Item.Definition.Type)
                    {
                    default:

                        List <Item> GeneratedGenericItems = new List <Item>();
                        double      ExpireTimestamp       = 0;

                        if (Item.Definition.Behavior == ItemBehavior.Rental)
                        {
                            ExpireTimestamp = UnixTimestamp.GetCurrent() + 3600;
                        }

                        GeneratedGenericItems.Add(ItemFactory.CreateItem(MySqlClient, Item.DefinitionId,
                                                                         Session.CharacterId, ItemFlags, ItemFlags, ExpireTimestamp));

                        switch (Item.Definition.Behavior)
                        {
                        case ItemBehavior.Teleporter:

                            Item LinkedItem = ItemFactory.CreateItem(MySqlClient, Item.DefinitionId,
                                                                     Session.CharacterId, GeneratedGenericItems[0].Id.ToString(), string.Empty,
                                                                     ExpireTimestamp);

                            GeneratedGenericItems[0].Flags = LinkedItem.Id.ToString();
                            GeneratedGenericItems[0].SynchronizeDatabase(MySqlClient, true);

                            GeneratedGenericItems.Add(LinkedItem);
                            break;
                        }

                        foreach (Item GeneratedItem in GeneratedGenericItems)
                        {
                            Session.InventoryCache.Add(GeneratedItem);

                            int TabId = GeneratedItem.Definition.Type == ItemType.FloorItem ? 1 : 2;

                            if (!NewItems.ContainsKey(TabId))
                            {
                                NewItems.Add(TabId, new List <uint>());
                            }

                            NewItems[TabId].Add(GeneratedItem.Id);
                        }

                        break;

                    case ItemType.AvatarEffect:

                        AvatarEffect Effect = null;

                        if (Session.AvatarEffectCache.HasEffect((int)Item.Definition.SpriteId))
                        {
                            Effect = Session.AvatarEffectCache.GetEffect((int)Item.Definition.SpriteId);

                            if (Effect != null)
                            {
                                Effect.AddToQuantity();
                            }
                        }
                        else
                        {
                            Effect = AvatarEffectFactory.CreateEffect(MySqlClient, Session.CharacterId, (int)Item.Definition.SpriteId, 3600);
                            Session.AvatarEffectCache.Add(Effect);
                        }

                        if (Effect != null)
                        {
                            Session.SendData(UserEffectAddedComposer.Compose(Effect));
                        }

                        break;

                    case ItemType.Pet:

                        Pet Pet = PetFactory.CreatePet(MySqlClient, Session.CharacterId, Item.Definition.BehaviorData, PetData[0], int.Parse(PetData[1]), Color.ToLower());
                        Session.PetInventoryCache.Add(Pet);

                        Session.SendData(InventoryPetAddedComposer.Compose(Pet));

                        if (!NewItems.ContainsKey(3))
                        {
                            NewItems.Add(3, new List <uint>());
                        }

                        NewItems[3].Add(Pet.Id);

                        break;
                    }
                }

                Session.SendData(CatalogPurchaseResultComposer.Compose(Item));
                Session.SendData(InventoryRefreshComposer.Compose());

                foreach (KeyValuePair <int, List <uint> > NewItemData in NewItems)
                {
                    foreach (uint NewItem in NewItemData.Value)
                    {
                        Session.NewItemsCache.MarkNewItem(MySqlClient, NewItemData.Key, NewItem);
                    }
                }

                if (NewItems.Count > 0)
                {
                    Session.SendData(InventoryNewItemsComposer.Compose(new Dictionary <int, List <uint> >(NewItems)));
                }
            }
        }
Example #5
0
        private static bool HandleFireworks(Session Session, Item Item, RoomInstance Instance, ItemEventType Event, int RequestData, uint Opcode)
        {
            int CurrentCharges = 0;

            int.TryParse(Item.Flags, out CurrentCharges);

            switch (Event)
            {
            case ItemEventType.InstanceLoaded:
            case ItemEventType.Placed:
            case ItemEventType.Moved:
            case ItemEventType.UpdateTick:

                string DesiredDisplayFlags = "0";

                if (CurrentCharges > 0)
                {
                    DesiredDisplayFlags = "1";
                }

                if (Item.DisplayFlags != DesiredDisplayFlags)
                {
                    Item.DisplayFlags = DesiredDisplayFlags;
                    Item.BroadcastStateUpdate(Instance);
                }

                break;

            case ItemEventType.Interact:

                RoomActor Actor = Instance.GetActorByReferenceId(Session.CharacterId);

                if (Actor == null)
                {
                    return(true);
                }

                if (Distance.Calculate(Actor.Position.GetVector2(), Item.RoomPosition.GetVector2()) > 1)
                {
                    Actor.MoveToItemAndInteract(Item, RequestData, Opcode, Item.SquareBehind);
                    return(true);
                }

                switch (RequestData)
                {
                // Purchase charges
                case 2:

                    if (Session.CharacterInfo.CreditsBalance < CHARGE_COSTS_CREDITS)
                    {
                        return(true);
                    }

                    if (Session.CharacterInfo.ActivityPointsBalance < CHARGE_COSTS_PIXELS)
                    {
                        return(true);
                    }

                    bool Update = (CurrentCharges <= 0);
                    CurrentCharges += CHARGE_AMOUNT;

                    Item.Flags = CurrentCharges.ToString();

                    using (SqlDatabaseClient MySqlClient = SqlDatabaseManager.GetClient())
                    {
                        if (CHARGE_COSTS_CREDITS > 0)
                        {
                            Session.CharacterInfo.UpdateCreditsBalance(MySqlClient, -CHARGE_COSTS_CREDITS);
                            Session.SendData(CreditsBalanceComposer.Compose(Session.CharacterInfo.CreditsBalance));
                        }

                        if (CHARGE_COSTS_PIXELS > 0)
                        {
                            Session.CharacterInfo.UpdateActivityPointsBalance(MySqlClient, -CHARGE_COSTS_PIXELS);
                            Session.SendData(ActivityPointsBalanceComposer.Compose(Session.CharacterInfo.ActivityPointsBalance,
                                                                                   -CHARGE_COSTS_PIXELS));
                        }

                        Item.SynchronizeDatabase(MySqlClient, true);
                    }

                    Session.SendData(FireworksChargeInfoComposer.Compose(Item.Id, CurrentCharges, CHARGE_COSTS_CREDITS,
                                                                         CHARGE_COSTS_PIXELS, CHARGE_AMOUNT));

                    if (Update)
                    {
                        Item.DisplayFlags = "1";
                        Item.BroadcastStateUpdate(Instance);
                    }

                    break;

                case 1:

                    Session.SendData(FireworksChargeInfoComposer.Compose(Item.Id, CurrentCharges, CHARGE_COSTS_CREDITS,
                                                                         CHARGE_COSTS_PIXELS, CHARGE_AMOUNT));
                    break;

                default:
                case 0:

                    if (Item.DisplayFlags == "2")
                    {
                        return(true);
                    }

                    if (CurrentCharges > 0)
                    {
                        Item.DisplayFlags = "2";
                        Item.BroadcastStateUpdate(Instance);

                        Item.Flags = (--CurrentCharges).ToString();
                        RoomManager.MarkWriteback(Item, true);

                        Item.RequestUpdate(Item.Definition.BehaviorData);
                    }
                    else
                    {
                        goto case 1;
                    }

                    break;
                }

                break;
            }

            return(true);
        }
Example #6
0
        public static bool ProgressUserAchievement(SqlDatabaseClient MySqlClient, Session Session, string AchievementGroup, int ProgressAmount)
        {
            if (!mAchievements.ContainsKey(AchievementGroup))
            {
                return(false);
            }

            Achievement AchievementData = null;

            lock (mSyncRoot)
            {
                AchievementData = mAchievements[AchievementGroup];
            }

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

            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 = (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;

                Badge BadgeData = RightsManager.GetBadgeByCode(AchievementGroup + TargetLevel);

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

                Session.BadgeCache.UpdateAchievementBadge(MySqlClient, AchievementGroup, BadgeData);
                Session.NewItemsCache.MarkNewItem(MySqlClient, 4, BadgeData.Id);
                Session.SendData(InventoryNewItemsComposer.Compose(4, BadgeData.Id));

                Session.CharacterInfo.UpdateActivityPointsBalance(MySqlClient, TargetLevelData.PixelReward);
                Session.SendData(ActivityPointsBalanceComposer.Compose(Session.CharacterInfo.ActivityPointsBalance,
                                                                       TargetLevelData.PixelReward));

                Session.SendData(AchievementUnlockedComposer.Compose(AchievementData, TargetLevel, TargetLevelData.PointsReward,
                                                                     TargetLevelData.PixelReward));

                Session.AchievementCache.AddOrUpdateData(MySqlClient, AchievementGroup, NewLevel, NewProgress);

                Session.CharacterInfo.UpdateScore(MySqlClient, TargetLevelData.PointsReward);
                Session.SendData(AchievementScoreUpdateComposer.Compose(Session.CharacterInfo.Score));

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

                Session.SendInfoUpdate();

                Session.MessengerFriendCache.BroadcastToFriends(MessengerFriendEventComposer.Compose(Session.CharacterId,
                                                                                                     MessengerFriendEventType.AchievementUnlocked, BadgeData.Code));

                RoomInstance Instance = RoomManager.GetInstanceByRoomId(Session.CurrentRoomId);

                if (Instance != null)
                {
                    Instance.BroadcastMessage(RoomUserBadgesComposer.Compose(Session.CharacterId,
                                                                             Session.BadgeCache.EquippedBadges));
                }

                if (ProgressRemainder > 0)
                {
                    ProgressUserAchievement(MySqlClient, Session, AchievementGroup, ProgressRemainder);
                }

                return(true);
            }

            Session.AchievementCache.AddOrUpdateData(MySqlClient, AchievementGroup, NewLevel, NewProgress);
            Session.SendData(AchievementProgressComposer.Compose(AchievementData, TargetLevel, TargetLevelData,
                                                                 TotalLevels, Session.AchievementCache.GetAchievementData(AchievementGroup)));
            return(false);
        }
Example #7
0
 private static void GetBalance(Session Session, ClientMessage Message)
 {
     Session.SendData(CreditsBalanceComposer.Compose(Session.CharacterInfo.CreditsBalance));
     Session.SendData(ActivityPointsBalanceComposer.Compose(Session.CharacterInfo.ActivityPointsBalance, 0));
 }
Example #8
0
        public static void ProgressUserQuest(Session Session, QuestType QuestType, uint EventData = 0)
        {
            if (Session.QuestCache.CurrentQuestId <= 0)
            {
                return;
            }

            Quest UserQuest = GetQuest(Session.QuestCache.CurrentQuestId);

            if (UserQuest == null || UserQuest.GoalType != QuestType)
            {
                return;
            }

            int  CurrentProgress = Session.QuestCache.GetQuestProgress(UserQuest.Id);
            int  NewProgress     = CurrentProgress;
            bool PassQuest       = false;

            switch (QuestType)
            {
            default:

                NewProgress++;

                if (NewProgress >= UserQuest.GoalData)
                {
                    PassQuest = true;
                }

                break;

            case QuestType.EXPLORE_FIND_ITEM:

                if (EventData != UserQuest.GoalData)
                {
                    return;
                }

                NewProgress = (int)UserQuest.GoalData;
                PassQuest   = true;
                break;
            }

            using (SqlDatabaseClient MySqlClient = SqlDatabaseManager.GetClient())
            {
                Session.QuestCache.AddOrUpdateData(MySqlClient, UserQuest.Id, NewProgress, !PassQuest);

                if (PassQuest)
                {
                    Session.CharacterInfo.UpdateActivityPointsBalance(MySqlClient, UserQuest.Reward);

                    Quest NextQuest = GetNextQuestInSeries(UserQuest.Category, UserQuest.Number + 1);

                    if (NextQuest != null)
                    {
                        Session.QuestCache.AddOrUpdateData(MySqlClient, NextQuest.Id, 0, true);
                    }
                }
            }

            Session.SendData(QuestStartedComposer.Compose(Session, UserQuest));

            if (PassQuest)
            {
                Session.SendData(QuestCompletedComposer.Compose(Session, UserQuest));
                Session.SendData(ActivityPointsBalanceComposer.Compose(Session.CharacterInfo.ActivityPointsBalance,
                                                                       UserQuest.Reward));
                GetList(Session, null);

                Session.MessengerFriendCache.BroadcastToFriends(MessengerFriendEventComposer.Compose(Session.CharacterId,
                                                                                                     MessengerFriendEventType.QuestCompleted, UserQuest.Category + "." + UserQuest.QuestName));
            }
        }