Skip to content

Instantly share code, notes, and snippets.

@NikolayIT
Last active July 1, 2018 14:51
Show Gist options
  • Save NikolayIT/a243a1e6bed5ec03959ad7a9330473ab to your computer and use it in GitHub Desktop.
Save NikolayIT/a243a1e6bed5ec03959ad7a9330473ab to your computer and use it in GitHub Desktop.
Tracking changes in Dota 2 leaderboard pages
namespace DotaLeaderboardsUpdates
{
using System;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using Newtonsoft.Json;
public static class Program
{
private const string EuropeLeaderboard =
"http://www.dota2.com/webapi/ILeaderboard/GetDivisionLeaderboard/v0001?division=europe";
private const int PlayersToTrack = 100;
private static Leaderboard lastLeaderboard = null;
private static HttpClient httpClient = new HttpClient();
public static void Main(string[] args)
{
Console.OutputEncoding = Encoding.Unicode;
var lastLeaderboard = GetLeaderboard();
PrintTopPlayers(lastLeaderboard, PlayersToTrack);
while (true)
{
Thread.Sleep(60 * 1000);
var leaderboard = GetLeaderboard();
if (leaderboard.time_posted != lastLeaderboard.time_posted)
{
Console.WriteLine(new string('-', 60));
Console.WriteLine(DateTime.Now);
Console.WriteLine(new string('-', 60));
}
foreach (var player in leaderboard.leaderboard.Take(PlayersToTrack))
{
var lastPlayer = lastLeaderboard.leaderboard.FirstOrDefault(
x => x.team_id == player.team_id && x.name == player.name);
if (player.rank != lastPlayer?.rank)
{
PrintPlayer(player);
Console.WriteLine($" from {lastPlayer?.rank} to {player.rank}");
}
}
lastLeaderboard = leaderboard;
}
}
private static void PrintTopPlayers(Leaderboard leaderboard, int topPlayers)
{
Console.WriteLine($"Top {topPlayers}:");
foreach (var player in leaderboard.leaderboard.Take(topPlayers))
{
Console.Write($"{player.rank:D2}. ");
PrintPlayer(player);
Console.WriteLine();
}
}
private static Leaderboard GetLeaderboard()
{
var response = httpClient.GetAsync(EuropeLeaderboard).GetAwaiter().GetResult();
var json = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var leaderboard = JsonConvert.DeserializeObject<Leaderboard>(json);
return leaderboard;
}
private static void PrintPlayer(Player player)
{
if (!string.IsNullOrWhiteSpace(player.team_tag))
{
PrintColor($"{player.team_tag}.", ConsoleColor.DarkGray);
}
PrintColor(player.name, ConsoleColor.Green);
if (!string.IsNullOrWhiteSpace(player.sponsor))
{
PrintColor($".{player.sponsor}", ConsoleColor.DarkGray);
}
if (!string.IsNullOrWhiteSpace(player.country))
{
PrintColor($" ({player.country})", ConsoleColor.DarkGreen);
}
}
private static void PrintColor(string text, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.Write(text);
Console.ResetColor();
}
}
public class Leaderboard
{
public int time_posted { get; set; }
public int next_scheduled_post_time { get; set; }
public int server_time { get; set; }
public Player[] leaderboard { get; set; }
}
public class Player
{
public int rank { get; set; }
public string name { get; set; }
public int team_id { get; set; }
public string team_tag { get; set; }
public string sponsor { get; set; }
public string country { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment