mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 09:48:26 -04:00
Added and applied styles for private readonly fields, private fields to Extensions and Common folders.
- Some renamings and code cleanups - Chained method calls, binary expressions and binary patterns will now break into newlines - Type param constraints and base constructor calls will be on the new line
This commit is contained in:
@@ -8,10 +8,15 @@ namespace NadekoBot.Db;
|
||||
|
||||
public static class DiscordUserExtensions
|
||||
{
|
||||
public static void EnsureUserCreated(this NadekoContext ctx, ulong userId, string username, string discrim, string avatarId)
|
||||
=> ctx.DiscordUser
|
||||
.ToLinqToDBTable()
|
||||
.InsertOrUpdate(() => new()
|
||||
public static void EnsureUserCreated(
|
||||
this NadekoContext ctx,
|
||||
ulong userId,
|
||||
string username,
|
||||
string discrim,
|
||||
string avatarId)
|
||||
=> ctx.DiscordUser.ToLinqToDBTable()
|
||||
.InsertOrUpdate(
|
||||
() => new()
|
||||
{
|
||||
UserId = userId,
|
||||
Username = username,
|
||||
@@ -20,36 +25,44 @@ public static class DiscordUserExtensions
|
||||
TotalXp = 0,
|
||||
CurrencyAmount = 0
|
||||
},
|
||||
old => new()
|
||||
{
|
||||
Username = username,
|
||||
Discriminator = discrim,
|
||||
AvatarId = avatarId,
|
||||
}, () => new()
|
||||
{
|
||||
UserId = userId
|
||||
});
|
||||
old => new() { Username = username, Discriminator = discrim, AvatarId = avatarId, },
|
||||
() => new() { UserId = userId }
|
||||
);
|
||||
|
||||
//temp is only used in updatecurrencystate, so that i don't overwrite real usernames/discrims with Unknown
|
||||
public static DiscordUser GetOrCreateUser(this NadekoContext ctx, ulong userId, string username, string discrim, string avatarId)
|
||||
public static DiscordUser GetOrCreateUser(
|
||||
this NadekoContext ctx,
|
||||
ulong userId,
|
||||
string username,
|
||||
string discrim,
|
||||
string avatarId)
|
||||
{
|
||||
ctx.EnsureUserCreated(userId, username, discrim, avatarId);
|
||||
return ctx.DiscordUser
|
||||
.Include(x => x.Club)
|
||||
ctx.EnsureUserCreated(userId,
|
||||
username,
|
||||
discrim,
|
||||
avatarId
|
||||
);
|
||||
return ctx.DiscordUser.Include(x => x.Club)
|
||||
.First(u => u.UserId == userId);
|
||||
}
|
||||
|
||||
public static DiscordUser GetOrCreateUser(this NadekoContext ctx, IUser original)
|
||||
=> ctx.GetOrCreateUser(original.Id, original.Username, original.Discriminator, original.AvatarId);
|
||||
=> ctx.GetOrCreateUser(original.Id,
|
||||
original.Username,
|
||||
original.Discriminator,
|
||||
original.AvatarId
|
||||
);
|
||||
|
||||
public static int GetUserGlobalRank(this DbSet<DiscordUser> users, ulong id)
|
||||
=> users.AsQueryable()
|
||||
.Where(x => x.TotalXp > users
|
||||
.AsQueryable()
|
||||
.Where(y => y.UserId == id)
|
||||
.Select(y => y.TotalXp)
|
||||
.FirstOrDefault())
|
||||
.Count() + 1;
|
||||
.Where(x => x.TotalXp >
|
||||
users.AsQueryable()
|
||||
.Where(y => y.UserId == id)
|
||||
.Select(y => y.TotalXp)
|
||||
.FirstOrDefault()
|
||||
)
|
||||
.Count() +
|
||||
1;
|
||||
|
||||
public static DiscordUser[] GetUsersXpLeaderboardFor(this DbSet<DiscordUser> users, int page)
|
||||
=> users.AsQueryable()
|
||||
@@ -59,7 +72,11 @@ public static class DiscordUserExtensions
|
||||
.AsEnumerable()
|
||||
.ToArray();
|
||||
|
||||
public static List<DiscordUser> GetTopRichest(this DbSet<DiscordUser> users, ulong botId, int count, int page = 0)
|
||||
public static List<DiscordUser> GetTopRichest(
|
||||
this DbSet<DiscordUser> users,
|
||||
ulong botId,
|
||||
int count,
|
||||
int page = 0)
|
||||
=> users.AsQueryable()
|
||||
.Where(c => c.CurrencyAmount > 0 && botId != c.UserId)
|
||||
.OrderByDescending(c => c.CurrencyAmount)
|
||||
@@ -67,40 +84,44 @@ public static class DiscordUserExtensions
|
||||
.Take(count)
|
||||
.ToList();
|
||||
|
||||
public static List<DiscordUser> GetTopRichest(this DbSet<DiscordUser> users, ulong botId, int count)
|
||||
=> users.AsQueryable()
|
||||
.Where(c => c.CurrencyAmount > 0 && botId != c.UserId)
|
||||
.OrderByDescending(c => c.CurrencyAmount)
|
||||
.Take(count)
|
||||
.ToList();
|
||||
|
||||
public static long GetUserCurrency(this DbSet<DiscordUser> users, ulong userId) =>
|
||||
users.AsNoTracking()
|
||||
.FirstOrDefault(x => x.UserId == userId)
|
||||
?.CurrencyAmount ?? 0;
|
||||
public static long GetUserCurrency(this DbSet<DiscordUser> users, ulong userId)
|
||||
=> users.AsNoTracking()
|
||||
.FirstOrDefault(x => x.UserId == userId)
|
||||
?.CurrencyAmount ??
|
||||
0;
|
||||
|
||||
public static void RemoveFromMany(this DbSet<DiscordUser> users, IEnumerable<ulong> ids)
|
||||
{
|
||||
var items = users.AsQueryable().Where(x => ids.Contains(x.UserId));
|
||||
var items = users.AsQueryable()
|
||||
.Where(x => ids.Contains(x.UserId));
|
||||
foreach (var item in items)
|
||||
{
|
||||
item.CurrencyAmount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryUpdateCurrencyState(this NadekoContext ctx, 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)
|
||||
return true;
|
||||
|
||||
// if remove - try to remove if he has more or equal than the amount
|
||||
// and return number of rows > 0 (was there a change)
|
||||
if (amount < 0 && !allowNegative)
|
||||
if (amount < 0 &&
|
||||
!allowNegative)
|
||||
{
|
||||
var rows = ctx.Database.ExecuteSqlInterpolated($@"
|
||||
UPDATE DiscordUser
|
||||
SET CurrencyAmount=CurrencyAmount+{amount}
|
||||
WHERE UserId={userId} AND CurrencyAmount>={-amount};");
|
||||
WHERE UserId={userId} AND CurrencyAmount>={-amount};"
|
||||
);
|
||||
return rows > 0;
|
||||
}
|
||||
|
||||
@@ -110,7 +131,8 @@ WHERE UserId={userId} AND CurrencyAmount>={-amount};");
|
||||
var rows = ctx.Database.ExecuteSqlInterpolated($@"
|
||||
UPDATE DiscordUser
|
||||
SET CurrencyAmount=CurrencyAmount+{amount}
|
||||
WHERE UserId={userId};");
|
||||
WHERE UserId={userId};"
|
||||
);
|
||||
return rows > 0;
|
||||
}
|
||||
|
||||
@@ -118,22 +140,22 @@ WHERE UserId={userId};");
|
||||
// if it exists, sum current amount with the new one, if it doesn't
|
||||
// he just has the new amount
|
||||
var updatedUserData = !string.IsNullOrWhiteSpace(name);
|
||||
name = name ?? "Unknown";
|
||||
discrim = discrim ?? "????";
|
||||
avatarId = avatarId ?? "";
|
||||
name ??= "Unknown";
|
||||
discrim ??= "????";
|
||||
avatarId ??= "";
|
||||
|
||||
// just update the amount, there is no new user data
|
||||
if (!updatedUserData)
|
||||
{
|
||||
var rows = ctx.Database.ExecuteSqlInterpolated($@"
|
||||
ctx.Database.ExecuteSqlInterpolated($@"
|
||||
UPDATE OR IGNORE DiscordUser
|
||||
SET CurrencyAmount=CurrencyAmount+{amount}
|
||||
WHERE UserId={userId};
|
||||
|
||||
INSERT OR IGNORE INTO DiscordUser (UserId, Username, Discriminator, AvatarId, CurrencyAmount, TotalXp)
|
||||
VALUES ({userId}, {name}, {discrim}, {avatarId}, {amount}, 0);
|
||||
");
|
||||
|
||||
"
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -147,14 +169,15 @@ WHERE UserId={userId};
|
||||
|
||||
INSERT OR IGNORE INTO DiscordUser (UserId, Username, Discriminator, AvatarId, CurrencyAmount, TotalXp)
|
||||
VALUES ({userId}, {name}, {discrim}, {avatarId}, {amount}, 0);
|
||||
");
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static decimal GetTotalCurrency(this DbSet<DiscordUser> users)
|
||||
=> users
|
||||
.Sum((Func<DiscordUser, decimal>)(x => x.CurrencyAmount));
|
||||
=> users.Sum((Func<DiscordUser, decimal>)(x => x.CurrencyAmount));
|
||||
|
||||
public static decimal GetTopOnePercentCurrency(this DbSet<DiscordUser> users, ulong botId)
|
||||
=> users.AsQueryable()
|
||||
|
Reference in New Issue
Block a user