-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
132 lines (109 loc) · 3.98 KB
/
Program.cs
File metadata and controls
132 lines (109 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
namespace Dota2Meta
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.SystemTextJson;
using Stratz.Types;
using System.Linq;
using System.IO;
using CultureInfo = System.Globalization.CultureInfo;
using Json = System.Text.Json.JsonSerializer;
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Dota2Meta, updating..");
// Get Config
Console.WriteLine("Reading config");
string cfgPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json");
var config = Config.ParseFromPath(cfgPath);
// Setup GQL
var gql = new GraphQLHttpClient("https://api.stratz.com/graphql", new SystemTextJsonSerializer());
var stratz = new Stratz.StratzProvider(gql);
// Deserialize existing grid
Console.WriteLine("Deserializing current hero grid");
var heroGrid = Json.Deserialize<Dota2.PickScreenLayout>(File.ReadAllText(config.gridPath));
// Remove existing grids
Console.WriteLine("Removing previous grids");
heroGrid.configs.RemoveAll(c => c.createdByDota2Meta);
int configNum = 1;
foreach (var grid in config.gridConfig)
{
Console.WriteLine($"Creating config {configNum}/{config.gridConfig.Count} ({grid.name})");
int bracketNum = 1;
foreach (var bracket in grid.brackets)
{
Console.WriteLine($"Creating grid for {bracket} bracket ({bracketNum}/{grid.brackets.Count})");
var currBracket = Enum.Parse<RankBracket>(bracket.ToUpper());
// Make the config name
string configName = grid.name
.Replace("$bracket$", CultureInfo.CurrentCulture.TextInfo.ToTitleCase(currBracket.ToString().ToLower()))
.Replace("$version$", await stratz.GetLatestVersionNumber())
.Replace("$shortdate$", DateTime.Now.ToShortDateString())
.Replace("$longdate$", DateTime.Now.ToLongDateString());
var pickConfig = new Dota2.Config();
pickConfig.config_name = configName;
pickConfig.createdByDota2Meta = true;
// Convert position to enum values
var positions = new List<MatchPlayerPositionType>();
grid.positions.ForEach(p => positions.Add(Enum.Parse<MatchPlayerPositionType>(p)));
double xPos = 0;
foreach (var pos in positions)
{
var c = new Dota2.Category();
// Create a better looking name based on the enum text
c.category_name = CultureInfo.CurrentCulture.TextInfo
.ToTitleCase(pos.ToString().ToLower())
.Replace('_', ' ');
c.x_position = xPos;
c.y_position = 0;
c.width = grid.width;
c.height = grid.height;
var wins = (await stratz.GetWinWeekForPosition(pos, currBracket, GameModeEnumType.ALL_PICK_RANKED))
.Where(w => w.matchCount > 100)
.OrderByDescending(w => w.WinRate)
.Take(grid.topWins);
c.hero_ids.AddRange(wins.Select(c => (int)c.heroId));
pickConfig.categories.Add(c);
xPos = c.x_position + c.width + 5;
}
// Add custom rows (if any)
if(grid.custom?.Count > 0)
{
foreach(var custom in grid.custom)
{
// Skip if section is defined, but has no heroes
if(custom.heroes.Count == 0) continue;
Console.WriteLine($"Adding custom grid '{custom.name}'");
var c = new Dota2.Category();
c.category_name = custom.name;
c.x_position = xPos;
c.y_position = 0;
c.width = grid.width;
c.height = grid.height;
foreach(var h in custom.heroes)
{
var hero = await stratz.GetHeroByDisplayName(h);
if(hero != null)
{
c.hero_ids.Add(hero.id);
}
}
pickConfig.categories.Add(c);
xPos = c.x_position + c.width + 5;
}
}
// Add config to the grid menu
heroGrid.configs.Add(pickConfig);
bracketNum++;
}
configNum++;
}
File.WriteAllText(config.gridPath, System.Text.Json.JsonSerializer.Serialize(heroGrid));
Console.WriteLine("Done, press any key to exit");
Console.ReadKey();
}
}
}