.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 NadekoBot.Core.Services;
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Gambling.Services
@ -11,9 +9,6 @@ namespace NadekoBot.Modules.Gambling.Services
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;

View File

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

View File

@ -16,5 +16,6 @@ namespace NadekoBot.Core.Services
TimeSpan? AddTimelyClaim(ulong id, int period);
void RemoveAllTimelyClaims();
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;
}
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;
}
}
}