Skip to content

Commit

Permalink
Automatically disable previous completed raffles
Browse files Browse the repository at this point in the history
Also fix up some additional issues
  • Loading branch information
SocksTheWolf committed Aug 18, 2024
1 parent fac9725 commit 00772c7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 24 deletions.
9 changes: 8 additions & 1 deletion MultiUserRaffleBot/Models/ConfigData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class ConfigData

/*** Raffle Settings ***/
[JsonProperty(Required = Required.Always)]
public RaffleItem[] RaffleData = [];
public List<RaffleItem> RaffleData = new();

/*** UI Settings ***/
[JsonProperty]
Expand Down Expand Up @@ -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
Expand Down
47 changes: 32 additions & 15 deletions MultiUserRaffleBot/Models/RaffleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,49 @@ public class RaffleService : BaseServiceTickable
private ConcurrentQueue<RaffleItem> RaffleQueue = new();
private Dictionary<double, RaffleItem> RaffleData = new Dictionary<double, RaffleItem>();
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<RaffleItem> 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);
}
}
}

Expand All @@ -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))
{
Expand All @@ -63,6 +78,8 @@ protected override async Task Tick()
Message = currentItem.Type
});

currentRaffleItem = currentItem;

// Wait however long the raffle is supposed to go
try
{
Expand Down
3 changes: 3 additions & 0 deletions MultiUserRaffleBot/Types/RaffleItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 15 additions & 5 deletions MultiUserRaffleBot/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

0 comments on commit 00772c7

Please sign in to comment.