.affinity cooldown will now work properly on multiple shards

This commit is contained in:
Master Kwoth 2017-11-03 12:35:27 +01:00
parent 30a609f9a6
commit 0dbd60b5ac
3 changed files with 19 additions and 16 deletions

View File

@ -54,8 +54,6 @@ namespace NadekoBot.Modules.Gambling
NotEnoughFunds, NotEnoughFunds,
InsufficientAmount InsufficientAmount
} }
private static readonly TimeSpan _affinityLimit = TimeSpan.FromMinutes(30);
private readonly IBotConfigProvider _bc; private readonly IBotConfigProvider _bc;
private readonly CurrencyService _cs; private readonly CurrencyService _cs;
private readonly DbService _db; private readonly DbService _db;
@ -309,8 +307,7 @@ namespace NadekoBot.Modules.Gambling
} }
DiscordUser oldAff = null; DiscordUser oldAff = null;
var sucess = false; var sucess = false;
var cooldown = false; TimeSpan? remaining = null;
var difference = TimeSpan.Zero;
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
{ {
var w = uow.Waifus.ByWaifuUserId(Context.User.Id); var w = uow.Waifus.ByWaifuUserId(Context.User.Id);
@ -320,15 +317,8 @@ namespace NadekoBot.Modules.Gambling
{ {
//todo don't let people change affinity on different shards //todo don't let people change affinity on different shards
} }
else if (_cache.Redis.TryAddAffinityCooldown(Context.User.Id)) else if (!_cache.TryAddAffinityCooldown(Context.User.Id, out remaining))
{ {
}
else if (_service.AffinityCooldowns.AddOrUpdate(Context.User.Id,
now,
(key, old) => ((difference = now.Subtract(old)) > _affinityLimit) ? now : old) != now)
{
cooldown = true;
} }
else if (w == null) else if (w == null)
{ {
@ -370,12 +360,11 @@ namespace NadekoBot.Modules.Gambling
} }
if (!sucess) if (!sucess)
{ {
if (cooldown) if (remaining != null)
{ {
var remaining = _affinityLimit.Subtract(difference);
await ReplyErrorLocalized("waifu_affinity_cooldown", await ReplyErrorLocalized("waifu_affinity_cooldown",
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);
} }
else else
{ {

View File

@ -15,5 +15,6 @@ namespace NadekoBot.Core.Services
Task SetNovelDataAsync(string link, string data); Task SetNovelDataAsync(string link, string data);
TimeSpan? AddTimelyClaim(ulong id, int period); TimeSpan? AddTimelyClaim(ulong id, int period);
void RemoveAllTimelyClaims(); void RemoveAllTimelyClaims();
bool TryAddAffinityCooldown(ulong userId, out TimeSpan? time);
} }
} }

View File

@ -83,5 +83,18 @@ namespace NadekoBot.Core.Services.Impl
_db.KeyDelete(k, CommandFlags.FireAndForget); _db.KeyDelete(k, CommandFlags.FireAndForget);
} }
} }
public bool TryAddAffinityCooldown(ulong userId, out TimeSpan? time)
{
time = _db.KeyTimeToLive($"{_redisKey}_affinity_{userId}");
if (time == null)
{
time = TimeSpan.FromMinutes(30);
_db.StringSet($"{_redisKey}_affinity_{userId}", true);
_db.KeyExpire($"{_redisKey}_affinity_{userId}", time);
return true;
}
return false;
}
} }
} }