mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-04 00:34:26 -05:00 
			
		
		
		
	Optimized .waifuinfo
This commit is contained in:
		@@ -10,6 +10,7 @@ namespace NadekoBot.Db;
 | 
			
		||||
 | 
			
		||||
public class WaifuInfoStats
 | 
			
		||||
{
 | 
			
		||||
    public int WaifuId { get; init; }
 | 
			
		||||
    public string FullName { get; init; }
 | 
			
		||||
    public long Price { get; init; }
 | 
			
		||||
    public string ClaimerName { get; init; }
 | 
			
		||||
@@ -17,9 +18,6 @@ public class WaifuInfoStats
 | 
			
		||||
    public int AffinityCount { get; init; }
 | 
			
		||||
    public int DivorceCount { get; init; }
 | 
			
		||||
    public int ClaimCount { get; init; }
 | 
			
		||||
    public List<WaifuItem> Items { get; init; }
 | 
			
		||||
    public List<int> Claims { get; init; }
 | 
			
		||||
    public List<int> Fans { get; init; }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
public static class WaifuExtensions
 | 
			
		||||
@@ -103,6 +101,7 @@ public static class WaifuExtensions
 | 
			
		||||
                                            .FirstOrDefault())
 | 
			
		||||
                          .Select(w => new WaifuInfoStats
 | 
			
		||||
                          {
 | 
			
		||||
                              WaifuId = w.WaifuId,
 | 
			
		||||
                              FullName =
 | 
			
		||||
                                  ctx.Set<DiscordUser>()
 | 
			
		||||
                                     .AsQueryable()
 | 
			
		||||
@@ -134,20 +133,7 @@ public static class WaifuExtensions
 | 
			
		||||
                                     .Count(x => x.OldId == w.WaifuId
 | 
			
		||||
                                                 && x.NewId == null
 | 
			
		||||
                                                 && x.UpdateType == WaifuUpdateType.Claimed),
 | 
			
		||||
                              
 | 
			
		||||
                              Price = w.Price,
 | 
			
		||||
                              
 | 
			
		||||
                              Claims = ctx.WaifuInfo
 | 
			
		||||
                                          .Where(x => x.ClaimerId == w.WaifuId)
 | 
			
		||||
                                          .Select(x => x.WaifuId)
 | 
			
		||||
                                          .ToList(),
 | 
			
		||||
                              
 | 
			
		||||
                              Fans = ctx.WaifuInfo
 | 
			
		||||
                                        .Where(x => x.AffinityId == w.WaifuId)
 | 
			
		||||
                                        .Select(x => x.WaifuId)
 | 
			
		||||
                                        .ToList(),
 | 
			
		||||
                              
 | 
			
		||||
                              Items = w.Items
 | 
			
		||||
                          })
 | 
			
		||||
                          .FirstOrDefault();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,7 @@ public class WaifuInfo : DbEntity
 | 
			
		||||
{
 | 
			
		||||
    public int WaifuId { get; set; }
 | 
			
		||||
    public DiscordUser Waifu { get; set; }
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    public int? ClaimerId { get; set; }
 | 
			
		||||
    public DiscordUser Claimer { get; set; }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -7,4 +7,4 @@ public class WaifuItem : DbEntity
 | 
			
		||||
    public int? WaifuInfoId { get; set; }
 | 
			
		||||
    public string ItemEmoji { get; set; }
 | 
			
		||||
    public string Name { get; set; }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -245,28 +245,28 @@ public partial class Gambling
 | 
			
		||||
            var waifuItems = _service.GetWaifuItems().ToDictionary(x => x.ItemEmoji, x => x);
 | 
			
		||||
 | 
			
		||||
            var nobody = GetText(strs.nobody);
 | 
			
		||||
            var itemsStr = !wi.Items.Any()
 | 
			
		||||
            var itemList = await _service.GetItems(wi.WaifuId);
 | 
			
		||||
            var itemsStr = !itemList.Any()
 | 
			
		||||
                ? "-"
 | 
			
		||||
                : string.Join("\n",
 | 
			
		||||
                    wi.Items.Where(x => waifuItems.TryGetValue(x.ItemEmoji, out _))
 | 
			
		||||
                      .OrderBy(x => waifuItems[x.ItemEmoji].Price)
 | 
			
		||||
                      .GroupBy(x => x.ItemEmoji)
 | 
			
		||||
                      .Select(x => $"{x.Key} x{x.Count(),-3}")
 | 
			
		||||
                      .Chunk(2)
 | 
			
		||||
                      .Select(x => string.Join(" ", x)));
 | 
			
		||||
                    itemList.Where(x => waifuItems.TryGetValue(x.ItemEmoji, out _))
 | 
			
		||||
                        .OrderBy(x => waifuItems[x.ItemEmoji].Price)
 | 
			
		||||
                        .GroupBy(x => x.ItemEmoji)
 | 
			
		||||
                        .Select(x => $"{x.Key} x{x.Count(),-3}")
 | 
			
		||||
                        .Chunk(2)
 | 
			
		||||
                        .Select(x => string.Join(" ", x)));
 | 
			
		||||
 | 
			
		||||
            var claimsNames = (await _service.GetBulkWaifuNames(wi.Claims));
 | 
			
		||||
            var claimsNames = (await _service.GetClaimNames(wi.WaifuId));
 | 
			
		||||
            var claimsStr = claimsNames
 | 
			
		||||
                .Shuffle()
 | 
			
		||||
                .Take(30)
 | 
			
		||||
                .Join('\n');
 | 
			
		||||
            
 | 
			
		||||
            var fansStr = (await _service.GetBulkWaifuNames(wi.Fans
 | 
			
		||||
                .Shuffle()
 | 
			
		||||
                .Take(30)))
 | 
			
		||||
 | 
			
		||||
            var fansList = await _service.GetFansNames(wi.WaifuId);
 | 
			
		||||
            var fansStr = fansList
 | 
			
		||||
                .Select((x) => claimsNames.Contains(x) ? $"{x} 💞" : x).Join('\n');
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            
 | 
			
		||||
            if (string.IsNullOrWhiteSpace(fansStr))
 | 
			
		||||
                fansStr = "-";
 | 
			
		||||
 | 
			
		||||
