fixed an issue in .gatari

fully cleaned osu related commands and moved to a service
This commit is contained in:
Toastie (DCS Team) 2024-08-14 17:50:58 +12:00
parent a39204b4a9
commit 79bc4e75ca
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
3 changed files with 131 additions and 124 deletions

View file

@ -110,135 +110,15 @@ public partial class Searches
return; return;
} }
using var http = _httpFactory.CreateClient(); var plays = await _service.GetOsuPlay(user, mode);
var m = 0;
if (!string.IsNullOrWhiteSpace(mode))
m = OsuService.ResolveGameMode(mode);
var reqString = "https://osu.ppy.sh/api/get_user_best"
+ $"?k={_creds.OsuApiKey}"
+ $"&u={Uri.EscapeDataString(user)}"
+ "&type=string"
+ "&limit=5"
+ $"&m={m}";
var resString = await http.GetStringAsync(reqString);
var obj = JsonConvert.DeserializeObject<List<OsuUserBests>>(resString);
var mapTasks = obj.Select(async item =>
{
var mapReqString = "https://osu.ppy.sh/api/get_beatmaps"
+ $"?k={_creds.OsuApiKey}"
+ $"&b={item.BeatmapId}";
var mapResString = await http.GetStringAsync(mapReqString);
var map = JsonConvert.DeserializeObject<List<OsuMapData>>(mapResString).FirstOrDefault();
if (map is null)
return default;
var pp = Math.Round(item.Pp, 2);
var acc = CalculateAcc(item, m);
var mods = ResolveMods(item.EnabledMods);
var title = $"{map.Artist}-{map.Title} ({map.Version})";
var desc = $@"[/b/{item.BeatmapId}](https://osu.ppy.sh/b/{item.BeatmapId})
{pp + "pp",-7} | {acc + "%",-7}
";
if (mods != "+")
desc += Format.Bold(mods);
return (title, desc);
});
var eb = _sender.CreateEmbed().WithOkColor().WithTitle($"Top 5 plays for {user}"); var eb = _sender.CreateEmbed().WithOkColor().WithTitle($"Top 5 plays for {user}");
var mapData = await mapTasks.WhenAll(); foreach (var (title, desc) in plays)
foreach (var (title, desc) in mapData.Where(x => x != default))
eb.AddField(title, desc); eb.AddField(title, desc);
await Response().Embed(eb).SendAsync(); await Response().Embed(eb).SendAsync();
} }
//https://osu.ppy.sh/wiki/Accuracy
private static double CalculateAcc(OsuUserBests play, int mode)
{
double hitPoints;
double totalHits;
if (mode == 0)
{
hitPoints = (play.Count50 * 50) + (play.Count100 * 100) + (play.Count300 * 300);
totalHits = play.Count50 + play.Count100 + play.Count300 + play.Countmiss;
totalHits *= 300;
}
else if (mode == 1)
{
hitPoints = (play.Countmiss * 0) + (play.Count100 * 0.5) + play.Count300;
totalHits = (play.Countmiss + play.Count100 + play.Count300) * 300;
hitPoints *= 300;
}
else if (mode == 2)
{
hitPoints = play.Count50 + play.Count100 + play.Count300;
totalHits = play.Countmiss + play.Count50 + play.Count100 + play.Count300 + play.Countkatu;
}
else
{
hitPoints = (play.Count50 * 50)
+ (play.Count100 * 100)
+ (play.Countkatu * 200)
+ ((play.Count300 + play.Countgeki) * 300);
totalHits = (play.Countmiss
+ play.Count50
+ play.Count100
+ play.Countkatu
+ play.Count300
+ play.Countgeki)
* 300;
}
return Math.Round(hitPoints / totalHits * 100, 2);
}
//https://github.com/ppy/osu-api/wiki#mods
private static string ResolveMods(int mods)
{
var modString = "+";
if (IsBitSet(mods, 0))
modString += "NF";
if (IsBitSet(mods, 1))
modString += "EZ";
if (IsBitSet(mods, 8))
modString += "HT";
if (IsBitSet(mods, 3))
modString += "HD";
if (IsBitSet(mods, 4))
modString += "HR";
if (IsBitSet(mods, 6) && !IsBitSet(mods, 9))
modString += "DT";
if (IsBitSet(mods, 9))
modString += "NC";
if (IsBitSet(mods, 10))
modString += "FL";
if (IsBitSet(mods, 5))
modString += "SD";
if (IsBitSet(mods, 14))
modString += "PF";
if (IsBitSet(mods, 7))
modString += "RX";
if (IsBitSet(mods, 11))
modString += "AT";
if (IsBitSet(mods, 12))
modString += "SO";
return modString;
}
private static bool IsBitSet(int mods, int pos)
=> (mods & (1 << pos)) != 0;
} }
} }

View file

