using System;
using System.Collections.Generic;
using RynthCore.Loot.T11;
namespace RynthCore.Plugin.RynthAi.Loot;
///
/// T11 (tier 11+ ACECustom) loot support for :
/// answers the virtual T11 keys () and puts back the wield-requirement and
/// rating properties the server strips from T11 appraisals. Parsed item text is cached per
/// object and re-parsed only when the text changes. Holds the player's "/aug" counters for
/// the "Can Wield" key. One character per process, so the state is static.
///
internal static class T11ItemSupport
{
/// The logged-in character's T11 wield counters.
public static T11PlayerAugs Player { get; } = new();
private static readonly T11AugReportParser AugParser = new();
private static readonly object Gate = new();
private static readonly Dictionary Parsed = new();
// Bound so a long session's corpse items can't grow the cache without limit; a full
// clear is fine because re-parsing is cheap and only happens for items still in use.
private const int MaxEntries = 4096;
private sealed class Entry
{
public string Name = string.Empty;
public string LongDesc = string.Empty;
public string Use = string.Empty;
public T11ItemInfo Info = T11ItemInfo.None;
}
/// True when carries the server's T11 drop prefix.
public static bool HasT11Name(string? name)
=> !string.IsNullOrEmpty(name) && name.StartsWith(T11Catalog.NamePrefix, StringComparison.Ordinal);
/// The parsed T11 view of ( for other items).
public static T11ItemInfo Get(WorldObject item)
{
string name = item.Name.Length > 0 ? item.Name : item.Values(StringValueKey.Name, string.Empty);
string longDesc = item.Values(StringValueKey.LongDesc, string.Empty);
string use = item.Values(StringValueKey.Use, string.Empty);
lock (Gate)
{
if (Parsed.TryGetValue(item.Id, out Entry? e)
&& string.Equals(e.Name, name, StringComparison.Ordinal)
&& string.Equals(e.LongDesc, longDesc, StringComparison.Ordinal)
&& string.Equals(e.Use, use, StringComparison.Ordinal))
return e.Info;
}
T11ItemInfo info = T11ItemInfo.Parse(name, longDesc, use);
lock (Gate)
{
if (Parsed.Count >= MaxEntries) Parsed.Clear();
Parsed[item.Id] = new Entry { Name = name, LongDesc = longDesc, Use = use, Info = info };
}
return info;
}
/// A virtual T11 key's value, or for an unused id.
public static int VirtualValue(WorldObject item, int key, int defaultValue)
{
try
{
return T11Keys.TryGetValue(Get(item), key, Player, out int value) ? value : defaultValue;
}
catch
{
return defaultValue;
}
}
///
/// The value a T11 appraisal stripped for a retail key (158/159/160, 370-379), else
/// . Only T11-named items are parsed, so other items pay a
/// string compare.
///
public static int StrippedValue(WorldObject item, int key, int fallback)
{
if (!T11Keys.IsStrippedKey(key)) return fallback;
try
{
string name = item.Name.Length > 0 ? item.Name : item.Values(StringValueKey.Name, string.Empty);
if (!HasT11Name(name)) return fallback;
return T11Keys.TryGetStrippedValue(Get(item), key, out int value) ? value : fallback;
}
catch
{
return fallback;
}
}
/// Feeds a chat line to the "/aug" reader. Returns true when a counter changed.
public static bool OnChat(string? text) => AugParser.Feed(text, Environment.TickCount64, Player);
/// Applies the saved item-aug override (-1 = automatic). Cheap; called every tick.
public static void SyncOverride(int itemAugsOverride)
=> Player.ItemAugOverride = itemAugsOverride >= 0 ? itemAugsOverride : null;
/// Drops the cached parse of a deleted object.
public static void Forget(int objectId)
{
lock (Gate) Parsed.Remove(objectId);
}
/// Forgets the character's counters and every cached item (logout).
public static void Reset()
{
Player.Clear();
lock (Gate) Parsed.Clear();
}
}