~ow fixed

This commit is contained in:
Master Kwoth 2017-05-17 19:43:30 +02:00
parent aebb4398fd
commit 95899f0542
2 changed files with 94 additions and 83 deletions

View File

@ -8,59 +8,55 @@ namespace NadekoBot.Modules.Searches.Models
{ {
public OverwatchPlayer Player { get; set; } public OverwatchPlayer Player { get; set; }
public class OverwatchResponse
{
public object _request;
public OverwatchPlayer Eu { get; set; }
public OverwatchPlayer Kr { get; set; }
public OverwatchPlayer Us { get; set; }
}
public class OverwatchPlayer public class OverwatchPlayer
{ {
public Data data { get; set; } public StatsField Stats { get; set; }
public class Data
{
public bool Missing { get; set; } = false;
public string username { get; set; }
public int level { get; set; }
public string avatar { get; set; }
public string levelFrame { get; set; }
public string star { get; set; }
[JsonProperty("games")]
public OverwatchGames Games { get; set; }
[JsonProperty("playtime")]
public OverwatchPlaytime Playtime { get; set; }
[JsonProperty("competitive")]
public OverwatchCompetitive Competitive { get; set; }
public class OverwatchGames
{
[JsonProperty("quick")]
public OverwatchQG Quick { get; set; }
[JsonProperty("competitive")]
public OverwatchCOMP Competitive { get; set; }
public class OverwatchQG
{
public string wins { get; set; }
}
public class OverwatchCOMP
{
public string wins { get; set; }
public int lost { get; set; }
public string played { get; set; }
}
}
public class OverwatchCompetitive
{
public string rank { get; set; }
public string rank_img { get; set; }
}
public class OverwatchPlaytime
{
public string quick { get; set; }
public string competitive { get; set; }
}
}
} }
//This is to strip the html from patch notes content
internal static string StripHTML(string input) public class StatsField
{ {
var re = Regex.Replace(input, "<.*?>", String.Empty); public Stats Quickplay { get; set; }
re = Regex.Replace(re, "&#160;", $@" "); public Stats Competitive { get; set; }
return re;
}
public class Stats
{
[JsonProperty("overall_stats")]
public OverallStats OverallStats { get; set; }
[JsonProperty("game_stats")]
public GameStats GameStats { get; set; }
}
public class OverallStats
{
public int? comprank;
public int level;
public int prestige;
public string avatar;
public int wins;
public int losses;
public int games;
public string rank_image;
}
public class GameStats
{
[JsonProperty("time_played")]
public float timePlayed;
}
public class Competitive
{
} }
} }
} }

View File

