.divorce cooldown now properly works on multiple shards

This commit is contained in:
Master Kwoth 2017-11-03 12:46:51 +01:00
parent 0dbd60b5ac
commit 1c157ddfc1
4 changed files with 18 additions and 14 deletions

View File

@ -1,7 +1,5 @@
using Discord; using Discord;
using NadekoBot.Core.Services; using NadekoBot.Core.Services;
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace NadekoBot.Modules.Gambling.Services namespace NadekoBot.Modules.Gambling.Services
@ -11,9 +9,6 @@ namespace NadekoBot.Modules.Gambling.Services
private readonly DbService _db; private readonly DbService _db;
private readonly CurrencyService _cs; 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) public WaifuService(DbService db, CurrencyService cs)
{ {
_db = db; _db = db;

View File

@ -213,8 +213,6 @@ namespace NadekoBot.Modules.Gambling
Cooldown Cooldown
} }
private static readonly TimeSpan _divorceLimit = TimeSpan.FromHours(6);
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
[Priority(0)] [Priority(0)]
@ -229,7 +227,7 @@ namespace NadekoBot.Modules.Gambling
return; return;
DivorceResult result; DivorceResult result;
var difference = TimeSpan.Zero; TimeSpan? remaining = null;
var amount = 0; var amount = 0;
WaifuInfo w = null; WaifuInfo w = null;
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
@ -238,9 +236,7 @@ namespace NadekoBot.Modules.Gambling
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
if (w?.Claimer == null || w.Claimer.UserId != Context.User.Id) if (w?.Claimer == null || w.Claimer.UserId != Context.User.Id)
result = DivorceResult.NotYourWife; result = DivorceResult.NotYourWife;
else if (_service.DivorceCooldowns.AddOrUpdate(Context.User.Id, else if (!_cache.TryAddDivorceCooldown(Context.User.Id, out remaining))
now,
(key, old) => ((difference = now.Subtract(old)) > _divorceLimit) ? now : old) != now)
{ {
result = DivorceResult.Cooldown; result = DivorceResult.Cooldown;
} }
@ -289,10 +285,9 @@ namespace NadekoBot.Modules.Gambling
} }
else else
{ {
var remaining = _divorceLimit.Subtract(difference);
await ReplyErrorLocalized("waifu_recent_divorce", await ReplyErrorLocalized("waifu_recent_divorce",
Format.Bold(((int)remaining.TotalHours).ToString()), Format.Bold(((int)remaining?.TotalHours).ToString()),
Format.Bold(remaining.Minutes.ToString())).ConfigureAwait(false); Format.Bold(remaining?.Minutes.ToString())).ConfigureAwait(false);
} }
} }

View File

@ -16,5 +16,6 @@ namespace NadekoBot.Core.Services
TimeSpan? AddTimelyClaim(ulong id, int period); TimeSpan? AddTimelyClaim(ulong id, int period);
void RemoveAllTimelyClaims(); void RemoveAllTimelyClaims();
bool TryAddAffinityCooldown(ulong userId, out TimeSpan? time); bool TryAddAffinityCooldown(ulong userId, out TimeSpan? time);
bool TryAddDivorceCooldown(ulong userId, out TimeSpan? time);
} }
} }

View File

@ -96,5 +96,18 @@ namespace NadekoBot.Core.Services.Impl
} }
return false; return false;
} }
public bool TryAddDivorceCooldown(ulong userId, out TimeSpan? time)
{
time = _db.KeyTimeToLive($"{_redisKey}_divorce_{userId}");
if (time == null)
{
time = TimeSpan.FromHours(6);
_db.StringSet($"{_redisKey}_divorce_{userId}", true);
_db.KeyExpire($"{_redisKey}_divorce_{userId}", time);
return true;
}
return false;
}
} }
} }