public void FetchClientList(string mode, Action <string> messageCallback) { int leaderboardId = LeaderboardIds[mode]; string baseUrl = "https://aoe2.net/api/leaderboard?game=aoe2de&leaderboard_id={leaderboard}&start={start}&count={increment}"; HttpClient client = new HttpClient(); int startPosition = 1; int increment = 1000; Elos.AllPlayersElos[mode] = new List <Tuple <int, string> >(); bool isDone = false; while (!isDone) { string fetchUrl = baseUrl.Replace("{leaderboard}", leaderboardId.ToString()).Replace("{start}", startPosition.ToString()).Replace("{increment}", increment.ToString()); HttpResponseMessage response = client.GetAsync(fetchUrl).Result; string content = response.Content.ReadAsStringAsync().Result; AoeLeaderboardResponse parsed = JsonConvert.DeserializeObject <AoeLeaderboardResponse>(content); foreach (AoePlayer aoePlayer in parsed.Leaderboard) { Elos.AllPlayersElos[mode].Add(Tuple.Create(aoePlayer.Rating.Value, aoePlayer.SteamId)); } Parent.LogMessage($"[*] <{mode}>: Loaded rank #{startPosition} - #{startPosition + parsed.Count}"); if (parsed.Count != increment) { messageCallback.Invoke($"<{mode}>: Loaded rank #1 - #{startPosition + parsed.Count}"); } if (parsed.Count != increment) { isDone = true; } else { startPosition += increment; } } }
public static void HandleStatsRequest(string nameSearch, int leaderboardId, Action <string> messageCallback) { AoeLeaderboardResponse response = GetLeaderboardResponse(leaderboardId, nameSearch, 2); if (response.Count == 0) { messageCallback.Invoke($"There were no players found on leaderboard '{leaderboardId}' with the name '{nameSearch}'"); return; } else if (response.Count > 1) { messageCallback.Invoke($"There were multiple players found on leaderboard '{leaderboardId}' with the name '{nameSearch}'"); return; } AoePlayer player = response.Leaderboard[0]; string clanAddition = player.Clan == null ? "" : $"[{player.Clan}]"; string leaderboardName = Leaderboards.First(kv => kv.Value == leaderboardId).Key; int ratingDiff = player.Rating.Value - player.PreviousRating.Value; string ratingDiffStr = ratingDiff < 0 ? ColorCoder.ErrorBright(ratingDiff) : ratingDiff > 0 ? ColorCoder.SuccessDim(ratingDiff) : $"{ratingDiff}"; double ratioPercent = (double)player.Wins.Value * 100 / player.Games.Value; string ratioPercentStr = $"{ratioPercent:0.##}%"; ratioPercentStr = ratioPercent < 50 ? ColorCoder.ErrorBright(ratioPercentStr) : ratioPercent > 50 ? ColorCoder.SuccessDim(ratioPercentStr) : $"{ratioPercentStr}"; string toPrint = $"{ColorCoder.Bold(leaderboardName.ToUpper())} stats for {ColorCoder.Bold(player.Name+clanAddition)}:"; toPrint += $"\n\tRating: {ColorCoder.Bold(player.Rating.Value)} ({ratingDiffStr}) (#{player.Rank})"; toPrint += $"\n\tPeak Rating: {player.HighestRating}"; toPrint += $"\n\tStreak: {player.Streak} | Lowest: {player.LowestStreak} | Highest: {player.HighestStreak}"; toPrint += $"\n\tGames: {player.Games} ({ColorCoder.SuccessDim(player.Wins.Value+"W")} - {ColorCoder.ErrorBright(player.Losses.Value+"L")} | {ratioPercentStr})"; toPrint += $"\n\tCountry: {player.Country}"; messageCallback.Invoke(toPrint); }
public static void HandleLastMatchRequest(string nameSearch, int leaderboardId, Action <string> messageCallback) { AoeLeaderboardResponse response = GetLeaderboardResponse(leaderboardId, nameSearch, 2); AoePlayer player; if (response.Count == 0) { messageCallback.Invoke($"There were no players found on leaderboard '{leaderboardId}' with the name '{nameSearch}'"); return; } else if (response.Count > 1) { messageCallback.Invoke($"There were multiple players found on leaderboard '{leaderboardId}' with the name '{nameSearch}', taking the highest rated player..."); player = response.Leaderboard[0]; } else { player = response.Leaderboard[0]; } AoeLastMatchResponse lastMatchResponse = GetLastMatchResponse(player.SteamId); Match lastMatch = lastMatchResponse.LastMatch; Dictionary <int, List <List <AoePlayer> > > matchTeams = new Dictionary <int, List <List <AoePlayer> > >(); foreach (AoePlayer matchPlayer in lastMatch.Players) { if (!matchTeams.ContainsKey(matchPlayer.Team.Value)) { matchTeams.Add(matchPlayer.Team.Value, new List <List <AoePlayer> >()); } List <List <AoePlayer> > team = matchTeams[matchPlayer.Team.Value]; List <AoePlayer> playerInfo = new List <AoePlayer>(); playerInfo.Add(matchPlayer); if (matchPlayer.SteamId != player.SteamId) { AoeLeaderboardResponse lb = GetLeaderboardResponse(leaderboardId, matchPlayer.ProfileId.Value.ToString(), 2, isProfileId: true); if (lb.Leaderboard.Count == 0) { playerInfo.Add(null); } else { playerInfo.Add(lb.Leaderboard[0]); } } else { playerInfo.Add(player); } team.Add(playerInfo); } string toPrint = ""; toPrint += lastMatch.Finished.HasValue ? "Last Match (" + (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(lastMatch.Finished.Value).ToLocalTime()).ToString("dd.MM.yy HH:mm") + "):" : "Current Match:"; toPrint += lastMatch.Ranked.HasValue && lastMatch.Ranked.Value ? " Ranked" : " Unranked"; string mapName = GetAoeString("map_type", lastMatch.MapType.Value); toPrint += $" on {mapName}"; bool firstTeam = true; foreach (int teamPos in matchTeams.Keys) { List <List <AoePlayer> > team = matchTeams[teamPos]; string winStatusString; if (!team[0][0].Won.HasValue) { winStatusString = "◯"; } else if (team[0][0].Won.Value) { winStatusString = "👑"; } else { winStatusString = "☠"; } if (firstTeam) { firstTeam = false; } else { toPrint += "\n\t\tvs."; } foreach (List <AoePlayer> playerInfo in team) { string playerName = playerInfo[0].Name; string civ = GetAoeString("civ", playerInfo[0].Civ.Value); if (playerName.ToLower().Contains(nameSearch.ToLower())) { playerName = ColorCoder.Bold(playerName); } if (playerInfo[1] == null) //Player is not yet ranked in this leaderboard { string line = $"\n{winStatusString} [--] {playerName} (<placements>) on {civ}"; toPrint += line; } else { string country = playerInfo[1].Country; string rating = playerInfo[1].Rating.HasValue ? $"{playerInfo[1].Rating.Value}" : "<placement>"; int games = playerInfo[1].Games.Value; int wins = playerInfo[1].Wins.Value; double ratioPercent = Math.Round((double)wins * 100 / games); string ratioPercentStr = $"{ratioPercent}%"; ratioPercentStr = ratioPercent < 50 ? ColorCoder.ErrorBright(ratioPercentStr) : ratioPercent > 50 ? ColorCoder.SuccessDim(ratioPercentStr) : $"{ratioPercentStr}"; string line = $"\n{winStatusString} [{country}] {playerName} ({rating}, {games} G, {ratioPercentStr}) on {civ}"; toPrint += line; } } } messageCallback.Invoke(toPrint); }