From 9643126cf8821d0c38f50bd016a3718fc7fd914f Mon Sep 17 00:00:00 2001 From: alogindtractor <251821224+A-Log-In-D-Tractor@users.noreply.github.com> Date: Sun, 8 Feb 2026 21:26:34 -0800 Subject: [PATCH] updates message stuff (#72) * Implement admin-only clear command for saved games Added 'clear' command for admin to reset saved games. * Refactor LastMessage handling in KasinoMines Refactor LastMessage handling in KasinoMines --- .../Commands/Kasino/MinesCommand.cs | 32 ++++++++++++++++++- KfChatDotNetBot/Services/KasinoMines.cs | 19 ++++++----- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/KfChatDotNetBot/Commands/Kasino/MinesCommand.cs b/KfChatDotNetBot/Commands/Kasino/MinesCommand.cs index 6c7b2bc..b5970e2 100644 --- a/KfChatDotNetBot/Commands/Kasino/MinesCommand.cs +++ b/KfChatDotNetBot/Commands/Kasino/MinesCommand.cs @@ -16,6 +16,8 @@ public class MinesCommand : ICommand new Regex(@"^mines\s+cashout$", RegexOptions.IgnoreCase), //refresh new Regex(@"^mines\s+refresh$", RegexOptions.IgnoreCase), + //clear - admin only + new Regex(@"^mines\s+clear$", RegexOptions.IgnoreCase), //start game with number of picks new Regex(@"^mines\s+(?\d+(?:\.\d+)?)\s+(?\d+)\s+(?\d+)\s+(?\d+)(?:\s+(?cashout))?$", RegexOptions.IgnoreCase), //start game with coordinate string (must contain comma) @@ -46,6 +48,7 @@ public class MinesCommand : ICommand CancellationToken ctx) { + var settings = await SettingsProvider.GetMultipleValuesAsync([ BuiltIn.Keys.KasinoMinesCleanupDelay, BuiltIn.Keys.KiwiFarmsGreenColor, BuiltIn.Keys.KiwiFarmsRedColor, BuiltIn.Keys.KasinoMinesEnabled, BuiltIn.Keys.KasinoGameDisabledMessageCleanupDelay @@ -64,6 +67,21 @@ public class MinesCommand : ICommand if (gambler == null) throw new InvalidOperationException($"Caught a null when retrieving gambler for {user.KfUsername}"); KasinoMines = new KasinoMines(botInstance, gambler.Id); + if (message.Message.Contains("clear")) + { + if (user.UserRight == UserRight.Admin || user.UserRight == UserRight.TrueAndHonest) + { + KasinoMines.GetSavedGames(gambler.Id); + KasinoMines.ActiveGames.Clear(); + KasinoMines.SaveActiveGames(gambler.Id); + return; + } + else + { + await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, you don't have permission to clear saved games.", true, autoDeleteAfter: cleanupDelay); + return; + } + } bool cashout = false; if (arguments.TryGetValue("cashout", out var cashOut)||message.Message.Contains("cashout")) cashout = true; @@ -239,7 +257,19 @@ public class MinesCommand : ICommand return; } - var msg = KasinoMines.ActiveGames[gambler.Id].LastMessage; + var lastmsg = KasinoMines.ActiveGames[gambler.Id].LastMessageId; + SentMessageTrackerModel msg; + if (lastmsg == 0) + { + msg = (await botInstance.SendChatMessageAsync($"{KasinoMines.ActiveGames[gambler.Id].ToString()}", true)); + await botInstance.WaitForChatMessageAsync(msg, ct: ctx); + if (msg.ChatMessageId == null) throw new InvalidOperationException("Timed out waiting for the message"); + } + else + { + msg = botInstance.GetSentMessageStatus(KasinoMines.ActiveGames[gambler.Id].LastMessageReference); + } + if (pick == 0) //if using coordinates { var game = KasinoMines.ActiveGames[gambler.Id]; diff --git a/KfChatDotNetBot/Services/KasinoMines.cs b/KfChatDotNetBot/Services/KasinoMines.cs index f967558..a2320d2 100644 --- a/KfChatDotNetBot/Services/KasinoMines.cs +++ b/KfChatDotNetBot/Services/KasinoMines.cs @@ -24,7 +24,8 @@ public class KasinoMines public int Size { get; set; } public int Mines { get; set; } public List<(int r, int c)> BetsPlaced; - public SentMessageTrackerModel LastMessage = null!; + public int LastMessageId = 0; + public string LastMessageReference = ""; public KasinoMinesGame(GamblerDbModel creator, decimal wager, int size, int mines) @@ -41,12 +42,13 @@ public class KasinoMines { _logger.Info("Resetting message"); // 0 is the default for int - if (LastMessage.ChatMessageId != null) + if (LastMessageId != 0) { - await _kfChatBot.KfClient.DeleteMessageAsync(LastMessage.ChatMessageId.Value); + await _kfChatBot.KfClient.DeleteMessageAsync(LastMessageId); } if (msg.ChatMessageId == null) throw new InvalidOperationException($"ChatMessageId was null for {msg.Reference}"); - LastMessage = msg; + LastMessageId = msg.ChatMessageId.Value; + LastMessageReference = msg.Reference; } public async Task RigBoard((int r, int c) coord) //moves one of the mines to a specified coordinate for house edge rigging @@ -79,7 +81,7 @@ public class KasinoMines } public async Task Explode((int r, int c) mineLocation, SentMessageTrackerModel msg) { - if (LastMessage.ChatMessageId == null || LastMessage.ChatMessageId != msg.ChatMessageId) + if (LastMessageId == 0 || LastMessageId != msg.ChatMessageId) { await ResetMessage(msg); } @@ -141,7 +143,7 @@ public class KasinoMines } await Task.Delay(100); - await _kfChatBot.KfClient.EditMessageAsync(LastMessage.ChatMessageId!.Value, $"{str}[br]{Creator.User.FormatUsername()}"); + await _kfChatBot.KfClient.EditMessageAsync(LastMessageId, $"{str}[br]{Creator.User.FormatUsername()}"); } await Task.Delay(TimeSpan.FromSeconds(10)); @@ -311,8 +313,9 @@ public class KasinoMines await GetSavedGames(gamblerId); var game = ActiveGames[gamblerId]; game.LastInteracted = DateTimeOffset.UtcNow; - if (game.LastMessage.ChatMessageId == null || game.LastMessage.ChatMessageId != msg.ChatMessageId) + if (game.LastMessageId == 0 || game.LastMessageId != msg.ChatMessageId) { + await game.ResetMessage(msg); } List<(int r, int c)> betCoords = new(); @@ -368,7 +371,7 @@ public class KasinoMines await GetSavedGames(gamblerId); var game = ActiveGames[gamblerId]; game.LastInteracted = DateTimeOffset.UtcNow; - if (game.LastMessage.ChatMessageId == null || game.LastMessage.ChatMessageId != msg.ChatMessageId) + if (game.LastMessageId == 0 || game.LastMessageId != msg.ChatMessageId) { await game.ResetMessage(msg); }