mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-05-02 04:22:04 -04:00
Another attempt to stop all the EF issues :(
This commit is contained in:
@@ -26,7 +26,7 @@ public class GetBalanceCommand : ICommand
|
|||||||
public async Task RunCommand(ChatBot botInstance, MessageModel message, UserDbModel user, GroupCollection arguments,
|
public async Task RunCommand(ChatBot botInstance, MessageModel message, UserDbModel user, GroupCollection arguments,
|
||||||
CancellationToken ctx)
|
CancellationToken ctx)
|
||||||
{
|
{
|
||||||
var gambler = await Money.GetGamblerEntityAsync(user, ct: ctx);
|
var gambler = await Money.GetGamblerEntityAsync(user.Id, ct: ctx);
|
||||||
await botInstance.SendChatMessageAsync(
|
await botInstance.SendChatMessageAsync(
|
||||||
$"{user.FormatUsername()}, your balance is {await gambler!.Balance.FormatKasinoCurrencyAsync()}", true);
|
$"{user.FormatUsername()}, your balance is {await gambler!.Balance.FormatKasinoCurrencyAsync()}", true);
|
||||||
}
|
}
|
||||||
@@ -45,12 +45,12 @@ public class GetExclusionCommand : ICommand
|
|||||||
public async Task RunCommand(ChatBot botInstance, MessageModel message, UserDbModel user, GroupCollection arguments,
|
public async Task RunCommand(ChatBot botInstance, MessageModel message, UserDbModel user, GroupCollection arguments,
|
||||||
CancellationToken ctx)
|
CancellationToken ctx)
|
||||||
{
|
{
|
||||||
var gambler = await Money.GetGamblerEntityAsync(user, ct: ctx);
|
var gambler = await Money.GetGamblerEntityAsync(user.Id, ct: ctx);
|
||||||
if (gambler == null)
|
if (gambler == null)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Caught a null when retrieving {user.Id}'s gambler entity");
|
throw new InvalidOperationException($"Caught a null when retrieving {user.Id}'s gambler entity");
|
||||||
}
|
}
|
||||||
var exclusion = await Money.GetActiveExclusionAsync(gambler, ct: ctx);
|
var exclusion = await Money.GetActiveExclusionAsync(gambler.Id, ct: ctx);
|
||||||
if (exclusion == null)
|
if (exclusion == null)
|
||||||
{
|
{
|
||||||
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, you are currently not excluded.", true);
|
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, you are currently not excluded.", true);
|
||||||
@@ -84,7 +84,7 @@ public class SendJuiceCommand : ICommand
|
|||||||
{
|
{
|
||||||
var logger = LogManager.GetCurrentClassLogger();
|
var logger = LogManager.GetCurrentClassLogger();
|
||||||
await using var db = new ApplicationDbContext();
|
await using var db = new ApplicationDbContext();
|
||||||
var gambler = await Money.GetGamblerEntityAsync(user, ct: ctx);
|
var gambler = await Money.GetGamblerEntityAsync(user.Id, ct: ctx);
|
||||||
var targetUser = await db.Users.FirstOrDefaultAsync(u => u.KfId == int.Parse(arguments["user_id"].Value), ctx);
|
var targetUser = await db.Users.FirstOrDefaultAsync(u => u.KfId == int.Parse(arguments["user_id"].Value), ctx);
|
||||||
var amount = decimal.Parse(arguments["amount"].Value);
|
var amount = decimal.Parse(arguments["amount"].Value);
|
||||||
if (gambler == null)
|
if (gambler == null)
|
||||||
@@ -104,17 +104,17 @@ public class SendJuiceCommand : ICommand
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var targetGambler = await Money.GetGamblerEntityAsync(targetUser, ct: ctx);
|
var targetGambler = await Money.GetGamblerEntityAsync(targetUser.Id, ct: ctx);
|
||||||
if (targetGambler == null)
|
if (targetGambler == null)
|
||||||
{
|
{
|
||||||
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, you can't juice a banned user", true);
|
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, you can't juice a banned user", true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await Money.ModifyBalanceAsync(gambler, -amount, TransactionSourceEventType.Juicer,
|
await Money.ModifyBalanceAsync(gambler.Id, -amount, TransactionSourceEventType.Juicer,
|
||||||
$"Juice sent to {targetUser.KfUsername}", ct: ctx);
|
$"Juice sent to {targetUser.KfUsername}", ct: ctx);
|
||||||
await Money.ModifyBalanceAsync(targetGambler, amount, TransactionSourceEventType.Juicer, $"Juice from {user.KfUsername}",
|
await Money.ModifyBalanceAsync(targetGambler.Id, amount, TransactionSourceEventType.Juicer, $"Juice from {user.KfUsername}",
|
||||||
gambler, ctx);
|
gambler.Id, ctx);
|
||||||
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, {await amount.FormatKasinoCurrencyAsync()} has been sent to {targetUser.KfUsername}", true);
|
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, {await amount.FormatKasinoCurrencyAsync()} has been sent to {targetUser.KfUsername}", true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -138,7 +138,7 @@ public class RakebackCommand : ICommand
|
|||||||
CancellationToken ctx)
|
CancellationToken ctx)
|
||||||
{
|
{
|
||||||
await using var db = new ApplicationDbContext();
|
await using var db = new ApplicationDbContext();
|
||||||
var gambler = await Money.GetGamblerEntityAsync(user, ct: ctx);
|
var gambler = await Money.GetGamblerEntityAsync(user.Id, ct: ctx);
|
||||||
if (gambler == null)
|
if (gambler == null)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Caught a null when retrieving {user.Id}'s gambler entity");
|
throw new InvalidOperationException($"Caught a null when retrieving {user.Id}'s gambler entity");
|
||||||
@@ -170,7 +170,7 @@ public class RakebackCommand : ICommand
|
|||||||
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, your rakeback payout of {await rakeback.FormatKasinoCurrencyAsync()} is below the minimum amount of {await minimumRakeback.FormatKasinoCurrencyAsync()}", true);
|
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, your rakeback payout of {await rakeback.FormatKasinoCurrencyAsync()} is below the minimum amount of {await minimumRakeback.FormatKasinoCurrencyAsync()}", true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await Money.ModifyBalanceAsync(gambler, rakeback, TransactionSourceEventType.Rakeback, "Rakeback claimed by gambler",
|
await Money.ModifyBalanceAsync(gambler.Id, rakeback, TransactionSourceEventType.Rakeback, "Rakeback claimed by gambler",
|
||||||
ct: ctx);
|
ct: ctx);
|
||||||
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, the hostess has given you {await rakeback.FormatKasinoCurrencyAsync()} rakeback", true);
|
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, the hostess has given you {await rakeback.FormatKasinoCurrencyAsync()} rakeback", true);
|
||||||
}
|
}
|
||||||
@@ -194,7 +194,7 @@ public class LossbackCommand : ICommand
|
|||||||
CancellationToken ctx)
|
CancellationToken ctx)
|
||||||
{
|
{
|
||||||
await using var db = new ApplicationDbContext();
|
await using var db = new ApplicationDbContext();
|
||||||
var gambler = await Money.GetGamblerEntityAsync(user, ct: ctx);
|
var gambler = await Money.GetGamblerEntityAsync(user.Id, ct: ctx);
|
||||||
if (gambler == null)
|
if (gambler == null)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Caught a null when retrieving {user.Id}'s gambler entity");
|
throw new InvalidOperationException($"Caught a null when retrieving {user.Id}'s gambler entity");
|
||||||
@@ -226,7 +226,7 @@ public class LossbackCommand : ICommand
|
|||||||
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, your lossback payout of {await lossback.FormatKasinoCurrencyAsync()} is below the minimum amount of {await minimumLossback.FormatKasinoCurrencyAsync()}", true);
|
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, your lossback payout of {await lossback.FormatKasinoCurrencyAsync()} is below the minimum amount of {await minimumLossback.FormatKasinoCurrencyAsync()}", true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await Money.ModifyBalanceAsync(gambler, lossback, TransactionSourceEventType.Lossback, "Lossback claimed by gambler",
|
await Money.ModifyBalanceAsync(gambler.Id, lossback, TransactionSourceEventType.Lossback, "Lossback claimed by gambler",
|
||||||
ct: ctx);
|
ct: ctx);
|
||||||
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, the hostess has given you {await lossback.FormatKasinoCurrencyAsync()} lossback", true);
|
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, the hostess has given you {await lossback.FormatKasinoCurrencyAsync()} lossback", true);
|
||||||
}
|
}
|
||||||
@@ -255,7 +255,7 @@ public class AbandonKasinoCommand : ICommand
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await using var db = new ApplicationDbContext();
|
await using var db = new ApplicationDbContext();
|
||||||
var gambler = await Money.GetGamblerEntityAsync(user, ct: ctx);
|
var gambler = await Money.GetGamblerEntityAsync(user.Id, ct: ctx);
|
||||||
if (gambler == null)
|
if (gambler == null)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Caught a null when retrieving {user.Id}'s gambler entity");
|
throw new InvalidOperationException($"Caught a null when retrieving {user.Id}'s gambler entity");
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ internal class BotCommands
|
|||||||
if (!kasinoEnabled) return;
|
if (!kasinoEnabled) return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kasinoCommand && Money.IsPermanentlyBannedAsync(user, _cancellationToken).Result)
|
if (kasinoCommand && Money.IsPermanentlyBannedAsync(user.Id, _cancellationToken).Result)
|
||||||
{
|
{
|
||||||
_bot.SendChatMessage($"@{message.Author.Username}, you've been permanently banned from the kasino. Contact support for more information.", true);
|
_bot.SendChatMessage($"@{message.Author.Username}, you've been permanently banned from the kasino. Contact support for more information.", true);
|
||||||
return;
|
return;
|
||||||
@@ -84,13 +84,16 @@ internal class BotCommands
|
|||||||
{
|
{
|
||||||
// GetGamblerEntity will only return null if the user is permanbanned
|
// GetGamblerEntity will only return null if the user is permanbanned
|
||||||
// and we have a check further up the chain for that hence ignoring the null
|
// and we have a check further up the chain for that hence ignoring the null
|
||||||
var gambler = Money.GetGamblerEntityAsync(user, ct: _cancellationToken).Result;
|
var gambler = Money.GetGamblerEntityAsync(user.Id, ct: _cancellationToken).Result;
|
||||||
var exclusion = Money.GetActiveExclusionAsync(gambler!, ct: _cancellationToken).Result;
|
if (gambler != null)
|
||||||
if (exclusion != null)
|
|
||||||
{
|
{
|
||||||
_bot.SendChatMessage(
|
var exclusion = Money.GetActiveExclusionAsync(gambler.Id, ct: _cancellationToken).Result;
|
||||||
$"@{message.Author.Username}, you're self excluded from the kasino for another {(exclusion.Expires - DateTimeOffset.UtcNow).Humanize(precision: 3)}", true);
|
if (exclusion != null)
|
||||||
return;
|
{
|
||||||
|
_bot.SendChatMessage(
|
||||||
|
$"@{message.Author.Username}, you're self excluded from the kasino for another {(exclusion.Expires - DateTimeOffset.UtcNow).Humanize(precision: 3)}", true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,14 +143,14 @@ internal class BotCommands
|
|||||||
if (!(await SettingsProvider.GetValueAsync(BuiltIn.Keys.MoneyEnabled)).ToBoolean()) return;
|
if (!(await SettingsProvider.GetValueAsync(BuiltIn.Keys.MoneyEnabled)).ToBoolean()) return;
|
||||||
var wagerCommand = HasAttribute<WagerCommand>(command);
|
var wagerCommand = HasAttribute<WagerCommand>(command);
|
||||||
if (!wagerCommand) return;
|
if (!wagerCommand) return;
|
||||||
var gambler = await Money.GetGamblerEntityAsync(user, ct: _cancellationToken);
|
var gambler = await Money.GetGamblerEntityAsync(user.Id, ct: _cancellationToken);
|
||||||
if (gambler == null) return;
|
if (gambler == null) return;
|
||||||
if (gambler.TotalWagered < gambler.NextVipLevelWagerRequirement) return;
|
if (gambler.TotalWagered < gambler.NextVipLevelWagerRequirement) return;
|
||||||
// The reason for doing this instead of passing in TotalWagered is that otherwise VIP levels might
|
// The reason for doing this instead of passing in TotalWagered is that otherwise VIP levels might
|
||||||
// get skipped if the user is a low VIP level but wagering very large amounts
|
// get skipped if the user is a low VIP level but wagering very large amounts
|
||||||
var newLevel = Money.GetNextVipLevel(gambler.NextVipLevelWagerRequirement);
|
var newLevel = Money.GetNextVipLevel(gambler.NextVipLevelWagerRequirement);
|
||||||
if (newLevel == null) return;
|
if (newLevel == null) return;
|
||||||
var payout = await Money.UpgradeVipLevelAsync(gambler, newLevel, _cancellationToken);
|
var payout = await Money.UpgradeVipLevelAsync(gambler.Id, newLevel, _cancellationToken);
|
||||||
await _bot.SendChatMessageAsync(
|
await _bot.SendChatMessageAsync(
|
||||||
$"🤑🤑 {user.FormatUsername()} has leveled up to to {newLevel.VipLevel.Icon} {newLevel.VipLevel.Name} Tier {newLevel.Tier} " +
|
$"🤑🤑 {user.FormatUsername()} has leveled up to to {newLevel.VipLevel.Icon} {newLevel.VipLevel.Name} Tier {newLevel.Tier} " +
|
||||||
$"and received a bonus of {await payout.FormatKasinoCurrencyAsync()}", true);
|
$"and received a bonus of {await payout.FormatKasinoCurrencyAsync()}", true);
|
||||||
|
|||||||
@@ -210,13 +210,18 @@ public static class Money
|
|||||||
/// Also returns null if the user was permanently banned from gambling
|
/// Also returns null if the user was permanently banned from gambling
|
||||||
/// If there are multiple "active" gamblers, only the newest is returned
|
/// If there are multiple "active" gamblers, only the newest is returned
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="user">User whose gambler entity you wish to retrieve</param>
|
/// <param name="userId">User whose gambler entity you wish to retrieve</param>
|
||||||
/// <param name="createIfNoneExists">Whether to create a gambler entity if none exists already</param>
|
/// <param name="createIfNoneExists">Whether to create a gambler entity if none exists already</param>
|
||||||
/// <param name="ct">Cancellation token</param>
|
/// <param name="ct">Cancellation token</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task<GamblerDbModel?> GetGamblerEntityAsync(UserDbModel user, bool createIfNoneExists = true, CancellationToken ct = default)
|
public static async Task<GamblerDbModel?> GetGamblerEntityAsync(int userId, bool createIfNoneExists = true, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
await using var db = new ApplicationDbContext();
|
await using var db = new ApplicationDbContext();
|
||||||
|
var user = await db.Users.FirstOrDefaultAsync(x => x.Id == userId, cancellationToken: ct);
|
||||||
|
if (user == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"User ID {userId} not found");
|
||||||
|
}
|
||||||
var gambler =
|
var gambler =
|
||||||
await db.Gamblers.OrderBy(x => x.Id).Include(x => x.User).LastOrDefaultAsync(g => g.User.Id == user.Id && g.State != GamblerState.PermanentlyBanned,
|
await db.Gamblers.OrderBy(x => x.Id).Include(x => x.User).LastOrDefaultAsync(g => g.User.Id == user.Id && g.State != GamblerState.PermanentlyBanned,
|
||||||
cancellationToken: ct);
|
cancellationToken: ct);
|
||||||
@@ -226,7 +231,7 @@ public static class Money
|
|||||||
_logger.Info($"Gambler entity details: {gambler.Id}, Created: {gambler.Created:o}");
|
_logger.Info($"Gambler entity details: {gambler.Id}, Created: {gambler.Created:o}");
|
||||||
}
|
}
|
||||||
if (!createIfNoneExists) return gambler;
|
if (!createIfNoneExists) return gambler;
|
||||||
var permaBanned = await IsPermanentlyBannedAsync(user, ct);
|
var permaBanned = await IsPermanentlyBannedAsync(userId, ct);
|
||||||
_logger.Info($"permaBanned => {permaBanned}");
|
_logger.Info($"permaBanned => {permaBanned}");
|
||||||
if (permaBanned) return null;
|
if (permaBanned) return null;
|
||||||
var initialBalance = (await SettingsProvider.GetValueAsync(BuiltIn.Keys.MoneyInitialBalance)).ToType<decimal>();
|
var initialBalance = (await SettingsProvider.GetValueAsync(BuiltIn.Keys.MoneyInitialBalance)).ToType<decimal>();
|
||||||
@@ -255,34 +260,39 @@ public static class Money
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Simple check to see whether a user has been permanently banned from the kasino
|
/// Simple check to see whether a user has been permanently banned from the kasino
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="user">User to check for the permaban</param>
|
/// <param name="userId">User to check for the permaban</param>
|
||||||
/// <param name="ct">Cancellation token</param>
|
/// <param name="ct">Cancellation token</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task<bool> IsPermanentlyBannedAsync(UserDbModel user, CancellationToken ct = default)
|
public static async Task<bool> IsPermanentlyBannedAsync(int userId, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
await using var db = new ApplicationDbContext();
|
await using var db = new ApplicationDbContext();
|
||||||
return await db.Gamblers.AnyAsync(u => u.User.Id == user.Id && u.State == GamblerState.PermanentlyBanned,
|
return await db.Gamblers.AnyAsync(u => u.User.Id == userId && u.State == GamblerState.PermanentlyBanned,
|
||||||
cancellationToken: ct);
|
cancellationToken: ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Modify a gambler's balance by a given +/- amount
|
/// Modify a gambler's balance by a given +/- amount
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="gambler">Gambler entity whose balance you wish to modify</param>
|
/// <param name="gamblerId">Gambler entity whose balance you wish to modify</param>
|
||||||
/// <param name="effect">The 'effect' of this modification, as in how much to add or remove</param>
|
/// <param name="effect">The 'effect' of this modification, as in how much to add or remove</param>
|
||||||
/// <param name="eventSource">The event which initiated this balance modification</param>
|
/// <param name="eventSource">The event which initiated this balance modification</param>
|
||||||
/// <param name="comment">Optional comment to provide for the transaction</param>
|
/// <param name="comment">Optional comment to provide for the transaction</param>
|
||||||
/// <param name="from">If applicable, who sent the transaction (e.g. if a juicer)</param>
|
/// <param name="fromId">If applicable, who sent the transaction (e.g. if a juicer)</param>
|
||||||
/// <param name="ct">Cancellation token</param>
|
/// <param name="ct">Cancellation token</param>
|
||||||
public static async Task ModifyBalanceAsync(GamblerDbModel gambler, decimal effect,
|
public static async Task ModifyBalanceAsync(int gamblerId, decimal effect,
|
||||||
TransactionSourceEventType eventSource, string? comment = null, GamblerDbModel? from = null,
|
TransactionSourceEventType eventSource, string? comment = null, int? fromId = null,
|
||||||
CancellationToken ct = default)
|
CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
await using var db = new ApplicationDbContext();
|
await using var db = new ApplicationDbContext();
|
||||||
db.Attach(gambler);
|
var gambler = await db.Gamblers.FirstOrDefaultAsync(x => x.Id == gamblerId, cancellationToken: ct);
|
||||||
|
if (gambler == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"Could not find gambler entity with given ID {gamblerId}");
|
||||||
|
}
|
||||||
_logger.Info($"Updating balance for {gambler.Id} with effect {effect:N}. Balance is currently {gambler.Balance:N}");
|
_logger.Info($"Updating balance for {gambler.Id} with effect {effect:N}. Balance is currently {gambler.Balance:N}");
|
||||||
gambler.Balance += effect;
|
gambler.Balance += effect;
|
||||||
_logger.Info($"Balance is now {gambler.Balance:N}");
|
_logger.Info($"Balance is now {gambler.Balance:N}");
|
||||||
|
var from = await db.Gamblers.FirstOrDefaultAsync(x => x.Id == fromId, cancellationToken: ct);
|
||||||
await db.Transactions.AddAsync(new TransactionDbModel
|
await db.Transactions.AddAsync(new TransactionDbModel
|
||||||
{
|
{
|
||||||
Gambler = gambler,
|
Gambler = gambler,
|
||||||
@@ -301,7 +311,7 @@ public static class Money
|
|||||||
/// Add a wager to the database
|
/// Add a wager to the database
|
||||||
/// Will also issue a balance update unless you explicitly disable autoModifyBalance
|
/// Will also issue a balance update unless you explicitly disable autoModifyBalance
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="gambler">Gambler who wagered</param>
|
/// <param name="gamblerId">Gambler who wagered</param>
|
||||||
/// <param name="wagerAmount">The amount they wagered</param>
|
/// <param name="wagerAmount">The amount they wagered</param>
|
||||||
/// <param name="wagerEffect">The effect of the wager on the gambler's balance.
|
/// <param name="wagerEffect">The effect of the wager on the gambler's balance.
|
||||||
/// Please note this includes the wager itself. So for a bet of 500 that paid out 50, you pass in an effect of -450
|
/// Please note this includes the wager itself. So for a bet of 500 that paid out 50, you pass in an effect of -450
|
||||||
@@ -315,15 +325,20 @@ public static class Money
|
|||||||
/// <param name="isComplete">Whether the game is 'complete'. Set to false for wagers with unknown outcomes.
|
/// <param name="isComplete">Whether the game is 'complete'. Set to false for wagers with unknown outcomes.
|
||||||
/// NOTE: wagerEffect will be ignored, instead value will be derived from the wagerAmount</param>
|
/// NOTE: wagerEffect will be ignored, instead value will be derived from the wagerAmount</param>
|
||||||
/// <param name="ct">Cancellation token</param>
|
/// <param name="ct">Cancellation token</param>
|
||||||
public static async Task NewWagerAsync(GamblerDbModel gambler, decimal wagerAmount, decimal wagerEffect,
|
public static async Task NewWagerAsync(int gamblerId, decimal wagerAmount, decimal wagerEffect,
|
||||||
WagerGame game, bool autoModifyBalance = true, dynamic? gameMeta = null, bool isComplete = true,
|
WagerGame game, bool autoModifyBalance = true, dynamic? gameMeta = null, bool isComplete = true,
|
||||||
CancellationToken ct = default)
|
CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
|
await using var db = new ApplicationDbContext();
|
||||||
|
var gambler = await db.Gamblers.Include(gamblerDbModel => gamblerDbModel.User)
|
||||||
|
.FirstOrDefaultAsync(x => x.Id == gamblerId, ct);
|
||||||
|
if (gambler == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"Tried to add wager for permanently excluded gambler {gamblerId}");
|
||||||
|
}
|
||||||
_logger.Info($"Adding a wager for {gambler.User.KfUsername}. wagerAmount => {wagerAmount:N}, " +
|
_logger.Info($"Adding a wager for {gambler.User.KfUsername}. wagerAmount => {wagerAmount:N}, " +
|
||||||
$"wagerEffect => {wagerEffect:N}, game => {game.Humanize()}, autoModifyBalance => {autoModifyBalance}, " +
|
$"wagerEffect => {wagerEffect:N}, game => {game.Humanize()}, autoModifyBalance => {autoModifyBalance}, " +
|
||||||
$"isComplete => {isComplete}");
|
$"isComplete => {isComplete}");
|
||||||
await using var db = new ApplicationDbContext();
|
|
||||||
db.Attach(gambler);
|
|
||||||
string? metaJson = null;
|
string? metaJson = null;
|
||||||
if (gameMeta != null)
|
if (gameMeta != null)
|
||||||
{
|
{
|
||||||
@@ -382,13 +397,13 @@ public static class Money
|
|||||||
/// Get an active exclusion, returns null if there's no active exclusion
|
/// Get an active exclusion, returns null if there's no active exclusion
|
||||||
/// If there's somehow multiple exclusions, will just grab the most recent one
|
/// If there's somehow multiple exclusions, will just grab the most recent one
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="gambler">Gambler entity to retrieve the exclusion for</param>
|
/// <param name="gamblerId">Gambler ID to retrieve the exclusion for</param>
|
||||||
/// <param name="ct">Cancellation token</param>
|
/// <param name="ct">Cancellation token</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task<GamblerExclusionDbModel?> GetActiveExclusionAsync(GamblerDbModel gambler, CancellationToken ct = default)
|
public static async Task<GamblerExclusionDbModel?> GetActiveExclusionAsync(int gamblerId, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
await using var db = new ApplicationDbContext();
|
await using var db = new ApplicationDbContext();
|
||||||
return (await db.Exclusions.Where(g => g.Gambler.Id == gambler.Id).ToListAsync(ct))
|
return (await db.Exclusions.Where(g => g.Gambler.Id == gamblerId).ToListAsync(ct))
|
||||||
.LastOrDefault(e => e.Expires <= DateTimeOffset.UtcNow);
|
.LastOrDefault(e => e.Expires <= DateTimeOffset.UtcNow);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -443,15 +458,19 @@ public static class Money
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Upgrade to the given VIP level. Grants a bonus as part of the level up.
|
/// Upgrade to the given VIP level. Grants a bonus as part of the level up.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="gambler">The gambler you wish to level up</param>
|
/// <param name="gamblerId">The gambler you wish to level up</param>
|
||||||
/// <param name="nextVipLevel">VIP level to grant them</param>
|
/// <param name="nextVipLevel">VIP level to grant them</param>
|
||||||
/// <param name="ct">Cancellation token</param>
|
/// <param name="ct">Cancellation token</param>
|
||||||
/// <returns>The bonus they received</returns>
|
/// <returns>The bonus they received</returns>
|
||||||
public static async Task<decimal> UpgradeVipLevelAsync(GamblerDbModel gambler, NextVipLevelModel nextVipLevel,
|
public static async Task<decimal> UpgradeVipLevelAsync(int gamblerId, NextVipLevelModel nextVipLevel,
|
||||||
CancellationToken ct = default)
|
CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
await using var db = new ApplicationDbContext();
|
await using var db = new ApplicationDbContext();
|
||||||
db.Attach(gambler);
|
var gambler = await db.Gamblers.FirstOrDefaultAsync(x => x.Id == gamblerId, ct);
|
||||||
|
if (gambler == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"Tried to upgrade VIP level for gambler with ID {gamblerId} who does not exist");
|
||||||
|
}
|
||||||
var payout = nextVipLevel.VipLevel.BonusPayout;
|
var payout = nextVipLevel.VipLevel.BonusPayout;
|
||||||
if (nextVipLevel.Tier > 1)
|
if (nextVipLevel.Tier > 1)
|
||||||
{
|
{
|
||||||
@@ -470,7 +489,7 @@ public static class Money
|
|||||||
}, ct);
|
}, ct);
|
||||||
gambler.NextVipLevelWagerRequirement = nextVipLevel.WagerRequirement;
|
gambler.NextVipLevelWagerRequirement = nextVipLevel.WagerRequirement;
|
||||||
await db.SaveChangesAsync(ct);
|
await db.SaveChangesAsync(ct);
|
||||||
await ModifyBalanceAsync(gambler, payout, TransactionSourceEventType.Bonus,
|
await ModifyBalanceAsync(gamblerId, payout, TransactionSourceEventType.Bonus,
|
||||||
$"VIP Level '{nextVipLevel.VipLevel.Icon} {nextVipLevel.VipLevel.Name}' Tier {nextVipLevel.Tier} level up bonus", ct: ct);
|
$"VIP Level '{nextVipLevel.VipLevel.Icon} {nextVipLevel.VipLevel.Name}' Tier {nextVipLevel.Tier} level up bonus", ct: ct);
|
||||||
return payout;
|
return payout;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user