@ -15,61 +15,67 @@ namespace NadekoBot.Modules.Searches
[Group] [Group]
public class OverwatchCommands : NadekoSubmodule public class OverwatchCommands : NadekoSubmodule
{ {
public enum Region
{
Eu,
Us,
Kr
}
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
public async Task Overwatch(string region, [Remainder] string query = null) public async Task Overwatch(Region region, [Remainder] string query = null)
{ {
if (string.IsNullOrWhiteSpace(query)) if (string.IsNullOrWhiteSpace(query))
return; return;
var battletag = query.Replace("#", "-"); var battletag = query.Replace("#", "-");
await Context.Channel.TriggerTypingAsync().ConfigureAwait(false); await Context.Channel.TriggerTypingAsync().ConfigureAwait(false);
try var model = (await GetProfile(region, battletag))?.Stats;
if (model != null)
{ {
await Context.Channel.TriggerTypingAsync().ConfigureAwait(false); if (model.Competitive == null)
var model = await GetProfile(region, battletag);
var rankimg = model.Competitive.rank_img;
var rank = model.Competitive.rank;
if (string.IsNullOrWhiteSpace(rank))
{ {
var qp = model.Quickplay;
var embed = new EmbedBuilder() var embed = new EmbedBuilder()
.WithAuthor(eau => eau.WithName($"{model.username}") .WithAuthor(eau => eau.WithName(query)
.WithUrl($"https://www.overbuff.com/players/pc/{battletag}") .WithUrl($"https://www.overbuff.com/players/pc/{battletag}")
.WithIconUrl($"{model.avatar}")) .WithIconUrl("https://cdn.discordapp.com/attachments/155726317222887425/255653487512256512/YZ4w2ey.png"))
.WithThumbnailUrl("https://cdn.discordapp.com/attachments/155726317222887425/255653487512256512/YZ4w2ey.png") .WithThumbnailUrl(qp.OverallStats.avatar)
.AddField(fb => fb.WithName(GetText("level")).WithValue($"{model.level}").WithIsInline(true)) .AddField(fb => fb.WithName(GetText("level")).WithValue(qp.OverallStats.level.ToString()).WithIsInline(true))
.AddField(fb => fb.WithName(GetText("quick_wins")).WithValue($"{model.Games.Quick.wins}").WithIsInline(true)) .AddField(fb => fb.WithName(GetText("quick_wins")).WithValue(qp.OverallStats.wins.ToString()).WithIsInline(true))
.AddField(fb => fb.WithName(GetText("compet_rank")).WithValue("0").WithIsInline(true)) .AddField(fb => fb.WithName(GetText("compet_rank")).WithValue("0").WithIsInline(true))
.AddField(fb => fb.WithName(GetText("quick_playtime")).WithValue($"{model.Playtime.quick}").WithIsInline(true)) .AddField(fb => fb.WithName(GetText("quick_playtime")).WithValue($"{qp.GameStats.timePlayed}hrs").WithIsInline(true))
.WithOkColor(); .WithOkColor();
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false); await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
} }
else else
{ {
var qp = model.Quickplay;
var compet = model.Competitive;
var embed = new EmbedBuilder() var embed = new EmbedBuilder()
.WithAuthor(eau => eau.WithName($"{model.username}") .WithAuthor(eau => eau.WithName(query)
.WithUrl($"https://www.overbuff.com/players/pc/{battletag}") .WithUrl($"https://www.overbuff.com/players/pc/{battletag}")
.WithIconUrl($"{model.avatar}")) .WithIconUrl(compet.OverallStats.rank_image))
.WithThumbnailUrl(rankimg) .WithThumbnailUrl(compet.OverallStats.avatar)
.AddField(fb => fb.WithName(GetText("level")).WithValue($"{model.level}").WithIsInline(true)) .AddField(fb => fb.WithName(GetText("level")).WithValue((compet.OverallStats.level + (compet.OverallStats.prestige * 100)).ToString()).WithIsInline(true))
.AddField(fb => fb.WithName(GetText("quick_wins")).WithValue($"{model.Games.Quick.wins}").WithIsInline(true)) .AddField(fb => fb.WithName(GetText("quick_wins")).WithValue(qp.OverallStats.wins.ToString()).WithIsInline(true))
.AddField(fb => fb.WithName(GetText("compet_wins")).WithValue($"{model.Games.Competitive.wins}").WithIsInline(true)) .AddField(fb => fb.WithName(GetText("compet_wins")).WithValue(compet.OverallStats.wins.ToString()).WithIsInline(true))
.AddField(fb => fb.WithName(GetText("compet_loses")).WithValue($"{model.Games.Competitive.lost}").WithIsInline(true)) .AddField(fb => fb.WithName(GetText("compet_loses")).WithValue(compet.OverallStats.losses.ToString()).WithIsInline(true))
.AddField(fb => fb.WithName(GetText("compet_played")).WithValue($"{model.Games.Competitive.played}").WithIsInline(true)) .AddField(fb => fb.WithName(GetText("compet_played")).WithValue(compet.OverallStats.games.ToString()).WithIsInline(true))
.AddField(fb => fb.WithName(GetText("compet_rank")).WithValue(rank).WithIsInline(true)) .AddField(fb => fb.WithName(GetText("compet_rank")).WithValue(compet.OverallStats.comprank.ToString()).WithIsInline(true))
.AddField(fb => fb.WithName(GetText("compet_playtime")).WithValue($"{model.Playtime.competitive}").WithIsInline(true)) .AddField(fb => fb.WithName(GetText("compet_playtime")).WithValue(compet.GameStats.timePlayed + "hrs").WithIsInline(true))
.AddField(fb => fb.WithName(GetText("quick_playtime")).WithValue($"{model.Playtime.quick}").WithIsInline(true)) .AddField(fb => fb.WithName(GetText("quick_playtime")).WithValue(qp.GameStats.timePlayed.ToString("F1") + "hrs").WithIsInline(true))
.WithColor(NadekoBot.OkColor); .WithColor(NadekoBot.OkColor);
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false); await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
} }
} }
catch else
{ {
await ReplyErrorLocalized("ow_user_not_found").ConfigureAwait(false); await ReplyErrorLocalized("ow_user_not_found").ConfigureAwait(false);
} }
} }
public async Task<OverwatchApiModel.OverwatchPlayer.Data> GetProfile(string region, string battletag) public async Task<OverwatchApiModel.OverwatchPlayer> GetProfile(Region region, string battletag)
{ {
try try
{ {
@ -78,11 +84,20 @@ namespace NadekoBot.Modules.Searches
handler.ServerCertificateCustomValidationCallback = (x, y, z, e) => true; handler.ServerCertificateCustomValidationCallback = (x, y, z, e) => true;
using (var http = new HttpClient(handler)) using (var http = new HttpClient(handler))
{ {
var url = http.AddFakeHeaders();
await http.GetStringAsync( var url = $"https://owapi.nadekobot.me/api/v3/u/{battletag}/stats";
$"https://api.lootbox.eu/pc/{region.ToLower()}/{battletag}/profile"); System.Console.WriteLine(url);
var model = JsonConvert.DeserializeObject<OverwatchApiModel.OverwatchPlayer>(url); var res = await http.GetStringAsync($"https://owapi.nadekobot.me/api/v3/u/{battletag}/stats");
return model.data; var model = JsonConvert.DeserializeObject<OverwatchApiModel.OverwatchResponse>(res);
switch (region)
{
case Region.Eu:
return model.Eu;
case Region.Kr:
return model.Kr;
default:
return model.Us;
}
} }
} }
} }