From 00772c7faa7754b3be80bcac1170a3e82cbebecc Mon Sep 17 00:00:00 2001 From: SocksTheWolf <132639882+SocksTheWolf@users.noreply.github.com> Date: Sun, 18 Aug 2024 15:46:12 -0700 Subject: [PATCH] Automatically disable previous completed raffles Also fix up some additional issues --- MultiUserRaffleBot/Models/ConfigData.cs | 9 +++- MultiUserRaffleBot/Models/RaffleService.cs | 47 +++++++++++++------ MultiUserRaffleBot/Types/RaffleItem.cs | 3 ++ .../ViewModels/MainViewModel.cs | 20 ++++++-- README.md | 6 +-- 5 files changed, 61 insertions(+), 24 deletions(-) diff --git a/MultiUserRaffleBot/Models/ConfigData.cs b/MultiUserRaffleBot/Models/ConfigData.cs index 56fdd9b..458829c 100644 --- a/MultiUserRaffleBot/Models/ConfigData.cs +++ b/MultiUserRaffleBot/Models/ConfigData.cs @@ -81,7 +81,7 @@ public class ConfigData /*** Raffle Settings ***/ [JsonProperty(Required = Required.Always)] - public RaffleItem[] RaffleData = []; + public List RaffleData = new(); /*** UI Settings ***/ [JsonProperty] @@ -140,6 +140,13 @@ public class ConfigData return null; } + public void MarkRaffleComplete(RaffleItem item) + { + RaffleItem? foundItem = RaffleData.FirstOrDefault(it => it.Amount == item.Amount); + if (foundItem != null) + foundItem.Enabled = false; + } + public void SaveConfigData(bool OverrideInvalid = false) { // If we are not valid, do not allow saving, unless override Invalid is true diff --git a/MultiUserRaffleBot/Models/RaffleService.cs b/MultiUserRaffleBot/Models/RaffleService.cs index fe556cf..4b8fbc8 100644 --- a/MultiUserRaffleBot/Models/RaffleService.cs +++ b/MultiUserRaffleBot/Models/RaffleService.cs @@ -13,34 +13,49 @@ public class RaffleService : BaseServiceTickable private ConcurrentQueue RaffleQueue = new(); private Dictionary RaffleData = new Dictionary(); private CancellationTokenSource cancelToken = new(); - private bool CanRaffle = true; + private RaffleItem? currentRaffleItem = null; + private bool canRaffle = true; - public override ConsoleSources GetSource() => ConsoleSources.Raffle; - public void DrawRaffleNow() + public void BuildRaffleData(List items) { - PrintMessage("Force drawing raffle now..."); - // Cancel any task delay waits - cancelToken.Cancel(); + if (items.Count < 0) + return; + + RaffleData.Clear(); + RaffleData = items.Where(itm => itm.Enabled).ToDictionary(itm => itm.Amount, itm => itm); + PrintMessage($"{RaffleData.Count} items have been added to raffle item dictionary"); } - public void SetCanRaffle(bool state) => CanRaffle = state; + public override ConsoleSources GetSource() => ConsoleSources.Raffle; + public RaffleItem? GetRaffleItem() => currentRaffleItem; - public void BuildRaffleData(RaffleItem[] items) + public void SetCanRaffle(bool state) { - if (items.Length < 0) - return; + // Clear out the current raffle item + if (state == true) + currentRaffleItem = null; - RaffleData.Clear(); - RaffleData = items.ToDictionary(itm => itm.Amount, itm => itm); + canRaffle = state; } + public void DrawRaffleNow() + { + PrintMessage("Force drawing raffle now..."); + // Cancel any task delay waits + cancelToken.Cancel(); + } + public void ReachMilestone(double milestone) { if (RaffleData.ContainsKey(milestone)) { - PrintMessage($"Enqueued a raffle for milestone {milestone}"); - RaffleQueue.Enqueue(RaffleData[milestone]); + RaffleItem currentItem = RaffleData[milestone]; + if (currentItem.Enabled) + { + PrintMessage($"Enqueued a raffle for milestone {milestone}"); + RaffleQueue.Enqueue(currentItem); + } } } @@ -50,7 +65,7 @@ protected override async Task Tick() while (ShouldRun) { // Check to see if we have any commands in the queue to run - if (!RaffleQueue.IsEmpty && CanRaffle) + if (!RaffleQueue.IsEmpty && canRaffle) { if (RaffleQueue.TryDequeue(out RaffleItem? currentItem)) { @@ -63,6 +78,8 @@ protected override async Task Tick() Message = currentItem.Type }); + currentRaffleItem = currentItem; + // Wait however long the raffle is supposed to go try { diff --git a/MultiUserRaffleBot/Types/RaffleItem.cs b/MultiUserRaffleBot/Types/RaffleItem.cs index 8156672..80254f2 100644 --- a/MultiUserRaffleBot/Types/RaffleItem.cs +++ b/MultiUserRaffleBot/Types/RaffleItem.cs @@ -5,6 +5,9 @@ namespace MultiUserRaffleBot.Types [JsonObject(MemberSerialization.OptOut, ItemRequired = Required.Always)] public class RaffleItem { + [JsonProperty(Required = Required.Default)] + public bool Enabled { get; set; } = true; + public string Artist { get; set; } = string.Empty; public string Type { get; set; } = string.Empty; public double Amount { get; set; } = 0.0; diff --git a/MultiUserRaffleBot/ViewModels/MainViewModel.cs b/MultiUserRaffleBot/ViewModels/MainViewModel.cs index f891fe0..ba9f06a 100644 --- a/MultiUserRaffleBot/ViewModels/MainViewModel.cs +++ b/MultiUserRaffleBot/ViewModels/MainViewModel.cs @@ -16,12 +16,15 @@ public partial class MainViewModel : ViewModelBase public MainViewModel() { - // Load all configuration data - LoadConfigs(); - // Start the console service Console.Start(); + // Push the raffle handling immediately + Raffle.OnConsolePrint = (msg) => Console.AddMessage(msg, Raffle); + + // Load all configuration data + LoadConfigs(); + /* Tiltify */ #pragma warning disable CS8602 // Possible null reference argument. CharityTracker = new TiltifyService(Config.TiltifySettings); @@ -39,12 +42,19 @@ public MainViewModel() Twitch.OnSourceEvent += (data) => { // When Twitch is done running said raffle (meaning something claimed or we ran out of entries) // then allow the raffle system to present another raffle entry + RaffleItem? lastRaffle = Raffle.GetRaffleItem(); + if (lastRaffle != null) + { + // Mark this raffle item as complete so we don't end up running it again upon next startup. + Config.MarkRaffleComplete(lastRaffle); + Config.SaveConfigData(); + } + Raffle.SetCanRaffle(true); }; Twitch.Start(); /* Raffle */ - Raffle.OnConsolePrint = (msg) => Console.AddMessage(msg, Raffle); Raffle.OnSourceEvent += (data) => { if (data.Type == SourceEventType.StartRaffle) Twitch.StartRaffle($"{data.Message} from {data.Name}", data.RaffleLength); @@ -57,7 +67,7 @@ public MainViewModel() if (!Config.IsValid) Console.AddMessage("Invalid configuration, please check configs and restart", ConsoleSources.Main); else - Console.AddMessage("Operations Running!", ConsoleSources.Main); + Console.AddMessage("Initalization Complete!", ConsoleSources.Main); CharityTracker.Start(); diff --git a/README.md b/README.md index 704bd79..fa937dd 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,8 @@ Once set, restart the application and keep it running for however long your even ## Notes * All raffle winners will contain the Twitch whisperable username of the winner in a generated `raffles.txt` file. -* There is no support for "restoring progress" from closing the app and opening it again. If you need to close the app, it's recommended that you remove all the completed raffles from your config file. Otherwise those previous raffles will run again. * This connects to all of the Twitch accounts in the channels list -* If no raffle entries are made, the raffle will be closed with no winner and still documented in the output raffles.txt file. +* If no raffle entries are made, the raffle will be closed with no winner and still documented in the output `raffles.txt` file. * This only works for Tiltify team campaigns! Use your Tiltify team campaign id. The app will watch the total amount your team has raised for the campaign. ## RaffleData Setup @@ -27,4 +26,5 @@ This is an array of RaffleItems, of which the schema is as follows: } ``` -Gaps are allowed in the RaffleData array, and if an amount doesnt exist, the milestone will be skipped. +Gaps are allowed in the RaffleData array, and if an amount doesnt exist, the milestone will be skipped. +You can also manually turn off a milestone by putting the flag `Enabled: false` into the json above for an item.