Hearthstone card search support

This commit is contained in:
Master Kwoth 2016-02-05 08:01:31 +01:00
parent 4fce93c5cf
commit 861a587212
5 changed files with 72 additions and 8 deletions

View File

@ -217,11 +217,13 @@ namespace NadekoBot.Extensions {
public static int GB(this int value) => value.MB() * 1000; public static int GB(this int value) => value.MB() * 1000;
public static Stream ToStream(this System.Drawing.Image img, System.Drawing.Imaging.ImageFormat format = null) { public static Stream ToStream(this System.Drawing.Image img, System.Drawing.Imaging.ImageFormat format = null) {
Console.WriteLine("To stream");
if (format == null) if (format == null)
format = System.Drawing.Imaging.ImageFormat.Jpeg; format = System.Drawing.Imaging.ImageFormat.Jpeg;
MemoryStream stream = new MemoryStream(); MemoryStream stream = new MemoryStream();
img.Save(stream, format); img.Save(stream, format);
stream.Position = 0; stream.Position = 0;
Console.WriteLine("To stream finished");
return stream; return stream;
} }
@ -230,23 +232,32 @@ namespace NadekoBot.Extensions {
/// </summary> /// </summary>
/// <param name="images">The Images you want to merge.</param> /// <param name="images">The Images you want to merge.</param>
/// <returns>Merged bitmap</returns> /// <returns>Merged bitmap</returns>
public static Bitmap Merge(this IEnumerable<Image> images) { public static Bitmap Merge(this IEnumerable<Image> images,int reverseScaleFactor = 1) {
Console.WriteLine("Start merge");
if (images.Count() == 0) return null; if (images.Count() == 0) return null;
int width = images.Sum(i => i.Width); int width = images.Sum(i => i.Width);
int height = images.First().Height ; int height = images.First().Height ;
Bitmap bitmap = new Bitmap(width, height); Bitmap bitmap = new Bitmap(width / reverseScaleFactor, height / reverseScaleFactor);
var r = new Random(); var r = new Random();
int offsetx = 0; int offsetx = 0;
foreach (var img in images) { foreach (var img in images) {
Bitmap bm = new Bitmap(img); Bitmap bm = new Bitmap(img);
for (int w = 0; w < img.Width; w++) { for (int w = 0; w < img.Width; w++) {
for (int h = 0; h < img.Height; h++) { for (int h = 0; h < bitmap.Height; h++) {
bitmap.SetPixel(w + offsetx, h, bm.GetPixel(w, h)); bitmap.SetPixel(w / reverseScaleFactor + offsetx, h , bm.GetPixel(w, h *reverseScaleFactor));
} }
} }
offsetx += img.Width; offsetx += img.Width/reverseScaleFactor;
} }
Console.WriteLine("Finish merge");
return bitmap; return bitmap;
} }
/// <summary>
/// Merges Images into 1 Image and returns a bitmap asynchronously.
/// </summary>
/// <param name="images">The Images you want to merge.</param>
/// <returns>Merged bitmap</returns>
public static async Task<Bitmap> MergeAsync(this IEnumerable<Image> images, int reverseScaleFactor = 1) =>
await Task.Run(() => images.Merge(reverseScaleFactor));
} }
} }

View File

