コード例 #1
0
 public bool MatchesEvent(ConstellationLiveEventModel liveEvent)
 {
     if (this.UniqueEventID.Equals(liveEvent.channel))
     {
         if (this.EventType == ConstellationEventTypeEnum.channel__id__followed)
         {
             JToken following;
             if (!liveEvent.payload.TryGetValue("following", out following) || !(bool)following)
             {
                 return(false);
             }
         }
         return(true);
     }
     return(false);
 }
コード例 #2
0
        private async void ConstellationClient_OnSubscribedEventOccurred(object sender, ConstellationLiveEventModel e)
        {
            try
            {
                UserViewModel user     = null;
                bool?         followed = null;
                ChannelModel  channel  = null;

                JToken payloadToken;
                if (e.payload.TryGetValue("user", out payloadToken))
                {
                    user = new UserViewModel(payloadToken.ToObject <UserModel>());

                    JToken subscribeStartToken;
                    if (e.payload.TryGetValue("since", out subscribeStartToken))
                    {
                        user.MixerSubscribeDate = subscribeStartToken.ToObject <DateTimeOffset>();
                    }

                    if (e.payload.TryGetValue("following", out JToken followedToken))
                    {
                        followed = (bool)followedToken;
                    }
                }
                else if (e.payload.TryGetValue("hoster", out payloadToken))
                {
                    channel = payloadToken.ToObject <ChannelModel>();
                    user    = new UserViewModel(channel.userId, channel.token);
                }

                if (user != null)
                {
                    user.UpdateLastActivity();
                }

                if (e.channel.Equals(ConstellationClientWrapper.ChannelUpdateEvent.ToString()))
                {
                    if (e.payload["online"] != null)
                    {
                        bool online = e.payload["online"].ToObject <bool>();
                        user = await ChannelSession.GetCurrentUser();

                        if (online)
                        {
                            if (this.CanUserRunEvent(user, EnumHelper.GetEnumName(OtherEventTypeEnum.MixerChannelStreamStart)))
                            {
                                this.LogUserRunEvent(user, EnumHelper.GetEnumName(OtherEventTypeEnum.MixerChannelStreamStart));
                                await this.RunEventCommand(this.FindMatchingEventCommand(EnumHelper.GetEnumName(OtherEventTypeEnum.MixerChannelStreamStart)), user);
                            }
                        }
                        else
                        {
                            if (this.CanUserRunEvent(user, EnumHelper.GetEnumName(OtherEventTypeEnum.MixerChannelStreamStop)))
                            {
                                this.LogUserRunEvent(user, EnumHelper.GetEnumName(OtherEventTypeEnum.MixerChannelStreamStop));
                                await this.RunEventCommand(this.FindMatchingEventCommand(EnumHelper.GetEnumName(OtherEventTypeEnum.MixerChannelStreamStop)), user);
                            }
                        }
                    }
                }
                else if (e.channel.Equals(ConstellationClientWrapper.ChannelFollowEvent.ToString()))
                {
                    if (followed.GetValueOrDefault())
                    {
                        if (this.CanUserRunEvent(user, ConstellationClientWrapper.ChannelFollowEvent.ToString()))
                        {
                            this.LogUserRunEvent(user, ConstellationClientWrapper.ChannelFollowEvent.ToString());

                            foreach (UserCurrencyViewModel currency in ChannelSession.Settings.Currencies.Values)
                            {
                                user.Data.AddCurrencyAmount(currency, currency.OnFollowBonus);
                            }

                            await this.RunEventCommand(this.FindMatchingEventCommand(e.channel), user);
                        }

                        GlobalEvents.FollowOccurred(user);
                    }
                    else
                    {
                        if (this.CanUserRunEvent(user, EnumHelper.GetEnumName(OtherEventTypeEnum.MixerUserUnfollow)))
                        {
                            this.LogUserRunEvent(user, EnumHelper.GetEnumName(OtherEventTypeEnum.MixerUserUnfollow));
                            await this.RunEventCommand(this.FindMatchingEventCommand(EnumHelper.GetEnumName(OtherEventTypeEnum.MixerUserUnfollow)), user);
                        }

                        GlobalEvents.UnfollowOccurred(user);
                    }
                }
                else if (e.channel.Equals(ConstellationClientWrapper.ChannelHostedEvent.ToString()))
                {
                    int viewerCount = 0;
                    if (channel != null)
                    {
                        viewerCount = (int)channel.viewersCurrent;
                    }

                    if (this.CanUserRunEvent(user, ConstellationClientWrapper.ChannelHostedEvent.ToString()))
                    {
                        this.LogUserRunEvent(user, ConstellationClientWrapper.ChannelHostedEvent.ToString());

                        foreach (UserCurrencyViewModel currency in ChannelSession.Settings.Currencies.Values)
                        {
                            user.Data.AddCurrencyAmount(currency, currency.OnHostBonus);
                        }

                        EventCommand command = this.FindMatchingEventCommand(e.channel);
                        if (command != null)
                        {
                            Dictionary <string, string> specialIdentifiers = new Dictionary <string, string>()
                            {
                                { "hostviewercount", viewerCount.ToString() }
                            };
                            await this.RunEventCommand(command, user, extraSpecialIdentifiers : specialIdentifiers);
                        }

                        GlobalEvents.HostOccurred(new Tuple <UserViewModel, int>(user, viewerCount));
                    }
                }
                else if (e.channel.Equals(ConstellationClientWrapper.ChannelSubscribedEvent.ToString()))
                {
                    if (this.CanUserRunEvent(user, ConstellationClientWrapper.ChannelSubscribedEvent.ToString()))
                    {
                        this.LogUserRunEvent(user, ConstellationClientWrapper.ChannelSubscribedEvent.ToString());

                        user.MixerSubscribeDate = DateTimeOffset.Now;
                        foreach (UserCurrencyViewModel currency in ChannelSession.Settings.Currencies.Values)
                        {
                            user.Data.AddCurrencyAmount(currency, currency.OnSubscribeBonus);
                        }

                        await this.RunEventCommand(this.FindMatchingEventCommand(e.channel), user);
                    }

                    GlobalEvents.SubscribeOccurred(user);
                }
                else if (e.channel.Equals(ConstellationClientWrapper.ChannelResubscribedEvent.ToString()) || e.channel.Equals(ConstellationClientWrapper.ChannelResubscribedSharedEvent.ToString()))
                {
                    int resubMonths = 0;
                    if (e.payload.TryGetValue("totalMonths", out JToken resubMonthsToken))
                    {
                        resubMonths = (int)resubMonthsToken;
                    }

                    if (this.CanUserRunEvent(user, ConstellationClientWrapper.ChannelResubscribedEvent.ToString()))
                    {
                        this.LogUserRunEvent(user, ConstellationClientWrapper.ChannelResubscribedEvent.ToString());

                        foreach (UserCurrencyViewModel currency in ChannelSession.Settings.Currencies.Values)
                        {
                            user.Data.AddCurrencyAmount(currency, currency.OnSubscribeBonus);
                        }

                        Dictionary <string, string> specialIdentifiers = new Dictionary <string, string>()
                        {
                            { "usersubmonths", resubMonths.ToString() }
                        };
                        await this.RunEventCommand(this.FindMatchingEventCommand(ConstellationClientWrapper.ChannelResubscribedEvent.ToString()), user, extraSpecialIdentifiers : specialIdentifiers);

                        GlobalEvents.ResubscribeOccurred(new Tuple <UserViewModel, int>(user, resubMonths));
                    }
                }
                else if (e.channel.Equals(ConstellationClientWrapper.ChannelSubscriptionGiftedEvent.ToString()))
                {
                    if (e.payload.TryGetValue("gifterId", out JToken gifterID) && e.payload.TryGetValue("giftReceiverId", out JToken receiverID))
                    {
                        UserModel gifterUserModel = await ChannelSession.Connection.GetUser(gifterID.ToObject <uint>());

                        UserModel receiverUserModel = await ChannelSession.Connection.GetUser(receiverID.ToObject <uint>());

                        if (gifterUserModel != null && receiverUserModel != null)
                        {
                            UserViewModel gifterUser   = new UserViewModel(gifterUserModel);
                            UserViewModel receiverUser = new UserViewModel(receiverUserModel);

                            EventCommand command = this.FindMatchingEventCommand(e.channel);
                            if (command != null)
                            {
                                await this.RunEventCommand(command, gifterUser, arguments : new List <string>()
                                {
                                    receiverUser.UserName
                                });
                            }

                            GlobalEvents.SubscriptionGiftedOccurred(gifterUser, receiverUser);
                        }
                    }
                }
                else if (e.channel.Equals(ConstellationClientWrapper.ProgressionLevelupEvent.ToString()))
                {
                    if (e.payload.TryGetValue("userId", out JToken userID))
                    {
                        UserModel userModel = await ChannelSession.Connection.GetUser(userID.ToObject <uint>());

                        if (userModel != null)
                        {
                            UserViewModel           userViewModel  = new UserViewModel(userModel);
                            UserFanProgressionModel fanProgression = e.payload.ToObject <UserFanProgressionModel>();
                            if (fanProgression != null)
                            {
                                EventCommand command = this.FindMatchingEventCommand(e.channel);
                                if (command != null)
                                {
                                    Dictionary <string, string> specialIdentifiers = new Dictionary <string, string>()
                                    {
                                        { "userfanprogressionnext", fanProgression.level.nextLevelXp.ToString() },
                                        { "userfanprogressionrank", fanProgression.level.level.ToString() },
                                        { "userfanprogressioncolor", fanProgression.level.color.ToString() },
                                        { "userfanprogressionimage", fanProgression.level.LargeGIFAssetURL.ToString() },
                                        { "userfanprogression", fanProgression.level.currentXp.ToString() },
                                    };
                                    await this.RunEventCommand(command, userViewModel, extraSpecialIdentifiers : specialIdentifiers);
                                }

                                GlobalEvents.ProgressionLevelUpOccurred(userViewModel);
                            }
                        }
                    }
                }
                else if (e.channel.Equals(ConstellationClientWrapper.ChannelSkillEvent.ToString()))
                {
                    if (e.payload["triggeringUserId"] != null)
                    {
                        uint userID = e.payload["triggeringUserId"].ToObject <uint>();
                        user = await ChannelSession.ActiveUsers.GetUserByID(userID);

                        if (user != null)
                        {
                            user = new UserViewModel(await ChannelSession.Connection.GetUser(userID));
                        }
                    }

                    SkillModel skill = null;
                    if (e.payload["skillId"] != null)
                    {
                        Guid skillID = e.payload["skillId"].ToObject <Guid>();
                        if (this.availableSkills.ContainsKey(skillID))
                        {
                            skill = this.availableSkills[skillID];
                        }
                    }

                    if (skill == null)
                    {
                        if (e.payload["manifest"] != null && e.payload["manifest"]["name"] != null)
                        {
                            string skillName = e.payload["manifest"]["name"].ToString();
                            if (skillName.Equals("beachball"))
                            {
                                skill = this.availableSkills.Values.FirstOrDefault(s => s.name.Equals("Beach Ball"));
                            }
                        }
                    }

                    uint price = e.payload["price"].ToObject <uint>();
                    if (user != null)
                    {
                        if (price > 0)
                        {
                            GlobalEvents.SparkUseOccurred(new Tuple <UserViewModel, int>(user, (int)price));
                        }

                        if (skill != null)
                        {
                            JObject            manifest      = (JObject)e.payload["manifest"];
                            JObject            parameters    = (JObject)e.payload["parameters"];
                            SkillInstanceModel skillInstance = new SkillInstanceModel(skill, manifest, parameters);

                            GlobalEvents.SkillUseOccurred(new SkillUsageModel(user, skillInstance));
                        }
                    }
                }
                else if (e.channel.Equals(ConstellationClientWrapper.ChannelPatronageUpdateEvent.ToString()))
                {
                    PatronageStatusModel patronageStatus = e.payload.ToObject <PatronageStatusModel>();
                    if (patronageStatus != null)
                    {
                        GlobalEvents.PatronageUpdateOccurred(patronageStatus);

                        bool milestoneUpdateOccurred = await this.patronageMilestonesSemaphore.WaitAndRelease(() =>
                        {
                            return(Task.FromResult(this.remainingPatronageMilestones.RemoveAll(m => m.target <= patronageStatus.patronageEarned) > 0));
                        });

                        if (milestoneUpdateOccurred)
                        {
                            PatronageMilestoneModel milestoneReached = this.allPatronageMilestones.OrderByDescending(m => m.target).FirstOrDefault(m => m.target <= patronageStatus.patronageEarned);
                            if (milestoneReached != null)
                            {
                                GlobalEvents.PatronageMilestoneReachedOccurred(milestoneReached);

                                Dictionary <string, string> specialIdentifiers = new Dictionary <string, string>()
                                {
                                    { SpecialIdentifierStringBuilder.MilestoneSpecialIdentifierHeader + "amount", milestoneReached.target.ToString() },
                                    { SpecialIdentifierStringBuilder.MilestoneSpecialIdentifierHeader + "reward", milestoneReached.DollarAmountText() },
                                };
                                await this.RunEventCommand(this.FindMatchingEventCommand(EnumHelper.GetEnumName(OtherEventTypeEnum.MixerMilestoneReached)), await ChannelSession.GetCurrentUser(), extraSpecialIdentifiers : specialIdentifiers);
                            }
                        }
                    }
                }

                if (this.OnEventOccurred != null)
                {
                    this.OnEventOccurred(this, e);
                }
            }
            catch (Exception ex) { Util.Logger.Log(ex); }
        }
