656 lines
30 KiB
C#
656 lines
30 KiB
C#
// IltGuardian.cs - Temple of Enlightenment guardians (ILT Hub "Guardian" window).
|
|
// Originally SilentKelpie's ILT Hub (RynthAi IltHub/); moved to RynthILT 2026-10-13.
|
|
//
|
|
// * Riddle translator: incoming "Guardian of ... tells you / says" lines (when chat auto-detect
|
|
// is on) and pasted text go through IltGuardianPhrasebook; the answer shows here and on the
|
|
// Mini Remote.
|
|
// * Hand-in: gives one of the answer item to the guardian (GiveObjectTo). For the Temple
|
|
// guardian, when none is carried and "buy missing" is on, it first buys one from the open or
|
|
// nearest vendor (VendorBuy). Runs as a small pump-thread state machine with timeouts; the
|
|
// client busy state is respected between steps.
|
|
// * Attribute tracker: IltTempleAttributes.
|
|
// * Reader: the same state as JSON with verbs back ("RynthILT.Data" version 3), for RynthOracle's
|
|
// Quests tab. The automation (auto hand-in, buy if missing) stays here either way.
|
|
//
|
|
// Threading: OnChat / Tick / commands run on the pump thread; Render only reads volatile fields
|
|
// and posts actions through IltHubContext.Post.
|
|
using System;
|
|
using System.Linq;
|
|
using System.Numerics;
|
|
using ImGuiNET;
|
|
using RynthCore.Loot;
|
|
using RynthCore.Plugin.Shared;
|
|
using RynthCore.PluginSdk;
|
|
|
|
namespace RynthCore.Plugin.RynthILT.Hub;
|
|
|
|
internal sealed class IltGuardian : IIltFeature
|
|
{
|
|
/// <summary>The same guardian + answer from chat within this window is one prompt (tells repeat lines).</summary>
|
|
private const long DebounceMs = 2500;
|
|
private const long HandInTimeoutMs = 45_000;
|
|
private const long VendorOpenTimeoutMs = 10_000;
|
|
private const long BuyTimeoutMs = 10_000;
|
|
private const long ItemArriveTimeoutMs = 8_000;
|
|
private const long BusyRetryMs = 400;
|
|
private const int MaxGiveAttempts = 5;
|
|
/// <summary>How far (meters) to look for the guardian and a vendor.</summary>
|
|
private const double SearchRangeMeters = 60.0;
|
|
/// <summary>
|
|
/// PublicWeenieDesc BF_VENDOR. The world cache files vendors as creatures (Monster / Npc), never
|
|
/// AcObjectClass.Vendor, so vendors are recognised by this weenie flag.
|
|
/// </summary>
|
|
private const uint BfVendor = 0x200;
|
|
/// <summary>The Mini Remote shows the last answer for this long.</summary>
|
|
public static readonly TimeSpan AnswerShownFor = TimeSpan.FromMinutes(10);
|
|
|
|
private enum Step { Idle, FindItem, OpenVendor, WaitVendor, Buy, WaitBuy, WaitItem, Give }
|
|
|
|
private readonly IltHubContext _ctx;
|
|
private string _lastFingerprint = string.Empty;
|
|
private long _lastEmitAt;
|
|
|
|
// Last prompt (written on the pump thread, read by the render thread).
|
|
private volatile string _lastLine = string.Empty;
|
|
private volatile string _lastAnswer = string.Empty;
|
|
private volatile string _lastNpc = string.Empty;
|
|
private long _lastAnswerTicksUtc;
|
|
private volatile string _handInStatus = "idle";
|
|
|
|
// Hand-in state machine (pump thread only).
|
|
private Step _step = Step.Idle;
|
|
private string _item = string.Empty;
|
|
private string _npc = string.Empty;
|
|
private bool _asked;
|
|
private long _startedAt;
|
|
private long _stepDeadline;
|
|
private long _nextStepAt;
|
|
private uint _vendorId;
|
|
private uint _buyRequest;
|
|
private int _giveAttempts;
|
|
|
|
// Render-thread UI state.
|
|
private string _input = string.Empty;
|
|
|
|
public IltGuardian(IltHubContext ctx, IltQuestBonus quests)
|
|
{
|
|
_ctx = ctx;
|
|
Attributes = new IltTempleAttributes(ctx, quests);
|
|
}
|
|
|
|
public IltTempleAttributes Attributes { get; }
|
|
|
|
private IltGuardianState S => _ctx.State.Guardian;
|
|
|
|
/// <summary>Last answer item ("" until a guardian prompt was translated).</summary>
|
|
public string LastAnswer => _lastAnswer;
|
|
/// <summary>Guardian the last answer goes to.</summary>
|
|
public string LastNpc => _lastNpc;
|
|
/// <summary>The hand-in sequence is running.</summary>
|
|
public bool HandInRunning => _step != Step.Idle;
|
|
public string HandInStatus => _handInStatus;
|
|
|
|
/// <summary>Time since the last answer (MaxValue when there is none).</summary>
|
|
public TimeSpan LastAnswerAge
|
|
{
|
|
get
|
|
{
|
|
long t = System.Threading.Interlocked.Read(ref _lastAnswerTicksUtc);
|
|
return t == 0 ? TimeSpan.MaxValue : DateTime.UtcNow - new DateTime(t, DateTimeKind.Utc);
|
|
}
|
|
}
|
|
|
|
// ── IIltFeature ─────────────────────────────────────────────────────────
|
|
|
|
public void Tick(long nowMs)
|
|
{
|
|
Attributes.Tick(nowMs);
|
|
StepHandIn(nowMs);
|
|
}
|
|
|
|
public bool OnChat(string text)
|
|
{
|
|
if (!S.AutoDetectFromChat || !IltGuardianPhrasebook.LooksLikeIncomingGuardianLine(text)) return false;
|
|
_lastLine = text.Trim();
|
|
string answer = IltGuardianPhrasebook.Translate(text);
|
|
if (!IltGuardianPhrasebook.IsAnswer(answer)) return false;
|
|
|
|
string npc = IltGuardianPhrasebook.TurnInNpcFromLine(text);
|
|
long now = IltHubContext.NowMs;
|
|
string fingerprint = npc + "|" + answer;
|
|
if (fingerprint == _lastFingerprint && now - _lastEmitAt < DebounceMs) return false;
|
|
_lastFingerprint = fingerprint;
|
|
_lastEmitAt = now;
|
|
|
|
SetAnswer(answer, npc);
|
|
_ctx.Chat($"[ILT Guardian] {npc} wants: {answer}");
|
|
RynthLog.Write(LogCat.IltHub, $"[IltGuardian] chat answer '{answer}' for {npc}");
|
|
if (S.AutoHandIn) StartHandIn(answer, npc, asked: false);
|
|
return false; // never hide the guardian's own lines
|
|
}
|
|
|
|
public void OnLogout()
|
|
{
|
|
StopHandIn("stopped (logout)");
|
|
Attributes.OnLogout();
|
|
}
|
|
|
|
private void SetAnswer(string answer, string npc)
|
|
{
|
|
_lastAnswer = answer;
|
|
_lastNpc = npc;
|
|
System.Threading.Interlocked.Exchange(ref _lastAnswerTicksUtc, DateTime.UtcNow.Ticks);
|
|
}
|
|
|
|
/// <summary>Translates pasted text; a real answer becomes the current answer. Pump thread.</summary>
|
|
public string TranslateManual(string text)
|
|
{
|
|
string answer = IltGuardianPhrasebook.Translate(text);
|
|
if (!IltGuardianPhrasebook.IsAnswer(answer)) return answer;
|
|
string npc = IltGuardianPhrasebook.TurnInNpcFromInput(text);
|
|
_lastLine = text.Trim();
|
|
SetAnswer(answer, npc);
|
|
return $"{answer} (give to {npc})";
|
|
}
|
|
|
|
// ── Hand-in ─────────────────────────────────────────────────────────────
|
|
|
|
/// <summary>Starts giving <paramref name="item"/> to <paramref name="npc"/>. Pump thread.</summary>
|
|
public string StartHandIn(string item, string npc, bool asked)
|
|
{
|
|
if (!IltGuardianPhrasebook.IsAnswer(item)) return "No answer to hand in yet.";
|
|
if (!_ctx.Host.HasGiveObjectTo) return SetStatus("can't give items: update the RynthCore engine (API v62+).");
|
|
_item = item;
|
|
_npc = string.IsNullOrWhiteSpace(npc) ? IltGuardianPhrasebook.AttributeGuardian : npc;
|
|
_asked = asked;
|
|
_startedAt = IltHubContext.NowMs;
|
|
_nextStepAt = 0;
|
|
_vendorId = 0;
|
|
_buyRequest = 0;
|
|
_giveAttempts = 0;
|
|
_step = Step.FindItem;
|
|
RynthLog.Write(LogCat.IltHub, $"[IltGuardian] hand-in start: '{_item}' -> {_npc} ({(asked ? "asked" : "auto")})");
|
|
return SetStatus($"handing {_item} to {_npc}...");
|
|
}
|
|
|
|
/// <summary>Stops a running hand-in.</summary>
|
|
public void StopHandIn(string why = "stopped")
|
|
{
|
|
if (_step == Step.Idle) return;
|
|
_step = Step.Idle;
|
|
SetStatus(why);
|
|
RynthLog.Write(LogCat.IltHub, $"[IltGuardian] hand-in {why}");
|
|
}
|
|
|
|
private string SetStatus(string text)
|
|
{
|
|
_handInStatus = text;
|
|
return text;
|
|
}
|
|
|
|
private void Fail(string why)
|
|
{
|
|
_step = Step.Idle;
|
|
SetStatus("failed: " + why);
|
|
_ctx.Chat($"[ILT Guardian] Hand-in failed: {why}");
|
|
RynthLog.Write(LogCat.IltHub, $"[IltGuardian] hand-in failed: {why}");
|
|
}
|
|
|
|
private void Next(Step step, long nowMs, long timeoutMs = 0)
|
|
{
|
|
_step = step;
|
|
_stepDeadline = timeoutMs > 0 ? nowMs + timeoutMs : 0;
|
|
}
|
|
|
|
private void StepHandIn(long nowMs)
|
|
{
|
|
if (_step == Step.Idle || nowMs < _nextStepAt) return;
|
|
if (nowMs - _startedAt > HandInTimeoutMs) { Fail("timed out"); return; }
|
|
if (_stepDeadline != 0 && nowMs > _stepDeadline) { Fail(TimeoutText(_step)); return; }
|
|
|
|
var host = _ctx.Host;
|
|
switch (_step)
|
|
{
|
|
case Step.FindItem:
|
|
if (FindHandInItem() != null) { Next(Step.Give, nowMs); break; }
|
|
if (!IsTempleGuardian(_npc) || !S.BuyMissingFromVendor) { LogItemNearMisses(); Fail($"no {_item} in your packs"); break; }
|
|
Next(Step.OpenVendor, nowMs);
|
|
break;
|
|
|
|
case Step.OpenVendor:
|
|
{
|
|
if (!host.HasVendorTrade) { Fail($"no {_item} carried and vendor buying needs a newer RynthCore engine"); break; }
|
|
if (host.TryGetVendorInfo(out var open)) { _vendorId = open.VendorId; Next(Step.Buy, nowMs); break; }
|
|
var vendor = Nearest(IsVendor);
|
|
if (vendor == null) { LogVendorSearch(); Fail($"no {_item} carried and no vendor within {SearchRangeMeters:0} m"); break; }
|
|
RynthLog.Write(LogCat.IltHub, $"[IltGuardian] vendor for '{_item}': {vendor.Name} 0x{(uint)vendor.Id:X8}");
|
|
SetStatus($"opening {vendor.Name} to buy {_item}...");
|
|
host.UseFor(unchecked((uint)vendor.Id), "IltGuardian", $"buy {_item} for the Temple guardian",
|
|
_asked ? UseKind.Asked : UseKind.Auto);
|
|
Next(Step.WaitVendor, nowMs, VendorOpenTimeoutMs);
|
|
break;
|
|
}
|
|
|
|
case Step.WaitVendor:
|
|
if (host.TryGetVendorInfo(out var info) && info.ItemCount > 0) { _vendorId = info.VendorId; Next(Step.Buy, nowMs); }
|
|
break;
|
|
|
|
case Step.Buy:
|
|
{
|
|
if (!host.TryGetVendorInfo(out var v)) { Fail("the vendor closed"); break; }
|
|
if (v.TradeInFlight) { _nextStepAt = nowMs + BusyRetryMs; break; }
|
|
string want = NormalizeName(_item);
|
|
var match = host.GetVendorItems().FirstOrDefault(i => NormalizeName(i.Name) == want);
|
|
if (match == null) { Fail($"{v.Name} doesn't sell {_item}"); break; }
|
|
_buyRequest = host.VendorBuy(match.ObjectId, 1, v.VendorId);
|
|
if (_buyRequest == 0)
|
|
{
|
|
string why = host.TryGetVendorTradeStatus(out var st) && st.Message.Length > 0 ? st.Message : "refused";
|
|
Fail($"couldn't buy {_item}: {why}");
|
|
break;
|
|
}
|
|
SetStatus($"buying {_item} from {v.Name}...");
|
|
Next(Step.WaitBuy, nowMs, BuyTimeoutMs);
|
|
break;
|
|
}
|
|
|
|
case Step.WaitBuy:
|
|
if (!host.TryGetVendorTradeStatus(out var status) || status.RequestId != _buyRequest || !status.IsFinished) break;
|
|
if (!status.Succeeded) { Fail($"couldn't buy {_item}: {(status.Message.Length > 0 ? status.Message : status.Result.ToString())}"); break; }
|
|
Next(Step.WaitItem, nowMs, ItemArriveTimeoutMs);
|
|
break;
|
|
|
|
case Step.WaitItem:
|
|
if (FindHandInItem() != null) Next(Step.Give, nowMs);
|
|
break;
|
|
|
|
case Step.Give:
|
|
GiveStep(nowMs);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void GiveStep(long nowMs)
|
|
{
|
|
if (_ctx.Inventory.IsBusy)
|
|
{
|
|
SetStatus("waiting for the client (busy)...");
|
|
_nextStepAt = nowMs + BusyRetryMs;
|
|
return;
|
|
}
|
|
var item = FindHandInItem();
|
|
if (item == null) { LogItemNearMisses(); Fail($"no {_item} in your packs"); return; }
|
|
var npc = Nearest(wo => wo.Name.Contains(_npc, StringComparison.OrdinalIgnoreCase));
|
|
if (npc == null) { Fail($"{_npc} isn't nearby"); return; }
|
|
|
|
int stack = _ctx.Inventory.StackSize(item);
|
|
bool ok = _ctx.Host.GiveObjectTo(unchecked((uint)item.Id), unchecked((uint)npc.Id), stack > 1 ? 1 : 0);
|
|
RynthLog.Write(LogCat.IltHub, $"[IltGuardian] give '{item.Name}' 0x{(uint)item.Id:X8} (stack {stack}) -> {npc.Name} 0x{(uint)npc.Id:X8}: {ok}");
|
|
if (ok)
|
|
{
|
|
_step = Step.Idle;
|
|
SetStatus($"gave {item.Name} to {npc.Name}");
|
|
_ctx.Chat($"[ILT Guardian] Gave {item.Name} to {npc.Name}.");
|
|
return;
|
|
}
|
|
if (++_giveAttempts >= MaxGiveAttempts) { Fail("the give was refused"); return; }
|
|
_nextStepAt = nowMs + BusyRetryMs;
|
|
}
|
|
|
|
private static string TimeoutText(Step step) => step switch
|
|
{
|
|
Step.WaitVendor => "the vendor didn't open",
|
|
Step.WaitBuy => "the vendor didn't answer the purchase",
|
|
Step.WaitItem => "the bought item didn't arrive",
|
|
_ => "timed out",
|
|
};
|
|
|
|
private static bool IsTempleGuardian(string npc) =>
|
|
npc.Contains(IltGuardianPhrasebook.TempleGuardian, StringComparison.OrdinalIgnoreCase);
|
|
|
|
/// <summary>
|
|
/// A carried (not wielded) item named like the answer (case and spacing ignored). Reads AC's
|
|
/// live container contents: the cache's inventory set lags a purchase and also holds the open
|
|
/// vendor's stock, which can't be given.
|
|
/// </summary>
|
|
private WorldObject? FindHandInItem()
|
|
{
|
|
string want = NormalizeName(_item);
|
|
return CarriedItems().FirstOrDefault(wo => NormalizeName(wo.Name) == want && !_ctx.Inventory.IsEquipped(wo));
|
|
}
|
|
|
|
/// <summary>Live walk of the player's pack and side packs (falls back to the cache without the engine call).</summary>
|
|
private System.Collections.Generic.IReadOnlyList<WorldObject> CarriedItems()
|
|
=> _ctx.Inventory.Cache?.GetDirectInventory(forceRefresh: true) ?? Array.Empty<WorldObject>();
|
|
|
|
/// <summary>Lower-case, trimmed, single-spaced name for comparisons.</summary>
|
|
private static string NormalizeName(string name)
|
|
=> string.Join(' ', (name ?? string.Empty).Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries)).ToLowerInvariant();
|
|
|
|
/// <summary>Logs carried items sharing a word with the answer, so a name mismatch shows in the log.</summary>
|
|
private void LogItemNearMisses()
|
|
{
|
|
var items = CarriedItems();
|
|
var words = NormalizeName(_item).Split(' ').Where(w => w.Length > 3).ToArray();
|
|
var similar = items.Where(wo => words.Any(w => wo.Name.Contains(w, StringComparison.OrdinalIgnoreCase)))
|
|
.Take(8).Select(wo => $"'{wo.Name}' 0x{(uint)wo.Id:X8}").ToArray();
|
|
RynthLog.Write(LogCat.IltHub,
|
|
$"[IltGuardian] no '{_item}' among {items.Count} carried items; similar names: {(similar.Length > 0 ? string.Join(", ", similar) : "none")}");
|
|
}
|
|
|
|
/// <summary>A vendor (weenie BF_VENDOR flag, or a cache entry already classed Vendor).</summary>
|
|
private bool IsVendor(WorldObject wo)
|
|
{
|
|
if (wo.ObjectClass == AcObjectClass.Vendor) return true;
|
|
var host = _ctx.Host;
|
|
return host.HasGetObjectBitfield && host.TryGetObjectBitfield(unchecked((uint)wo.Id), out uint bf) && (bf & BfVendor) != 0;
|
|
}
|
|
|
|
/// <summary>Logs every vendor the cache sees with its distance (vendor search found none in range).</summary>
|
|
private void LogVendorSearch()
|
|
{
|
|
var cache = _ctx.Inventory.Cache;
|
|
int playerId = unchecked((int)_ctx.Inventory.PlayerId);
|
|
if (cache == null || playerId == 0) { RynthLog.Write(LogCat.IltHub, "[IltGuardian] vendor search: no world cache / player"); return; }
|
|
var landscape = cache.GetLandscapeObjects().ToList();
|
|
var vendors = landscape.Where(IsVendor).Take(8)
|
|
.Select(wo => $"'{wo.Name}' 0x{(uint)wo.Id:X8} {cache.Distance(playerId, wo.Id):0.0} m").ToArray();
|
|
RynthLog.Write(LogCat.IltHub,
|
|
$"[IltGuardian] vendor search: {landscape.Count} landscape objects, bitfield={(_ctx.Host.HasGetObjectBitfield ? "yes" : "no")}, "
|
|
+ $"vendors: {(vendors.Length > 0 ? string.Join(", ", vendors) : "none")}");
|
|
}
|
|
|
|
/// <summary>Nearest landscape object matching <paramref name="match"/> within SearchRangeMeters.</summary>
|
|
private WorldObject? Nearest(Func<WorldObject, bool> match)
|
|
{
|
|
var cache = _ctx.Inventory.Cache;
|
|
int playerId = unchecked((int)_ctx.Inventory.PlayerId);
|
|
if (cache == null || playerId == 0) return null;
|
|
WorldObject? best = null;
|
|
double bestDist = SearchRangeMeters;
|
|
foreach (var wo in cache.GetLandscapeObjects())
|
|
{
|
|
if (!match(wo)) continue;
|
|
double d = cache.Distance(playerId, wo.Id);
|
|
if (d <= bestDist) { bestDist = d; best = wo; }
|
|
}
|
|
return best;
|
|
}
|
|
|
|
// ── Commands ────────────────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// "/ra guardian translate <text>|handin|stop|chat on|off|auto on|off|buy on|off|refresh|status".
|
|
/// "window" is handled by the controller. Pump thread.
|
|
/// </summary>
|
|
public void HandleCommand(string[] args)
|
|
{
|
|
string sub = args.Length > 0 ? args[0].ToLowerInvariant() : "status";
|
|
string rest = args.Length > 1 ? string.Join(" ", args.Skip(1)) : string.Empty;
|
|
switch (sub)
|
|
{
|
|
case "translate":
|
|
case "t":
|
|
_ctx.Chat("[ILT Guardian] " + TranslateManual(rest));
|
|
break;
|
|
case "handin":
|
|
case "give":
|
|
_ctx.Chat("[ILT Guardian] " + StartHandIn(_lastAnswer, _lastNpc, asked: true));
|
|
break;
|
|
case "stop":
|
|
StopHandIn();
|
|
_ctx.Chat("[ILT Guardian] Hand-in stopped.");
|
|
break;
|
|
case "chat":
|
|
S.AutoDetectFromChat = Toggle(rest, S.AutoDetectFromChat);
|
|
_ctx.Chat($"[ILT Guardian] Chat auto-detect {(S.AutoDetectFromChat ? "on" : "off")}.");
|
|
break;
|
|
case "auto":
|
|
S.AutoHandIn = Toggle(rest, S.AutoHandIn);
|
|
_ctx.Chat($"[ILT Guardian] Auto hand-in {(S.AutoHandIn ? "on" : "off")}.");
|
|
break;
|
|
case "buy":
|
|
S.BuyMissingFromVendor = Toggle(rest, S.BuyMissingFromVendor);
|
|
_ctx.Chat($"[ILT Guardian] Buy missing Temple items {(S.BuyMissingFromVendor ? "on" : "off")}.");
|
|
break;
|
|
case "refresh":
|
|
_ctx.Chat("[ILT Guardian] " + Attributes.Refresh());
|
|
break;
|
|
case "status":
|
|
_ctx.Chat($"[ILT Guardian] chat={(S.AutoDetectFromChat ? "on" : "off")} auto={(S.AutoHandIn ? "on" : "off")} buy={(S.BuyMissingFromVendor ? "on" : "off")} "
|
|
+ $"answer={(_lastAnswer.Length > 0 ? $"{_lastAnswer} -> {_lastNpc}" : "-")} hand-in: {_handInStatus}");
|
|
foreach (var r in Attributes.Snapshot)
|
|
_ctx.Chat($"[ILT Guardian] {r.Attribute,-12} {r.Npc,-16} #{r.Count} {r.Status} {r.Eta}");
|
|
break;
|
|
default:
|
|
_ctx.Chat("[ILT Guardian] /ra guardian [window [show|hide]|translate <text>|handin|stop|chat on|off|auto on|off|buy on|off|refresh|status]");
|
|
break;
|
|
}
|
|
}
|
|
|
|
// ── Reader ("RynthILT.Data" GetGuardianJson / GuardianCommand; RynthOracle's Quests tab) ──
|
|
|
|
/// <summary>
|
|
/// The reader JSON (Plugins/Shared/RynthIltDataApi.cs header has the shape): the switches, the
|
|
/// last answer and hand-in status, and the attribute turn-in rows. Built on demand: everything it
|
|
/// reads is volatile or an immutable snapshot. Any thread.
|
|
/// </summary>
|
|
public string ReaderJson(string world)
|
|
{
|
|
TimeSpan age = LastAnswerAge;
|
|
return BuildJson(world, S.AutoDetectFromChat, S.AutoHandIn, S.BuyMissingFromVendor, S.CooldownHoursAssumed,
|
|
_lastAnswer, _lastNpc, age == TimeSpan.MaxValue ? -1 : (long)age.TotalSeconds,
|
|
IltGuardianPhrasebook.MessageOf(_lastLine), HandInRunning, _handInStatus, Attributes.SyncedUtc, Attributes.Snapshot);
|
|
}
|
|
|
|
/// <summary>The reader JSON from its parts. Pure: the tests drive it.</summary>
|
|
internal static string BuildJson(string world, bool chat, bool auto, bool buy, int cooldownHours,
|
|
string answer, string npc, long answerAgeSec, string heard, bool running, string status,
|
|
DateTime syncedUtc, System.Collections.Generic.IReadOnlyList<IltTempleAttrStatus> rows)
|
|
{
|
|
var sb = new System.Text.StringBuilder(1024);
|
|
sb.Append("{\"available\":true");
|
|
Str(sb, "world", world);
|
|
sb.Append(",\"chat\":").Append(chat ? "true" : "false");
|
|
sb.Append(",\"auto\":").Append(auto ? "true" : "false");
|
|
sb.Append(",\"buy\":").Append(buy ? "true" : "false");
|
|
sb.Append(",\"cooldownHours\":").Append(cooldownHours);
|
|
Str(sb, "answer", answer);
|
|
Str(sb, "npc", npc);
|
|
sb.Append(",\"answerAgeSec\":").Append(answerAgeSec);
|
|
Str(sb, "heard", heard);
|
|
sb.Append(",\"running\":").Append(running ? "true" : "false");
|
|
Str(sb, "status", status);
|
|
Str(sb, "synced", syncedUtc == DateTime.MinValue ? "" : syncedUtc.ToString("yyyy-MM-ddTHH:mm:ssZ", System.Globalization.CultureInfo.InvariantCulture));
|
|
sb.Append(",\"rows\":[");
|
|
for (int i = 0; i < rows.Count; i++)
|
|
{
|
|
IltTempleAttrStatus r = rows[i];
|
|
if (i > 0) sb.Append(',');
|
|
sb.Append('{');
|
|
Str(sb, "attribute", r.Attribute, first: true);
|
|
Str(sb, "npc", r.Npc);
|
|
sb.Append(",\"count\":").Append(r.Count);
|
|
Str(sb, "status", r.Status);
|
|
Str(sb, "eta", r.Eta);
|
|
sb.Append(",\"ready\":").Append(r.Ready ? "true" : "false");
|
|
sb.Append(",\"exact\":").Append(r.FromMyQuests ? "true" : "false");
|
|
sb.Append('}');
|
|
}
|
|
sb.Append("]}");
|
|
return sb.ToString();
|
|
}
|
|
|
|
private static void Str(System.Text.StringBuilder sb, string name, string? value, bool first = false)
|
|
{
|
|
if (!first) sb.Append(',');
|
|
sb.Append('"').Append(name).Append("\":\"").Append(IltStyle.JsonEscape(value ?? string.Empty)).Append('"');
|
|
}
|
|
|
|
/// <summary>
|
|
/// A reader's verb line (header of Plugins/Shared/RynthIltDataApi.cs): handin | stop | refresh |
|
|
/// chat on|off | auto on|off | buy on|off. The reader shows the new state, so the switches don't
|
|
/// echo in chat; hand-in and refresh report as the Guardian window's buttons do. Pump thread.
|
|
/// </summary>
|
|
public void RunVerb(string line)
|
|
{
|
|
string[] parts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
if (parts.Length == 0) return;
|
|
string arg = parts.Length > 1 ? parts[1] : "";
|
|
switch (parts[0].ToLowerInvariant())
|
|
{
|
|
case "handin": StartHandIn(_lastAnswer, _lastNpc, asked: true); break;
|
|
case "stop": StopHandIn(); break;
|
|
case "refresh": _ctx.Chat("[ILT Guardian] " + Attributes.Refresh()); break;
|
|
case "chat": S.AutoDetectFromChat = Toggle(arg, S.AutoDetectFromChat); break;
|
|
case "auto": S.AutoHandIn = Toggle(arg, S.AutoHandIn); break;
|
|
case "buy": S.BuyMissingFromVendor = Toggle(arg, S.BuyMissingFromVendor); break;
|
|
default:
|
|
RynthLog.Trace(LogCat.IltHub, $"[IltGuardian] reader verb ignored: {line}");
|
|
return;
|
|
}
|
|
RynthLog.Write(LogCat.IltHub, $"[IltGuardian] reader verb: {line}");
|
|
}
|
|
|
|
private static bool Toggle(string mode, bool current) => mode.Trim().ToLowerInvariant() switch
|
|
{
|
|
"on" or "true" or "1" or "show" => true,
|
|
"off" or "false" or "0" or "hide" => false,
|
|
_ => !current,
|
|
};
|
|
|
|
// ── UI (render thread) ──────────────────────────────────────────────────
|
|
|
|
/// <summary>Guardian window body.</summary>
|
|
public void Render()
|
|
{
|
|
if (ImGui.CollapsingHeader("Riddle translator", ImGuiTreeNodeFlags.DefaultOpen)) RenderTranslator();
|
|
ImGui.Spacing();
|
|
if (ImGui.CollapsingHeader("Attribute turn-ins", ImGuiTreeNodeFlags.DefaultOpen)) RenderTracker();
|
|
}
|
|
|
|
private void RenderTranslator()
|
|
{
|
|
var s = S;
|
|
// Plain bool writes; the pump thread reads them on its next tick.
|
|
bool chat = s.AutoDetectFromChat;
|
|
if (ImGui.Checkbox("Detect from chat##grdchat", ref chat)) s.AutoDetectFromChat = chat;
|
|
if (ImGui.IsItemHovered()) ImGui.SetTooltip("Translate the guardians' tells as they arrive and show the answer here and on the Mini Remote.");
|
|
ImGui.SameLine();
|
|
bool auto = s.AutoHandIn;
|
|
if (ImGui.Checkbox("Auto hand-in##grdauto", ref auto)) s.AutoHandIn = auto;
|
|
if (ImGui.IsItemHovered()) ImGui.SetTooltip("After a chat answer, give one of the item to the guardian automatically.");
|
|
ImGui.SameLine();
|
|
bool buy = s.BuyMissingFromVendor;
|
|
if (ImGui.Checkbox("Buy if missing##grdbuy", ref buy)) s.BuyMissingFromVendor = buy;
|
|
if (ImGui.IsItemHovered()) ImGui.SetTooltip("Temple guardian: when you carry none, buy one from the open or nearest vendor first.");
|
|
|
|
string line = _lastLine;
|
|
if (line.Length > 0)
|
|
{
|
|
ImGui.TextDisabled("Heard:");
|
|
ImGui.SameLine();
|
|
ImGui.PushTextWrapPos(0f);
|
|
SafeImGui.TextDisabled(IltGuardianPhrasebook.MessageOf(line));
|
|
ImGui.PopTextWrapPos();
|
|
}
|
|
|
|
string answer = _lastAnswer, npc = _lastNpc;
|
|
if (answer.Length > 0)
|
|
{
|
|
SafeImGui.TextColored(IltStyle.ColTeal, answer);
|
|
ImGui.SameLine();
|
|
SafeImGui.TextDisabled("-> " + npc);
|
|
if (HandInRunning)
|
|
{
|
|
if (ImGui.SmallButton("Stop##grdstop")) _ctx.Post(() => StopHandIn());
|
|
}
|
|
else if (ImGui.SmallButton("Hand in##grdgive"))
|
|
{
|
|
_ctx.Post(() => StartHandIn(answer, npc, asked: true));
|
|
}
|
|
}
|
|
else ImGui.TextDisabled("No guardian answer yet.");
|
|
SafeImGui.TextDisabled("Hand-in: " + _handInStatus);
|
|
|
|
ImGui.Spacing();
|
|
ImGui.InputTextMultiline("##grdinput", ref _input, 2048u, new Vector2(-1, 54));
|
|
if (ImGui.Button("Translate##grdtr"))
|
|
{
|
|
string text = _input;
|
|
_ctx.Post(() => _ctx.Chat("[ILT Guardian] " + TranslateManual(text)));
|
|
}
|
|
ImGui.SameLine();
|
|
if (ImGui.Button("Clear##grdclr")) _input = string.Empty;
|
|
ImGui.SameLine();
|
|
ImGui.TextDisabled("Paste a spell word or the riddle's question line.");
|
|
}
|
|
|
|
private void RenderTracker()
|
|
{
|
|
if (ImGui.SmallButton("Refresh##grdref")) _ctx.Post(() => _ctx.Chat("[ILT Guardian] " + Attributes.Refresh()));
|
|
if (ImGui.IsItemHovered()) ImGui.SetTooltip("Re-read /myquests and /qb list (the server allows each once a minute).");
|
|
ImGui.SameLine();
|
|
DateTime synced = Attributes.SyncedUtc;
|
|
SafeImGui.TextDisabled(synced == DateTime.MinValue ? "/qb not read this session" : $"/qb read {synced.ToLocalTime():t}");
|
|
ImGui.SameLine();
|
|
if (ImGui.SmallButton("Reset counts##grdreset"))
|
|
_ctx.Confirm("Reset turn-in counts", "Forget the turn-in counts and cooldown estimates taken from /qb wait stamps? (/myquests counts are not affected.)",
|
|
Attributes.ResetCounts, "Reset");
|
|
|
|
var rows = Attributes.Snapshot;
|
|
if (rows.Length > 0 && ImGui.BeginTable("##grdattr", 5, ImGuiTableFlags.RowBg | ImGuiTableFlags.BordersInnerH | ImGuiTableFlags.SizingStretchProp))
|
|
{
|
|
ImGui.TableSetupColumn("Attribute", ImGuiTableColumnFlags.None, 1.0f);
|
|
ImGui.TableSetupColumn("NPC", ImGuiTableColumnFlags.None, 1.3f);
|
|
ImGui.TableSetupColumn("#", ImGuiTableColumnFlags.None, 0.35f);
|
|
ImGui.TableSetupColumn("Status", ImGuiTableColumnFlags.None, 0.9f);
|
|
ImGui.TableSetupColumn("Next", ImGuiTableColumnFlags.None, 1.5f);
|
|
ImGui.TableHeadersRow();
|
|
foreach (var r in rows)
|
|
{
|
|
ImGui.TableNextRow();
|
|
ImGui.TableNextColumn(); ImGui.TextUnformatted(r.Attribute);
|
|
ImGui.TableNextColumn(); ImGui.TextUnformatted(r.Npc);
|
|
ImGui.TableNextColumn(); ImGui.TextUnformatted(r.Count.ToString());
|
|
ImGui.TableNextColumn();
|
|
var col = r.Ready ? IltStyle.ColGreen
|
|
: r.Status == "Unknown" ? IltStyle.ColTextMute
|
|
: IltStyle.ColAmber;
|
|
SafeImGui.TextColored(col, r.Status);
|
|
if (ImGui.IsItemHovered())
|
|
SafeImGui.SetTooltip(r.FromMyQuests ? "From /myquests (exact)." : "From /qb wait stamps (count and timer are estimates).");
|
|
ImGui.TableNextColumn(); ImGui.TextUnformatted(r.Eta);
|
|
}
|
|
ImGui.EndTable();
|
|
}
|
|
|
|
int hours = S.CooldownHoursAssumed;
|
|
ImGui.SetNextItemWidth(140);
|
|
if (ImGui.SliderInt("Assumed cooldown (h)##grdhours", ref hours, 1, 72)) S.CooldownHoursAssumed = hours;
|
|
if (ImGui.IsItemHovered()) ImGui.SetTooltip("Used only for rows /myquests doesn't report: how long a /qb wait stamp lasts.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Mini Remote line: the last answer (for AnswerShownFor) with a Give / Stop button.
|
|
/// Draws nothing when there is no recent answer. Render thread.
|
|
/// </summary>
|
|
public void RenderRemoteLine()
|
|
{
|
|
string answer = _lastAnswer, npc = _lastNpc;
|
|
if (answer.Length == 0 || LastAnswerAge > AnswerShownFor) return;
|
|
ImGui.TextColored(IltStyle.ColTextMute, "Guardian:");
|
|
ImGui.SameLine();
|
|
SafeImGui.TextColored(IltStyle.ColTeal, answer);
|
|
if (ImGui.IsItemHovered()) SafeImGui.SetTooltip($"Give to {npc}\nHand-in: {_handInStatus}");
|
|
ImGui.SameLine();
|
|
if (HandInRunning)
|
|
{
|
|
if (ImGui.SmallButton("Stop##mrgrd")) _ctx.Post(() => StopHandIn());
|
|
}
|
|
else if (ImGui.SmallButton("Give##mrgrd"))
|
|
{
|
|
_ctx.Post(() => StartHandIn(answer, npc, asked: true));
|
|
}
|
|
}
|
|
}
|