// IltSessionRates.cs — Session earnings / per-hour rates for the ILT Hub Character tab. // Originally SilentKelpie's ILT Hub (RynthAi IltHub/); moved to RynthILT 2026-10-13. // // Sources (all passive — nothing is sent to the server except the optional bank refresh // the Banking tab already does): // XP, luminance (total, last 5 min, allegiance pass-up) and kills: RynthAi's Mini Remote // session stats (RynthAi.Hub SessionStats) — RynthTracker's SessionStats code, so this // table, the Mini Remote and the Tracker panel count them the same way (2026-10-11). // Without RynthAi those rows are left out. // Coins positive bank Enlightened Coins deltas + carried coin (WCID 300004) gains. // Pyreals positive bank pyreal deltas + carried pyreal (WCID 273) gains; MMD = 250k. // Items gains of each item in the item→coin conversion table (carried count), // converted to potential coins with ItemsPerConversion / CoinsPerConversion. // A deposit or withdrawal only moves money between pack and bank: while Banking reports one // (MovingMoney) the coin and pyreal baselines follow along without counting a gain. using System; using System.Collections.Generic; using System.Linq; using ImGuiNET; using RynthCore.Plugin.Shared; namespace RynthCore.Plugin.RynthILT.Hub; internal sealed class IltSessionRates : IIltFeature { private const long PyrealsPerMmd = 250_000; private const long SampleIntervalMs = 5000; private const long RowsIntervalMs = 1000; private const string PassUpLabel = " pass-up"; private readonly IltHubContext _ctx; private DateTime _start = DateTime.UtcNow; private long _coins; private long _pyreals; // Carried-item baselines (last seen counts) for gain tracking. private long _lastInvCoins = -1; private long _lastInvPyreals = -1; private readonly Dictionary _lastItemCounts = new(StringComparer.OrdinalIgnoreCase); private readonly Dictionary _itemGains = new(StringComparer.OrdinalIgnoreCase); private long _lastSampleAt; private long _lastRowsAt; // Render snapshot. private volatile RateRow[] _rows = Array.Empty(); private sealed record RateRow(string Label, string Total, string PerHour); /// Coin and pyreal per-hour figures for the Mini Remote HUD (rebuilt every sample). public sealed record RateSummary(double CoinsPerHour, double PyrealsPerHour); private static readonly RateSummary EmptySummary = new(0, 0); private volatile RateSummary _summary = EmptySummary; /// Latest coin / pyreal summary (safe from the render thread). public RateSummary Summary => _summary; /// When the coin / pyreal session (or the last Reset) started. public DateTime SessionStartUtc => _start; public IltSessionRates(IltHubContext ctx) => _ctx = ctx; /// True while money moves between pack and bank (set by the controller from Banking). Pump thread. public Func? MovingMoney { get; set; } private bool Moving => MovingMoney?.Invoke() ?? false; /// Banking hook: bank balance changed (pump thread). public void OnBankBalanceChanged(string key, long oldValue, long newValue) { RynthLog.Trace(LogCat.IltRates, $"balance {key}: {oldValue} -> {newValue}"); // The first read after login only establishes the baseline (old value is the stale cache). if (_lastSampleAt == 0) return; if (Moving) return; // a deposit, not income long delta = newValue - oldValue; if (delta <= 0) return; // spending / sending isn't income if (key == "EnlightenedCoins") _coins += delta; else if (key == "Pyreals") _pyreals += delta; } /// Coins, pyreals and item gains start over (login, or with ). public void Reset() { RynthLog.Trace(LogCat.IltRates, $"Reset()"); _start = DateTime.UtcNow; _coins = _pyreals = 0; _lastInvCoins = _lastInvPyreals = -1; _lastItemCounts.Clear(); _itemGains.Clear(); _lastSampleAt = 0; _lastRowsAt = 0; _summary = EmptySummary; } /// The player's reset (this tab, the Mini Remote): coins here and the shared session stats together. Pump thread. public void ResetAll() { Reset(); _ctx.Ai.ResetSessionStats(); } // ── IIltFeature ───────────────────────────────────────────────────────── public void Tick(long nowMs) { // The shared stats move every 0.5 s; their rows refresh each second, coins every 5 s. if (nowMs - _lastSampleAt < SampleIntervalMs) { if (nowMs - _lastRowsAt >= RowsIntervalMs) { _lastRowsAt = nowMs; BuildRows(); } return; } _lastSampleAt = nowMs; _lastRowsAt = nowMs; var inv = _ctx.Inventory; long coinGain = Gain(ref _lastInvCoins, inv.CountByWcid(IltInventory.WcidEnlightenedCoin)); long pyrealGain = Gain(ref _lastInvPyreals, inv.CountByWcid(IltInventory.WcidPyreal)); // A withdrawal: the baselines moved, nothing earned. if (!Moving) { _coins += coinGain; _pyreals += pyrealGain; } // ToArray: the render thread edits this table. foreach (var conv in _ctx.State.Character.ItemConversions.ToArray()) { if (string.IsNullOrWhiteSpace(conv.ItemName)) continue; long count = inv.CountByName(conv.ItemName); long last = _lastItemCounts.TryGetValue(conv.ItemName, out long l) ? l : -1; long gain = Gain(ref last, count); _lastItemCounts[conv.ItemName] = last; if (gain > 0) _itemGains[conv.ItemName] = (_itemGains.TryGetValue(conv.ItemName, out long g) ? g : 0) + gain; } BuildRows(); } /// Positive change since the last sample; the first sample only sets the baseline. private static long Gain(ref long last, long current) { if (last < 0) { last = current; return 0; } long d = current - last; last = current; return d > 0 ? d : 0; } // Luminance chat goes to RynthAi's session stats (its OnChatWindowText). public bool OnChat(string text) => false; public void OnLogout() => Reset(); // ── Rows ──────────────────────────────────────────────────────────────── private void BuildRows() { double hours = Math.Max((DateTime.UtcNow - _start).TotalHours, 1.0 / 3600); _summary = new RateSummary(_coins / hours, _pyreals / hours); var rows = new List(); if (_ctx.Ai.TryGetSessionStats(out SessionStatsV1 s)) { rows.Add(new RateRow("XP", IltParse.Compact(s.Xp), IltParse.Compact(s.XpPerHour))); rows.Add(new RateRow("Luminance", IltParse.Compact(s.Lum), IltParse.Compact(s.LumPerHour))); // Per hour at the last five minutes' pace. rows.Add(new RateRow(" last 5 min", IltParse.Compact(s.LumLast5Min), IltParse.Compact(s.LumLast5Min * 12.0))); if (s.PassUpTracked != 0) rows.Add(new RateRow(PassUpLabel, IltParse.Compact(s.LumPassUp), IltParse.Compact(s.PassUpPerHour))); rows.Add(new RateRow("Kills", s.Kills.ToString(), s.KillsPerHour.ToString("0"))); } rows.Add(new RateRow("Enlightened Coins", IltParse.Compact(_coins), IltParse.Compact(_coins / hours))); rows.Add(new RateRow("Pyreals", IltParse.Compact(_pyreals), IltParse.Compact(_pyreals / hours))); rows.Add(new RateRow(" as MMD", (_pyreals / (double)PyrealsPerMmd).ToString("0.0"), (_pyreals / (double)PyrealsPerMmd / hours).ToString("0.0"))); double potentialCoins = 0; foreach (var conv in _ctx.State.Character.ItemConversions.ToArray()) { if (!_itemGains.TryGetValue(conv.ItemName, out long gained) || gained == 0) continue; double coins = conv.ItemsPerConversion > 0 ? gained / (double)conv.ItemsPerConversion * conv.CoinsPerConversion : 0; potentialCoins += coins; rows.Add(new RateRow(" " + conv.ItemName, IltParse.N0(gained), IltParse.Compact(gained / hours))); } if (potentialCoins > 0) rows.Add(new RateRow("Potential coins (items)", potentialCoins.ToString("0"), (potentialCoins / hours).ToString("0.0"))); _rows = rows.ToArray(); } /// What the pass-up figure is (shared with the Mini Remote's Stats). public const string PassUpTooltip = "Luminance that reached your bank without a \"You've banked\" line: your vassals' pass-up.\n" + "Fellowship and Zone Share splits arrive the same silent way and are included."; // ── UI (render thread) ────────────────────────────────────────────────── public void Render() { // The session clock is RynthAi's session stats' (it and the coins reset together). var start = _ctx.Ai.TryGetSessionStats(out SessionStatsV1 v) && v.SessionSeconds > 0 && v.StartUtcTicks > 0 ? new DateTime(v.StartUtcTicks, DateTimeKind.Utc) : _start; ImGui.TextDisabled($"Session {IltParse.Duration(DateTime.UtcNow - start)}"); ImGui.SameLine(); if (ImGui.SmallButton("Reset##rates")) _ctx.Post(ResetAll); if (ImGui.BeginTable("##iltrates", 3, ImGuiTableFlags.RowBg | ImGuiTableFlags.BordersInnerV)) { ImGui.TableSetupColumn("Metric"); ImGui.TableSetupColumn("Session"); ImGui.TableSetupColumn("Per hour"); ImGui.TableHeadersRow(); foreach (var r in _rows) { ImGui.TableNextRow(); ImGui.TableNextColumn(); ImGui.TextUnformatted(r.Label); if (ReferenceEquals(r.Label, PassUpLabel) && ImGui.IsItemHovered()) ImGui.SetTooltip(PassUpTooltip); ImGui.TableNextColumn(); ImGui.TextUnformatted(r.Total); ImGui.TableNextColumn(); ImGui.TextColored(IltStyle.ColTeal, r.PerHour); } ImGui.EndTable(); } if (ImGui.TreeNode("Item -> coin conversions")) { RenderConversionEditor(); ImGui.TreePop(); } } private string _newItemName = string.Empty; private void RenderConversionEditor() { var list = _ctx.State.Character.ItemConversions; int remove = -1; for (int i = 0; i < list.Count; i++) { var c = list[i]; ImGui.PushID(i); ImGui.TextUnformatted(c.ItemName); ImGui.SameLine(220); int items = c.ItemsPerConversion, coins = c.CoinsPerConversion; ImGui.SetNextItemWidth(70); if (ImGui.InputInt("items", ref items, 0)) c.ItemsPerConversion = Math.Max(1, items); ImGui.SameLine(); ImGui.SetNextItemWidth(70); if (ImGui.InputInt("coins", ref coins, 0)) c.CoinsPerConversion = Math.Max(0, coins); ImGui.SameLine(); if (ImGui.SmallButton("x")) remove = i; ImGui.PopID(); } if (remove >= 0) { var victim = list[remove]; _ctx.Post(() => list.Remove(victim)); } ImGui.SetNextItemWidth(200); ImGui.InputTextWithHint("##newconv", "exact item name", ref _newItemName, 96u); ImGui.SameLine(); if (ImGui.SmallButton("Add") && _newItemName.Trim().Length > 0 && !list.Any(c => c.ItemName.Equals(_newItemName.Trim(), StringComparison.OrdinalIgnoreCase))) { var added = new IltItemConversion { ItemName = _newItemName.Trim(), ItemsPerConversion = 100, CoinsPerConversion = 0 }; _ctx.Post(() => list.Add(added)); _newItemName = string.Empty; } } }