2016-08-21 16:00:03 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using NadekoBot.Extensions;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
using NadekoBot.Services;
|
2016-08-21 16:00:03 +00:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common.Attributes;
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2016-08-21 16:00:03 +00:00
|
|
|
|
namespace NadekoBot.Modules.Searches
|
|
|
|
|
{
|
|
|
|
|
public partial class Searches
|
|
|
|
|
{
|
|
|
|
|
[Group]
|
2017-02-24 16:10:44 +00:00
|
|
|
|
public class OsuCommands : NadekoSubmodule
|
2016-08-21 16:00:03 +00:00
|
|
|
|
{
|
2017-05-27 08:19:27 +00:00
|
|
|
|
private readonly IGoogleApiService _google;
|
|
|
|
|
private readonly IBotCredentials _creds;
|
|
|
|
|
|
|
|
|
|
public OsuCommands(IGoogleApiService google, IBotCredentials creds)
|
|
|
|
|
{
|
|
|
|
|
_google = google;
|
|
|
|
|
_creds = creds;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 03:09:44 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-12-16 18:43:57 +00:00
|
|
|
|
public async Task Osu(string usr, [Remainder] string mode = null)
|
2016-08-21 16:00:03 +00:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(usr))
|
|
|
|
|
return;
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2017-02-24 16:10:44 +00:00
|
|
|
|
using (var http = new HttpClient())
|
2016-08-21 16:00:03 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var m = 0;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(mode))
|
|
|
|
|
{
|
|
|
|
|
m = ResolveGameMode(mode);
|
|
|
|
|
}
|
|
|
|
|
http.AddFakeHeaders();
|
|
|
|
|
var res = await http.GetStreamAsync(new Uri($"http://lemmmy.pw/osusig/sig.php?uname={ usr }&flagshadow&xpbar&xpbarhex&pp=2&mode={m}")).ConfigureAwait(false);
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2017-02-24 16:10:44 +00:00
|
|
|
|
var ms = new MemoryStream();
|
2016-10-13 01:18:32 +00:00
|
|
|
|
res.CopyTo(ms);
|
|
|
|
|
ms.Position = 0;
|
2017-02-24 16:10:44 +00:00
|
|
|
|
await Context.Channel.SendFileAsync(ms, $"{usr}.png", $"🎧 **{GetText("profile_link")}** <https://new.ppy.sh/u/{Uri.EscapeDataString(usr)}>\n`Image provided by https://lemmmy.pw/osusig`").ConfigureAwait(false);
|
2016-08-21 16:00:03 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2017-02-24 16:10:44 +00:00
|
|
|
|
await ReplyErrorLocalized("osu_failed").ConfigureAwait(false);
|
|
|
|
|
_log.Warn(ex);
|
2016-08-21 16:00:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2016-10-05 03:09:44 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-12-17 00:16:14 +00:00
|
|
|
|
public async Task Osub([Remainder] string map)
|
2016-08-21 16:00:03 +00:00
|
|
|
|
{
|
2017-05-27 08:19:27 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(_creds.OsuApiKey))
|
2016-08-21 16:00:03 +00:00
|
|
|
|
{
|
2017-02-24 16:10:44 +00:00
|
|
|
|
await ReplyErrorLocalized("osu_api_key").ConfigureAwait(false);
|
2016-08-21 16:00:03 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2016-08-21 16:00:03 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(map))
|
|
|
|
|
return;
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2016-08-21 16:00:03 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var http = new HttpClient())
|
|
|
|
|
{
|
|
|
|
|
var mapId = ResolveMap(map);
|
2017-05-27 08:19:27 +00:00
|
|
|
|
var reqString = $"https://osu.ppy.sh/api/get_beatmaps?k={_creds.OsuApiKey}&{mapId}";
|
2016-08-21 16:00:03 +00:00
|
|
|
|
var obj = JArray.Parse(await http.GetStringAsync(reqString).ConfigureAwait(false))[0];
|
|
|
|
|
var sb = new System.Text.StringBuilder();
|
2017-02-24 16:10:44 +00:00
|
|
|
|
var starRating = Math.Round(double.Parse($"{obj["difficultyrating"]}", CultureInfo.InvariantCulture), 2);
|
|
|
|
|
var time = TimeSpan.FromSeconds(double.Parse($"{obj["total_length"]}")).ToString(@"mm\:ss");
|
2016-08-21 16:00:03 +00:00
|
|
|
|
sb.AppendLine($"{obj["artist"]} - {obj["title"]}, mapped by {obj["creator"]}. https://osu.ppy.sh/s/{obj["beatmapset_id"]}");
|
|
|
|
|
sb.AppendLine($"{starRating} stars, {obj["bpm"]} BPM | AR{obj["diff_approach"]}, CS{obj["diff_size"]}, OD{obj["diff_overall"]} | Length: {time}");
|
2016-12-17 00:16:14 +00:00
|
|
|
|
await Context.Channel.SendMessageAsync(sb.ToString()).ConfigureAwait(false);
|
2016-08-21 16:00:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2017-02-24 16:10:44 +00:00
|
|
|
|
await ReplyErrorLocalized("something_went_wrong").ConfigureAwait(false);
|
|
|
|
|
_log.Warn(ex);
|
2016-08-21 16:00:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2016-10-05 03:09:44 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-12-17 00:16:14 +00:00
|
|
|
|
public async Task Osu5(string user, [Remainder] string mode = null)
|
2016-08-21 16:00:03 +00:00
|
|
|
|
{
|
2016-12-16 18:43:57 +00:00
|
|
|
|
var channel = (ITextChannel)Context.Channel;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(_creds.OsuApiKey))
|
2016-08-21 16:00:03 +00:00
|
|
|
|
{
|
2016-12-09 03:22:23 +00:00
|
|
|
|
await channel.SendErrorAsync("An osu! API key is required.").ConfigureAwait(false);
|
2016-08-21 16:00:03 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2016-08-21 16:00:03 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(user))
|
|
|
|
|
{
|
2016-12-09 03:22:23 +00:00
|
|
|
|
await channel.SendErrorAsync("Please provide a username.").ConfigureAwait(false);
|
2016-08-21 16:00:03 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
using (var http = new HttpClient())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var m = 0;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(mode))
|
|
|
|
|
{
|
|
|
|
|
m = ResolveGameMode(mode);
|
|
|
|
|
}
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2017-05-27 08:19:27 +00:00
|
|
|
|
var reqString = $"https://osu.ppy.sh/api/get_user_best?k={_creds.OsuApiKey}&u={Uri.EscapeDataString(user)}&type=string&limit=5&m={m}";
|
2016-08-21 16:00:03 +00:00
|
|
|
|
var obj = JArray.Parse(await http.GetStringAsync(reqString).ConfigureAwait(false));
|
|
|
|
|
var sb = new System.Text.StringBuilder($"`Top 5 plays for {user}:`\n```xl" + Environment.NewLine);
|
|
|
|
|
foreach (var item in obj)
|
|
|
|
|
{
|
2017-05-27 08:19:27 +00:00
|
|
|
|
var mapReqString = $"https://osu.ppy.sh/api/get_beatmaps?k={_creds.OsuApiKey}&b={item["beatmap_id"]}";
|
2016-08-21 16:00:03 +00:00
|
|
|
|
var map = JArray.Parse(await http.GetStringAsync(mapReqString).ConfigureAwait(false))[0];
|
2017-02-24 16:10:44 +00:00
|
|
|
|
var pp = Math.Round(double.Parse($"{item["pp"]}", CultureInfo.InvariantCulture), 2);
|
2016-08-21 16:00:03 +00:00
|
|
|
|
var acc = CalculateAcc(item, m);
|
2017-02-24 16:10:44 +00:00
|
|
|
|
var mods = ResolveMods(int.Parse($"{item["enabled_mods"]}"));
|
|
|
|
|
sb.AppendLine(mods != "+"
|
|
|
|
|
? $"{pp + "pp",-7} | {acc + "%",-7} | {map["artist"] + "-" + map["title"] + " (" + map["version"] + ")",-40} | **{mods,-10}** | /b/{item["beatmap_id"]}"
|
|
|
|
|
: $"{pp + "pp",-7} | {acc + "%",-7} | {map["artist"] + "-" + map["title"] + " (" + map["version"] + ")",-40} | /b/{item["beatmap_id"]}");
|
2016-08-21 16:00:03 +00:00
|
|
|
|
}
|
|
|
|
|
sb.Append("```");
|
|
|
|
|
await channel.SendMessageAsync(sb.ToString()).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2017-02-24 16:10:44 +00:00
|
|
|
|
await ReplyErrorLocalized("something_went_wrong").ConfigureAwait(false);
|
|
|
|
|
_log.Warn(ex);
|
2016-08-21 16:00:03 +00:00
|
|
|
|
}
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2016-08-21 16:00:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2016-08-21 16:00:03 +00:00
|
|
|
|
//https://osu.ppy.sh/wiki/Accuracy
|
2017-02-24 16:10:44 +00:00
|
|
|
|
private static double CalculateAcc(JToken play, int mode)
|
2016-08-21 16:00:03 +00:00
|
|
|
|
{
|
|
|
|
|
if (mode == 0)
|
|
|
|
|
{
|
2017-02-24 16:10:44 +00:00
|
|
|
|
var hitPoints = double.Parse($"{play["count50"]}") * 50 + double.Parse($"{play["count100"]}") * 100 + double.Parse($"{play["count300"]}") * 300;
|
|
|
|
|
var totalHits = double.Parse($"{play["count50"]}") + double.Parse($"{play["count100"]}") + double.Parse($"{play["count300"]}") + double.Parse($"{play["countmiss"]}");
|
2016-08-21 16:00:03 +00:00
|
|
|
|
totalHits *= 300;
|
|
|
|
|
return Math.Round(hitPoints / totalHits * 100, 2);
|
|
|
|
|
}
|
|
|
|
|
else if (mode == 1)
|
|
|
|
|
{
|
2017-02-24 16:10:44 +00:00
|
|
|
|
var hitPoints = double.Parse($"{play["countmiss"]}") * 0 + double.Parse($"{play["count100"]}") * 0.5 + double.Parse($"{play["count300"]}") * 1;
|
|
|
|
|
var totalHits = double.Parse($"{play["countmiss"]}") + double.Parse($"{play["count100"]}") + double.Parse($"{play["count300"]}");
|
2016-08-21 16:00:03 +00:00
|
|
|
|
hitPoints *= 300;
|
|
|
|
|
totalHits *= 300;
|
|
|
|
|
return Math.Round(hitPoints / totalHits * 100, 2);
|
|
|
|
|
}
|
|
|
|
|
else if (mode == 2)
|
|
|
|
|
{
|
2017-02-24 16:10:44 +00:00
|
|
|
|
var fruitsCaught = double.Parse($"{play["count50"]}") + double.Parse($"{play["count100"]}") + double.Parse($"{play["count300"]}");
|
|
|
|
|
var totalFruits = double.Parse($"{play["countmiss"]}") + double.Parse($"{play["count50"]}") + double.Parse($"{play["count100"]}") + double.Parse($"{play["count300"]}") + double.Parse($"{play["countkatu"]}");
|
2016-08-21 16:00:03 +00:00
|
|
|
|
return Math.Round(fruitsCaught / totalFruits * 100, 2);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-02-24 16:10:44 +00:00
|
|
|
|
var hitPoints = double.Parse($"{play["count50"]}") * 50 + double.Parse($"{play["count100"]}") * 100 + double.Parse($"{play["countkatu"]}") * 200 + (double.Parse($"{play["count300"]}") + double.Parse($"{play["countgeki"]}")) * 300;
|
|
|
|
|
var totalHits = double.Parse($"{play["countmiss"]}") + double.Parse($"{play["count50"]}") + double.Parse($"{play["count100"]}") + double.Parse($"{play["countkatu"]}") + double.Parse($"{play["count300"]}") + double.Parse($"{play["countgeki"]}");
|
2016-08-21 16:00:03 +00:00
|
|
|
|
totalHits *= 300;
|
|
|
|
|
return Math.Round(hitPoints / totalHits * 100, 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2016-08-21 16:00:03 +00:00
|
|
|
|
private static string ResolveMap(string mapLink)
|
|
|
|
|
{
|
2017-02-24 16:10:44 +00:00
|
|
|
|
var s = new Regex(@"osu.ppy.sh\/s\/", RegexOptions.IgnoreCase).Match(mapLink);
|
|
|
|
|
var b = new Regex(@"osu.ppy.sh\/b\/", RegexOptions.IgnoreCase).Match(mapLink);
|
|
|
|
|
var p = new Regex(@"osu.ppy.sh\/p\/", RegexOptions.IgnoreCase).Match(mapLink);
|
|
|
|
|
var m = new Regex(@"&m=", RegexOptions.IgnoreCase).Match(mapLink);
|
2016-08-21 16:00:03 +00:00
|
|
|
|
if (s.Success)
|
|
|
|
|
{
|
|
|
|
|
var mapId = mapLink.Substring(mapLink.IndexOf("/s/") + 3);
|
|
|
|
|
return $"s={mapId}";
|
|
|
|
|
}
|
|
|
|
|
else if (b.Success)
|
|
|
|
|
{
|
|
|
|
|
if (m.Success)
|
|
|
|
|
return $"b={mapLink.Substring(mapLink.IndexOf("/b/") + 3, mapLink.IndexOf("&m") - (mapLink.IndexOf("/b/") + 3))}";
|
|
|
|
|
else
|
|
|
|
|
return $"b={mapLink.Substring(mapLink.IndexOf("/b/") + 3)}";
|
|
|
|
|
}
|
|
|
|
|
else if (p.Success)
|
|
|
|
|
{
|
|
|
|
|
if (m.Success)
|
|
|
|
|
return $"b={mapLink.Substring(mapLink.IndexOf("?b=") + 3, mapLink.IndexOf("&m") - (mapLink.IndexOf("?b=") + 3))}";
|
|
|
|
|
else
|
|
|
|
|
return $"b={mapLink.Substring(mapLink.IndexOf("?b=") + 3)}";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return $"s={mapLink}"; //just a default incase an ID number was provided by itself (non-url)?
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2016-08-21 16:00:03 +00:00
|
|
|
|
private static int ResolveGameMode(string mode)
|
|
|
|
|
{
|
|
|
|
|
switch (mode.ToLower())
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2016-08-21 16:00:03 +00:00
|
|
|
|
//https://github.com/ppy/osu-api/wiki#mods
|
|
|
|
|
private static string ResolveMods(int mods)
|
|
|
|
|
{
|
|
|
|
|
var modString = $"+";
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2016-08-21 16:00:03 +00:00
|
|
|
|
if (IsBitSet(mods, 0))
|
|
|
|
|
modString += "NF";
|
|
|
|
|
if (IsBitSet(mods, 1))
|
|
|
|
|
modString += "EZ";
|
|
|
|
|
if (IsBitSet(mods, 8))
|
|
|
|
|
modString += "HT";
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2016-08-21 16:00:03 +00:00
|
|
|
|
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";
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2016-08-21 16:00:03 +00:00
|
|
|
|
if (IsBitSet(mods, 5))
|
|
|
|
|
modString += "SD";
|
|
|
|
|
if (IsBitSet(mods, 14))
|
|
|
|
|
modString += "PF";
|
2016-06-21 15:57:24 +00:00
|
|
|
|
|
2016-08-21 16:00:03 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-21 16:23:17 +00:00
|
|
|
|
}
|