mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-05-02 12:32:03 -04:00
Kasino game access control (#25)
* Blackjack * sync * Kasino game enable/disable control
This commit is contained in:
committed by
GitHub
parent
47771a0f4c
commit
7e3ba4e641
@@ -4,6 +4,7 @@ using KfChatDotNetBot.Extensions;
|
||||
using KfChatDotNetBot.Models;
|
||||
using KfChatDotNetBot.Models.DbModels;
|
||||
using KfChatDotNetBot.Services;
|
||||
using KfChatDotNetBot.Settings;
|
||||
using KfChatDotNetWsClient.Models.Events;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@@ -73,4 +74,121 @@ public class TempExcludeCommand : ICommand
|
||||
await db.SaveChangesAsync(ctx);
|
||||
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, excluded {targetUser.KfUsername} for {exclusionTime.Humanize()}", true);
|
||||
}
|
||||
}
|
||||
|
||||
internal record KasinoGameSetting(WagerGame Game, string SettingKey, string Alias);
|
||||
|
||||
internal static class KasinoGameSettingMap
|
||||
{
|
||||
internal static readonly IReadOnlyList<KasinoGameSetting> All = new List<KasinoGameSetting>
|
||||
{
|
||||
new(WagerGame.GuessWhatNumber, BuiltIn.Keys.KasinoGuessWhatNumberEnabled, "guesswhatnumber"),
|
||||
new(WagerGame.Dice, BuiltIn.Keys.KasinoDiceEnabled, "dice"),
|
||||
new(WagerGame.Limbo, BuiltIn.Keys.KasinoLimboEnabled, "limbo"),
|
||||
new(WagerGame.Mines, BuiltIn.Keys.KasinoMinesEnabled, "mines"),
|
||||
new(WagerGame.Wheel, BuiltIn.Keys.KasinoWheelEnabled, "wheel"),
|
||||
new(WagerGame.Blackjack, BuiltIn.Keys.KasinoBlackjackEnabled, "blackjack"),
|
||||
new(WagerGame.Planes, BuiltIn.Keys.KasinoPlanesEnabled, "planes"),
|
||||
new(WagerGame.LambChop, BuiltIn.Keys.KasinoLambchopEnabled, "lambchop"),
|
||||
new(WagerGame.Keno, BuiltIn.Keys.KasinoKenoEnabled, "keno"),
|
||||
new(WagerGame.CoinFlip, BuiltIn.Keys.KasinoCoinflipEnabled, "coinflip"),
|
||||
new(WagerGame.Slots, BuiltIn.Keys.KasinoSlotsEnabled, "slots")
|
||||
};
|
||||
|
||||
internal static KasinoGameSetting? FindByAlias(string alias) =>
|
||||
All.FirstOrDefault(g =>
|
||||
g.Alias.Equals(alias, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
public class KasinoGameToggleCommand : ICommand
|
||||
{
|
||||
public List<Regex> Patterns => [
|
||||
new Regex(@"^admin kasino (?<game>\w+) (?<action>enable|disable)$", RegexOptions.IgnoreCase)
|
||||
];
|
||||
|
||||
public string? HelpText => "Enable or disable a Kasino game (use 'all' to toggle all games)";
|
||||
public UserRight RequiredRight => UserRight.TrueAndHonest;
|
||||
public TimeSpan Timeout => TimeSpan.FromSeconds(10);
|
||||
public RateLimitOptionsModel? RateLimitOptions => null;
|
||||
|
||||
public async Task RunCommand(ChatBot botInstance, MessageModel message, UserDbModel user, GroupCollection arguments,
|
||||
CancellationToken ctx)
|
||||
{
|
||||
if (!arguments.TryGetValue("game", out var gameArg) || !arguments.TryGetValue("action", out var actionArg))
|
||||
{
|
||||
await botInstance.SendChatMessageAsync(
|
||||
$"{user.FormatUsername()}, usage: !admin kasino <game|all> enable|disable", true);
|
||||
return;
|
||||
}
|
||||
|
||||
var gameName = gameArg.Value.ToLower();
|
||||
var action = actionArg.Value.ToLower();
|
||||
var shouldEnable = action == "enable";
|
||||
|
||||
var status = shouldEnable ? "enabled" : "disabled";
|
||||
|
||||
// Handle "all" games
|
||||
if (gameName == "all")
|
||||
{
|
||||
foreach (var gameInfo in KasinoGameSettingMap.All)
|
||||
{
|
||||
await SettingsProvider.SetValueAsBooleanAsync(gameInfo.SettingKey, shouldEnable);
|
||||
}
|
||||
|
||||
await botInstance.SendChatMessageAsync(
|
||||
$"{user.FormatUsername()}, all {KasinoGameSettingMap.All.Count} Kasino games have been {status}.", true);
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle individual game
|
||||
var gameInfoMap = KasinoGameSettingMap.FindByAlias(gameName);
|
||||
|
||||
if (gameInfoMap is null)
|
||||
{
|
||||
await botInstance.SendChatMessageAsync(
|
||||
$"{user.FormatUsername()}, unknown game '{gameName}'. Use '!admin kasino games' to see available games, or use 'all' to toggle all games.",
|
||||
true);
|
||||
return;
|
||||
}
|
||||
|
||||
await SettingsProvider.SetValueAsBooleanAsync(gameInfoMap.SettingKey, shouldEnable);
|
||||
|
||||
var gameDisplayName = gameInfoMap.Game.Humanize();
|
||||
|
||||
await botInstance.SendChatMessageAsync(
|
||||
$"{user.FormatUsername()}, {gameDisplayName} has been {status}.", true);
|
||||
}
|
||||
}
|
||||
|
||||
public class KasinoGameListCommand : ICommand
|
||||
{
|
||||
public List<Regex> Patterns => [
|
||||
new Regex(@"^admin kasino games$", RegexOptions.IgnoreCase)
|
||||
];
|
||||
|
||||
public string? HelpText => "List all kasino games and their status";
|
||||
public UserRight RequiredRight => UserRight.TrueAndHonest;
|
||||
public TimeSpan Timeout => TimeSpan.FromSeconds(10);
|
||||
public RateLimitOptionsModel? RateLimitOptions => null;
|
||||
|
||||
public async Task RunCommand(ChatBot botInstance, MessageModel message, UserDbModel user, GroupCollection arguments,
|
||||
CancellationToken ctx)
|
||||
{
|
||||
var response = $"{user.FormatUsername()}, Kasino games:[br]";
|
||||
|
||||
foreach (var game in KasinoGameSettingMap.All
|
||||
.OrderBy(g => g.Game.ToString()))
|
||||
{
|
||||
var isEnabled = (await SettingsProvider
|
||||
.GetValueAsync(game.SettingKey))
|
||||
.ToBoolean();
|
||||
|
||||
var status = isEnabled
|
||||
? $"[B][COLOR={BuiltIn.Keys.KiwiFarmsGreenColor}]ENABLED[/COLOR][/B]"
|
||||
: $"[B][COLOR={BuiltIn.Keys.KiwiFarmsRedColor}]DISABLED[/COLOR][/B]";
|
||||
|
||||
response += $"{game.Game.Humanize()}: {status}[br]";
|
||||
}
|
||||
|
||||
await botInstance.SendChatMessageAsync(response,true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user