various formatting fixes and whatnot

This commit is contained in:
13xforever
2019-03-05 22:50:41 +05:00
parent dca59dbaa9
commit 1919183095
7 changed files with 88 additions and 8 deletions

View File

@@ -26,7 +26,7 @@ namespace CompatBot.Commands
internal sealed class CompatList : BaseCommandModuleCustom
{
private static readonly Client client = new Client();
private static SemaphoreSlim updateCheck = new SemaphoreSlim(1, 1);
private static readonly SemaphoreSlim updateCheck = new SemaphoreSlim(1, 1);
private static string lastUpdateInfo = null;
private const string Rpcs3UpdateStateKey = "Rpcs3UpdateState";
private static UpdateInfo CachedUpdateInfo = null;

View File

@@ -339,7 +339,7 @@ namespace CompatBot.Commands
await ch.SendMessageAsync($"{ctx.User.Mention} congratulations, you're the meme").ConfigureAwait(false);
}
[Command("download"), Cooldown(1, 20, CooldownBucketType.Channel)]
[Command("download"), Cooldown(1, 5, CooldownBucketType.Channel)]
[Description("Find games to download")]
public Task Download(CommandContext ctx, [RemainingText] string game)
=> Psn.SearchForGame(ctx, game);

View File

@@ -87,8 +87,7 @@ namespace CompatBot.Commands
var responseEU = await psnResponseEUTask.ConfigureAwait(false);
var responseJP = await psnResponseJPTask.ConfigureAwait(false);
msg = await msgTask.ConfigureAwait(false);
await msg.DeleteAsync().ConfigureAwait(false);
msg = null;
msg = await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Preparing results...").ConfigureAwait(false);
var usGame = GetBestMatch(responseUS.Included, search);
var euGame = GetBestMatch(responseEU.Included, search);
var jpGame = GetBestMatch(responseJP.Included, search);
@@ -99,7 +98,7 @@ namespace CompatBot.Commands
var score = g.Attributes.StarRating?.Score == null ? "N/A" : $"{StringUtils.GetStars(g.Attributes.StarRating?.Score)} ({g.Attributes.StarRating?.Score})";
var result = new DiscordEmbedBuilder
{
Color = new DiscordColor(0x0071cd),
Color = ColorGetter.Analyze(thumb.image, new DiscordColor(0x0071cd)),
Title = $"⏬ {g.Attributes.Name} [{region}] ({g.Attributes.FileSize?.Value} {g.Attributes.FileSize?.Unit})",
Url = $"https://store.playstation.com/{locale}/product/{g.Id}",
Description = $"Rating: {score}",
@@ -108,7 +107,9 @@ namespace CompatBot.Commands
hasResults = true;
await ctx.RespondAsync(embed: result).ConfigureAwait(false);
}
if (!hasResults)
if (hasResults)
await msg.DeleteAsync().ConfigureAwait(false);
else
await msg.UpdateOrCreateMessageAsync(ctx.Channel, "No results").ConfigureAwait(false);
}

View File

@@ -27,6 +27,7 @@
<PackageReference Include="DSharpPlus.Interactivity" Version="4.0.0-nightly-00573" />
<PackageReference Include="DuoVia.FuzzyStrings" Version="2.0.1" />
<PackageReference Include="Google.Apis.Drive.v3" Version="1.38.0.1512" />
<PackageReference Include="ksemenenko.ColorThief" Version="1.1.1.4" />
<PackageReference Include="MathParser.org-mXparser" Version="4.3.3" />
<PackageReference Include="MegaApiClient" Version="1.7.1" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
@@ -41,6 +42,7 @@
<PackageReference Include="NLog" Version="4.5.11" />
<PackageReference Include="NReco.Text.AhoCorasickDoubleArrayTrie" Version="1.0.1" />
<PackageReference Include="SharpCompress" Version="0.23.0" />
<PackageReference Include="System.Drawing.Common" Version="4.5.1" />
<PackageReference Include="System.IO.Pipelines" Version="4.5.3" />
</ItemGroup>

View File

@@ -0,0 +1,48 @@
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using ColorThiefDotNet;
using DSharpPlus.Entities;
namespace CompatBot.Utils
{
internal static class ColorGetter
{
public static DiscordColor Analyze(byte[] jpg, DiscordColor defaultColor)
{
try
{
var analyzer = new ColorThief();
using (var stream = new MemoryStream(jpg))
{
//var img = Bitmap.FromStream(stream, false, true);
var bmp = new Bitmap(stream, false);
var palette = analyzer.GetPalette(bmp, ignoreWhite: false);
/*
var bright = palette.FirstOrDefault(p => !p.IsDark);
if (bright != null)
return new DiscordColor(bright.Color.R, bright.Color.G, bright.Color.B);
*/
var colors = palette
.Select(p => new {c = p.Color, hsl = p.Color.ToHsl()})
.OrderBy(p => Math.Abs(0.5 - p.hsl.L))
.ThenByDescending(p => p.hsl.S)
.ToList();
#if DEBUG
foreach (var cl in colors)
Config.Log.Trace($"{cl.c.ToHexString()}, HSL: {cl.hsl.H:000} {cl.hsl.S:0.00} {cl.hsl.L:0.00}");
#endif
var c = colors[0].c;
return new DiscordColor(c.R, c.G, c.B);
}
}
catch (Exception e)
{
Config.Log.Warn(e, "Failed to extract image palette");
return defaultColor;
}
}
}
}

View File

@@ -177,13 +177,13 @@ namespace CompatBot.Utils
var fullStars = (int)stars;
var halfStar = Math.Round((stars.Value - fullStars)*4, MidpointRounding.ToEven);
var noStars = 5 - (halfStar > 0 && halfStar < 4 ? 1 : 0) - fullStars;
var noStars = 5 - (halfStar > 0 && halfStar <= 4 ? 1 : 0) - fullStars;
var result = "";
for (var i = 0; i < fullStars; i++)
result += "🌕";
if (halfStar > 3)
;
result += "🌕";
else if (halfStar > 2)
result += "🌖";
else if (halfStar > 1)

29
Tests/StarsFormatTest.cs Normal file
View File

@@ -0,0 +1,29 @@
using CompatBot.Utils;
using NUnit.Framework;
namespace Tests
{
[TestFixture]
public class StarsFormatTest
{
[TestCase(0.0, "🌑🌑🌑🌑🌑")]
[TestCase(0.1, "🌑🌑🌑🌑🌑")]
[TestCase(0.2, "🌘🌑🌑🌑🌑")]
[TestCase(0.3, "🌘🌑🌑🌑🌑")]
[TestCase(0.4, "🌗🌑🌑🌑🌑")]
[TestCase(0.5, "🌗🌑🌑🌑🌑")]
[TestCase(0.6, "🌗🌑🌑🌑🌑")]
[TestCase(0.7, "🌖🌑🌑🌑🌑")]
[TestCase(0.8, "🌖🌑🌑🌑🌑")]
[TestCase(0.9, "🌕🌑🌑🌑🌑")]
[TestCase(1.0, "🌕🌑🌑🌑🌑")]
public void FormatTest(decimal score, string expectedValue)
{
Assert.That(StringUtils.GetStars(score), Is.EqualTo(expectedValue), "Failed for " + score);
}
}
}