Ejemplo n.º 1
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);
            }
        }