mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 17:28:27 -04:00
DiscordUser repository removed
This commit is contained in:
@@ -76,7 +76,7 @@ namespace NadekoBot.Core.Common.TypeReaders
|
|||||||
long cur;
|
long cur;
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
cur = uow.DiscordUsers.GetUserCurrency(ctx.User.Id);
|
cur = uow._context.DiscordUser.GetUserCurrency(ctx.User.Id);
|
||||||
uow.SaveChanges();
|
uow.SaveChanges();
|
||||||
}
|
}
|
||||||
return cur;
|
return cur;
|
||||||
|
@@ -51,7 +51,7 @@ namespace NadekoBot.Modules.Gambling
|
|||||||
{
|
{
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
return n(uow.DiscordUsers.GetUserCurrency(id));
|
return n(uow._context.DiscordUser.GetUserCurrency(id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -522,7 +522,7 @@ namespace NadekoBot.Modules.Gambling
|
|||||||
|
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
cleanRichest = uow.DiscordUsers.GetTopRichest(_client.CurrentUser.Id, 10_000);
|
cleanRichest = uow._context.DiscordUser.GetTopRichest(_client.CurrentUser.Id, 10_000);
|
||||||
}
|
}
|
||||||
|
|
||||||
await Context.Channel.TriggerTypingAsync().ConfigureAwait(false);
|
await Context.Channel.TriggerTypingAsync().ConfigureAwait(false);
|
||||||
@@ -536,7 +536,7 @@ namespace NadekoBot.Modules.Gambling
|
|||||||
{
|
{
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
cleanRichest = uow.DiscordUsers.GetTopRichest(_client.CurrentUser.Id, 9, page).ToList();
|
cleanRichest = uow._context.DiscordUser.GetTopRichest(_client.CurrentUser.Id, 9, page).ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -551,7 +551,7 @@ namespace NadekoBot.Modules.Gambling
|
|||||||
{
|
{
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
toSend = uow.DiscordUsers.GetTopRichest(_client.CurrentUser.Id, 9, curPage);
|
toSend = uow._context.DiscordUser.GetTopRichest(_client.CurrentUser.Id, 9, curPage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@@ -10,6 +10,7 @@ using System.Linq;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using NadekoBot.Common;
|
||||||
using NadekoBot.Modules.Gambling.Services;
|
using NadekoBot.Modules.Gambling.Services;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
|
||||||
@@ -135,11 +136,11 @@ WHERE CurrencyAmount > {config.Decay.MinThreshold} AND UserId!={_client.CurrentU
|
|||||||
|
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
cash = uow.DiscordUsers.GetTotalCurrency();
|
cash = uow._context.DiscordUser.GetTotalCurrency();
|
||||||
onePercent = uow.DiscordUsers.GetTopOnePercentCurrency(_client.CurrentUser.Id);
|
onePercent = uow._context.DiscordUser.GetTopOnePercentCurrency(_client.CurrentUser.Id);
|
||||||
planted = uow._context.PlantedCurrency.AsQueryable().Sum(x => x.Amount);
|
planted = uow._context.PlantedCurrency.AsQueryable().Sum(x => x.Amount);
|
||||||
waifus = uow._context.WaifuInfo.GetTotalValue();
|
waifus = uow._context.WaifuInfo.GetTotalValue();
|
||||||
bot = uow.DiscordUsers.GetUserCurrency(_client.CurrentUser.Id);
|
bot = uow._context.DiscordUser.GetUserCurrency(_client.CurrentUser.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = new EconomyResult
|
var result = new EconomyResult
|
||||||
|
@@ -47,7 +47,7 @@ namespace NadekoBot.Modules.Gambling.Services
|
|||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
var waifu = uow._context.WaifuInfo.ByWaifuUserId(waifuId);
|
var waifu = uow._context.WaifuInfo.ByWaifuUserId(waifuId);
|
||||||
var ownerUser = uow.DiscordUsers.GetOrCreate(owner);
|
var ownerUser = uow._context.GetOrCreateUser(owner);
|
||||||
|
|
||||||
// owner has to be the owner of the waifu
|
// owner has to be the owner of the waifu
|
||||||
if (waifu == null || waifu.ClaimerId != ownerUser.Id)
|
if (waifu == null || waifu.ClaimerId != ownerUser.Id)
|
||||||
@@ -82,7 +82,7 @@ namespace NadekoBot.Modules.Gambling.Services
|
|||||||
}
|
}
|
||||||
|
|
||||||
//new claimerId is the id of the new owner
|
//new claimerId is the id of the new owner
|
||||||
var newOwnerUser = uow.DiscordUsers.GetOrCreate(newOwner);
|
var newOwnerUser = uow._context.GetOrCreateUser(newOwner);
|
||||||
waifu.ClaimerId = newOwnerUser.Id;
|
waifu.ClaimerId = newOwnerUser.Id;
|
||||||
|
|
||||||
await uow.SaveChangesAsync();
|
await uow.SaveChangesAsync();
|
||||||
@@ -171,8 +171,8 @@ namespace NadekoBot.Modules.Gambling.Services
|
|||||||
isAffinity = (w?.Affinity?.UserId == user.Id);
|
isAffinity = (w?.Affinity?.UserId == user.Id);
|
||||||
if (w == null)
|
if (w == null)
|
||||||
{
|
{
|
||||||
var claimer = uow.DiscordUsers.GetOrCreate(user);
|
var claimer = uow._context.GetOrCreateUser(user);
|
||||||
var waifu = uow.DiscordUsers.GetOrCreate(target);
|
var waifu = uow._context.GetOrCreateUser(target);
|
||||||
if (!await _cs.RemoveAsync(user.Id, "Claimed Waifu", amount, gamble: true))
|
if (!await _cs.RemoveAsync(user.Id, "Claimed Waifu", amount, gamble: true))
|
||||||
{
|
{
|
||||||
result = WaifuClaimResult.NotEnoughFunds;
|
result = WaifuClaimResult.NotEnoughFunds;
|
||||||
@@ -205,7 +205,7 @@ namespace NadekoBot.Modules.Gambling.Services
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var oldClaimer = w.Claimer;
|
var oldClaimer = w.Claimer;
|
||||||
w.Claimer = uow.DiscordUsers.GetOrCreate(user);
|
w.Claimer = uow._context.GetOrCreateUser(user);
|
||||||
w.Price = amount + (amount / 4);
|
w.Price = amount + (amount / 4);
|
||||||
result = WaifuClaimResult.Success;
|
result = WaifuClaimResult.Success;
|
||||||
|
|
||||||
@@ -227,7 +227,7 @@ namespace NadekoBot.Modules.Gambling.Services
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var oldClaimer = w.Claimer;
|
var oldClaimer = w.Claimer;
|
||||||
w.Claimer = uow.DiscordUsers.GetOrCreate(user);
|
w.Claimer = uow._context.GetOrCreateUser(user);
|
||||||
w.Price = amount;
|
w.Price = amount;
|
||||||
result = WaifuClaimResult.Success;
|
result = WaifuClaimResult.Success;
|
||||||
|
|
||||||
@@ -258,7 +258,7 @@ namespace NadekoBot.Modules.Gambling.Services
|
|||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
var w = uow._context.WaifuInfo.ByWaifuUserId(user.Id);
|
var w = uow._context.WaifuInfo.ByWaifuUserId(user.Id);
|
||||||
var newAff = target == null ? null : uow.DiscordUsers.GetOrCreate(target);
|
var newAff = target == null ? null : uow._context.GetOrCreateUser(target);
|
||||||
if (w?.Affinity?.UserId == target?.Id)
|
if (w?.Affinity?.UserId == target?.Id)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -267,7 +267,7 @@ namespace NadekoBot.Modules.Gambling.Services
|
|||||||
}
|
}
|
||||||
else if (w == null)
|
else if (w == null)
|
||||||
{
|
{
|
||||||
var thisUser = uow.DiscordUsers.GetOrCreate(user);
|
var thisUser = uow._context.GetOrCreateUser(user);
|
||||||
uow._context.WaifuInfo.Add(new WaifuInfo()
|
uow._context.WaifuInfo.Add(new WaifuInfo()
|
||||||
{
|
{
|
||||||
Affinity = newAff,
|
Affinity = newAff,
|
||||||
@@ -391,7 +391,7 @@ namespace NadekoBot.Modules.Gambling.Services
|
|||||||
Affinity = null,
|
Affinity = null,
|
||||||
Claimer = null,
|
Claimer = null,
|
||||||
Price = 1,
|
Price = 1,
|
||||||
Waifu = uow.DiscordUsers.GetOrCreate(giftedWaifu),
|
Waifu = uow._context.GetOrCreateUser(giftedWaifu),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -445,7 +445,7 @@ namespace NadekoBot.Modules.Gambling.Services
|
|||||||
{
|
{
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
var du = uow.DiscordUsers.GetOrCreate(target);
|
var du = uow._context.GetOrCreateUser(target);
|
||||||
|
|
||||||
return GetFullWaifuInfoAsync(target.Id);
|
return GetFullWaifuInfoAsync(target.Id);
|
||||||
}
|
}
|
||||||
|
@@ -110,7 +110,7 @@ namespace NadekoBot.Modules.Permissions.Services
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
//clear their currencies
|
//clear their currencies
|
||||||
uow.DiscordUsers.RemoveFromMany(toBlacklist);
|
uow._context.DiscordUser.RemoveFromMany(toBlacklist);
|
||||||
uow.SaveChanges();
|
uow.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -29,7 +29,7 @@ namespace NadekoBot.Modules.Xp.Services
|
|||||||
|
|
||||||
club = null;
|
club = null;
|
||||||
using var uow = _db.GetDbContext();
|
using var uow = _db.GetDbContext();
|
||||||
var du = uow.DiscordUsers.GetOrCreate(user);
|
var du = uow._context.GetOrCreateUser(user);
|
||||||
uow._context.SaveChanges();
|
uow._context.SaveChanges();
|
||||||
var xp = new LevelStats(du.TotalXp);
|
var xp = new LevelStats(du.TotalXp);
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@ namespace NadekoBot.Modules.Xp.Services
|
|||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
club = uow._context.Clubs.GetByOwner(from.Id);
|
club = uow._context.Clubs.GetByOwner(from.Id);
|
||||||
var newOwnerUser = uow.DiscordUsers.GetOrCreate(newOwner);
|
var newOwnerUser = uow._context.GetOrCreateUser(newOwner);
|
||||||
|
|
||||||
if (club == null ||
|
if (club == null ||
|
||||||
club.Owner.UserId != from.Id ||
|
club.Owner.UserId != from.Id ||
|
||||||
@@ -85,7 +85,7 @@ namespace NadekoBot.Modules.Xp.Services
|
|||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
var club = uow._context.Clubs.GetByOwner(owner.Id);
|
var club = uow._context.Clubs.GetByOwner(owner.Id);
|
||||||
var adminUser = uow.DiscordUsers.GetOrCreate(toAdmin);
|
var adminUser = uow._context.GetOrCreateUser(toAdmin);
|
||||||
|
|
||||||
if (club == null || club.Owner.UserId != owner.Id ||
|
if (club == null || club.Owner.UserId != owner.Id ||
|
||||||
!club.Users.Contains(adminUser))
|
!club.Users.Contains(adminUser))
|
||||||
@@ -160,7 +160,7 @@ namespace NadekoBot.Modules.Xp.Services
|
|||||||
{
|
{
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
var du = uow.DiscordUsers.GetOrCreate(user);
|
var du = uow._context.GetOrCreateUser(user);
|
||||||
uow._context.SaveChanges();
|
uow._context.SaveChanges();
|
||||||
|
|
||||||
if (du.Club != null
|
if (du.Club != null
|
||||||
@@ -227,7 +227,7 @@ namespace NadekoBot.Modules.Xp.Services
|
|||||||
{
|
{
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
var du = uow.DiscordUsers.GetOrCreate(user);
|
var du = uow._context.GetOrCreateUser(user);
|
||||||
if (du.Club == null || du.Club.OwnerId == du.Id)
|
if (du.Club == null || du.Club.OwnerId == du.Id)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@@ -168,7 +168,7 @@ namespace NadekoBot.Modules.Xp.Services
|
|||||||
// amounts for every user (in order to give rewards)
|
// amounts for every user (in order to give rewards)
|
||||||
|
|
||||||
var usr = uow._context.GetOrCreateUserXpStats(item.Key.GuildId, item.Key.User.Id);
|
var usr = uow._context.GetOrCreateUserXpStats(item.Key.GuildId, item.Key.User.Id);
|
||||||
var du = uow.DiscordUsers.GetOrCreate(item.Key.User);
|
var du = uow._context.GetOrCreateUser(item.Key.User);
|
||||||
|
|
||||||
var globalXp = du.TotalXp;
|
var globalXp = du.TotalXp;
|
||||||
var oldGlobalLevelData = new LevelStats(globalXp);
|
var oldGlobalLevelData = new LevelStats(globalXp);
|
||||||
@@ -425,7 +425,7 @@ namespace NadekoBot.Modules.Xp.Services
|
|||||||
{
|
{
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
return uow.DiscordUsers.GetUsersXpLeaderboardFor(page);
|
return uow._context.DiscordUser.GetUsersXpLeaderboardFor(page);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -452,7 +452,7 @@ namespace NadekoBot.Modules.Xp.Services
|
|||||||
{
|
{
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
return uow.DiscordUsers.GetOrCreate(user).NotifyOnLevelUp;
|
return uow._context.GetOrCreateUser(user).NotifyOnLevelUp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -460,7 +460,7 @@ namespace NadekoBot.Modules.Xp.Services
|
|||||||
{
|
{
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
var du = uow.DiscordUsers.GetOrCreate(user);
|
var du = uow._context.GetOrCreateUser(user);
|
||||||
du.NotifyOnLevelUp = type;
|
du.NotifyOnLevelUp = type;
|
||||||
await uow.SaveChangesAsync();
|
await uow.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
@@ -706,9 +706,9 @@ namespace NadekoBot.Modules.Xp.Services
|
|||||||
int guildRank;
|
int guildRank;
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
du = uow.DiscordUsers.GetOrCreate(user);
|
du = uow._context.GetOrCreateUser(user);
|
||||||
totalXp = du.TotalXp;
|
totalXp = du.TotalXp;
|
||||||
globalRank = uow.DiscordUsers.GetUserGlobalRank(user.Id);
|
globalRank = uow._context.DiscordUser.GetUserGlobalRank(user.Id);
|
||||||
guildRank = uow._context.UserXpStats.GetUserGuildRanking(user.Id, user.GuildId);
|
guildRank = uow._context.UserXpStats.GetUserGuildRanking(user.Id, user.GuildId);
|
||||||
stats = uow._context.GetOrCreateUserXpStats(user.GuildId, user.Id);
|
stats = uow._context.GetOrCreateUserXpStats(user.GuildId, user.Id);
|
||||||
await uow.SaveChangesAsync();
|
await uow.SaveChangesAsync();
|
||||||
|
@@ -7,7 +7,6 @@ namespace NadekoBot.Core.Services.Database
|
|||||||
public interface IUnitOfWork : IDisposable
|
public interface IUnitOfWork : IDisposable
|
||||||
{
|
{
|
||||||
NadekoContext _context { get; }
|
NadekoContext _context { get; }
|
||||||
IDiscordUserRepository DiscordUsers { get; }
|
|
||||||
|
|
||||||
int SaveChanges();
|
int SaveChanges();
|
||||||
Task<int> SaveChangesAsync();
|
Task<int> SaveChangesAsync();
|
||||||
|
@@ -1,28 +0,0 @@
|
|||||||
using System;
|
|
||||||
using Discord;
|
|
||||||
using NadekoBot.Core.Services.Database.Models;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using NadekoBot.Services.Database.Models;
|
|
||||||
using NadekoBot.Services.Database.Repositories;
|
|
||||||
|
|
||||||
namespace NadekoBot.Core.Services.Database.Repositories
|
|
||||||
{
|
|
||||||
public interface IDiscordUserRepository : IRepository<DiscordUser>
|
|
||||||
{
|
|
||||||
void EnsureCreated(ulong userId, string username, string discrim, string avatarId);
|
|
||||||
DiscordUser GetOrCreate(ulong userId, string username, string discrim, string avatarId);
|
|
||||||
DiscordUser GetOrCreate(IUser original);
|
|
||||||
int GetUserGlobalRank(ulong id);
|
|
||||||
DiscordUser[] GetUsersXpLeaderboardFor(int page);
|
|
||||||
|
|
||||||
long GetUserCurrency(ulong userId);
|
|
||||||
bool TryUpdateCurrencyState(ulong userId, string name, string discrim, string avatar, long change, bool allowNegative = false);
|
|
||||||
List<DiscordUser> GetTopRichest(ulong botId, int count, int page);
|
|
||||||
List<DiscordUser> GetTopRichest(ulong botId, int count);
|
|
||||||
void RemoveFromMany(IEnumerable<ulong> ids);
|
|
||||||
decimal GetTotalCurrency();
|
|
||||||
decimal GetTopOnePercentCurrency(ulong botId);
|
|
||||||
}
|
|
||||||
}
|
|
@@ -4,19 +4,16 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using Discord;
|
using Discord;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System;
|
using System;
|
||||||
|
using NadekoBot.Core.Services.Database;
|
||||||
using NadekoBot.Core.Services.Database.Repositories;
|
using NadekoBot.Core.Services.Database.Repositories;
|
||||||
|
|
||||||
namespace NadekoBot.Services.Database.Repositories.Impl
|
namespace NadekoBot.Core.Services
|
||||||
{
|
{
|
||||||
public class DiscordUserRepository : Repository<DiscordUser>, IDiscordUserRepository
|
public static class DiscordUserExtensions
|
||||||
{
|
{
|
||||||
public DiscordUserRepository(DbContext context) : base(context)
|
public static void EnsureUserCreated(this NadekoContext ctx, ulong userId, string username, string discrim, string avatarId)
|
||||||
{
|
{
|
||||||
}
|
ctx.Database.ExecuteSqlInterpolated($@"
|
||||||
|
|
||||||
public void EnsureCreated(ulong userId, string username, string discrim, string avatarId)
|
|
||||||
{
|
|
||||||
_context.Database.ExecuteSqlInterpolated($@"
|
|
||||||
UPDATE OR IGNORE DiscordUser
|
UPDATE OR IGNORE DiscordUser
|
||||||
SET Username={username},
|
SET Username={username},
|
||||||
Discriminator={discrim},
|
Discriminator={discrim},
|
||||||
@@ -29,27 +26,21 @@ VALUES ({userId}, {username}, {discrim}, {avatarId});
|
|||||||
}
|
}
|
||||||
|
|
||||||
//temp is only used in updatecurrencystate, so that i don't overwrite real usernames/discrims with Unknown
|
//temp is only used in updatecurrencystate, so that i don't overwrite real usernames/discrims with Unknown
|
||||||
public DiscordUser GetOrCreate(ulong userId, string username, string discrim, string avatarId)
|
public static DiscordUser GetOrCreateUser(this NadekoContext ctx, ulong userId, string username, string discrim, string avatarId)
|
||||||
{
|
{
|
||||||
EnsureCreated(userId, username, discrim, avatarId);
|
ctx.EnsureUserCreated(userId, username, discrim, avatarId);
|
||||||
return _set
|
return ctx.DiscordUser
|
||||||
.Include(x => x.Club)
|
.Include(x => x.Club)
|
||||||
.First(u => u.UserId == userId);
|
.First(u => u.UserId == userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DiscordUser GetOrCreate(IUser original)
|
public static DiscordUser GetOrCreateUser(this NadekoContext ctx, IUser original)
|
||||||
=> GetOrCreate(original.Id, original.Username, original.Discriminator, original.AvatarId);
|
=> ctx.GetOrCreateUser(original.Id, original.Username, original.Discriminator, original.AvatarId);
|
||||||
|
|
||||||
public int GetUserGlobalRank(ulong id)
|
public static int GetUserGlobalRank(this DbSet<DiscordUser> users, ulong id)
|
||||||
{
|
{
|
||||||
// @"SELECT COUNT(*) + 1
|
return users.AsQueryable()
|
||||||
//FROM DiscordUser
|
.Where(x => x.TotalXp > (users
|
||||||
//WHERE TotalXp > COALESCE((SELECT TotalXp
|
|
||||||
// FROM DiscordUser
|
|
||||||
// WHERE UserId = @p1
|
|
||||||
// LIMIT 1), 0);"
|
|
||||||
return _set.AsQueryable()
|
|
||||||
.Where(x => x.TotalXp > (_set
|
|
||||||
.AsQueryable()
|
.AsQueryable()
|
||||||
.Where(y => y.UserId == id)
|
.Where(y => y.UserId == id)
|
||||||
.Select(y => y.TotalXp)
|
.Select(y => y.TotalXp)
|
||||||
@@ -57,9 +48,9 @@ VALUES ({userId}, {username}, {discrim}, {avatarId});
|
|||||||
.Count() + 1;
|
.Count() + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DiscordUser[] GetUsersXpLeaderboardFor(int page)
|
public static DiscordUser[] GetUsersXpLeaderboardFor(this DbSet<DiscordUser> users, int page)
|
||||||
{
|
{
|
||||||
return _set.AsQueryable()
|
return users.AsQueryable()
|
||||||
.OrderByDescending(x => x.TotalXp)
|
.OrderByDescending(x => x.TotalXp)
|
||||||
.Skip(page * 9)
|
.Skip(page * 9)
|
||||||
.Take(9)
|
.Take(9)
|
||||||
@@ -67,9 +58,9 @@ VALUES ({userId}, {username}, {discrim}, {avatarId});
|
|||||||
.ToArray();
|
.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<DiscordUser> GetTopRichest(ulong botId, int count, int page = 0)
|
public static List<DiscordUser> GetTopRichest(this DbSet<DiscordUser> users, ulong botId, int count, int page = 0)
|
||||||
{
|
{
|
||||||
return _set.AsQueryable()
|
return users.AsQueryable()
|
||||||
.Where(c => c.CurrencyAmount > 0 && botId != c.UserId)
|
.Where(c => c.CurrencyAmount > 0 && botId != c.UserId)
|
||||||
.OrderByDescending(c => c.CurrencyAmount)
|
.OrderByDescending(c => c.CurrencyAmount)
|
||||||
.Skip(page * 9)
|
.Skip(page * 9)
|
||||||
@@ -77,30 +68,30 @@ VALUES ({userId}, {username}, {discrim}, {avatarId});
|
|||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<DiscordUser> GetTopRichest(ulong botId, int count)
|
public static List<DiscordUser> GetTopRichest(this DbSet<DiscordUser> users, ulong botId, int count)
|
||||||
{
|
{
|
||||||
return _set.AsQueryable()
|
return users.AsQueryable()
|
||||||
.Where(c => c.CurrencyAmount > 0 && botId != c.UserId)
|
.Where(c => c.CurrencyAmount > 0 && botId != c.UserId)
|
||||||
.OrderByDescending(c => c.CurrencyAmount)
|
.OrderByDescending(c => c.CurrencyAmount)
|
||||||
.Take(count)
|
.Take(count)
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public long GetUserCurrency(ulong userId) =>
|
public static long GetUserCurrency(this DbSet<DiscordUser> users, ulong userId) =>
|
||||||
_set.AsNoTracking()
|
users.AsNoTracking()
|
||||||
.FirstOrDefault(x => x.UserId == userId)
|
.FirstOrDefault(x => x.UserId == userId)
|
||||||
?.CurrencyAmount ?? 0;
|
?.CurrencyAmount ?? 0;
|
||||||
|
|
||||||
public void RemoveFromMany(IEnumerable<ulong> ids)
|
public static void RemoveFromMany(this DbSet<DiscordUser> users, IEnumerable<ulong> ids)
|
||||||
{
|
{
|
||||||
var items = _set.AsQueryable().Where(x => ids.Contains(x.UserId));
|
var items = users.AsQueryable().Where(x => ids.Contains(x.UserId));
|
||||||
foreach (var item in items)
|
foreach (var item in items)
|
||||||
{
|
{
|
||||||
item.CurrencyAmount = 0;
|
item.CurrencyAmount = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool TryUpdateCurrencyState(ulong userId, string name, string discrim, string avatarId, long amount, bool allowNegative = false)
|
public static bool TryUpdateCurrencyState(this NadekoContext ctx, ulong userId, string name, string discrim, string avatarId, long amount, bool allowNegative = false)
|
||||||
{
|
{
|
||||||
if (amount == 0)
|
if (amount == 0)
|
||||||
return true;
|
return true;
|
||||||
@@ -109,7 +100,7 @@ VALUES ({userId}, {username}, {discrim}, {avatarId});
|
|||||||
// and return number of rows > 0 (was there a change)
|
// and return number of rows > 0 (was there a change)
|
||||||
if (amount < 0 && !allowNegative)
|
if (amount < 0 && !allowNegative)
|
||||||
{
|
{
|
||||||
var rows = _context.Database.ExecuteSqlInterpolated($@"
|
var rows = ctx.Database.ExecuteSqlInterpolated($@"
|
||||||
UPDATE DiscordUser
|
UPDATE DiscordUser
|
||||||
SET CurrencyAmount=CurrencyAmount+{amount}
|
SET CurrencyAmount=CurrencyAmount+{amount}
|
||||||
WHERE UserId={userId} AND CurrencyAmount>={-amount};");
|
WHERE UserId={userId} AND CurrencyAmount>={-amount};");
|
||||||
@@ -119,7 +110,7 @@ WHERE UserId={userId} AND CurrencyAmount>={-amount};");
|
|||||||
// if remove and negative is allowed, just remove without any condition
|
// if remove and negative is allowed, just remove without any condition
|
||||||
if (amount < 0 && allowNegative)
|
if (amount < 0 && allowNegative)
|
||||||
{
|
{
|
||||||
var rows = _context.Database.ExecuteSqlInterpolated($@"
|
var rows = ctx.Database.ExecuteSqlInterpolated($@"
|
||||||
UPDATE DiscordUser
|
UPDATE DiscordUser
|
||||||
SET CurrencyAmount=CurrencyAmount+{amount}
|
SET CurrencyAmount=CurrencyAmount+{amount}
|
||||||
WHERE UserId={userId};");
|
WHERE UserId={userId};");
|
||||||
@@ -137,7 +128,7 @@ WHERE UserId={userId};");
|
|||||||
// just update the amount, there is no new user data
|
// just update the amount, there is no new user data
|
||||||
if (!updatedUserData)
|
if (!updatedUserData)
|
||||||
{
|
{
|
||||||
_context.Database.ExecuteSqlInterpolated($@"
|
ctx.Database.ExecuteSqlInterpolated($@"
|
||||||
UPDATE OR IGNORE DiscordUser
|
UPDATE OR IGNORE DiscordUser
|
||||||
SET CurrencyAmount=CurrencyAmount+{amount}
|
SET CurrencyAmount=CurrencyAmount+{amount}
|
||||||
WHERE UserId={userId};
|
WHERE UserId={userId};
|
||||||
@@ -148,7 +139,7 @@ VALUES ({userId}, {name}, {discrim}, {avatarId}, {amount});
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_context.Database.ExecuteSqlInterpolated($@"
|
ctx.Database.ExecuteSqlInterpolated($@"
|
||||||
UPDATE OR IGNORE DiscordUser
|
UPDATE OR IGNORE DiscordUser
|
||||||
SET CurrencyAmount=CurrencyAmount+{amount},
|
SET CurrencyAmount=CurrencyAmount+{amount},
|
||||||
Username={name},
|
Username={name},
|
||||||
@@ -163,18 +154,18 @@ VALUES ({userId}, {name}, {discrim}, {avatarId}, {amount});
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public decimal GetTotalCurrency()
|
public static decimal GetTotalCurrency(this DbSet<DiscordUser> users)
|
||||||
{
|
{
|
||||||
return _set
|
return users
|
||||||
.Sum(x => x.CurrencyAmount);
|
.Sum(x => x.CurrencyAmount);
|
||||||
}
|
}
|
||||||
|
|
||||||
public decimal GetTopOnePercentCurrency(ulong botId)
|
public static decimal GetTopOnePercentCurrency(this DbSet<DiscordUser> users, ulong botId)
|
||||||
{
|
{
|
||||||
return _set.AsQueryable()
|
return users.AsQueryable()
|
||||||
.Where(x => x.UserId != botId)
|
.Where(x => x.UserId != botId)
|
||||||
.OrderByDescending(x => x.CurrencyAmount)
|
.OrderByDescending(x => x.CurrencyAmount)
|
||||||
.Take(_set.Count() / 100 == 0 ? 1 : _set.Count() / 100)
|
.Take(users.Count() / 100 == 0 ? 1 : users.Count() / 100)
|
||||||
.Sum(x => x.CurrencyAmount);
|
.Sum(x => x.CurrencyAmount);
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -11,9 +11,6 @@ namespace NadekoBot.Services.Database
|
|||||||
{
|
{
|
||||||
public NadekoContext _context { get; }
|
public NadekoContext _context { get; }
|
||||||
|
|
||||||
private IDiscordUserRepository _discordUsers;
|
|
||||||
public IDiscordUserRepository DiscordUsers => _discordUsers ?? (_discordUsers = new DiscordUserRepository(_context));
|
|
||||||
|
|
||||||
public UnitOfWork(NadekoContext context)
|
public UnitOfWork(NadekoContext context)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
|
@@ -35,7 +35,7 @@ namespace NadekoBot.Core.Services
|
|||||||
private bool InternalChange(ulong userId, string userName, string discrim, string avatar,
|
private bool InternalChange(ulong userId, string userName, string discrim, string avatar,
|
||||||
string reason, long amount, bool gamble, IUnitOfWork uow)
|
string reason, long amount, bool gamble, IUnitOfWork uow)
|
||||||
{
|
{
|
||||||
var result = uow.DiscordUsers.TryUpdateCurrencyState(userId, userName, discrim, avatar, amount);
|
var result = uow._context.TryUpdateCurrencyState(userId, userName, discrim, avatar, amount);
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
var t = GetCurrencyTransaction(userId, reason, amount);
|
var t = GetCurrencyTransaction(userId, reason, amount);
|
||||||
@@ -45,7 +45,7 @@ namespace NadekoBot.Core.Services
|
|||||||
{
|
{
|
||||||
var t2 = GetCurrencyTransaction(_bot.Id, reason, -amount);
|
var t2 = GetCurrencyTransaction(_bot.Id, reason, -amount);
|
||||||
uow._context.CurrencyTransactions.Add(t2);
|
uow._context.CurrencyTransactions.Add(t2);
|
||||||
uow.DiscordUsers.TryUpdateCurrencyState(_bot.Id, _bot.Username, _bot.Discriminator, _bot.AvatarId, -amount, true);
|
uow._context.TryUpdateCurrencyState(_bot.Id, _bot.Username, _bot.Discriminator, _bot.AvatarId, -amount, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@@ -147,7 +147,7 @@ namespace NadekoBot
|
|||||||
|
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
uow.DiscordUsers.EnsureCreated(_bot.Id, _bot.Username, _bot.Discriminator, _bot.AvatarId);
|
uow._context.EnsureUserCreated(_bot.Id, _bot.Username, _bot.Discriminator, _bot.AvatarId);
|
||||||
AllGuildConfigs = uow._context.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();
|
AllGuildConfigs = uow._context.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user