@ -76,6 +76,9 @@ namespace NadekoBot
try { try {
var obj = new ParseObject("Stats"); var obj = new ParseObject("Stats");
obj["OnlineUsers"] = await Task.Run(() => NadekoBot.client.Servers.Sum(x => x.Users.Count())); obj["OnlineUsers"] = await Task.Run(() => NadekoBot.client.Servers.Sum(x => x.Users.Count()));
obj["RealOnlineUsers"] = await Task.Run(() => NadekoBot
.client.Servers
.Sum(x => x.Users.Where(u => u.Status == UserStatus.Online).Count()));
obj["ConnectedServers"] = NadekoBot.client.Servers.Count(); obj["ConnectedServers"] = NadekoBot.client.Servers.Count();
await obj.SaveAsync(); await obj.SaveAsync();

View File

@ -13,6 +13,7 @@
public bool? ForwardMessages; public bool? ForwardMessages;
public string OsuApiKey; public string OsuApiKey;
public string SoundCloudClientID; public string SoundCloudClientID;
public string MashapeKey;
} }
public class AnimeResult public class AnimeResult
{ {

View File

@ -74,7 +74,7 @@ namespace NadekoBot.Modules {
toSend += "**Song queue is full!**\n"; toSend += "**Song queue is full!**\n";
await e.Send(toSend); await e.Send(toSend);
int number = 1; int number = 1;
await e.Send(string.Join("\n", player.SongQueue.Select(v => $"**#{number++}** {v.Title.TrimTo(60)}").Take(10))); await e.Send(string.Join("\n", player.SongQueue.Select(v => $"`{number++}.` {v.Title.TrimTo(60)}").Take(10)));
}); });
cgb.CreateCommand("np") cgb.CreateCommand("np")
@ -161,7 +161,7 @@ namespace NadekoBot.Modules {
}); });
cgb.CreateCommand("pl") cgb.CreateCommand("pl")
.Description("Queues up to 25 songs from a youtube playlist") .Description("Queues up to 25 songs from a youtube playlist specified by a link, or keywords.")
.Parameter("playlist", ParameterType.Unparsed) .Parameter("playlist", ParameterType.Unparsed)
.Do(async e => { .Do(async e => {
var ids = await Searches.GetVideoIDs(await Searches.GetPlaylistIdByKeyword(e.GetArg("playlist"))); var ids = await Searches.GetVideoIDs(await Searches.GetPlaylistIdByKeyword(e.GetArg("playlist")));

View File

@ -9,11 +9,14 @@ using Newtonsoft.Json;
using Discord.Commands; using Discord.Commands;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace NadekoBot.Modules { namespace NadekoBot.Modules {
class Searches : DiscordModule { class Searches : DiscordModule {
private Random _r;
public Searches() : base() { public Searches() : base() {
// commands.Add(new OsuCommands()); // commands.Add(new OsuCommands());
_r = new Random();
} }
public override void Install(ModuleManager manager) { public override void Install(ModuleManager manager) {
@ -139,12 +142,58 @@ namespace NadekoBot.Modules {
if (e.GetArg("ffs") == null || e.GetArg("ffs").Length < 1) return; if (e.GetArg("ffs") == null || e.GetArg("ffs").Length < 1) return;
await e.Send(await $"http://lmgtfy.com/?q={ Uri.EscapeUriString(e.GetArg("ffs").ToString()) }".ShortenUrl()); await e.Send(await $"http://lmgtfy.com/?q={ Uri.EscapeUriString(e.GetArg("ffs").ToString()) }".ShortenUrl());
}); });
cgb.CreateCommand("~hs")
.Description("Searches for a Hearthstone card and shows its image.")
.Parameter("name", ParameterType.Unparsed)
.Do(async e => {
var arg = e.GetArg("name");
if (string.IsNullOrWhiteSpace(arg)) return;
var res = await GetResponseAsync($"https://omgvamp-hearthstone-v1.p.mashape.com/cards/search/{Uri.EscapeUriString(arg)}",
new Tuple<string, string>[] {
new Tuple<string, string>("X-Mashape-Key", NadekoBot.creds.MashapeKey),
});
try {
var items = JArray.Parse(res);
List<System.Drawing.Image> images = new List<System.Drawing.Image>();
if (items == null)
throw new KeyNotFoundException("Cannot find a card by that name");
int cnt = 0;
foreach (var item in items) {
if (cnt >= 4)
break;
if (!item.HasValues || item["img"] == null)
continue;
cnt++;
images.Add(System.Drawing.Bitmap.FromStream(await GetResponseStream(item["img"].ToString())));
}
if (items.Count > 4) {
await e.Send(":exclamation: Found over 4 images. Showing random 4.");
}
Console.WriteLine("Start");
await e.Channel.SendFile(arg + ".png", (await images.MergeAsync()).ToStream(System.Drawing.Imaging.ImageFormat.Png));
Console.WriteLine("Finish");
} catch (Exception ex) {
await e.Send($":anger: Error {ex}");
}
});
}); });
} }
public static async Task<Stream> GetResponseStream(string v) =>
(await ((HttpWebRequest)WebRequest.Create(v)).GetResponseAsync()).GetResponseStream();
public static async Task<string> GetResponseAsync(string v) => public static async Task<string> GetResponseAsync(string v) =>
await new StreamReader((await ((HttpWebRequest)WebRequest.Create(v)).GetResponseAsync()).GetResponseStream()).ReadToEndAsync(); await new StreamReader((await ((HttpWebRequest)WebRequest.Create(v)).GetResponseAsync()).GetResponseStream()).ReadToEndAsync();
public static async Task<string> GetResponseAsync(string v, IEnumerable<Tuple<string, string>> headers) {
var wr = (HttpWebRequest)WebRequest.Create(v);
foreach (var header in headers) {
wr.Headers.Add(header.Item1, header.Item2);
}
return await new StreamReader((await wr.GetResponseAsync()).GetResponseStream()).ReadToEndAsync();
}
private string token = ""; private string token = "";
private async Task<AnimeResult> GetAnimeQueryResultLink(string query) { private async Task<AnimeResult> GetAnimeQueryResultLink(string query) {
try { try {