Possible optimization for .waifuinfo

This commit is contained in:
Kwoth
2022-11-13 20:40:37 +01:00
parent e49e3eec69
commit 9ce2837f5a
3 changed files with 31 additions and 11 deletions

View File

@@ -18,8 +18,8 @@ public class WaifuInfoStats
public int DivorceCount { get; init; }
public int ClaimCount { get; init; }
public List<WaifuItem> Items { get; init; }
public List<string> Claims { get; init; }
public List<string> Fans { get; init; }
public List<int> Claims { get; init; }
public List<int> Fans { get; init; }
}
public static class WaifuExtensions
@@ -134,17 +134,19 @@ public static class WaifuExtensions
.Count(x => x.OldId == w.WaifuId
&& x.NewId == null
&& x.UpdateType == WaifuUpdateType.Claimed),
Price = w.Price,
Claims = ctx.WaifuInfo.AsQueryable()
.Include(x => x.Waifu)
Claims = ctx.WaifuInfo
.Where(x => x.ClaimerId == w.WaifuId)
.Select(x => x.Waifu.Username + "#" + x.Waifu.Discriminator)
.Select(x => x.WaifuId)
.ToList(),
Fans = ctx.WaifuInfo.AsQueryable()
.Include(x => x.Waifu)
Fans = ctx.WaifuInfo
.Where(x => x.AffinityId == w.WaifuId)
.Select(x => x.Waifu.Username + "#" + x.Waifu.Discriminator)
.Select(x => x.WaifuId)
.ToList(),
Items = w.Items
})
.FirstOrDefault();

View File

@@ -244,7 +244,6 @@ public partial class Gambling
var waifuItems = _service.GetWaifuItems().ToDictionary(x => x.ItemEmoji, x => x);
var nobody = GetText(strs.nobody);
var itemsStr = !wi.Items.Any()
? "-"
@@ -256,7 +255,17 @@ public partial class Gambling
.Chunk(2)
.Select(x => string.Join(" ", x)));
var fansStr = wi.Fans.Shuffle().Take(30).Select(x => wi.Claims.Contains(x) ? $"{x} 💞" : x).Join('\n');
var claimsNames = (await _service.GetBulkWaifuNames(wi.Claims));
var claimsStr = claimsNames
.Shuffle()
.Take(30)
.Join('\n');
var fansStr = (await _service.GetBulkWaifuNames(wi.Fans
.Shuffle()
.Take(30)))
.Select((x) => claimsNames.Contains(x) ? $"{x} 💞" : x).Join('\n');
if (string.IsNullOrWhiteSpace(fansStr))
fansStr = "-";
@@ -277,7 +286,7 @@ public partial class Gambling
.AddField("\u200B", "\u200B", true)
.AddField(GetText(strs.fans(wi.Fans.Count)), fansStr, true)
.AddField($"Waifus ({wi.ClaimCount})",
wi.ClaimCount == 0 ? nobody : string.Join("\n", wi.Claims.Shuffle().Take(30)),
wi.ClaimCount == 0 ? nobody : claimsStr,
true)
.AddField(GetText(strs.gifts), itemsStr, true);

View File

@@ -557,4 +557,13 @@ public class WaifuService : INService, IReadyExecutor
}
}
}
public async Task<IReadOnlyCollection<string>> GetBulkWaifuNames(IEnumerable<int> take)
{
await using var ctx = _db.GetDbContext();
return await ctx.GetTable<DiscordUser>()
.Where(x => take.Contains(x.Id))
.Select(x => $"{x.Username}#{x.Discriminator}")
.ToListAsyncLinqToDB();
}
}