private async Task <int[]> GetNumberOfWinsAndLosses(Player player)
        {
            string  jsonString;
            dynamic responseInJson;
            bool    isError = false;

            do
            {
                try
                {
                    isError    = false;
                    jsonString = await NetComm.GetResponseOfURL($"players/{player.SteamId}/wl", _httpClient);

                    responseInJson = JsonToFrom.Deserialize <dynamic>(jsonString);
                    int[] winAndLose = new int[2] {
                        responseInJson.win, responseInJson.lose
                    };
                    return(winAndLose);
                }
                catch (Exception)
                {
                    isError = true;
                    Thread.Sleep(10 * 1000);
                }
            } while (isError);
            return(new int[] { 0, 0 });
        }
Example #2
0
        private async Task <string> GetStatsAsync(Player player, double timeInHours)
        {
            string url            = $"players/{player.SteamId}/wl?date={timeInHours / 24}";
            var    responseInJson = await NetComm.GetResponseOfURL(url);

            dynamic response = responseInJson.Deserialize <dynamic>();
            int     wins     = response.win;
            int     losses   = response.lose;
            string  content  = $"<@{player.DiscordId}>'s score: {wins}/{losses} in last {timeInHours} hours.";

            return(content);
        }
Example #3
0
        private async Task <string> GetMedal(ulong id)
        {
            string steamId    = PlayerConfiguration.Players.Where(x => x.DiscordId.Equals(id.ToString())).FirstOrDefault().SteamId;
            var    url        = $"players/{steamId}";
            var    jsonString = await NetComm.GetResponseOfURL(url, _httpClient);

            var    playerInfo  = JsonToFrom.Deserialize <dynamic>(jsonString);
            string playerMedal = (string)playerInfo.rank_tier;

            if (string.IsNullOrEmpty(playerMedal))
            {
                return($"<@{id}> is uncalibrated.");
            }
            var actualMedal = (Medal)((int)char.GetNumericValue(playerMedal[0]) - 1);
            var star        = (int)char.GetNumericValue(playerMedal[1]);

            return($"<@{id}> is {actualMedal}[{star}].");
        }
        private async Task FindAndDisplayTotalMatchesPlayed()
        {
            var channel = await Program.Discord.GetChannelAsync(BotDetails.BotFeedChannel);

            foreach (var player in PlayerConfiguration.Players)
            {
                var jsonString = await NetComm.GetResponseOfURL($"players/{player.SteamId}/wl?date=1", _httpClient);

                var responseInJson = JsonToFrom.Deserialize <dynamic>(jsonString);
                int wins           = responseInJson.win;
                int losses         = responseInJson.lose;
                if (wins + losses > 0)
                {
                    string content = $"<@{player.DiscordId}> won {wins} game{GetPlural(wins)} out of {wins + losses} game{GetPlural(wins + losses)} today.";
                    channel.SendMessageAsync(content);
                }
            }
        }
        public async Task <string> ProcessAsync(DiscordMessage message)
        {
            string url        = $"players/{message.Author.SteamId()}/heroes?limit={_matchesLimit}&having={_havingAtleastMatches}";
            var    jsonString = await NetComm.GetResponseOfURL(url);

            var        heroDetails    = JsonToFrom.Deserialize <dynamic>(jsonString);
            List <int> randomedHeroId = GetRandomedHeroIds(heroDetails);

            string toReturn = string.Empty;

            toReturn += $"<@{message.Author.Id}> Here are some recent heroes which you have played:\n";
            for (int i = 0; i < randomedHeroId.Count; i += _delimiter)
            {
                toReturn += '`';
                for (int j = i; j < i + _delimiter && j < randomedHeroId.Count; j++)
                {
                    string currentHeroName = HeroDetails.GetHeroById(randomedHeroId[j]).localized_name;
                    toReturn += $"{currentHeroName}" + GetSpacesForHeroName(currentHeroName);
                }
                toReturn += "`\n";
            }

            return(toReturn);
        }
        public override async void Run(DiscordClient discord)
        {
            _botTestingChannel = await discord.GetChannelAsync(BotDetails.BotFeedChannel);

            var listOfPlayers = PlayerConfiguration.Players;

            _httpClient.BaseAddress = new Uri(OpenDotaConfiguration.OpenDotaAPIAddress);
            foreach (var player in listOfPlayers)
            {
                player.TotalMatches = await GetTotalMatches(player);
            }
            while (true)
            {
                try
                {
                    Program.DumpLogger.Log("Fetching Wins and Losses for players.");
                    Dictionary <string, List <Player> > matchIdToPlayersMapping = new Dictionary <string, List <Player> >();
                    foreach (var player in listOfPlayers)
                    {
                        var currentMatches = await GetTotalMatches(player);

                        if (currentMatches != player.TotalMatches)
                        {
                            int extraGames = currentMatches - player.TotalMatches;
                            player.TotalMatches = currentMatches;

                            var jsonString = await NetComm.GetResponseOfURL($"players/{player.SteamId}/matches?limit=1", _httpClient);

                            dynamic lastMatch = JsonToFrom.Deserialize <dynamic>(jsonString)[0];
                            string  matchId   = lastMatch.match_id;
                            if (!matchIdToPlayersMapping.ContainsKey(matchId))
                            {
                                matchIdToPlayersMapping[matchId] = new List <Player>();
                            }

                            matchIdToPlayersMapping[matchId].Add(player);
                        }
                    }
                    foreach (var matchId in matchIdToPlayersMapping.Keys)
                    {
                        string matchDetailsString = await NetComm.GetResponseOfURL($"matches/{matchId}", _httpClient);

                        dynamic matchDetails   = JsonToFrom.Deserialize <dynamic>(matchDetailsString);
                        string  normalOrRanked = GetNormalOrRankedMatch(matchDetails);
                        string  reply          = string.Empty;
                        foreach (var player in matchIdToPlayersMapping[matchId])
                        {
                            reply += GenerateMatchString(matchDetails, normalOrRanked, player);
                        }
                        reply += GenerateDotaBuffLink(matchId);
                        await _botTestingChannel.SendMessageAsync(reply);

                        Program.Logger.Log($"Match Details logged for match id: {matchId}");
                    }
                }
                catch (Exception e)
                {
                    Program.DumpLogger.Log($"Exception in {GetType().Name}\nMessage: {e.Message}\nStack Trace: {e.StackTrace}");
                    Thread.Sleep(30 * 1000);
                    continue;
                }
                Thread.Sleep(5 * 60 * 1000);
            }
        }
        public override async void Run(DiscordClient discord)
        {
            var channel = await discord.GetChannelAsync(BotDetails.BotFeedChannel);

            var lastMessage = (await channel.GetMessagesAsync(1)).FirstOrDefault();
            var matchId     = Regex.Match(lastMessage.Content, $"[0-9]+$").Value;

            if (matchId == null || matchId == string.Empty)
            {
                do
                {
                    var lastNMessage = (await channel.GetMessagesAsync(1, before: lastMessage.Id)).FirstOrDefault();
                    matchId     = Regex.Match(lastNMessage.Content, $"[0-9]+$").Value;
                    lastMessage = lastNMessage;
                } while (matchId == null || matchId == string.Empty);
            }

            var matchDetailsString = KeyValueCache.Get(matchId);

            if (matchDetailsString == null)
            {
                matchDetailsString = await NetComm.GetResponseOfURL($"matches/{matchId}", _httpClient);

                KeyValueCache.Put(matchId, matchDetailsString);
            }
            dynamic matchDetails = JsonToFrom.Deserialize <dynamic>(matchDetailsString);
            long    matchEndTime = (long)matchDetails.start_time + (long)matchDetails.duration;
            double  diffInTime   = GetDiffInDays(matchEndTime);

            foreach (var player in PlayerConfiguration.Players)
            {
                var recentMatches = JsonToFrom.Deserialize <dynamic>(await NetComm.GetResponseOfURL($"players/{player.SteamId}/matches?date={diffInTime}", _httpClient));
                foreach (var recentMatch in recentMatches)
                {
                    string recentMatchId = recentMatch.match_id;
                    if (!_matchMap.ContainsKey(recentMatchId))
                    {
                        _matchMap[recentMatchId] = new List <Player>();
                    }
                    _matchMap[recentMatchId].Add(player);
                }
            }
            var _matchMapOrdered = _matchMap.OrderBy(x => x.Key);

            foreach (var matchMap in _matchMapOrdered)
            {
                if (KeyValueCache.Get(matchMap.Key) == null)
                {
                    KeyValueCache.Put(matchMap.Key, await NetComm.GetResponseOfURL($"matches/{matchMap.Key}", _httpClient));
                }
                dynamic parsedMatch = JsonToFrom.Deserialize <dynamic>(KeyValueCache.Get(matchMap.Key));
                string  matchString = string.Empty;
                foreach (var player in matchMap.Value)
                {
                    matchString += GenerateMatchString(parsedMatch, GetNormalOrRankedMatch(parsedMatch), player);
                }
                matchString += GenerateDotaBuffLink(matchMap.Key);
                channel.SendMessageAsync(matchString);
                Program.Logger.Log($"Offline Tracker logged match {matchMap.Key}.");
            }
        }