From a39204b4a99424fad081c998e66f763cf6da4ef1 Mon Sep 17 00:00:00 2001
From: Toastie <toastie@toastiet0ast.com>
Date: Tue, 13 Aug 2024 14:21:31 +1200
Subject: [PATCH] Further cleanup of osu commands

---
 src/EllieBot/Modules/Searches/OsuCommands.cs  | 57 ++-----------------
 src/EllieBot/Modules/Searches/OsuService.cs   | 42 +++++++++++++-
 .../Searches/_common/GatariUserResponse.cs    |  4 +-
 .../_common/GatariUserStatsResponse.cs        |  4 +-
 4 files changed, 51 insertions(+), 56 deletions(-)

diff --git a/src/EllieBot/Modules/Searches/OsuCommands.cs b/src/EllieBot/Modules/Searches/OsuCommands.cs
index 2217419..8a702e9 100644
--- a/src/EllieBot/Modules/Searches/OsuCommands.cs
+++ b/src/EllieBot/Modules/Searches/OsuCommands.cs
@@ -40,7 +40,7 @@ public partial class Searches
                 }
 
                 var userId = obj.UserId;
-                var smode = ResolveGameMode(obj.ModeNumber);
+                var smode = OsuService.ResolveGameMode(obj.ModeNumber);
 
 
                 await Response()
@@ -69,24 +69,15 @@ public partial class Searches
         [Cmd]
         public async Task Gatari(string user, [Leftover] string mode = null)
         {
-            using var http = _httpFactory.CreateClient();
-            var modeNumber = string.IsNullOrWhiteSpace(mode) ? 0 : ResolveGameMode(mode);
-
-            var modeStr = ResolveGameMode(modeNumber);
-            var resString = await http.GetStringAsync($"https://api.gatari.pw/user/stats?u={user}&mode={modeNumber}");
-
-            var statsResponse = JsonConvert.DeserializeObject<GatariUserStatsResponse>(resString);
-            if (statsResponse.Code != 200 || statsResponse.Stats.Id == 0)
+            var modeNumber = OsuService.ResolveGameMode(mode);
+            var modeStr = OsuService.ResolveGameMode(modeNumber);
+            var (userData, userStats) = await _service.GetGatariDataAsync(user, mode);
+            if (userStats is null)
             {
                 await Response().Error(strs.osu_user_not_found).SendAsync();
                 return;
             }
 
-            var usrResString = await http.GetStringAsync($"https://api.gatari.pw/users/get?u={user}");
-
-            var userData = JsonConvert.DeserializeObject<GatariUserResponse>(usrResString).Users[0];
-            var userStats = statsResponse.Stats;
-
             var embed = _sender.CreateEmbed()
                                .WithOkColor()
                                .WithTitle($"osu!Gatari {modeStr} profile for {user}")
@@ -122,7 +113,7 @@ public partial class Searches
             using var http = _httpFactory.CreateClient();
             var m = 0;
             if (!string.IsNullOrWhiteSpace(mode))
-                m = ResolveGameMode(mode);
+                m = OsuService.ResolveGameMode(mode);
 
             var reqString = "https://osu.ppy.sh/api/get_user_best"
                             + $"?k={_creds.OsuApiKey}"
@@ -209,42 +200,6 @@ public partial class Searches
             return Math.Round(hitPoints / totalHits * 100, 2);
         }
 
-        private static int ResolveGameMode(string mode)
-        {
-            switch (mode.ToUpperInvariant())
-            {
-                case "STD":
-                case "STANDARD":
-                    return 0;
-                case "TAIKO":
-                    return 1;
-                case "CTB":
-                case "CATCHTHEBEAT":
-                    return 2;
-                case "MANIA":
-                case "OSU!MANIA":
-                    return 3;
-                default:
-                    return 0;
-            }
-        }
-
-        private static string ResolveGameMode(int mode)
-        {
-            switch (mode)
-            {
-                case 0:
-                    return "Standard";
-                case 1:
-                    return "Taiko";
-                case 2:
-                    return "Catch";
-                case 3:
-                    return "Mania";
-                default:
-                    return "Standard";
-            }
-        }
 
         //https://github.com/ppy/osu-api/wiki#mods
         private static string ResolveMods(int mods)
