NadekoBot/NadekoBot.Core/Services/CurrencyService.cs

119 lines
3.9 KiB
C#
Raw Normal View History

using System;
using System.Threading.Tasks;
using Discord;
using NadekoBot.Extensions;
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Core.Services.Database;
using NadekoBot.Core.Services;
namespace NadekoBot.Core.Services
{
public class CurrencyService : INService
{
private readonly IBotConfigProvider _config;
private readonly DbService _db;
2017-05-22 23:59:31 +00:00
public CurrencyService(IBotConfigProvider config, DbService db)
2017-05-22 23:59:31 +00:00
{
_config = config;
_db = db;
}
public async Task<bool> RemoveAsync(IUser author, string reason, long amount, bool sendMessage)
{
var success = await RemoveAsync(author.Id, reason, amount);
if (success && sendMessage)
try { await author.SendErrorAsync($"`You lost:` {amount} {_config.BotConfig.CurrencySign}\n`Reason:` {reason}").ConfigureAwait(false); } catch { }
return success;
}
public async Task<bool> RemoveAsync(ulong authorId, string reason, long amount, IUnitOfWork uow = null)
{
if (amount < 0)
throw new ArgumentNullException(nameof(amount));
if (uow == null)
{
2017-05-22 23:59:31 +00:00
using (uow = _db.UnitOfWork)
{
var toReturn = InternalRemoveCurrency(authorId, reason, amount, uow);
await uow.CompleteAsync().ConfigureAwait(false);
return toReturn;
}
}
return InternalRemoveCurrency(authorId, reason, amount, uow);
}
2017-05-22 23:59:31 +00:00
private bool InternalRemoveCurrency(ulong authorId, string reason, long amount, IUnitOfWork uow)
{
var success = uow.Currency.TryUpdateState(authorId, -amount);
if (!success)
return false;
uow.CurrencyTransactions.Add(new CurrencyTransaction()
{
UserId = authorId,
Reason = reason,
Amount = -amount,
});
return true;
}
public async Task AddToManyAsync(string reason, long amount, params ulong[] userIds)
{
using (var uow = _db.UnitOfWork)
{
foreach (var userId in userIds)
{
var transaction = new CurrencyTransaction()
{
UserId = userId,
Reason = reason,
Amount = amount,
};
uow.Currency.TryUpdateState(userId, amount);
uow.CurrencyTransactions.Add(transaction);
}
await uow.CompleteAsync();
}
}
public async Task AddAsync(IUser author, string reason, long amount, bool sendMessage)
2016-09-14 12:23:09 +00:00
{
await AddAsync(author.Id, reason, amount);
2016-09-14 12:23:09 +00:00
if (sendMessage)
try { await author.SendConfirmAsync($"`You received:` {amount} {_config.BotConfig.CurrencySign}\n`Reason:` {reason}").ConfigureAwait(false); } catch { }
2016-09-14 12:23:09 +00:00
}
public async Task AddAsync(ulong receiverId, string reason, long amount, IUnitOfWork uow = null)
{
if (amount < 0)
throw new ArgumentNullException(nameof(amount));
var transaction = new CurrencyTransaction()
{
UserId = receiverId,
Reason = reason,
Amount = amount,
};
if (uow == null)
2017-05-22 23:59:31 +00:00
using (uow = _db.UnitOfWork)
{
uow.Currency.TryUpdateState(receiverId, amount);
uow.CurrencyTransactions.Add(transaction);
await uow.CompleteAsync();
}
else
{
2016-09-14 12:23:09 +00:00
uow.Currency.TryUpdateState(receiverId, amount);
uow.CurrencyTransactions.Add(transaction);
}
}
}
}