コード例 #3
0
        private async void ConstellationClient_OnSubscribedEventOccurred(object sender, ConstellationLiveEventModel e)
        {
            UserViewModel user     = null;
            bool?         followed = null;
            ChannelModel  channel  = null;

            JToken userToken;

            if (e.payload.TryGetValue("user", out userToken))
            {
                user = new UserViewModel(userToken.ToObject <UserModel>());

                JToken subscribeStartToken;
                if (e.payload.TryGetValue("since", out subscribeStartToken))
                {
                    user.SubscribeDate = subscribeStartToken.ToObject <DateTimeOffset>();
                }

                if (e.payload.TryGetValue("following", out JToken followedToken))
                {
                    followed = (bool)followedToken;
                }
            }
            else if (e.payload.TryGetValue("hoster", out userToken))
            {
                channel = userToken.ToObject <ChannelModel>();
                user    = new UserViewModel(channel.id, channel.token);
            }

            if (e.channel.Equals(ConstellationClientWrapper.ChannelUpdateEvent.ToString()))
            {
                if (e.payload["online"] != null)
                {
                    bool online = e.payload["online"].ToObject <bool>();
                    user = ChannelSession.GetCurrentUser();
                    if (online)
                    {
                        if (this.CanUserRunEvent(user, EnumHelper.GetEnumName(OtherEventTypeEnum.MixerChannelStreamStart)))
                        {
                            this.LogUserRunEvent(user, EnumHelper.GetEnumName(OtherEventTypeEnum.MixerChannelStreamStart));
                            await this.RunEventCommand(this.FindMatchingEventCommand(EnumHelper.GetEnumName(OtherEventTypeEnum.MixerChannelStreamStart)), user);
                        }
                    }
                    else
                    {
                        if (this.CanUserRunEvent(user, EnumHelper.GetEnumName(OtherEventTypeEnum.MixerChannelStreamStop)))
                        {
                            this.LogUserRunEvent(user, EnumHelper.GetEnumName(OtherEventTypeEnum.MixerChannelStreamStop));
                            await this.RunEventCommand(this.FindMatchingEventCommand(EnumHelper.GetEnumName(OtherEventTypeEnum.MixerChannelStreamStop)), user);
                        }
                    }
                }
            }
            else if (e.channel.Equals(ConstellationClientWrapper.ChannelFollowEvent.ToString()))
            {
                if (followed.GetValueOrDefault())
                {
                    if (this.CanUserRunEvent(user, ConstellationClientWrapper.ChannelFollowEvent.ToString()))
                    {
                        this.LogUserRunEvent(user, ConstellationClientWrapper.ChannelFollowEvent.ToString());

                        foreach (UserCurrencyViewModel currency in ChannelSession.Settings.Currencies.Values)
                        {
                            user.Data.AddCurrencyAmount(currency, currency.OnFollowBonus);
                        }

                        if (this.OnFollowOccurred != null)
                        {
                            this.OnFollowOccurred(this, user);
                        }

                        await this.RunEventCommand(this.FindMatchingEventCommand(e.channel), user);
                    }
                }
                else
                {
                    if (this.CanUserRunEvent(user, EnumHelper.GetEnumName(OtherEventTypeEnum.MixerUserUnfollow)))
                    {
                        this.LogUserRunEvent(user, EnumHelper.GetEnumName(OtherEventTypeEnum.MixerUserUnfollow));
                        await this.RunEventCommand(this.FindMatchingEventCommand(EnumHelper.GetEnumName(OtherEventTypeEnum.MixerUserUnfollow)), user);
                    }

                    if (this.OnUnfollowOccurred != null)
                    {
                        this.OnUnfollowOccurred(this, user);
                    }
                }
            }
            else if (e.channel.Equals(ConstellationClientWrapper.ChannelHostedEvent.ToString()))
            {
                if (this.CanUserRunEvent(user, ConstellationClientWrapper.ChannelHostedEvent.ToString()))
                {
                    this.LogUserRunEvent(user, ConstellationClientWrapper.ChannelHostedEvent.ToString());

                    int viewerCount = 0;
                    if (channel != null)
                    {
                        viewerCount = (int)channel.viewersCurrent;
                    }

                    foreach (UserCurrencyViewModel currency in ChannelSession.Settings.Currencies.Values)
                    {
                        user.Data.AddCurrencyAmount(currency, currency.OnHostBonus);
                    }

                    if (this.OnHostedOccurred != null)
                    {
                        this.OnHostedOccurred(this, new Tuple <UserViewModel, int>(user, viewerCount));
                    }

                    EventCommand command = this.FindMatchingEventCommand(e.channel);
                    if (command != null)
                    {
                        command.AddSpecialIdentifier("hostviewercount", viewerCount.ToString());
                        await this.RunEventCommand(command, user);
                    }
                }
            }
            else if (e.channel.Equals(ConstellationClientWrapper.ChannelSubscribedEvent.ToString()))
            {
                if (this.CanUserRunEvent(user, ConstellationClientWrapper.ChannelSubscribedEvent.ToString()))
                {
                    this.LogUserRunEvent(user, ConstellationClientWrapper.ChannelSubscribedEvent.ToString());

                    user.SubscribeDate = DateTimeOffset.Now;
                    foreach (UserCurrencyViewModel currency in ChannelSession.Settings.Currencies.Values)
                    {
                        user.Data.AddCurrencyAmount(currency, currency.OnSubscribeBonus);
                    }

                    if (this.OnSubscribedOccurred != null)
                    {
                        this.OnSubscribedOccurred(this, user);
                    }

                    await this.RunEventCommand(this.FindMatchingEventCommand(e.channel), user);
                }
            }
            else if (e.channel.Equals(ConstellationClientWrapper.ChannelResubscribedEvent.ToString()) || e.channel.Equals(ConstellationClientWrapper.ChannelResubscribedSharedEvent.ToString()))
            {
                if (this.CanUserRunEvent(user, ConstellationClientWrapper.ChannelResubscribedEvent.ToString()))
                {
                    this.LogUserRunEvent(user, ConstellationClientWrapper.ChannelResubscribedEvent.ToString());

                    foreach (UserCurrencyViewModel currency in ChannelSession.Settings.Currencies.Values)
                    {
                        user.Data.AddCurrencyAmount(currency, currency.OnSubscribeBonus);
                    }

                    int resubMonths = 0;
                    if (e.payload.TryGetValue("totalMonths", out JToken resubMonthsToken))
                    {
                        resubMonths = (int)resubMonthsToken;
                    }

                    if (this.OnResubscribedOccurred != null)
                    {
                        this.OnResubscribedOccurred(this, new Tuple <UserViewModel, int>(user, resubMonths));
                    }

                    await this.RunEventCommand(this.FindMatchingEventCommand(ConstellationClientWrapper.ChannelResubscribedEvent.ToString()), user);
                }
            }

            if (this.OnEventOccurred != null)
            {
                this.OnEventOccurred(this, e);
            }
        }