diff --git a/src/EllieBot/Modules/Searches/OsuService.cs b/src/EllieBot/Modules/Searches/OsuService.cs
index 3c20bcd..66d287f 100644
--- a/src/EllieBot/Modules/Searches/OsuService.cs
+++ b/src/EllieBot/Modules/Searches/OsuService.cs
@@ -38,7 +38,7 @@ public sealed class OsuService : IEService
         return obj;
     }
 
-    private static int ResolveGameMode(string mode)
+    public static int ResolveGameMode(string mode)
     {
         switch (mode.ToUpperInvariant())
         {
@@ -57,4 +57,44 @@ public sealed class OsuService : IEService
                 return 0;
         }
     }
+
+    public static string ResolveGameMode(int mode)
+    {
+        switch (mode)
+        {
+            case 0:
+                return "Standard";
+            case 1:
+                return "Taiko";
+            case 2:
+                return "Catch";
+            case 3:
+                return "Mania";
+            default:
+                return "Standard";
+        }
+    }
+
+    public async Task<(GatariUserData userData, GatariUserStats userStats)> GetGatariDataAsync(
+        string user,
+        string mode)
+    {
+        using var http = _httpFactory.CreateClient();
+        var modeNumber = string.IsNullOrWhiteSpace(mode) ? 0 : ResolveGameMode(mode);
+
+        var resString = await http.GetStringAsync($"https://api.gatari.pw/user/stats?u={user}&mode={modeNumber}");
+
+        var statsResponse = JsonConvert.DeserializeObject<GatariUserStatsResponse>(resString);
+        if (statsResponse.Code != 200 || statsResponse.Stats.Id == 0)
+        {
+            return default;
+        }
+
+        var usrResString = await http.GetStringAsync($"https://api.gatari.pw/users/get?u={user}");
+
+        var userData = JsonConvert.DeserializeObject<GatariUserResponse>(usrResString).Users[0];
+        var userStats = statsResponse.Stats;
+
+        return (userData, userStats);
+    }
 }
\ No newline at end of file
diff --git a/src/EllieBot/Modules/Searches/_common/GatariUserResponse.cs b/src/EllieBot/Modules/Searches/_common/GatariUserResponse.cs
index 412522a..63114ab 100644
--- a/src/EllieBot/Modules/Searches/_common/GatariUserResponse.cs
+++ b/src/EllieBot/Modules/Searches/_common/GatariUserResponse.cs
@@ -3,7 +3,7 @@ using Newtonsoft.Json;
 
 namespace EllieBot.Modules.Searches.Common;
 
-public class UserData
+public class GatariUserData
 {
     [JsonProperty("abbr")]
     public object Abbr { get; set; }
@@ -48,5 +48,5 @@ public class GatariUserResponse
     public int Code { get; set; }
 
     [JsonProperty("users")]
-    public List<UserData> Users { get; set; }
+    public List<GatariUserData> Users { get; set; }
 }
\ No newline at end of file
diff --git a/src/EllieBot/Modules/Searches/_common/GatariUserStatsResponse.cs b/src/EllieBot/Modules/Searches/_common/GatariUserStatsResponse.cs
index 97fee69..5e17737 100644
--- a/src/EllieBot/Modules/Searches/_common/GatariUserStatsResponse.cs
+++ b/src/EllieBot/Modules/Searches/_common/GatariUserStatsResponse.cs
@@ -3,7 +3,7 @@ using Newtonsoft.Json;
 
 namespace EllieBot.Modules.Searches.Common;
 
-public class UserStats
+public class GatariUserStats
 {
     [JsonProperty("a_count")]
     public int ACount { get; set; }
@@ -72,5 +72,5 @@ public class GatariUserStatsResponse
     public int Code { get; set; }
 
     [JsonProperty("stats")]
-    public UserStats Stats { get; set; }
+    public GatariUserStats Stats { get; set; }
 }
\ No newline at end of file