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,42 +686,53 @@ namespace NadekoBot.Modules.Xp.Services
} }
//club image //club image
if (!string.IsNullOrWhiteSpace(stats.User.Club?.ImageUrl)) await DrawClubImage(img, stats).ConfigureAwait(false);
{
var imgUrl = stats.User.Club.ImageUrl;
try
{
var (succ, data) = await _cache.TryGetImageDataAsync(imgUrl);
if (!succ)
{
//todo make sure it's a picture
using (var temp = await http.GetStreamAsync(imgUrl))
using (var tempDraw = Image.Load(temp).Resize(45, 45))
{
ApplyRoundedCorners(tempDraw, 22.5f);
data = tempDraw.ToStream().ToArray();
}
await _cache.SetImageDataAsync(imgUrl, data);
}
var toDraw = Image.Load(data);
img.DrawImage(toDraw,
1,
new Size(45, 45),
new Point(722, 25));
}
catch (Exception ex)
{
_log.Warn(ex);
}
}
return img.Resize(432, 211).ToStream(); return img.Resize(432, 211).ToStream();
} }
}); });
private async Task DrawClubImage(Image<Rgba32> img, FullUserStats stats)
{
if (!string.IsNullOrWhiteSpace(stats.User.Club?.ImageUrl))
{
var imgUrl = stats.User.Club.ImageUrl;
try
{
var (succ, data) = await _cache.TryGetImageDataAsync(imgUrl);
if (!succ)
{
//todo make sure it's a picture
using (var temp = await http.GetAsync(imgUrl, HttpCompletionOption.ResponseHeadersRead))
{
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);
data = tempDraw.ToStream().ToArray();
}
}
await _cache.SetImageDataAsync(imgUrl, data);
}
var toDraw = Image.Load(data);
img.DrawImage(toDraw,
1,
new Size(45, 45),
new Point(722, 25));
}
catch (Exception ex)
{
_log.Warn(ex);
}
}
}
// 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();
} }