Files
KfChatDotNet/KfChatDotNetBot/Commands/Kasino/KrashCommand.cs
barelyprofessional 95707f58ee Return after krashing
2026-04-07 23:50:06 -05:00

83 lines
3.0 KiB
C#

using System.Text.RegularExpressions;
using KfChatDotNetBot.Extensions;
using KfChatDotNetBot.Models;
using KfChatDotNetBot.Models.DbModels;
using KfChatDotNetBot.Services;
using KfChatDotNetBot.Settings;
using KfChatDotNetWsClient.Models.Events;
namespace KfChatDotNetBot.Commands.Kasino;
[KasinoCommand]
[WagerCommand]
public class KrashBetCommand : ICommand
{
public List<Regex> Patterns => [
new Regex(@"^krash (?<amount>\d+(?:\.\d+)?) (?<multi>\d+(?:\.\d+)?)$", RegexOptions.IgnoreCase),
new Regex(@"^krash (?<amount>\d+(?:\.\d+)?)$", RegexOptions.IgnoreCase),
new Regex(@"^krash", RegexOptions.IgnoreCase)
];
public string? HelpText => "!rain <amount> to start a rain, !rain to join all active rains";
public UserRight RequiredRight => UserRight.Loser;
public TimeSpan Timeout => TimeSpan.FromSeconds(90);
public RateLimitOptionsModel? RateLimitOptions => null;
public bool WhisperCanInvoke => false;
public async Task RunCommand(ChatBot botInstance, BotCommandMessageModel message, UserDbModel user,
GroupCollection arguments,
CancellationToken ctx)
{
var cleanupDelay = TimeSpan.FromSeconds(10);
if (message is { IsWhisper: false, MessageUuid: not null })
{
await botInstance.KfClient.DeleteMessageAsync(message.MessageUuid);
}
var gambler = await Money.GetGamblerEntityAsync(user.Id, ct: ctx);
if (gambler == null)
throw new InvalidOperationException($"Caught a null when retrieving gambler for {user.KfUsername}");
if (botInstance.BotServices.KasinoKrash == null)
{
await botInstance.SendChatMessageAsync("Krash is not currently running.", true, autoDeleteAfter: cleanupDelay);
return;
}
decimal multi;
decimal wager;
if (!arguments.TryGetValue("amount", out var amountGroup))
{
//attempt to cash out a currently running game
await botInstance.BotServices.KasinoKrash.AttemptKrash(gambler);
return;
}
if (!arguments.TryGetValue("multi", out var multiGroup))
{
multi = -1;
}
else
{
multi = Convert.ToDecimal(multiGroup.Value);
}
wager = Convert.ToDecimal(amountGroup.Value);
if (wager > gambler.Balance)
{
await botInstance.SendChatMessageAsync(
$"{user.FormatUsername()}, your balance of {gambler.Balance} is not enough to bet {wager} on krash.",
true, autoDeleteAfter: TimeSpan.FromSeconds(5));
return;
}
if (botInstance.BotServices.KasinoKrash.TheGame == null)
{
//start a new game
await botInstance.BotServices.KasinoKrash.StartGame(gambler, wager, multi);
}
else
{
//add to the existing game
await botInstance.BotServices.KasinoKrash.AddParticipant(gambler, wager, multi);
}
}
}