@@ -284,7 +284,7 @@ public partial class Gambling
 | 
			
		||||
                           .AddField(GetText(strs.changes_of_heart), $"{wi.AffinityCount} - \"the {affInfo}\"", true)
 | 
			
		||||
                           .AddField(GetText(strs.divorces), wi.DivorceCount.ToString(), true)
 | 
			
		||||
                           .AddField("\u200B", "\u200B", true)
 | 
			
		||||
                           .AddField(GetText(strs.fans(wi.Fans.Count)), fansStr, true)
 | 
			
		||||
                           .AddField(GetText(strs.fans(fansList.Count)), fansStr, true)
 | 
			
		||||
                           .AddField($"Waifus ({wi.ClaimCount})",
 | 
			
		||||
                               wi.ClaimCount == 0 ? nobody : claimsStr,
 | 
			
		||||
                               true)
 | 
			
		||||
 
 | 
			
		||||
@@ -414,11 +414,8 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
                AffinityName = null,
 | 
			
		||||
                ClaimCount = 0,
 | 
			
		||||
                ClaimerName = null,
 | 
			
		||||
                Claims = new(),
 | 
			
		||||
                Fans = new(),
 | 
			
		||||
                DivorceCount = 0,
 | 
			
		||||
                FullName = null,
 | 
			
		||||
                Items = new(),
 | 
			
		||||
                Price = 1
 | 
			
		||||
            };
 | 
			
		||||
        }
 | 
			
		||||
@@ -426,14 +423,6 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
        return wi;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async Task<WaifuInfoStats> GetFullWaifuInfoAsync(IGuildUser target)
 | 
			
		||||
    {
 | 
			
		||||
        await using var uow = _db.GetDbContext();
 | 
			
		||||
        _ = uow.GetOrCreateUser(target);
 | 
			
		||||
 | 
			
		||||
        return await GetFullWaifuInfoAsync(target.Id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public string GetClaimTitle(int count)
 | 
			
		||||
    {
 | 
			
		||||
        ClaimTitle title;
 | 
			
		||||
@@ -558,12 +547,37 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async Task<IReadOnlyCollection<string>> GetBulkWaifuNames(IEnumerable<int> take)
 | 
			
		||||
    public async Task<IReadOnlyCollection<string>> GetClaimNames(int waifuId)
 | 
			
		||||
    {
 | 
			
		||||
        await using var ctx = _db.GetDbContext();
 | 
			
		||||
        return await ctx.GetTable<DiscordUser>()
 | 
			
		||||
            .Where(x => take.Contains(x.Id))
 | 
			
		||||
            .Where(x => ctx.GetTable<WaifuInfo>()
 | 
			
		||||
                .Where(wi => wi.ClaimerId == waifuId)
 | 
			
		||||
                .Select(wi => wi.WaifuId)
 | 
			
		||||
                .Contains(x.Id))
 | 
			
		||||
            .Select(x => $"{x.Username}#{x.Discriminator}")
 | 
			
		||||
            .ToListAsyncLinqToDB();
 | 
			
		||||
            .ToListAsyncEF();
 | 
			
		||||
    }
 | 
			
		||||
    public async Task<IReadOnlyCollection<string>> GetFansNames(int waifuId)
 | 
			
		||||
    {
 | 
			
		||||
        await using var ctx = _db.GetDbContext();
 | 
			
		||||
        return await ctx.GetTable<DiscordUser>()
 | 
			
		||||
            .Where(x => ctx.GetTable<WaifuInfo>()
 | 
			
		||||
                .Where(wi => wi.AffinityId == waifuId)
 | 
			
		||||
                .Select(wi => wi.WaifuId)
 | 
			
		||||
                .Contains(x.Id))
 | 
			
		||||
            .Select(x => $"{x.Username}#{x.Discriminator}")
 | 
			
		||||
            .ToListAsyncEF();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async Task<IReadOnlyCollection<WaifuItem>> GetItems(int waifuId)
 | 
			
		||||
    {
 | 
			
		||||
        await using var ctx = _db.GetDbContext();
 | 
			
		||||
        return await ctx.GetTable<WaifuItem>()
 | 
			
		||||
            .Where(x => x.WaifuInfoId == ctx.GetTable<WaifuInfo>()
 | 
			
		||||
                .Where(x => x.WaifuId == waifuId)
 | 
			
		||||
                .Select(x => x.Id)
 | 
			
		||||
                .FirstOrDefault())
 | 
			
		||||
            .ToListAsyncEF();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user