mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-03 16:24:27 -05:00 
			
		
		
		
	Optimized .waifuinfo
This commit is contained in:
		@@ -10,6 +10,7 @@ namespace NadekoBot.Db;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
public class WaifuInfoStats
 | 
					public class WaifuInfoStats
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    public int WaifuId { get; init; }
 | 
				
			||||||
    public string FullName { get; init; }
 | 
					    public string FullName { get; init; }
 | 
				
			||||||
    public long Price { get; init; }
 | 
					    public long Price { get; init; }
 | 
				
			||||||
    public string ClaimerName { get; init; }
 | 
					    public string ClaimerName { get; init; }
 | 
				
			||||||
@@ -17,9 +18,6 @@ public class WaifuInfoStats
 | 
				
			|||||||
    public int AffinityCount { get; init; }
 | 
					    public int AffinityCount { get; init; }
 | 
				
			||||||
    public int DivorceCount { get; init; }
 | 
					    public int DivorceCount { get; init; }
 | 
				
			||||||
    public int ClaimCount { 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
 | 
					public static class WaifuExtensions
 | 
				
			||||||
@@ -103,6 +101,7 @@ public static class WaifuExtensions
 | 
				
			|||||||
                                            .FirstOrDefault())
 | 
					                                            .FirstOrDefault())
 | 
				
			||||||
                          .Select(w => new WaifuInfoStats
 | 
					                          .Select(w => new WaifuInfoStats
 | 
				
			||||||
                          {
 | 
					                          {
 | 
				
			||||||
 | 
					                              WaifuId = w.WaifuId,
 | 
				
			||||||
                              FullName =
 | 
					                              FullName =
 | 
				
			||||||
                                  ctx.Set<DiscordUser>()
 | 
					                                  ctx.Set<DiscordUser>()
 | 
				
			||||||
                                     .AsQueryable()
 | 
					                                     .AsQueryable()
 | 
				
			||||||
@@ -134,20 +133,7 @@ public static class WaifuExtensions
 | 
				
			|||||||
                                     .Count(x => x.OldId == w.WaifuId
 | 
					                                     .Count(x => x.OldId == w.WaifuId
 | 
				
			||||||
                                                 && x.NewId == null
 | 
					                                                 && x.NewId == null
 | 
				
			||||||
                                                 && x.UpdateType == WaifuUpdateType.Claimed),
 | 
					                                                 && x.UpdateType == WaifuUpdateType.Claimed),
 | 
				
			||||||
                              
 | 
					 | 
				
			||||||
                              Price = w.Price,
 | 
					                              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();
 | 
					                          .FirstOrDefault();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -245,25 +245,25 @@ public partial class Gambling
 | 
				
			|||||||
            var waifuItems = _service.GetWaifuItems().ToDictionary(x => x.ItemEmoji, x => x);
 | 
					            var waifuItems = _service.GetWaifuItems().ToDictionary(x => x.ItemEmoji, x => x);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var nobody = GetText(strs.nobody);
 | 
					            var nobody = GetText(strs.nobody);
 | 
				
			||||||
            var itemsStr = !wi.Items.Any()
 | 
					            var itemList = await _service.GetItems(wi.WaifuId);
 | 
				
			||||||
 | 
					            var itemsStr = !itemList.Any()
 | 
				
			||||||
                ? "-"
 | 
					                ? "-"
 | 
				
			||||||
                : string.Join("\n",
 | 
					                : string.Join("\n",
 | 
				
			||||||
                    wi.Items.Where(x => waifuItems.TryGetValue(x.ItemEmoji, out _))
 | 
					                    itemList.Where(x => waifuItems.TryGetValue(x.ItemEmoji, out _))
 | 
				
			||||||
                        .OrderBy(x => waifuItems[x.ItemEmoji].Price)
 | 
					                        .OrderBy(x => waifuItems[x.ItemEmoji].Price)
 | 
				
			||||||
                        .GroupBy(x => x.ItemEmoji)
 | 
					                        .GroupBy(x => x.ItemEmoji)
 | 
				
			||||||
                        .Select(x => $"{x.Key} x{x.Count(),-3}")
 | 
					                        .Select(x => $"{x.Key} x{x.Count(),-3}")
 | 
				
			||||||
                        .Chunk(2)
 | 
					                        .Chunk(2)
 | 
				
			||||||
                        .Select(x => string.Join(" ", x)));
 | 
					                        .Select(x => string.Join(" ", x)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var claimsNames = (await _service.GetBulkWaifuNames(wi.Claims));
 | 
					            var claimsNames = (await _service.GetClaimNames(wi.WaifuId));
 | 
				
			||||||
            var claimsStr = claimsNames
 | 
					            var claimsStr = claimsNames
 | 
				
			||||||
                .Shuffle()
 | 
					                .Shuffle()
 | 
				
			||||||
                .Take(30)
 | 
					                .Take(30)
 | 
				
			||||||
                .Join('\n');
 | 
					                .Join('\n');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var fansStr = (await _service.GetBulkWaifuNames(wi.Fans
 | 
					            var fansList = await _service.GetFansNames(wi.WaifuId);
 | 
				
			||||||
                .Shuffle()
 | 
					            var fansStr = fansList
 | 
				
			||||||
                .Take(30)))
 | 
					 | 
				
			||||||
                .Select((x) => claimsNames.Contains(x) ? $"{x} 💞" : x).Join('\n');
 | 
					                .Select((x) => claimsNames.Contains(x) ? $"{x} 💞" : x).Join('\n');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
@@ -284,7 +284,7 @@ public partial class Gambling
 | 
				
			|||||||
                           .AddField(GetText(strs.changes_of_heart), $"{wi.AffinityCount} - \"the {affInfo}\"", true)
 | 
					                           .AddField(GetText(strs.changes_of_heart), $"{wi.AffinityCount} - \"the {affInfo}\"", true)
 | 
				
			||||||
                           .AddField(GetText(strs.divorces), wi.DivorceCount.ToString(), true)
 | 
					                           .AddField(GetText(strs.divorces), wi.DivorceCount.ToString(), true)
 | 
				
			||||||
                           .AddField("\u200B", "\u200B", 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})",
 | 
					                           .AddField($"Waifus ({wi.ClaimCount})",
 | 
				
			||||||
                               wi.ClaimCount == 0 ? nobody : claimsStr,
 | 
					                               wi.ClaimCount == 0 ? nobody : claimsStr,
 | 
				
			||||||
                               true)
 | 
					                               true)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -414,11 +414,8 @@ public class WaifuService : INService, IReadyExecutor
 | 
				
			|||||||
                AffinityName = null,
 | 
					                AffinityName = null,
 | 
				
			||||||
                ClaimCount = 0,
 | 
					                ClaimCount = 0,
 | 
				
			||||||
                ClaimerName = null,
 | 
					                ClaimerName = null,
 | 
				
			||||||
                Claims = new(),
 | 
					 | 
				
			||||||
                Fans = new(),
 | 
					 | 
				
			||||||
                DivorceCount = 0,
 | 
					                DivorceCount = 0,
 | 
				
			||||||
                FullName = null,
 | 
					                FullName = null,
 | 
				
			||||||
                Items = new(),
 | 
					 | 
				
			||||||
                Price = 1
 | 
					                Price = 1
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -426,14 +423,6 @@ public class WaifuService : INService, IReadyExecutor
 | 
				
			|||||||
        return wi;
 | 
					        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)
 | 
					    public string GetClaimTitle(int count)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        ClaimTitle title;
 | 
					        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();
 | 
					        await using var ctx = _db.GetDbContext();
 | 
				
			||||||
        return await ctx.GetTable<DiscordUser>()
 | 
					        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}")
 | 
					            .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