Skip to content

Commit baa4ea2

Browse files
committed
Issue1: Added RealmId to the config file format. Added a Version code as well that is now 1 for the current version. Older versions will start at 0, which is the default if the Version element is not present in the file. I also added a small flow that will update the existing config file to the new version and use default values for the new RealmId field (1). The readme has been updated to reflect the new version.
1 parent b8e269b commit baa4ea2

File tree

6 files changed

+152
-10
lines changed

6 files changed

+152
-10
lines changed

Config.json.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
2+
"Version": 1,
23
"MsPerRead": 5000,
34
"DataDirectory": "",
45
"MmrFilePath": "mmr.txt",
56
"RegionId": "US",
7+
"RealmId": 1,
68
"ProfileId": 1986271,
79
"LadderId": 274006,
810
"ClientId": "GetThisFromTheBlizzardDeveloperApiSite",

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,26 @@ It requires a Blizzard API key which you can get for free but will need to creat
88
To get started, create a file called "Config.json" (or whatever you want) and copy the contents of "Config.json.example" into it.
99
Then change the settings to your preferences and put in your own ClientId and ClientSecret.
1010
This is what the different settings are and how to set them:
11-
* "MsPerRead": This is how often to ask for MMR updates in milliseconds. I found that MMR typically takes 10 seconds or so anyway to be updated on Blizzard's server so this doesn't have to be too quick.
12-
* "DataDirectory": This is a directory to store temp files that the application needs. It can be a path relative to the executable or an absolute path. "" just means put the files in the same directory as the executable.
13-
* "MmrFilePath": This is a file path to put the MMR in. If the file doesn't exist it is created. If they file exists it is overwritten. The path can be relative to the executable or an absolute path.
14-
* "RegionId": This is the region to use. It can be "US", "EU", "KO" or "CN".
15-
* "ProfileId": This is the ID of the profile you want to get the MMR for. You can find this by navigating to Starcraft2.com, log into your Blizzard account and click View Profile. The ProfileId will be the last number in the URL at the top. For example: in "https://starcraft2.com/en-us/profile/1/1/1986271", the ProfileId is 1986271.
16-
* "LadderId": This is the ID of the particular Ladder you want the MMR for (since you have separate MMR for each race and for team games). To find this, go to Ladders on the Blizzard account profile site mentioned above and click the ladder you would like to get the MMR for. The LadderId will be the last number in the URL at the top. For example: in "https://starcraft2.com/en-us/profile/1/1/1986271/ladders?ladderId=274006", the LadderId is 274006.
17-
* "ClientId": You will need to sign up for a Blizzard API key to get a ClientId and ClientSecret. This will give you access to Blizzard's API for access to ladder information. To do this, go to https://develop.battle.net/access and log in with your Blizzard account. If you don't have an Authenticator set up, it will make you do this first. Once you're logged in, create a new Client. Click on the Client and it will tell you the ClientId and ClientSecret. Just copy/paste them in.
18-
* "ClientSecret": See instructions for ClientId above.
11+
* **Version**
12+
This identifies the version of the config file. If any parameters are added, removed or changed this will be incremented. Users don't have to change this unless they are manually upgrading to the next version.
13+
* **MsPerRead**
14+
This is how often to ask for MMR updates in milliseconds. I found that MMR typically takes 10 seconds or so anyway to be updated on Blizzard's server so this doesn't have to be too quick.
15+
* **DataDirectory**
16+
This is a directory to store temp files that the application needs. It can be a path relative to the executable or an absolute path. "" just means put the files in the same directory as the executable.
17+
* **MmrFilePath**
18+
This is a file path to put the MMR in. If the file doesn't exist it is created. If they file exists it is overwritten. The path can be relative to the executable or an absolute path.
19+
* **RegionId**
20+
This is the region to use. It can be "US", "EU", "KO" or "CN".
21+
* **RealmId**
22+
This is the realm ID of the profile and can be 1 or 2. You can see what it is for your account by logging into your Blizzard account on Startcraft2.com, clicking View Profile and taking the number in the [RealmId] position in the URL: "ht<span>tps://starcraft2.com/en-us/profile/</span>[RegionId]/**[RealmId]**/...". For example, the realm ID is "1" in the following URL: "ht<span>tps://starcraft2.com/en-us/profile/2/</span>**1**/1986271"
23+
* **ProfileId**
24+
This is the ID of the profile you want to get the MMR for. You can find this by navigating to Starcraft2.com, log into your Blizzard account and click View Profile. The ProfileId will be the last number in the URL at the top. For example: in "https://starcraft2.com/en-us/profile/1/1/1986271", the ProfileId is 1986271.
25+
* **LadderId**
26+
This is the ID of the particular Ladder you want the MMR for (since you have separate MMR for each race and for team games). To find this, go to Ladders on the Blizzard account profile site mentioned above and click the ladder you would like to get the MMR for. The LadderId will be the last number in the URL at the top. For example: in "https://starcraft2.com/en-us/profile/1/1/1986271/ladders?ladderId=274006", the LadderId is 274006.
27+
* **ClientId**
28+
You will need to sign up for a Blizzard API key to get a ClientId and ClientSecret. This will give you access to Blizzard's API for access to ladder information. To do this, go to https://develop.battle.net/access and log in with your Blizzard account. If you don't have an Authenticator set up, it will make you do this first. Once you're logged in, create a new Client. Click on the Client and it will tell you the ClientId and ClientSecret. Just copy/paste them in.
29+
* **ClientSecret**
30+
See instructions for ClientId above.
1931