@ -40,7 +40,7 @@ public sealed class OsuService : IEService
public static int ResolveGameMode(string mode) public static int ResolveGameMode(string mode)
{ {
switch (mode.ToUpperInvariant()) switch (mode?.ToUpperInvariant())
{ {
case "STD": case "STD":
case "STANDARD": case "STANDARD":
@ -97,4 +97,131 @@ public sealed class OsuService : IEService
return (userData, userStats); return (userData, userStats);
} }
public async Task<(string title, string desc)[]> GetOsuPlay(string user, string mode)
{
using var http = _httpFactory.CreateClient();
var m = 0;
if (!string.IsNullOrWhiteSpace(mode))
m = OsuService.ResolveGameMode(mode);
var reqString = "https://osu.ppy.sh/api/get_user_best"
+ $"?k={_creds.OsuApiKey}"
+ $"&u={Uri.EscapeDataString(user)}"
+ "&type=string"
+ "&limit=5"
+ $"&m={m}";
var resString = await http.GetStringAsync(reqString);
var obj = JsonConvert.DeserializeObject<List<OsuUserBests>>(resString);
var mapTasks = obj.Select(async item =>
{
var mapReqString = "https://osu.ppy.sh/api/get_beatmaps"
+ $"?k={_creds.OsuApiKey}"
+ $"&b={item.BeatmapId}";
var mapResString = await http.GetStringAsync(mapReqString);
var map = JsonConvert.DeserializeObject<List<OsuMapData>>(mapResString).FirstOrDefault();
if (map is null)
return default;
var pp = Math.Round(item.Pp, 2);
var acc = CalculateAcc(item, m);
var mods = ResolveMods(item.EnabledMods);
var title = $"{map.Artist}-{map.Title} ({map.Version})";
var desc = $@"[/b/{item.BeatmapId}](https://osu.ppy.sh/b/{item.BeatmapId})
{pp + "pp",-7} | {acc + "%",-7}
";
if (mods != "+")
desc += Format.Bold(mods);
return (title, desc);
});
return await Task.WhenAll(mapTasks);
}
//https://osu.ppy.sh/wiki/Accuracy
private static double CalculateAcc(OsuUserBests play, int mode)
{
double hitPoints;
double totalHits;
if (mode == 0)
{
hitPoints = (play.Count50 * 50) + (play.Count100 * 100) + (play.Count300 * 300);
totalHits = play.Count50 + play.Count100 + play.Count300 + play.Countmiss;
totalHits *= 300;
}
else if (mode == 1)
{
hitPoints = (play.Countmiss * 0) + (play.Count100 * 0.5) + play.Count300;
totalHits = (play.Countmiss + play.Count100 + play.Count300) * 300;
hitPoints *= 300;
}
else if (mode == 2)
{
hitPoints = play.Count50 + play.Count100 + play.Count300;
totalHits = play.Countmiss + play.Count50 + play.Count100 + play.Count300 + play.Countkatu;
}
else
{
hitPoints = (play.Count50 * 50)
+ (play.Count100 * 100)
+ (play.Countkatu * 200)
+ ((play.Count300 + play.Countgeki) * 300);
totalHits = (play.Countmiss
+ play.Count50
+ play.Count100
+ play.Countkatu
+ play.Count300
+ play.Countgeki)
* 300;
}
return Math.Round(hitPoints / totalHits * 100, 2);
}
//https://github.com/ppy/osu-api/wiki#mods
private static string ResolveMods(int mods)
{
var modString = "+";
if (IsBitSet(mods, 0))
modString += "NF";
if (IsBitSet(mods, 1))
modString += "EZ";
if (IsBitSet(mods, 8))
modString += "HT";
if (IsBitSet(mods, 3))
modString += "HD";
if (IsBitSet(mods, 4))
modString += "HR";
if (IsBitSet(mods, 6) && !IsBitSet(mods, 9))
modString += "DT";
if (IsBitSet(mods, 9))
modString += "NC";
if (IsBitSet(mods, 10))
modString += "FL";
if (IsBitSet(mods, 5))
modString += "SD";
if (IsBitSet(mods, 14))
modString += "PF";
if (IsBitSet(mods, 7))
modString += "RX";
if (IsBitSet(mods, 11))
modString += "AT";
if (IsBitSet(mods, 12))
modString += "SO";
return modString;
}
private static bool IsBitSet(int mods, int pos)
=> (mods & (1 << pos)) != 0;
} }

View file

@ -39,7 +39,7 @@ public class GatariUserStats
public int Pp { get; set; } public int Pp { get; set; }
[JsonProperty("rank")] [JsonProperty("rank")]
public int Rank { get; set; } public int? Rank { get; set; }
[JsonProperty("ranked_score")] [JsonProperty("ranked_score")]
public int RankedScore { get; set; } public int RankedScore { get; set; }