mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 17:28:27 -04:00
dev: Using built in rng.Shuffle, using some new .net types, removed some transactions
This commit is contained in:
@@ -417,8 +417,7 @@ namespace NadekoBot.Coordinator
|
|||||||
{
|
{
|
||||||
lock (locker)
|
lock (locker)
|
||||||
{
|
{
|
||||||
if (shardId >= _shardStatuses.Length)
|
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(shardId, _shardStatuses.Length);
|
||||||
throw new ArgumentOutOfRangeException(nameof(shardId));
|
|
||||||
|
|
||||||
return _shardStatuses[shardId];
|
return _shardStatuses[shardId];
|
||||||
}
|
}
|
||||||
|
@@ -291,8 +291,7 @@ public class MuteService : INService
|
|||||||
|
|
||||||
public async Task<IRole> GetMuteRole(IGuild guild)
|
public async Task<IRole> GetMuteRole(IGuild guild)
|
||||||
{
|
{
|
||||||
if (guild is null)
|
ArgumentNullException.ThrowIfNull(guild);
|
||||||
throw new ArgumentNullException(nameof(guild));
|
|
||||||
|
|
||||||
const string defaultMuteRoleName = "nadeko-mute";
|
const string defaultMuteRoleName = "nadeko-mute";
|
||||||
|
|
||||||
|
@@ -130,8 +130,7 @@ public class VcRoleService : INService
|
|||||||
|
|
||||||
public void AddVcRole(ulong guildId, IRole role, ulong vcId)
|
public void AddVcRole(ulong guildId, IRole role, ulong vcId)
|
||||||
{
|
{
|
||||||
if (role is null)
|
ArgumentNullException.ThrowIfNull(role);
|
||||||
throw new ArgumentNullException(nameof(role));
|
|
||||||
|
|
||||||
var guildVcRoles = VcRoles.GetOrAdd(guildId, new ConcurrentDictionary<ulong, IRole>());
|
var guildVcRoles = VcRoles.GetOrAdd(guildId, new ConcurrentDictionary<ulong, IRole>());
|
||||||
|
|
||||||
|
@@ -24,6 +24,7 @@ public abstract class GamblingModule<TService> : NadekoModule<TService>
|
|||||||
{
|
{
|
||||||
if (amount < 1)
|
if (amount < 1)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (amount < Config.MinBet)
|
if (amount < Config.MinBet)
|
||||||
{
|
{
|
||||||
await Response().Error(strs.min_bet_limit(Format.Bold(Config.MinBet.ToString()) + CurrencySign)).SendAsync();
|
await Response().Error(strs.min_bet_limit(Format.Bold(Config.MinBet.ToString()) + CurrencySign)).SendAsync();
|
||||||
|
@@ -267,20 +267,8 @@ public sealed partial class MusicQueue : IMusicQueue
|
|||||||
{
|
{
|
||||||
lock (_locker)
|
lock (_locker)
|
||||||
{
|
{
|
||||||
var list = tracks.ToList();
|
var list = tracks.ToArray();
|
||||||
|
rng.Shuffle(list);
|
||||||
for (var i = 0; i < list.Count; i++)
|
|
||||||
{
|
|
||||||
var struck = rng.Next(i, list.Count);
|
|
||||||
(list[struck], list[i]) = (list[i], list[struck]);
|
|
||||||
|
|
||||||
// could preserving the index during shuffling be done better?
|
|
||||||
if (i == index)
|
|
||||||
index = struck;
|
|
||||||
else if (struck == index)
|
|
||||||
index = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
tracks = new(list);
|
tracks = new(list);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -111,7 +111,6 @@ public sealed class PatronageService
|
|||||||
var lastDate = lastRun.ToDateOnly();
|
var lastDate = lastRun.ToDateOnly();
|
||||||
|
|
||||||
await using var ctx = _db.GetDbContext();
|
await using var ctx = _db.GetDbContext();
|
||||||
await using var tran = await ctx.Database.BeginTransactionAsync();
|
|
||||||
|
|
||||||
if ((lastDate.Day == 1 || (lastDate.Month != nowDate.Month)) && nowDate.Day > 1)
|
if ((lastDate.Day == 1 || (lastDate.Month != nowDate.Month)) && nowDate.Day > 1)
|
||||||
{
|
{
|
||||||
@@ -141,7 +140,6 @@ public sealed class PatronageService
|
|||||||
|
|
||||||
// assumes that the code above runs in less than an hour
|
// assumes that the code above runs in less than an hour
|
||||||
await _cache.AddAsync(_quotaKey, now.ToBinary());
|
await _cache.AddAsync(_quotaKey, now.ToBinary());
|
||||||
await tran.CommitAsync();
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -171,7 +169,6 @@ public sealed class PatronageService
|
|||||||
|
|
||||||
var lastChargeUtc = subscriber.LastCharge.Value.ToUniversalTime();
|
var lastChargeUtc = subscriber.LastCharge.Value.ToUniversalTime();
|
||||||
var dateInOneMonth = lastChargeUtc.Date.AddMonths(1);
|
var dateInOneMonth = lastChargeUtc.Date.AddMonths(1);
|
||||||
// await using var tran = await ctx.Database.BeginTransactionAsync();
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var dbPatron = await ctx.GetTable<PatronUser>()
|
var dbPatron = await ctx.GetTable<PatronUser>()
|
||||||
|
@@ -1469,7 +1469,6 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
await using var ctx = _db.GetDbContext();
|
await using var ctx = _db.GetDbContext();
|
||||||
// await using var tran = await ctx.Database.BeginTransactionAsync();
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (await ctx.GetTable<XpShopOwnedItem>()
|
if (await ctx.GetTable<XpShopOwnedItem>()
|
||||||
|
@@ -39,7 +39,6 @@ public sealed class NadekoRandom : Random
|
|||||||
public long NextLong(long minValue, long maxValue)
|
public long NextLong(long minValue, long maxValue)
|
||||||
{
|
{
|
||||||
ArgumentOutOfRangeException.ThrowIfGreaterThan(minValue, maxValue);
|
ArgumentOutOfRangeException.ThrowIfGreaterThan(minValue, maxValue);
|
||||||
|
|
||||||
if (minValue == maxValue)
|
if (minValue == maxValue)
|
||||||
return minValue;
|
return minValue;
|
||||||
var bytes = new byte[sizeof(long)];
|
var bytes = new byte[sizeof(long)];
|
||||||
|
@@ -12,5 +12,5 @@ public abstract class DbService
|
|||||||
public abstract Task SetupAsync();
|
public abstract Task SetupAsync();
|
||||||
|
|
||||||
public abstract DbContext CreateRawDbContext(string dbType, string connString);
|
public abstract DbContext CreateRawDbContext(string dbType, string connString);
|
||||||
public abstract DbContext GetDbContext();
|
public abstract NadekoContext GetDbContext();
|
||||||
}
|
}
|
@@ -94,10 +94,8 @@ public class CommandHandler : INService, IReadyExecutor, ICommandHandler
|
|||||||
|
|
||||||
public string SetPrefix(IGuild guild, string prefix)
|
public string SetPrefix(IGuild guild, string prefix)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(prefix))
|
ArgumentNullException.ThrowIfNullOrWhiteSpace(prefix);
|
||||||
throw new ArgumentNullException(nameof(prefix));
|
ArgumentNullException.ThrowIfNull(guild);
|
||||||
if (guild is null)
|
|
||||||
throw new ArgumentNullException(nameof(guild));
|
|
||||||
|
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
|
@@ -20,10 +20,10 @@ public class DefaultWallet : IWallet
|
|||||||
await using var ctx = _db.GetDbContext();
|
await using var ctx = _db.GetDbContext();
|
||||||
var userId = UserId;
|
var userId = UserId;
|
||||||
return await ctx
|
return await ctx
|
||||||
.GetTable<DiscordUser>()
|
.GetTable<DiscordUser>()
|
||||||
.Where(x => x.UserId == userId)
|
.Where(x => x.UserId == userId)
|
||||||
.Select(x => x.CurrencyAmount)
|
.Select(x => x.CurrencyAmount)
|
||||||
.FirstOrDefaultAsync();
|
.FirstOrDefaultAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> Take(long amount, TxData? txData)
|
public async Task<bool> Take(long amount, TxData? txData)
|
||||||
@@ -35,12 +35,12 @@ public class DefaultWallet : IWallet
|
|||||||
|
|
||||||
var userId = UserId;
|
var userId = UserId;
|
||||||
var changed = await ctx
|
var changed = await ctx
|
||||||
.GetTable<DiscordUser>()
|
.GetTable<DiscordUser>()
|
||||||
.Where(x => x.UserId == userId && x.CurrencyAmount >= amount)
|
.Where(x => x.UserId == userId && x.CurrencyAmount >= amount)
|
||||||
.UpdateAsync(x => new()
|
.UpdateAsync(x => new()
|
||||||
{
|
{
|
||||||
CurrencyAmount = x.CurrencyAmount - amount
|
CurrencyAmount = x.CurrencyAmount - amount
|
||||||
});
|
});
|
||||||
|
|
||||||
if (changed == 0)
|
if (changed == 0)
|
||||||
return false;
|
return false;
|
||||||
@@ -48,17 +48,17 @@ public class DefaultWallet : IWallet
|
|||||||
if (txData is not null)
|
if (txData is not null)
|
||||||
{
|
{
|
||||||
await ctx
|
await ctx
|
||||||
.GetTable<CurrencyTransaction>()
|
.GetTable<CurrencyTransaction>()
|
||||||
.InsertAsync(() => new()
|
.InsertAsync(() => new()
|
||||||
{
|
{
|
||||||
Amount = -amount,
|
Amount = -amount,
|
||||||
Note = txData.Note,
|
Note = txData.Note,
|
||||||
UserId = userId,
|
UserId = userId,
|
||||||
Type = txData.Type,
|
Type = txData.Type,
|
||||||
Extra = txData.Extra,
|
Extra = txData.Extra,
|
||||||
OtherId = txData.OtherId,
|
OtherId = txData.OtherId,
|
||||||
DateAdded = DateTime.UtcNow
|
DateAdded = DateTime.UtcNow
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -72,43 +72,37 @@ public class DefaultWallet : IWallet
|
|||||||
await using var ctx = _db.GetDbContext();
|
await using var ctx = _db.GetDbContext();
|
||||||
var userId = UserId;
|
var userId = UserId;
|
||||||
|
|
||||||
await using (var tran = await ctx.Database.BeginTransactionAsync())
|
|
||||||
{
|
|
||||||
var changed = await ctx
|
|
||||||
.GetTable<DiscordUser>()
|
|
||||||
.Where(x => x.UserId == userId)
|
|
||||||
.UpdateAsync(x => new()
|
|
||||||
{
|
|
||||||
CurrencyAmount = x.CurrencyAmount + amount
|
|
||||||
});
|
|
||||||
|
|
||||||
if (changed == 0)
|
await ctx.GetTable<DiscordUser>()
|
||||||
{
|
.InsertOrUpdateAsync(() => new()
|
||||||
await ctx
|
{
|
||||||
.GetTable<DiscordUser>()
|
UserId = userId,
|
||||||
.Value(x => x.UserId, userId)
|
Username = "Unknown",
|
||||||
.Value(x => x.Username, "Unknown")
|
Discriminator = "????",
|
||||||
.Value(x => x.Discriminator, "????")
|
CurrencyAmount = amount,
|
||||||
.Value(x => x.CurrencyAmount, amount)
|
},
|
||||||
.InsertAsync();
|
(old) => new()
|
||||||
}
|
{
|
||||||
|
CurrencyAmount = old.CurrencyAmount + amount
|
||||||
await tran.CommitAsync();
|
},
|
||||||
}
|
() => new()
|
||||||
|
{
|
||||||
|
UserId = userId
|
||||||
|
});
|
||||||
|
|
||||||
if (txData is not null)
|
if (txData is not null)
|
||||||
{
|
{
|
||||||
await ctx.GetTable<CurrencyTransaction>()
|
await ctx.GetTable<CurrencyTransaction>()
|
||||||
.InsertAsync(() => new()
|
.InsertAsync(() => new()
|
||||||
{
|
{
|
||||||
Amount = amount,
|
Amount = amount,
|
||||||
UserId = userId,
|
UserId = userId,
|
||||||
Note = txData.Note,
|
Note = txData.Note,
|
||||||
Type = txData.Type,
|
Type = txData.Type,
|
||||||
Extra = txData.Extra,
|
Extra = txData.Extra,
|
||||||
OtherId = txData.OtherId,
|
OtherId = txData.OtherId,
|
||||||
DateAdded = DateTime.UtcNow
|
DateAdded = DateTime.UtcNow
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user