Merge pull request #761 from fkndean/1.0

Add BF3/BF4 API Searches (Online Players and User Search)
This commit is contained in:
Master Kwoth 2016-11-13 02:36:27 +01:00 committed by GitHub
commit eab88e5051
3 changed files with 227 additions and 1 deletions

View File

@ -493,6 +493,160 @@ $@"🌍 **Weather for** 【{obj["target"]}】
return matches[rng.Next(0, matches.Count)].Groups["url"].Value; return matches[rng.Next(0, matches.Count)].Groups["url"].Value;
} }
} }
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task BFO(IUserMessage umsg, [Remainder] string game = null)
{
var channel = (ITextChannel)umsg.Channel;
if (string.IsNullOrWhiteSpace(game))
{
await channel.SendMessageAsync("💢 Please enter a game `(bf3, bf4)`").ConfigureAwait(false);
return;
}
await umsg.Channel.TriggerTypingAsync().ConfigureAwait(false);
using (var http = new HttpClient())
{
http.DefaultRequestHeaders.Clear();
try
{
if (game.Equals("bf3", StringComparison.OrdinalIgnoreCase))
{
var res = await http.GetStringAsync($"http://api.bf3stats.com/global/onlinestats/").ConfigureAwait(false);
var items = JObject.Parse(res);
var sb = new StringBuilder();
var status = items["status"];
var x360 = items["360"];
var ps3 = items["ps3"];
var pc = items["pc"];
var response = $@"```css
[☕ BF3 Status: {status.ToString().ToUpper()}]
XBOX360: [{x360.ToString()}]
PS3: [{ps3.ToString()}]
PC: [{pc.ToString()}]
```";
await channel.SendMessageAsync(response);
}
else if (game.Equals("bf4", StringComparison.OrdinalIgnoreCase))
{
var res = await http.GetStringAsync($"http://api.bf4stats.com/api/onlinePlayers?output=json").ConfigureAwait(false);
var items = JObject.Parse(res);
var sb = new StringBuilder();
var status = !string.IsNullOrEmpty(items.ToString()) ? "OK" : "BAD";
var pc = items["pc"];
var ps3 = items["ps3"];
var ps4 = items["ps4"];
var xbox = items["xbox"];
var xone = items["xone"];
sb.AppendLine("```css");
sb.AppendLine($"[☕ BF4 Status: {status}]");
foreach (var i in items) {
var plat = items[i.Key];
sb.AppendLine($"{plat["label"]}: ✔[{plat["count"]}] / ↑[{plat["peak24"]}]");
}
sb.Append("```");
await channel.SendMessageAsync(sb.ToString());
}
} catch
{
await channel.SendMessageAsync($"💢 BF3/BF4 API is most likely not working at the moment or could not find {game}.").ConfigureAwait(false);
}
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task BFU(IUserMessage umsg, string platform, string game, [Remainder] string query = null)
{
var channel = (ITextChannel)umsg.Channel;
if (string.IsNullOrWhiteSpace(platform) || string.IsNullOrWhiteSpace(game) || string.IsNullOrWhiteSpace(query))
{
await channel.SendMessageAsync("💢 Please enter a platform `(pc, xbox, ps3, xone, ps4)`, game `(bf3, bf4)`, followed by a search query.").ConfigureAwait(false);
return;
}
await umsg.Channel.TriggerTypingAsync().ConfigureAwait(false);
using (var http = new HttpClient())
{
http.DefaultRequestHeaders.Clear();
try
{
if (game.Equals("bf3", StringComparison.OrdinalIgnoreCase))
{
var res = await http.GetStringAsync($"http://api.bf3stats.com/{Uri.EscapeUriString(platform)}/playerlist/players={Uri.EscapeUriString(query)}?output=json").ConfigureAwait(false);
var items = JObject.Parse(res);
var sb = new StringBuilder();
var playerName = items["list"][query];
var playerTag = playerName["tag"];
var playerCountryName = playerName["country_name"];
var playerStats = playerName["stats"];
var playerRank = playerStats["rank"];
var playerRank_name = playerRank["name"];
var playerGlobal_Kills = playerStats["global"]["kills"];
var playerGlobal_Deaths = playerStats["global"]["deaths"];
var playerGlobal_KD = Math.Round(Double.Parse(playerGlobal_Kills.ToString()) / Double.Parse(playerGlobal_Deaths.ToString()), 2);
var playerGlobal_Wins = playerStats["global"]["wins"];
var playerGlobal_Losses = playerStats["global"]["losses"];
var playerGlobal_WL = Math.Round(Double.Parse(playerGlobal_Wins.ToString()) / Double.Parse(playerGlobal_Losses.ToString()), 2);
var playerGlobal_Shots = playerStats["global"]["shots"];
var playerGlobal_Hits = playerStats["global"]["hits"];
var playerGlobal_Accuracy = Math.Round(Double.Parse(playerGlobal_Hits.ToString()) / Double.Parse(playerGlobal_Shots.ToString()), 2);
var playerGlobal_ELO = playerStats["global"]["elo"];
var response = $@"```css
[☕ BF3 Player: {query}]
Platform: [{platform.ToUpper()}]
Tag: [{playerTag.ToString()}]
K/D: [{playerGlobal_KD.ToString()}]
W/L: [{playerGlobal_WL.ToString()}]
Accuracy: %[{playerGlobal_Accuracy.ToString()}]
ELO: [{playerGlobal_ELO.ToString()}]
```";
await channel.SendMessageAsync(response);
} else if (game.Equals("bf4", StringComparison.OrdinalIgnoreCase))
{
var res = await http.GetStringAsync($"http://api.bf4stats.com/api/playerInfo?plat={Uri.EscapeUriString(platform)}&name={Uri.EscapeUriString(query)}&output=json").ConfigureAwait(false);
var items = JObject.Parse(res);
var sb = new StringBuilder();
var player = items["player"];
var playerStats = items["stats"];
var playerName = player["name"];
var playerTag = player["tag"];
var playerPlatform = player["plat"];
var playerKills = playerStats["kills"];
var playerDeaths = playerStats["deaths"];
var player_KD = Math.Round(Double.Parse(playerKills.ToString()) / Double.Parse(playerDeaths.ToString()), 2);
var playerWins = playerStats["numWins"];
var playerRounds = playerStats["numRounds"];
var player_WL = Math.Round(Double.Parse(playerWins.ToString()) / Double.Parse(playerRounds.ToString()), 2);
var shotsFired = playerStats["shotsFired"];
var shotsHit = playerStats["shotsHit"];
var accuracy = Math.Round(Double.Parse(shotsHit.ToString()) / Double.Parse(shotsFired.ToString()), 2);
var playerELO = playerStats["elo"];
var response = $@"```css
[☕ BF4 Player: {playerName.ToString()}]
Platform: [{playerPlatform.ToString().ToUpper()}]
Tag: [{playerTag.ToString()}]
K/D: [{player_KD.ToString()}]
W/L: [{player_WL.ToString()}]
Accuracy: %[{accuracy.ToString()}]
ELO: [{playerELO.ToString()}]
```";
await channel.SendMessageAsync(response);
}
}
catch
{
await channel.SendMessageAsync($"💢 BF3/BF4 API is most likely not working at the moment or could not find {query}.").ConfigureAwait(false);
}
}
}
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]

View File

@ -761,6 +761,60 @@ namespace NadekoBot.Resources {
} }
} }
/// <summary>
/// Looks up a localized string similar to bfonline bfo.
/// </summary>
public static string bfo_cmd {
get {
return ResourceManager.GetString("bfo_cmd", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Gives you online players for BF3 and BF4.
/// </summary>
public static string bfo_desc {
get {
return ResourceManager.GetString("bfo_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}bfo bf3` or `{0}bfo bf4`.
/// </summary>
public static string bfo_usage {
get {
return ResourceManager.GetString("bfo_usage", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to bfuser bfu.
/// </summary>
public static string bfu_cmd {
get {
return ResourceManager.GetString("bfu_cmd", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Gives you back a battlefield user&apos;s stats..
/// </summary>
public static string bfu_desc {
get {
return ResourceManager.GetString("bfu_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}bfu platform game user`.
/// </summary>
public static string bfu_usage {
get {
return ResourceManager.GetString("bfu_usage", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to boobs. /// Looks up a localized string similar to boobs.
/// </summary> /// </summary>
@ -5829,7 +5883,7 @@ namespace NadekoBot.Resources {
} }
/// <summary> /// <summary>
/// Looks up a localized string similar to `{0}send serverid|c:channelid` or `{0}send serverid|u:userid`. /// Looks up a localized string similar to `{0}send serverid|c:channelid message` or `{0}send serverid|u:userid message`.
/// </summary> /// </summary>
public static string send_usage { public static string send_usage {
get { get {

View File

@ -2673,4 +2673,22 @@
<data name="wikia_usage" xml:space="preserve"> <data name="wikia_usage" xml:space="preserve">
<value>`{0}wikia mtg Vigilance` or `{0}wikia mlp Dashy`</value> <value>`{0}wikia mtg Vigilance` or `{0}wikia mlp Dashy`</value>
</data> </data>
<data name="bfo_cmd" xml:space="preserve">
<value>bfonline bfo</value>
</data>
<data name="bfo_desc" xml:space="preserve">
<value>Gives you online players for BF3 and BF4</value>
</data>
<data name="bfo_usage" xml:space="preserve">
<value>`{0}bfo bf3` or `{0}bfo bf4`</value>
</data>
<data name="bfu_cmd" xml:space="preserve">
<value>bfuser bfu</value>
</data>
<data name="bfu_desc" xml:space="preserve">
<value>Gives you back a battlefield user's stats.</value>
</data>
<data name="bfu_usage" xml:space="preserve">
<value>`{0}bfu platform game user`</value>
</data>
</root> </root>