// ============================================================================
// RynthAi - MonsterStats/KillXpMatcher.cs
// Which kill an experience rise belongs to (2026-11-08, Tom: "The monster tab
// should record how much experience you get for killing x mob").
//
// The server says nothing that ties XP to a kill: the kill notice names the
// monster, and the player's TotalExperience (quad property 1) goes up a moment
// before or after it. So: each kill notice that credits a monster is queued
// with its time; the plugin hands in TotalExperience every tick; a rise is
// paired with the oldest kill still waiting, one rise per kill. A kill waits
// up to KillWaitSec for its rise; a rise that comes first waits up to
// RiseWaitSec for its kill notice. A rise with no kill (a quest, a fellow's
// kill shared with us, a turn-in) finds none and is dropped.
//
// Pure: every call takes the time, so the tests drive it. One thread (the
// plugin pump).
// ============================================================================
using System;
using System.Collections.Generic;
namespace RynthCore.Plugin.RynthAi.MonsterStats;
/// One kill with the experience it gave.
internal readonly record struct KillXp(string Name, long Xp, DateTime At);
internal sealed class KillXpMatcher
{
/// A kill notice waits this long for its experience rise.
public double KillWaitSec { get; set; } = 3.0;
/// A rise that came before its kill notice waits this long for it.
public double RiseWaitSec { get; set; } = 1.0;
/// Kills (and rises) kept waiting at most; past it the oldest goes (a ring spell's ten kills fit).
public const int MaxWaiting = 32;
private readonly List<(string Name, DateTime At)> _kills = new();
private readonly List<(long Xp, DateTime At)> _rises = new();
private long? _lastTotal;
/// Kills waiting for their rise (tests).
internal int WaitingKills => _kills.Count;
/// Rises waiting for their kill (tests).
internal int WaitingRises => _rises.Count;
/// A kill notice credited . Returns the kills paired now (a rise that came first).
public IReadOnlyList OnKill(string? name, DateTime now)
{
if (string.IsNullOrWhiteSpace(name)) return Array.Empty();
if (_kills.Count >= MaxWaiting) _kills.RemoveAt(0);
_kills.Add((name.Trim(), now));
return Match(now);
}
///
/// The player's TotalExperience now. The first value read is the starting point (a login, a
/// reload); a value lower than the last (another character) starts over from it. Returns the
/// kills paired now.
///
public IReadOnlyList OnTotal(long total, DateTime now)
{
if (_lastTotal is not long last || total < last)
{
_lastTotal = total;
return Match(now);
}
if (total > last)
{
if (_rises.Count >= MaxWaiting) _rises.RemoveAt(0);
_rises.Add((total - last, now));
_lastTotal = total;
}
return Match(now);
}
/// A logout: nothing waits on, and the next value read is a new starting point.
public void Reset()
{
_kills.Clear();
_rises.Clear();
_lastTotal = null;
}
private IReadOnlyList Match(DateTime now)
{
List? done = null;
while (_kills.Count > 0 && _rises.Count > 0)
{
var k = _kills[0];
var r = _rises[0];
double riseAfterKill = (r.At - k.At).TotalSeconds;
if (riseAfterKill > KillWaitSec) { _kills.RemoveAt(0); continue; } // the kill gave nothing we saw
if (-riseAfterKill > RiseWaitSec) { _rises.RemoveAt(0); continue; } // the rise was no kill's
_kills.RemoveAt(0);
_rises.RemoveAt(0);
(done ??= new List()).Add(new KillXp(k.Name, r.Xp, k.At > r.At ? k.At : r.At));
}
// What waited too long goes: a kill with no rise (a kill worth nothing), a rise with no kill (a quest).
while (_kills.Count > 0 && (now - _kills[0].At).TotalSeconds > KillWaitSec) _kills.RemoveAt(0);
while (_rises.Count > 0 && (now - _rises[0].At).TotalSeconds > RiseWaitSec) _rises.RemoveAt(0);
return done ?? (IReadOnlyList)Array.Empty();
}
}