コード例 #4
0
        private async void ConstellationClient_OnSubscribedEventOccurred(object sender, ConstellationLiveEventModel e)
        {
            ChannelModel  channel = null;
            UserViewModel user    = null;

            JToken userToken;

            if (e.payload.TryGetValue("user", out userToken))
            {
                user = new UserViewModel(userToken.ToObject <UserModel>());

                JToken subscribeStartToken;
                if (e.payload.TryGetValue("since", out subscribeStartToken))
                {
                    user.SubscribeDate = subscribeStartToken.ToObject <DateTimeOffset>();
                }
            }
            else if (e.payload.TryGetValue("hoster", out userToken))
            {
                channel = userToken.ToObject <ChannelModel>();
                user    = new UserViewModel(channel.id, channel.token);
            }

            if (user != null)
            {
                UserDataViewModel userData = ChannelSession.Settings.UserData.GetValueIfExists(user.ID, new UserDataViewModel(user));

                if (e.channel.Equals(ConstellationClientWrapper.ChannelFollowEvent.ToString()))
                {
                    foreach (UserCurrencyViewModel currency in ChannelSession.Settings.Currencies.Values)
                    {
                        userData.SetCurrencyAmount(currency, currency.OnFollowBonus);
                    }
                }
                else if (e.channel.Equals(ConstellationClientWrapper.ChannelHostedEvent.ToString()))
                {
                    foreach (UserCurrencyViewModel currency in ChannelSession.Settings.Currencies.Values)
                    {
                        userData.SetCurrencyAmount(currency, currency.OnHostBonus);
                    }
                }
                else if (e.channel.Equals(ConstellationClientWrapper.ChannelSubscribedEvent.ToString()) || e.channel.Equals(ConstellationClientWrapper.ChannelResubscribedEvent.ToString()) ||
                         e.channel.Equals(ConstellationClientWrapper.ChannelResubscribedSharedEvent.ToString()))
                {
                    foreach (UserCurrencyViewModel currency in ChannelSession.Settings.Currencies.Values)
                    {
                        userData.SetCurrencyAmount(currency, currency.OnSubscribeBonus);
                    }
                }

                if (e.channel.Equals(ConstellationClientWrapper.ChannelSubscribedEvent.ToString()))
                {
                    user.SubscribeDate = DateTimeOffset.Now;
                }
            }

            if (e.channel.Equals(ConstellationClientWrapper.ChannelUpdateEvent.ToString()))
            {
                IDictionary <string, JToken> payloadValues = e.payload;
                if (payloadValues.ContainsKey("online") && (bool)payloadValues["online"])
                {
                    UptimeChatCommand.SetUptime(DateTimeOffset.Now);
                }
            }
            else
            {
                foreach (EventCommand command in ChannelSession.Settings.EventCommands)
                {
                    EventCommand foundCommand = null;

                    if (command.MatchesEvent(e))
                    {
                        foundCommand = command;
                    }

                    if (command.EventType == ConstellationEventTypeEnum.channel__id__subscribed && e.channel.Equals(ConstellationClientWrapper.ChannelResubscribeSharedEvent.ToString()))
                    {
                        foundCommand = command;
                    }

                    if (foundCommand != null)
                    {
                        if (command.EventType == ConstellationEventTypeEnum.channel__id__hosted && channel != null)
                        {
                            foundCommand.AddSpecialIdentifier("hostviewercount", channel.viewersCurrent.ToString());
                        }

                        if (user != null)
                        {
                            await foundCommand.Perform(user);
                        }
                        else
                        {
                            await foundCommand.Perform();
                        }

                        return;
                    }
                }
            }

            if (this.OnEventOccurred != null)
            {
                this.OnEventOccurred(this, e);
            }
        }