Ejemplo n.º 1
0
        public async Task AddAsync(ulong receiverId, string reason, long amount, IUnitOfWork uow = null, bool gamble = false, IUser user = null)
        {
            if (amount < 0)
            {
                throw new ArgumentNullException(nameof(amount));
            }

            var transaction = new CurrencyTransaction()
            {
                UserId = receiverId,
                Reason = reason,
                Amount = amount,
            };

            if (uow == null)
            {
                using (uow = _db.UnitOfWork)
                {
                    if (user != null)
                    {
                        uow.DiscordUsers.GetOrCreate(user);
                    }
                    uow.DiscordUsers.TryUpdateCurrencyState(receiverId, amount);
                    if (gamble)
                    {
                        var botTr = transaction.Clone();
                        botTr.UserId  = _botId;
                        botTr.Amount *= -1;

                        uow.DiscordUsers.TryUpdateCurrencyState(_botId, -amount, true);
                        uow.CurrencyTransactions.Add(botTr);
                    }
                    uow.CurrencyTransactions.Add(transaction);
                    await uow.CompleteAsync();
                }
            }
            else
            {
                uow.DiscordUsers.TryUpdateCurrencyState(receiverId, amount);
                if (gamble)
                {
                    var botTr = transaction.Clone();
                    botTr.UserId  = _botId;
                    botTr.Amount *= -1;

                    uow.DiscordUsers.TryUpdateCurrencyState(_botId, -amount, true);
                    uow.CurrencyTransactions.Add(botTr);
                }
                uow.CurrencyTransactions.Add(transaction);
            }
        }
Ejemplo n.º 2
0
        private bool InternalRemoveCurrency(ulong authorId, string reason, long amount, IUnitOfWork uow, bool addToBot)
        {
            var success = uow.DiscordUsers.TryUpdateCurrencyState(authorId, -amount);

            if (!success)
            {
                return(false);
            }

            var transaction = new CurrencyTransaction()
            {
                UserId = authorId,
                Reason = reason,
                Amount = -amount,
            };

            if (addToBot)
            {
                var botTr = transaction.Clone();
                botTr.UserId  = _botId;
                botTr.Amount *= -1;

                uow.DiscordUsers.TryUpdateCurrencyState(_botId, amount);
                uow.CurrencyTransactions.Add(botTr);
            }

            uow.CurrencyTransactions.Add(transaction);
            return(true);
        }