mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-25 23:18:56 -07:00
Radicade Public Code
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace RadicadeServer.API
|
||||
{
|
||||
public static class RecentImageReferences
|
||||
{
|
||||
public static readonly TimeSpan Window = TimeSpan.FromMinutes(10);
|
||||
|
||||
private sealed record Lookup(long ImageId, DateTime At);
|
||||
|
||||
private static readonly ConcurrentDictionary<long, Lookup> ByPlayer = new();
|
||||
|
||||
public static void Remember(long accountId, long imageId)
|
||||
=> Remember(accountId, imageId, DateTime.UtcNow);
|
||||
|
||||
public static void Remember(long accountId, long imageId, DateTime at)
|
||||
{
|
||||
if (accountId <= 0 || imageId <= 0) return;
|
||||
|
||||
Prune(at);
|
||||
ByPlayer[accountId] = new Lookup(imageId, at);
|
||||
}
|
||||
|
||||
public static bool TryGet(long accountId, out long imageId)
|
||||
{
|
||||
imageId = 0;
|
||||
if (accountId <= 0) return false;
|
||||
if (!ByPlayer.TryGetValue(accountId, out var lookup)) return false;
|
||||
if (DateTime.UtcNow - lookup.At > Window) return false;
|
||||
|
||||
imageId = lookup.ImageId;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void Clear() => ByPlayer.Clear();
|
||||
|
||||
private static void Prune(DateTime now)
|
||||
{
|
||||
foreach (var pair in ByPlayer)
|
||||
{
|
||||
if (now - pair.Value.At > Window)
|
||||
ByPlayer.TryRemove(pair.Key, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user