Fixed a performance and stability bug with club images.

This commit is contained in:
Master Kwoth 2017-10-13 03:44:29 +02:00
parent e32446335e
commit 998c048874
3 changed files with 49 additions and 32 deletions

View File

@ -110,7 +110,8 @@ namespace NadekoBot.Services
public async Task RunAsync() public async Task RunAsync()
{ {
for (int i = 0; i < _creds.TotalShards; i++) //todo change to 0
for (int i = 1; i < _creds.TotalShards; i++)
{ {
var p = StartShard(i); var p = StartShard(i);

View File

@ -152,7 +152,8 @@ namespace NadekoBot.Modules.Xp.Services
var oldGuildLevelData = new LevelStats(usr.Xp + usr.AwardedXp); var oldGuildLevelData = new LevelStats(usr.Xp + usr.AwardedXp);
usr.Xp += xp; usr.Xp += xp;
du.TotalXp += xp; //todo revert
du.TotalXp += xp * 1000;
if (du.Club != null) if (du.Club != null)
du.Club.Xp += xp; du.Club.Xp += xp;
var newGuildLevelData = new LevelStats(usr.Xp + usr.AwardedXp); var newGuildLevelData = new LevelStats(usr.Xp + usr.AwardedXp);
@ -685,6 +686,15 @@ namespace NadekoBot.Modules.Xp.Services
} }
//club image //club image
await DrawClubImage(img, stats).ConfigureAwait(false);
return img.Resize(432, 211).ToStream();
}
});
private async Task DrawClubImage(Image<Rgba32> img, FullUserStats stats)
{
if (!string.IsNullOrWhiteSpace(stats.User.Club?.ImageUrl)) if (!string.IsNullOrWhiteSpace(stats.User.Club?.ImageUrl))
{ {
var imgUrl = stats.User.Club.ImageUrl; var imgUrl = stats.User.Club.ImageUrl;
@ -694,12 +704,18 @@ namespace NadekoBot.Modules.Xp.Services
if (!succ) if (!succ)
{ {
//todo make sure it's a picture //todo make sure it's a picture
using (var temp = await http.GetStreamAsync(imgUrl)) using (var temp = await http.GetAsync(imgUrl, HttpCompletionOption.ResponseHeadersRead))
using (var tempDraw = Image.Load(temp).Resize(45, 45)) {
if (temp.Content.Headers.ContentType.MediaType != "image/png"
&& temp.Content.Headers.ContentType.MediaType != "image/jpeg"
&& temp.Content.Headers.ContentType.MediaType != "image/gif")
return;
using (var tempDraw = Image.Load(await temp.Content.ReadAsStreamAsync()).Resize(45, 45))
{ {
ApplyRoundedCorners(tempDraw, 22.5f); ApplyRoundedCorners(tempDraw, 22.5f);
data = tempDraw.ToStream().ToArray(); data = tempDraw.ToStream().ToArray();
} }
}
await _cache.SetImageDataAsync(imgUrl, data); await _cache.SetImageDataAsync(imgUrl, data);
} }
@ -715,11 +731,7 @@ namespace NadekoBot.Modules.Xp.Services
_log.Warn(ex); _log.Warn(ex);
} }
} }
return img.Resize(432, 211).ToStream();
} }
});
// https://github.com/SixLabors/ImageSharp/tree/master/samples/AvatarWithRoundedCorner // https://github.com/SixLabors/ImageSharp/tree/master/samples/AvatarWithRoundedCorner
public static void ApplyRoundedCorners(Image<Rgba32> img, float cornerRadius) public static void ApplyRoundedCorners(Image<Rgba32> img, float cornerRadius)

View File

@ -1,4 +1,5 @@
using NadekoBot.Services; using NadekoBot.Services;
using System.Diagnostics;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace NadekoBot namespace NadekoBot
@ -16,6 +17,9 @@ namespace NadekoBot
} }
else else
{ {
//todo revert
var _ = new NadekoBot(0, Process.GetCurrentProcess().Id)
.RunAsync(args);
return new ShardsCoordinator() return new ShardsCoordinator()
.RunAndBlockAsync(); .RunAndBlockAsync();
} }