Removed waifu repository

This commit is contained in:
Kwoth
2021-06-19 04:49:49 +02:00
parent 15dac7e3ed
commit f34951c81c
7 changed files with 62 additions and 117 deletions

View File

@@ -138,7 +138,7 @@ WHERE CurrencyAmount > {config.Decay.MinThreshold} AND UserId!={_client.CurrentU
cash = uow.DiscordUsers.GetTotalCurrency();
onePercent = uow.DiscordUsers.GetTopOnePercentCurrency(_client.CurrentUser.Id);
planted = uow._context.PlantedCurrency.AsQueryable().Sum(x => x.Amount);
waifus = uow.Waifus.GetTotalValue();
waifus = uow._context.WaifuInfo.GetTotalValue();
bot = uow.DiscordUsers.GetUserCurrency(_client.CurrentUser.Id);
}

View File

@@ -46,7 +46,7 @@ namespace NadekoBot.Modules.Gambling.Services
using (var uow = _db.GetDbContext())
{
var waifu = uow.Waifus.ByWaifuUserId(waifuId);
var waifu = uow._context.WaifuInfo.ByWaifuUserId(waifuId);
var ownerUser = uow.DiscordUsers.GetOrCreate(owner);
// owner has to be the owner of the waifu
@@ -96,7 +96,7 @@ namespace NadekoBot.Modules.Gambling.Services
var settings = _gss.Data;
using (var uow = _db.GetDbContext())
{
var waifu = uow.Waifus.ByWaifuUserId(user.Id);
var waifu = uow._context.WaifuInfo.ByWaifuUserId(user.Id);
if (waifu == null)
return settings.Waifu.MinPrice;
@@ -143,7 +143,7 @@ namespace NadekoBot.Modules.Gambling.Services
uow._context.WaifuUpdates.RemoveRange(affs);
//reset divorces to 0
uow._context.WaifuUpdates.RemoveRange(divorces);
var waifu = uow.Waifus.ByWaifuUserId(user.Id);
var waifu = uow._context.WaifuInfo.ByWaifuUserId(user.Id);
//reset price, remove items
//remove owner, remove affinity
waifu.Price = 50;
@@ -167,7 +167,7 @@ namespace NadekoBot.Modules.Gambling.Services
bool isAffinity;
using (var uow = _db.GetDbContext())
{
w = uow.Waifus.ByWaifuUserId(target.Id);
w = uow._context.WaifuInfo.ByWaifuUserId(target.Id);
isAffinity = (w?.Affinity?.UserId == user.Id);
if (w == null)
{
@@ -179,7 +179,7 @@ namespace NadekoBot.Modules.Gambling.Services
}
else
{
uow.Waifus.Add(w = new WaifuInfo()
uow._context.WaifuInfo.Add(w = new WaifuInfo()
{
Waifu = waifu,
Claimer = claimer,
@@ -257,7 +257,7 @@ namespace NadekoBot.Modules.Gambling.Services
TimeSpan? remaining = null;
using (var uow = _db.GetDbContext())
{
var w = uow.Waifus.ByWaifuUserId(user.Id);
var w = uow._context.WaifuInfo.ByWaifuUserId(user.Id);
var newAff = target == null ? null : uow.DiscordUsers.GetOrCreate(target);
if (w?.Affinity?.UserId == target?.Id)
{
@@ -268,7 +268,7 @@ namespace NadekoBot.Modules.Gambling.Services
else if (w == null)
{
var thisUser = uow.DiscordUsers.GetOrCreate(user);
uow.Waifus.Add(new WaifuInfo()
uow._context.WaifuInfo.Add(new WaifuInfo()
{
Affinity = newAff,
Waifu = thisUser,
@@ -311,14 +311,14 @@ namespace NadekoBot.Modules.Gambling.Services
{
using (var uow = _db.GetDbContext())
{
return uow.Waifus.GetTop(9, page * 9);
return uow._context.WaifuInfo.GetTop(9, page * 9);
}
}
public ulong GetWaifuUserId(ulong ownerId, string name)
{
using var uow = _db.GetDbContext();
return uow.Waifus.GetWaifuUserId(ownerId, name);
return uow._context.WaifuInfo.GetWaifuUserId(ownerId, name);
}
public async Task<(WaifuInfo, DivorceResult, long, TimeSpan?)> DivorceWaifuAsync(IUser user, ulong targetId)
@@ -329,7 +329,7 @@ namespace NadekoBot.Modules.Gambling.Services
WaifuInfo w = null;
using (var uow = _db.GetDbContext())
{
w = uow.Waifus.ByWaifuUserId(targetId);
w = uow._context.WaifuInfo.ByWaifuUserId(targetId);
var now = DateTime.UtcNow;
if (w?.Claimer == null || w.Claimer.UserId != user.Id)
result = DivorceResult.NotYourWife;
@@ -381,12 +381,12 @@ namespace NadekoBot.Modules.Gambling.Services
using (var uow = _db.GetDbContext())
{
var w = uow.Waifus.ByWaifuUserId(giftedWaifu.Id,
var w = uow._context.WaifuInfo.ByWaifuUserId(giftedWaifu.Id,
set => set.Include(x => x.Items)
.Include(x => x.Claimer));
if (w == null)
{
uow.Waifus.Add(w = new WaifuInfo()
uow._context.WaifuInfo.Add(w = new WaifuInfo()
{
Affinity = null,
Claimer = null,
@@ -420,7 +420,7 @@ namespace NadekoBot.Modules.Gambling.Services
{
using (var uow = _db.GetDbContext())
{
var wi = uow.Waifus.GetWaifuInfo(targetId);
var wi = uow._context.GetWaifuInfo(targetId);
if (wi is null)
{
wi = new WaifuInfoStats

View File

@@ -7,7 +7,6 @@ namespace NadekoBot.Core.Services.Database
public interface IUnitOfWork : IDisposable
{
NadekoContext _context { get; }
IWaifuRepository Waifus { get; }
IDiscordUserRepository DiscordUsers { get; }
int SaveChanges();

View File

@@ -59,6 +59,7 @@ namespace NadekoBot.Core.Services.Database
public DbSet<MusicPlayerSettings> MusicPlayerSettings { get; set; }
public DbSet<Repeater> Repeaters { get; set; }
public DbSet<Poll> Poll { get; set; }
public DbSet<WaifuInfo> WaifuInfo { get; set; }
public NadekoContext(DbContextOptions<NadekoContext> options) : base(options)
{

View File

@@ -1,34 +0,0 @@
using Microsoft.EntityFrameworkCore;
using NadekoBot.Core.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using NadekoBot.Services.Database.Repositories;
namespace NadekoBot.Core.Services.Database.Repositories
{
public interface IWaifuRepository : IRepository<WaifuInfo>
{
IEnumerable<WaifuLbResult> GetTop(int count, int skip = 0);
WaifuInfo ByWaifuUserId(ulong userId, Func<DbSet<WaifuInfo>, IQueryable<WaifuInfo>> includes = null);
IEnumerable<string> GetWaifuNames(ulong userId);
decimal GetTotalValue();
int AffinityCount(ulong userId);
WaifuInfoStats GetWaifuInfo(ulong id);
public ulong GetWaifuUserId(ulong ownerId, string name);
}
public class WaifuInfoStats
{
public string FullName { get; set; }
public int Price { get; set; }
public string ClaimerName { get; set; }
public string AffinityName { get; set; }
public int AffinityCount { get; set; }
public int DivorceCount { get; set; }
public int ClaimCount { get; set; }
public List<WaifuItem> Items { get; set; }
public List<string> Claims { get; set; }
public List<string> Fans { get; set; }
}
}

View File

@@ -1,69 +1,55 @@
using Microsoft.EntityFrameworkCore;
using NadekoBot.Services.Database.Models;

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Core.Services.Database;
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Core.Services.Database.Repositories;
using NadekoBot.Migrations;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Services.Database.Repositories.Impl
namespace NadekoBot.Modules.Gambling
{
public class WaifuRepository : Repository<WaifuInfo>, IWaifuRepository
{
public WaifuRepository(DbContext context) : base(context)
public class WaifuInfoStats
{
public string FullName { get; set; }
public int Price { get; set; }
public string ClaimerName { get; set; }
public string AffinityName { get; set; }
public int AffinityCount { get; set; }
public int DivorceCount { get; set; }
public int ClaimCount { get; set; }
public List<WaifuItem> Items { get; set; }
public List<string> Claims { get; set; }
public List<string> Fans { get; set; }
}
public WaifuInfo ByWaifuUserId(ulong userId, Func<DbSet<WaifuInfo>, IQueryable<WaifuInfo>> includes = null)
public static class WaifuExtensions
{
public static WaifuInfo ByWaifuUserId(this DbSet<WaifuInfo> waifus, ulong userId, Func<DbSet<WaifuInfo>, IQueryable<WaifuInfo>> includes = null)
{
if (includes == null)
{
return _set.Include(wi => wi.Waifu)
return waifus.Include(wi => wi.Waifu)
.Include(wi => wi.Affinity)
.Include(wi => wi.Claimer)
.Include(wi => wi.Items)
.FirstOrDefault(wi => wi.WaifuId == _context.Set<DiscordUser>()
.AsQueryable()
.Where(x => x.UserId == userId)
.Select(x => x.Id)
.FirstOrDefault());
.FirstOrDefault(wi => wi.Waifu.UserId == userId);
}
return includes(_set)
return includes(waifus)
.AsQueryable()
.FirstOrDefault(wi => wi.WaifuId == _context.Set<DiscordUser>()
.AsQueryable()
.Where(x => x.UserId == userId)
.Select(x => x.Id)
.FirstOrDefault());
.FirstOrDefault(wi => wi.Waifu.UserId == userId);
}
public IEnumerable<string> GetWaifuNames(ulong userId)
{
var waifus = _set.AsQueryable().Where(x => x.ClaimerId != null &&
x.ClaimerId == _context.Set<DiscordUser>()
.AsQueryable()
.Where(y => y.UserId == userId)
.Select(y => y.Id)
.FirstOrDefault())
.Select(x => x.WaifuId);
return _context.Set<DiscordUser>()
.AsQueryable()
.Where(x => waifus.Contains(x.Id))
.Select(x => x.Username + "#" + x.Discriminator)
.ToList();
}
public IEnumerable<WaifuLbResult> GetTop(int count, int skip = 0)
public static IEnumerable<WaifuLbResult> GetTop(this DbSet<WaifuInfo> waifus, int count, int skip = 0)
{
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count));
if (count == 0)
return new List<WaifuLbResult>();
return _set.Include(wi => wi.Waifu)
return waifus.Include(wi => wi.Waifu)
.Include(wi => wi.Affinity)
.Include(wi => wi.Claimer)
.OrderByDescending(wi => wi.Price)
@@ -83,22 +69,17 @@ namespace NadekoBot.Services.Database.Repositories.Impl
}
public decimal GetTotalValue()
public static decimal GetTotalValue(this DbSet<WaifuInfo> waifus)
{
return _set
return waifus
.AsQueryable()
.Where(x => x.ClaimerId != null)
.Sum(x => x.Price);
}
public int AffinityCount(ulong userId)
public static int AffinityCount(this DbSet<WaifuUpdate> updates, ulong userId)
{
//return _context.Set<WaifuUpdate>()
// .Count(w => w.User.UserId == userId &&
// w.UpdateType == WaifuUpdateType.AffinityChanged &&
// w.New != null));
return _context.Set<WaifuUpdate>()
return updates
.FromSqlInterpolated($@"SELECT 1
FROM WaifuUpdates
WHERE UserId = (SELECT Id from DiscordUser WHERE UserId={userId}) AND
@@ -107,9 +88,9 @@ WHERE UserId = (SELECT Id from DiscordUser WHERE UserId={userId}) AND
.Count();
}
public ulong GetWaifuUserId(ulong ownerId, string name)
public static ulong GetWaifuUserId(this DbSet<WaifuInfo> waifus, ulong ownerId, string name)
{
return _set
return waifus
.AsQueryable()
.AsNoTracking()
.Where(x => x.Claimer.UserId == ownerId
@@ -118,48 +99,49 @@ WHERE UserId = (SELECT Id from DiscordUser WHERE UserId={userId}) AND
.FirstOrDefault();
}
public WaifuInfoStats GetWaifuInfo(ulong userId)
public static WaifuInfoStats GetWaifuInfo(this NadekoContext ctx, ulong userId)
{
_context.Database.ExecuteSqlInterpolated($@"
ctx.Database.ExecuteSqlInterpolated($@"
INSERT OR IGNORE INTO WaifuInfo (AffinityId, ClaimerId, Price, WaifuId)
VALUES ({null}, {null}, {1}, (SELECT Id FROM DiscordUser WHERE UserId={userId}));");
var toReturn = _set.AsQueryable()
.Where(w => w.WaifuId == _context.Set<DiscordUser>()
var toReturn = ctx.WaifuInfo
.AsQueryable()
.Where(w => w.WaifuId == ctx.Set<DiscordUser>()
.AsQueryable()
.Where(u => u.UserId == userId)
.Select(u => u.Id).FirstOrDefault())
.Select(w => new WaifuInfoStats
{
FullName = _context.Set<DiscordUser>()
FullName = ctx.Set<DiscordUser>()
.AsQueryable()
.Where(u => u.UserId == userId)
.Select(u => u.Username + "#" + u.Discriminator)
.FirstOrDefault(),
AffinityCount = _context.Set<WaifuUpdate>()
AffinityCount = ctx.Set<WaifuUpdate>()
.AsQueryable()
.Count(x => x.UserId == w.WaifuId &&
x.UpdateType == WaifuUpdateType.AffinityChanged &&
x.NewId != null),
AffinityName = _context.Set<DiscordUser>()
AffinityName = ctx.Set<DiscordUser>()
.AsQueryable()
.Where(u => u.Id == w.AffinityId)
.Select(u => u.Username + "#" + u.Discriminator)
.FirstOrDefault(),
ClaimCount = _set
ClaimCount = ctx.WaifuInfo
.AsQueryable()
.Count(x => x.ClaimerId == w.WaifuId),
ClaimerName = _context.Set<DiscordUser>()
ClaimerName = ctx.Set<DiscordUser>()
.AsQueryable()
.Where(u => u.Id == w.ClaimerId)
.Select(u => u.Username + "#" + u.Discriminator)
.FirstOrDefault(),
DivorceCount = _context
DivorceCount = ctx
.Set<WaifuUpdate>()
.AsQueryable()
.Count(x => x.OldId == w.WaifuId &&
@@ -168,14 +150,14 @@ VALUES ({null}, {null}, {1}, (SELECT Id FROM DiscordUser WHERE UserId={userId}))
Price = w.Price,
Claims = _set
Claims = ctx.WaifuInfo
.AsQueryable()
.Include(x => x.Waifu)
.Where(x => x.ClaimerId == w.WaifuId)
.Select(x => x.Waifu.Username + "#" + x.Waifu.Discriminator)
.ToList(),
Fans = _set
Fans = ctx.WaifuInfo
.AsQueryable()
.Include(x => x.Waifu)
.Where(x => x.AffinityId == w.WaifuId)

View File

@@ -11,9 +11,6 @@ namespace NadekoBot.Services.Database
{
public NadekoContext _context { get; }
private IWaifuRepository _waifus;
public IWaifuRepository Waifus => _waifus ?? (_waifus = new WaifuRepository(_context));
private IDiscordUserRepository _discordUsers;
public IDiscordUserRepository DiscordUsers => _discordUsers ?? (_discordUsers = new DiscordUserRepository(_context));