using System; using System.Buffers; using System.Collections.Generic; using System.Text; using System.Text.Json; namespace RynthCore.Plugin.RynthNav; /// A route maker job's state, as Poll reports it. internal enum RouteJobState { Queued, Working, Done, Failed } /// /// One request to the route maker (a /rnav command or the "RynthNav.Routes" interface's Request): what /// was asked, where it is (queued, working, done, failed), a line saying so, and the result as JSON. /// The job table changes it; everyone else reads it through the table. /// internal sealed class RouteJob { public int Id; public RouteRequest Request = new(); public RouteJobState State; public string Detail = ""; /// The op's result (JSON), "null" until there is one. public string ResultJson = "null"; public long CreatedMs, StartedMs, EndedMs; /// The go op: how many gotos had ended when its walk started (the tick's bookkeeping). public int Seq; public bool Finished => State is RouteJobState.Done or RouteJobState.Failed; } /// /// The route maker's jobs: the last of them, oldest dropped first once /// finished. A new job is refused while every one kept is still queued or working. Ids go up from /// the first one given (the plugin starts them from the clock, so a reload doesn't reuse a caller's /// old ids); an id not kept any more is unknown. Locked: the interface's Request and Poll and the /// tick may all come at once. Host-free: the route tests drive it. /// internal sealed class RouteJobTable { public const int Capacity = 32; private readonly object _lock = new(); private readonly List _jobs = new(); private int _nextId; public RouteJobTable(int firstId) { _nextId = Math.Max(1, firstId); } public int Count { get { lock (_lock) return _jobs.Count; } } /// A new queued job for ; null (and why) when all the room is taken by unfinished jobs. public RouteJob? Add(RouteRequest request, long nowMs, out string error) { error = ""; lock (_lock) { if (_jobs.Count >= Capacity) { int old = _jobs.FindIndex(j => j.Finished); if (old < 0) { error = $"{Capacity} jobs are already waiting or working: poll them, then ask again"; return null; } _jobs.RemoveAt(old); } var job = new RouteJob { Id = _nextId, Request = request, State = RouteJobState.Queued, Detail = "queued", CreatedMs = nowMs }; _nextId = _nextId == int.MaxValue ? 1 : _nextId + 1; _jobs.Add(job); return job; } } public RouteJob? Get(int id) { lock (_lock) return _jobs.Find(j => j.Id == id); } /// The queued jobs, oldest first. public List Queued() { lock (_lock) return _jobs.FindAll(j => j.State == RouteJobState.Queued); } /// The working jobs, oldest first. public List Working() { lock (_lock) return _jobs.FindAll(j => j.State == RouteJobState.Working); } public void Start(RouteJob job, string detail, long nowMs) { lock (_lock) { if (job.Finished) return; job.State = RouteJobState.Working; job.Detail = detail; job.StartedMs = nowMs; } } /// A working job's line (a walk's distance left); a result to show while it works. public void Update(RouteJob job, string detail, string? resultJson = null) { lock (_lock) { if (job.Finished) return; job.Detail = detail; if (resultJson != null) job.ResultJson = resultJson; } } /// The job is over: done () or failed, with its line and result. A finished job stays as it is. public void Finish(RouteJob job, bool ok, string detail, string? resultJson, long nowMs) { lock (_lock) { if (job.Finished) return; job.State = ok ? RouteJobState.Done : RouteJobState.Failed; job.Detail = detail; if (resultJson != null) job.ResultJson = resultJson; job.EndedMs = nowMs; } } /// /// How a go job's walk ended, from the goto's last line: "arrived" (FinishGoto's "arrived", a dungeon's /// "arrived inside X" / "went through X") and "stopped" (a stop: the stop op, /rnav stop, the panel) /// are done (true); anything else ("stopped: no progress...", "stopped: logged out") failed. /// public static bool GoOutcome(string status, out string outcome) { if (status.StartsWith("arrived", StringComparison.Ordinal) || status.StartsWith("went through", StringComparison.Ordinal)) { outcome = "arrived"; return true; } if (status == "stopped") { outcome = "stopped"; return true; } outcome = "failed"; return false; } public static string StateName(RouteJobState s) => s switch { RouteJobState.Queued => "queued", RouteJobState.Working => "working", RouteJobState.Done => "done", _ => "failed", }; /// Poll's answer: {id, op, state, detail, result}; for an id not kept, {id, state: "unknown", error}. public string PollJson(int id, out bool known) { lock (_lock) { RouteJob? j = _jobs.Find(x => x.Id == id); known = j != null; if (j == null) return RouteJson.Write(w => { w.WriteStartObject(); w.WriteNumber("id", id); w.WriteString("state", "unknown"); w.WriteString("error", $"no job {id}: only the last {Capacity} are kept, and a RynthNav reload forgets them"); w.WriteEndObject(); }); return JobJson(j); } } /// Request's answer for a job just queued (the same fields as Poll). public string Accepted(RouteJob job) { lock (_lock) return JobJson(job); } private static string JobJson(RouteJob j) => RouteJson.Write(w => { w.WriteStartObject(); w.WriteNumber("id", j.Id); w.WriteString("op", RouteRequest.OpName(j.Request.Op)); w.WriteString("state", StateName(j.State)); w.WriteString("detail", j.Detail); w.WritePropertyName("result"); w.WriteRawValue(j.ResultJson, skipInputValidation: false); w.WriteEndObject(); }); } /// Small helpers for writing the route maker's JSON (System.Text.Json's writer: no reflection, AOT-safe). internal static class RouteJson { /// One JSON value written by , as a string. public static string Write(Action body) { var buf = new ArrayBufferWriter(512); using (var w = new Utf8JsonWriter(buf)) body(w); return Encoding.UTF8.GetString(buf.WrittenSpan); } /// {"error": why, "usage": usage}: a refused request. public static string Error(string why, string usage) => Write(w => { w.WriteStartObject(); w.WriteString("error", why); if (usage.Length > 0) w.WriteString("usage", usage); w.WriteEndObject(); }); /// A number rounded to , or null when it isn't one (the writer refuses NaN). public static void Num(Utf8JsonWriter w, string name, double v, int digits) { if (double.IsFinite(v)) w.WriteNumber(name, Math.Round(v, digits)); else w.WriteNull(name); } /// "name": {"ns": n, "ew": e} (4 decimals: 0.02 yd). public static void Place(Utf8JsonWriter w, string name, double ns, double ew) { w.WriteStartObject(name); Num(w, "ns", ns, 4); Num(w, "ew", ew, 4); w.WriteEndObject(); } public static void Strings(Utf8JsonWriter w, string name, IEnumerable values) { w.WriteStartArray(name); foreach (string s in values) w.WriteStringValue(s); w.WriteEndArray(); } }