From 162fe4825d4ec51f9c74f0a36062928654252f09 Mon Sep 17 00:00:00 2001 From: GameboySR Date: Tue, 20 Dec 2022 00:14:12 +0100 Subject: [PATCH] fixed update cycle functionality --- SSU/Config.cs | 129 +++++++++++++++++++++++++++++-------------------- SSU/Program.cs | 12 ++++- 2 files changed, 87 insertions(+), 54 deletions(-) diff --git a/SSU/Config.cs b/SSU/Config.cs index e156083..79029e1 100644 --- a/SSU/Config.cs +++ b/SSU/Config.cs @@ -33,12 +33,14 @@ public class Config ///private bool _verifiedOnly = false; | verifiedonly=true /// public Dictionary Parameters = new Dictionary(); - public List Range = new List(); + public List Range = new List( new List { "", "", ""} ); private bool _configParsed = false; private bool _emulator = true; private bool _verifiedOnly = false; private readonly string _path; + private List _leaderboards = new List(); + private Dictionary _resetParameters = new Dictionary(); private readonly string LEADERBOARD_LINK = @"https://www.speedrun.com/api/v1/leaderboards/"; private readonly string LEVEL_LINK = @"https://www.speedrun.com/api/v1/levels/"; @@ -63,10 +65,8 @@ public Config(string path, ref HttpClient client) /// Maybe later /// /// A list of processed leaderboards data. - public List Parse() + public void Parse() { - List result = new List(); - using (var fileStream = File.OpenRead(_path)) { using (var reader = new StreamReader(fileStream, Encoding.UTF8, true)) @@ -90,6 +90,11 @@ public Config(string path, ref HttpClient client) if (line.StartsWith("[LEADERBOARDS]")) { _configParsed = true; + // save an untouched copy for next runs + _resetParameters = Parameters.ToDictionary( + keyValue => keyValue.Key, + keyValue => keyValue.Value + ); // check if necessary parts of config are there if (!Parameters.ContainsKey("game") || @@ -113,21 +118,15 @@ public Config(string path, ref HttpClient client) { _verifiedOnly = true; } - - // get range for Spreadsheet call - // first to values are beginning and end of the row range - // third value is the primary time cell index within all cells - var sorted = Parameters.Values.Where(value => char.IsUpper(value[0]) && char.IsDigit(value[1])).ToList(); - sorted.Sort(); - Range.Add(sorted[0]); - Range.Add(sorted[sorted.Count - 1]); + + GetRange(); int index = 0; foreach (var pair in Parameters) { if (pair.Key == Parameters["primarytime"]) { - Range.Add(index.ToString()); + Range[2] = index.ToString(); break; } index++; @@ -135,68 +134,92 @@ public Config(string path, ref HttpClient client) Console.WriteLine("Emulator runs: " + (_emulator ? "enabled." : "disabled.")); Console.WriteLine("Verfied-only runs: " + (_verifiedOnly ? "enabled." : "disabled.")); - Console.WriteLine("Config successfully parsed.\n\n====================================\n"); + Console.WriteLine("\n====================================\n"); continue; } ParseConfig(line); continue; } - var leaderboard = FetchLeaderboard(line); - - if (leaderboard == null || leaderboard.data == null) - { - Console.WriteLine("Skipping leaderboard…"); - result.Add(null); - continue; - } - - GetFastestRun(leaderboard); - leaderboard.data.level = FormatLeaderboardName(leaderboard.data.level); - - if (leaderboard.data.runs == null) - { - Console.WriteLine("No elligible runs for " + leaderboard.data.level); - result.Add(null); - continue; - } - - FormatLeaderboard(leaderboard.data.runs[0]); - result.Add(leaderboard); - Console.WriteLine(leaderboard.data.level + " successully formatted."); + _leaderboards.Add(line); } - Console.WriteLine("All elligible leaderboards successfully formatted.\n\n====================================\n"); + Console.WriteLine("Configuration loaded.\n\n====================================\n"); } } - - return result; } /// /// Makes 3 attempts at fetching the leaderboard info, - /// waiting 5 and 10 seconds between each attempt + /// waiting 5 and 10 seconds between each attempt. /// - /// Leaderboard ID - /// - private LeaderboardData? FetchLeaderboard(string line) + /// Formatted list of the fastest elligible leaderboard runs. + public List FetchLeaderboards() { - for (int i = 1; i < 3; i++) + List result = new List(); + LeaderboardData? leaderboard = null; + + foreach (string line in _leaderboards) { - var leaderboard = SendRequest(LEADERBOARD_LINK + BuildLeaderboardRequestParams(line)) - .Content.ReadAsAsync().Result; + // try to fetch each leaderboard 3 times max + for (int i = 1; i < 3; i++) + { + leaderboard = SendRequest(LEADERBOARD_LINK + BuildLeaderboardRequestParams(line)) + .Content.ReadAsAsync().Result; + + if (leaderboard == null || leaderboard.data == null) + { + Console.WriteLine("Failed to fetch leaderboard: " + line); + Console.WriteLine("Retrying in " + 5 * i + " seconds…"); + Thread.Sleep(5 * i * 1000); + continue; + } + + GetFastestRun(leaderboard); + leaderboard.data.level = FormatLeaderboardName(leaderboard.data.level); + + if (leaderboard.data.runs == null) + { + Console.WriteLine("No elligible runs for " + leaderboard.data.level); + result.Add(null); + break; + } + + FormatLeaderboard(leaderboard.data.runs[0]); + result.Add(leaderboard); + Console.WriteLine(leaderboard.data.level + " successully formatted."); + break; + } if (leaderboard == null || leaderboard.data == null) { - Console.WriteLine("Failed to fetch leaderboard: " + line); - Console.WriteLine("Retrying in " + 5 * i + " seconds…"); - Thread.Sleep(5 * i * 1000); - continue; + Console.WriteLine("Skipping leaderboard…"); + result.Add(null); } - - return leaderboard; } - return null; + Console.WriteLine("\nAll elligible leaderboards fetched.\n\n====================================\n"); + return result; + } + + public void GetRange() + { + // get range for Spreadsheet call + // first to values are beginning and end of the row range + // third value is the primary time cell index within all cells + var sorted = Parameters.Values.Where(value => char.IsUpper(value[0]) && char.IsDigit(value[1])).ToList(); + sorted.Sort(); + Range[0] = sorted[0]; + Range[1] = sorted[sorted.Count - 1]; + } + + public void ResetParameters() + { + // get the starting columns back + Parameters.Clear(); + Parameters = _resetParameters.ToDictionary( + keyValue => keyValue.Key, + keyValue => keyValue.Value + ); } /// diff --git a/SSU/Program.cs b/SSU/Program.cs index ca1d830..567d2b9 100644 --- a/SSU/Program.cs +++ b/SSU/Program.cs @@ -11,6 +11,9 @@ class Program public static void Main(string[] args) { Console.OutputEncoding = System.Text.Encoding.UTF8; + Console.WriteLine("\nSpeedrun Spreadsheet Updater"); + Console.WriteLine("\n====================================\n"); + string configPath = ""; int timer = 60 * 60 * 1000; // 60 minutes - 3 600 seconds - 3 600 000 miliseconds @@ -31,6 +34,7 @@ public static void Main(string[] args) HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); Config config = new Config(configPath, ref client); + config.Parse(); MainLoop(ref client, ref config, timer); } @@ -41,7 +45,7 @@ private static void MainLoop(ref HttpClient client, ref Config config, int timer { try { - var leaderboards = config.Parse(); + var leaderboards = config.FetchLeaderboards(); GoogleSheetsClient sheetClient = new GoogleSheetsClient(config.Parameters["spreadsheet"]); foreach (var leaderboard in leaderboards) @@ -63,6 +67,12 @@ private static void MainLoop(ref HttpClient client, ref Config config, int timer " has been updated.\n\n" + "Next update will be at " + DateTime.Now.AddMilliseconds(timer) + "\n\n====================================\n"); + + // resetting cell-related variables + // back to the starting cells from the config file + config.ResetParameters(); + config.GetRange(); + Thread.Sleep(timer); } catch (FormatException ex)