/// <summary> /// GetFollowers method helper /// </summary> /// <param name="theCache">Cache to help follower tracking</param> /// <param name="helixdata">Helix API data</param> /// <param name="i">index of the helixdata during recursion</param> /// <param name="size">size of cache</param> /// <param name="followerCollection">List of Twitch user id</param> /// <returns>List of Twitch user id that are new</returns> private List <int> FollowTaskHelper(FollowerCache theCache, HelixData helixdata, int i, int size, List <int> followerCollection) { if (i < size && !theCache.containsUser(helixdata.data[i].from_id)) { FollowTaskHelper(theCache, helixdata, i + 1, size, followerCollection); theCache.addFollower(helixdata.data[i].from_id); followerCollection.Add(helixdata.data[i].from_id); } return(followerCollection); }
/// <summary> /// Takes Twitch Helix API data and attempts to update chatUsers if the user exists. /// </summary> /// <param name="theData">Twitch Helix API data to pull from</param> public void UpdateChatUser(HelixData theData) { foreach (HelixData.GenericHelixObject item in theData.data) { User tempUser; if (chatUsers.TryGetValue(item.login, out tempUser)) { User convertedUser = item.convertToUser(); User.lazyUpdate(tempUser, convertedUser, out convertedUser); chatUsers.TryUpdate(convertedUser.name, convertedUser, chatUsers[convertedUser.name]); } } }
/// <summary> /// Starts a recursive call to FollowTaskHelper to identify new followers from /// Twitch Helix /// </summary> /// <param name="helixdata">Helix API data to sort through</param> /// <returns>List of Twitch user id that are new to channel</returns> public List <int> GetFollowers(HelixData helixdata) { int checkSize = followsCache.checkSize >= helixdata.data.Count() ? followsCache.checkSize : helixdata.data.Count(); //Debug.WriteLine($"helixdata count is {helixdata.data.Count()} and CheckSize of {checkSize}"); List <int> newFollowers = FollowTaskHelper(followsCache, helixdata, 0, checkSize, new List <int>()); if (!followsCache.setupComplete) { followsCache.setupComplete = true; } else { return(newFollowers); } return(null); }
/// <summary> /// Calls to Twitch Helix API and deseralizes to HelixData object. /// </summary> /// <param name="url">Specified url</param> /// <param name="channel">Target channel</param> /// <param name="callType">The API call type</param> /// <returns>Deserialized HelixData Object</returns> public async Task <HelixData> helixCall(string url, string channel, ApiCallType callType) { if (rateRemaining == 0) { Debug.WriteLine("Could not execute call. No more rate remaining."); return(null); } else { string result = ""; HelixData helixData = null; Debug.WriteLine($"Calling to {baseUrl}{url}"); try { using (HttpResponseMessage response = await httpClient.GetAsync(baseUrl + url)) using (HttpContent content = response.Content) { rateRemaining = Int32.Parse(response.Headers.GetValues("ratelimit-remaining").FirstOrDefault()); Debug.WriteLine($"Rate left: {rateRemaining}"); result = await content.ReadAsStringAsync(); Debug.WriteLine("Api call resulted in the following: " + result.Substring(0, 50) + "..."); helixData = JsonConvert.DeserializeObject <HelixData>(result); if (helixData.data.Count() > 0 && callType != ApiCallType.NoCall) { onHelixCallArgs?.Invoke(this, new OnHelixCallArgs { response = helixData, type = callType, channel = channel }); } } return(helixData); } catch (Exception e) { Debug.WriteLine(e.StackTrace); } return(helixData); } }
/// <summary> /// This eventhandler captures the Twitch Helix call results and routes it appropriately /// to the next required step. /// </summary> /// <param name="sender">Required but not utilized; this will be the TwitchHelix object.</param> /// <param name="e">Callback results for next steps.</param> private void onHelixCall(Object sender, OnHelixCallArgs e) { //Debug.WriteLine($"hhhhhhhhhhhh onHelixCall thread is {Thread.CurrentThread.ManagedThreadId} hhhhhhhhhhhh"); switch (e.type) { case ApiCallType.FollowerCall: List <int> newFollowers = channelArray[e.channel].GetFollowers(e.response); if (newFollowers != null && newFollowers.Count > 0) { StringBuilder urlString = new StringBuilder("users?"); foreach (int item in newFollowers) { urlString.Append("&id=" + item); } Task <HelixData> userCallData = twitchHelix.helixCall(urlString.ToString(), e.channel, ApiCallType.NoCall); HelixData userData = userCallData.Result; List <User> newFollowerIRCUsers = new List <User>(); foreach (HelixData.GenericHelixObject item in userData.data) { UsersToUpdate.Enqueue(new MessageInfo(item.login, e.channel, "") { header = IRCMessageType.FOLLOWER }); newFollowerIRCUsers.Add(new User(item.login) { followed_at = item.followed_at, userId = item.id, displayName = item.display_name }); } channelArray[e.channel].confirmActiveAddRecents(newFollowerIRCUsers); } break; case ApiCallType.OverwatchHeroCall: if (e.response != null && e.response.data.Count() != 0 && e.response.data[0].overwatch != null) { if (e.response.data[0].overwatch.broadcaster != null) { Hero theHero = e.response.data[0].overwatch.broadcaster.hero; if (theHero != null) { Hero[] heroArray; bool heroUpdated = channelArray[e.channel].setCurrentHero(theHero, out heroArray); if (heroUpdated && !(heroArray[0] == null && heroArray != null && channelArray[e.channel].lastHero == heroArray[1].name)) { string messageToSend = ""; if (heroArray[0] != null) { messageToSend += $" played {heroArray[0].name} before " + $"with {heroArray[0].returnDuration()} played time"; } if (heroArray[1] != null) { if (messageToSend != "") { messageToSend += " and"; } messageToSend += $" is now playing the hero {heroArray[1].name}."; } if (messageToSend != "") { messageToSend = e.channel + messageToSend; string[] enqueueMessage = new string[] { "akiraion", messageToSend }; irc.sendQ.Enqueue(enqueueMessage); } } } else { Hero[] heroArr; channelArray[e.channel].setCurrentHero(null, out heroArr); } } } break; case ApiCallType.DoesFollowerCall: if (e.response.data.Count() > 0) { StringBuilder urlString = new StringBuilder("users?"); foreach (HelixData.GenericHelixObject item in e.response.data) { urlString.Append("&id=" + item.from_id); } Task <HelixData> userCallData = twitchHelix.helixCall(urlString.ToString(), e.channel, ApiCallType.NoCall); HelixData userData = userCallData.Result; channelArray[e.channel].UpdateChatUser(userData); } break; } updateUserView(); }