2032
# Running
2133
To run the program, either double click on the exe or run this from command line:

Sc2MmrReader/MmrReader.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Sc2MmrReader {
1010
/// <summary>
11-
/// Reads the MMR of kirby every "msPerRead" milliseconds into the file at the given path.
11+
/// Reads the MMR of the given profile every "MsPerRead" milliseconds into the file at the given path.
1212
/// </summary>
1313
public class MmrReader {
1414
/// <summary>
@@ -89,7 +89,7 @@ private void RefreshMmr() {
8989
// Build the URL
9090
String url = "https://us.api.blizzard.com/sc2/profile/";
9191
url += _regionIdMap[_configuration.RegionId] + "/";
92-
url += "1/"; // Realm ID - not sure what this is in SC2
92+
url += _configuration.RealmId + "/";
9393
url += _configuration.ProfileId + "/";
9494
url += "ladder/";
9595
url += _configuration.LadderId;
@@ -107,6 +107,13 @@ private void RefreshMmr() {
107107
if (ex.InnerException != null) {
108108
Console.WriteLine("\tInnerException: " + ex.InnerException.Message);
109109
}
110+
111+
// Since we had an exception, try refreshing the Auth token as well.
112+
try {
113+
File.Delete(_accessCacheFile);
114+
} catch (Exception deleteEx) {
115+
Console.WriteLine("After a response error, could not delete the access cache file: " + deleteEx.Message);
116+
}
110117
}
111118

112119
if (succeeded) {

Sc2MmrReader/Program.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,48 @@ private static ReaderConfig ReadConfigFile(String path) {
3636
return configFile;
3737
}
3838

39+
/// <summary>
40+
/// Saves the given config file to the given path.
41+
/// </summary>
42+
/// <param name="config">The config to write.</param>
43+
/// <param name="path">The path to save the config file to</param>
44+
/// <returns>Returns true if it succeeded, false if there was an error.</returns>
45+
private static bool SaveConfigFile(ReaderConfig config, String path) {
46+
JavaScriptSerializer serializer = new JavaScriptSerializer();
47+
String configString = null;
48+
try {
49+
configString = serializer.Serialize(config);
50+
} catch (Exception ex) {
51+
Console.WriteLine("Could not serialize the given config.");
52+
Console.WriteLine("Exception: " + ex.Message);
53+
if (ex.InnerException != null) {
54+
Console.WriteLine("Inner Exception: " + ex.InnerException.Message);
55+
}
56+
57+
return false;
58+
}
59+
60+
// Do a poor mans prettify since JavaScriptSerializer doesn't support
61+
// formatting the output. This could be improved by using an actual library like JSON.net
62+
configString = configString.Insert(configString.IndexOf("{") + 1, Environment.NewLine + "\t");
63+
configString = configString.Insert(configString.LastIndexOf("}"), Environment.NewLine);
64+
configString = configString.Replace("\":", "\": ");
65+
configString = configString.Replace(",\"", "," + Environment.NewLine + "\t\"");
66+
67+
// Write it to a file
68+
try {
69+
File.WriteAllText(path, configString);
70+
} catch (Exception ex) {
71+
Console.WriteLine("Could not write the config file to " + path);
72+
Console.WriteLine("Exception: " + ex.Message);
73+
if (ex.InnerException != null) {
74+
Console.WriteLine("Inner Exception: " + ex.InnerException.Message);
75+
}
76+
}
77+
78+
return true;
79+
}
80+
3981
/// <summary>
4082
/// Application entry point. Reads the config file and starts the MmrReader.
4183
/// </summary>
@@ -54,6 +96,47 @@ public static void Main(string[] args) {
5496
ReaderConfig config = ReadConfigFile(configFilePath);
5597
if (config == null) {
5698
Console.WriteLine("There was a problem reading the config file.");
99+
Console.WriteLine("Press any key to exit.");
100+
Console.ReadKey();
101+
return;
102+
}
103+
104+
// Check the version of the config file
105+
if (config.Version < ReaderConfig.ReaderConfigVersion) {
106+
Console.WriteLine("The specified config file is an older version. See Config.json.example for the current format.");
107+
Console.WriteLine("Would you like Sc2MmrReader to attempt to upgrade to the new format? New parameters will get default values.");
108+
109+
String response = "";
110+
while (response != "yes" && response != "no") {
111+
Console.WriteLine("Enter yes or no:");
112+
response = Console.ReadLine();
113+
}
114+
115+
// Just quit if they said no. They can set the config settings themselves.
116+
if (response == "no") {
117+
return;
118+
}
119+
120+
// Otherwise update the config file
121+
config.Version = ReaderConfig.ReaderConfigVersion;
122+
if (SaveConfigFile(config, configFilePath)) {
123+
Console.WriteLine("The config file has been updated. Please check that the settings are correct and then restart Sc2MmrReader. The path is:");
124+
Console.WriteLine("\t" + configFilePath);
125+
Console.WriteLine("Press any key to exit.");
126+
Console.ReadKey();
127+
return;
128+
} else {
129+
Console.WriteLine("There was a problem saving the config file. Please update it manually.");
130+
Console.WriteLine("Press any key to exit.");
131+
Console.ReadKey();
132+
return;
133+
}
134+
135+
} else if (config.Version > ReaderConfig.ReaderConfigVersion) {
136+
// If it's too new, tell them to update
137+
Console.WriteLine("The specified config file version is for a new version of Sc2MmrReader. Please update to the latest version or downgrade your config file to version " + ReaderConfig.ReaderConfigVersion);
138+
Console.WriteLine("Press any key to exit.");
139+
Console.ReadKey();
57140
return;
58141
}
59142

Sc2MmrReader/ReaderConfig.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@ namespace Sc2MmrReader {
66
/// deserialized into it. So if you rename or change parameters here, you must upgrade the config file format as well.
77
/// </summary>
88
public class ReaderConfig {
9+
// This is the current version and should always be incremented when changing the config file format.
10+
public static int ReaderConfigVersion = 1;
11+
12+
// Version
13+
public int Version { get; set; } = 0; // This identifies the version of the config file. If it's not set it will be at 0.
14+
15+
// Config settings:
16+
public long MsPerRead { get; set; } // How many milliseconds inbetween reads
17+
public String DataDirectory { get; set; } // Where to store cached files
18+
public String MmrFilePath { get; set; } // Where to output the MMR to
19+
20+
// Information about the account to get the MMR for:
21+
public String RegionId { get; set; } // Can be "US", "EU", "KO" or "CN"
22+
public int RealmId { get; set; } = 1; // Can be 1 or 2. You get this from the link below ".../profile/<regionId>/<realmId>/...". This defaults to 1 for backwards compatibility.
23+
public long ProfileId { get; set; } // Example: https://starcraft2.com/en-us/profile/1/1/1986271/ladders?ladderId=274006, profile ID is 1986271
24+
public long LadderId { get; set; } // In the above link, the LadderId is 274006
25+
26+
// Information about who is connecting:
27+
public String ClientId { get; set; } // Get this by making a developer account for the Blizzard API.
28+
public String ClientSecret { get; set; } // Same as above.
29+
}
30+
31+
// Keep track of old config versions in case we want to be
32+
// able to update old versions in a smarter way.
33+
#region OldConfigVersions
34+
public class ReaderConfigV0 {
935
// Config settings:
1036
public long MsPerRead { get; set; } // How many milliseconds inbetween reads
1137
public String DataDirectory { get; set; } // Where to store cached files
@@ -20,4 +46,5 @@ public class ReaderConfig {
2046
public String ClientId { get; set; } // Get this by making a developer account for the Blizzard API.
2147
public String ClientSecret { get; set; } // Same as above.
2248
}
49+
#endregion
2350
}

Sc2MmrReader/Sc2MmrReader.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,16 @@
5050
<Compile Include="Properties\AssemblyInfo.cs" />
5151
<Compile Include="ReaderConfig.cs" />
5252
</ItemGroup>
53+
<ItemGroup>
54+
<None Include="Config.json.example">
55+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
56+
</None>
57+
<None Include="LICENSE">
58+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
59+
</None>
60+
<None Include="README.md">
61+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
62+
</None>
63+
</ItemGroup>
5364
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5465
</Project>

0 commit comments

Comments
 (0)