Begin Hnefatafl from the game kit

This commit is contained in:
Eric Wagoner
2026-09-23 12:45:44 -04:00
commit ed1dcad259
59 changed files with 8048 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
// A sliding-window count per key, for the doors a stranger can knock on.
export class RateLimit {
private hits = new Map<string, number[]>();
constructor(
private limit: number,
private windowMs: number
) {}
/** True if the key may act now; the act is recorded when it is allowed. */
allow(key: string, now = Date.now()): boolean {
const since = now - this.windowMs;
const recent = (this.hits.get(key) ?? []).filter((t) => t > since);
if (recent.length >= this.limit) {
this.hits.set(key, recent);
return false;
}
recent.push(now);
this.hits.set(key, recent);
if (this.hits.size > 10000) this.prune(now);
return true;
}
prune(now = Date.now()): void {
const since = now - this.windowMs;
for (const [key, times] of this.hits) {
if (!times.some((t) => t > since)) this.hits.delete(key);
}
}
}