This commit is contained in:
Master Kwoth
2017-11-03 11:42:16 +01:00
9 changed files with 1128 additions and 916 deletions

View File

@ -1,14 +1,54 @@
using NadekoBot.Core.Services;
using Discord;
using NadekoBot.Core.Services;
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Gambling.Services
{
public class WaifuService : INService
{
private readonly DbService _db;
private readonly CurrencyService _cs;
public ConcurrentDictionary<ulong, DateTime> DivorceCooldowns { get; } = new ConcurrentDictionary<ulong, DateTime>();
public ConcurrentDictionary<ulong, DateTime> AffinityCooldowns { get; } = new ConcurrentDictionary<ulong, DateTime>();
public WaifuService(DbService db, CurrencyService cs)
{
_db = db;
_cs = cs;
}
public async Task<bool> WaifuTransfer(IUser owner, ulong waifuId, IUser newOwner)
{
if (owner.Id == newOwner.Id || waifuId == newOwner.Id)
return false;
using (var uow = _db.UnitOfWork)
{
var waifu = uow.Waifus.ByWaifuUserId(waifuId);
var ownerUser = uow.DiscordUsers.GetOrCreate(owner);
// owner has to be the owner of the waifu
if (waifu == null || waifu.ClaimerId != ownerUser.Id)
return false;
if (!await _cs.RemoveAsync(owner.Id,
"Waifu Transfer",
waifu.Price / 10,
uow).ConfigureAwait(false))
{
return false;
}
//new claimerId is the id of the new owner
var newOwnerUser = uow.DiscordUsers.GetOrCreate(newOwner);
waifu.ClaimerId = newOwnerUser.Id;
await uow.CompleteAsync().ConfigureAwait(false);
}
return true;
}
}
}

View File

@ -55,6 +55,11 @@ namespace NadekoBot.Modules.Gambling
InsufficientAmount
}
private static readonly TimeSpan _affinityLimit = TimeSpan.FromMinutes(30);
private readonly IBotConfigProvider _bc;
private readonly CurrencyService _cs;
private readonly DbService _db;
public WaifuClaimCommands(IBotConfigProvider bc, CurrencyService cs, DbService db)
{
_bc = bc;
@ -183,6 +188,23 @@ namespace NadekoBot.Modules.Gambling
await Context.Channel.SendConfirmAsync(Context.User.Mention + msg).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task WaifuTransfer(IUser waifu, IUser newOwner)
{
if(!await _service.WaifuTransfer(Context.User, waifu.Id, newOwner)
.ConfigureAwait(false))
{
await ReplyErrorLocalized("waifu_transfer_fail").ConfigureAwait(false);
return;
}
await ReplyConfirmLocalized("waifu_transfer_success",
Format.Bold(waifu.ToString()),
Format.Bold(Context.User.ToString()),
Format.Bold(newOwner.ToString())).ConfigureAwait(false);
}
public enum DivorceResult
{
Success,
@ -274,11 +296,6 @@ namespace NadekoBot.Modules.Gambling
}
}
private static readonly TimeSpan _affinityLimit = TimeSpan.FromMinutes(30);
private readonly IBotConfigProvider _bc;
private readonly CurrencyService _cs;
private readonly DbService _db;
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task WaifuClaimerAffinity([Remainder]IGuildUser u = null)
@ -299,6 +316,7 @@ namespace NadekoBot.Modules.Gambling
var now = DateTime.UtcNow;
if (w?.Affinity?.UserId == u?.Id)
{
//todo don't let people change affinity on different shards
}
else if (_service.AffinityCooldowns.AddOrUpdate(Context.User.Id,
now,