diff --git a/NadekoBot.Core/Modules/Gambling/Common/CurrencyRaffleGame.cs b/NadekoBot.Core/Modules/Gambling/Common/CurrencyRaffleGame.cs new file mode 100644 index 00000000..20869bdd --- /dev/null +++ b/NadekoBot.Core/Modules/Gambling/Common/CurrencyRaffleGame.cs @@ -0,0 +1,40 @@ +using NadekoBot.Core.Services; +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace NadekoBot.Core.Modules.Gambling.Common +{ + public class CurrencyRaffleGame + { + private readonly HashSet<(string, ulong)> _users = new HashSet<(string, ulong)>(); + private readonly int _amount; + private readonly CurrencyService _cs; + private readonly DbService _db; + private bool running; + + public CurrencyRaffleGame(int amount, CurrencyService cs, DbService db) + { + if (amount < 1) + throw new ArgumentOutOfRangeException(); + + _amount = amount; + _cs = cs; + _db = db; + } + + public async Task AddUser(string username, ulong userId) + { + + } + + public void ForceStop() + { + lock (_locker) + { + running = false; + } + } + } +} diff --git a/NadekoBot.Core/Modules/Gambling/CurrencyRaffleCommands.cs b/NadekoBot.Core/Modules/Gambling/CurrencyRaffleCommands.cs new file mode 100644 index 00000000..05a286f7 --- /dev/null +++ b/NadekoBot.Core/Modules/Gambling/CurrencyRaffleCommands.cs @@ -0,0 +1,22 @@ +using NadekoBot.Common.Attributes; +using NadekoBot.Core.Modules.Gambling.Services; +using System.Threading.Tasks; + +namespace NadekoBot.Modules.Gambling +{ + public partial class Gambling + { + public class CurrencyRaffleCommands : NadekoSubmodule + { + [NadekoCommand, Usage, Description, Aliases] + public async Task RaffleCur(int amount) + { + if (_service.Games.TryAdd(Context.Channel.Id, + )) + { + + } + } + } + } +} diff --git a/NadekoBot.Core/Modules/Gambling/Services/CurrencyRaffleService.cs b/NadekoBot.Core/Modules/Gambling/Services/CurrencyRaffleService.cs new file mode 100644 index 00000000..5a917905 --- /dev/null +++ b/NadekoBot.Core/Modules/Gambling/Services/CurrencyRaffleService.cs @@ -0,0 +1,45 @@ +using System.Threading.Tasks; +using NadekoBot.Core.Services; +using System.Collections.Concurrent; +using NadekoBot.Core.Modules.Gambling.Common; +using System.Threading; + +namespace NadekoBot.Core.Modules.Gambling.Services +{ + public class CurrencyRaffleService : INService + { + private readonly SemaphoreSlim _locker = new SemaphoreSlim(1, 1); + private readonly DbService _db; + + public ConcurrentDictionary Games { get; } + + public CurrencyRaffleService(DbService db) + { + _db = db; + } + + public async Task JoinOrCreateGame(ulong channelId, string username, + ulong userId) + { + await _locker.WaitAsync().ConfigureAwait(false); + try + { + using (var uow = _db.UnitOfWork) + { + //remove money + if (!await _cs.RemoveAsync(userId, "Currency Raffle Join", _amount).ConfigureAwait(false)) + return false; + } + + //add to to list + if (_users.Add((username, userId))) + return false; + return true; + } + finally + { + _locker.Release(); + } + } + } +}