diff --git a/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebLauncher.cs b/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebLauncher.cs new file mode 100644 index 0000000..56d70f6 --- /dev/null +++ b/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebLauncher.cs @@ -0,0 +1,90 @@ +// The launch request state machine for the website buttons: one "/weblogin" at a time, a 10 s +// reply window, and a status line for the card. Pure (no engine calls), so RynthServerPack.Tests +// compiles it directly. The code is handed to the caller through OnChatLine and goes nowhere +// else: never into Status, never into the message, never into a field. +using System; + +namespace RynthCore.Plugin.RynthDreamWeave; + +internal enum LaunchBegin { Started, Busy, TooSoon } +internal enum LaunchLine { NotOurs, Code, Refused } + +internal sealed class WebLauncher +{ + /// Held by the chat pump and the render thread around every call. + public readonly object Gate = new(); + + private WebDestination? _pending; + private long _deadlineMs; + private long _lastBeginMs; + private bool _everBegun; + + public WebDestination? Pending => _pending; + public string Status { get; private set; } = ""; + + /// Busy while a request is pending (checked first); else TooSoon within MinIntervalMs of the last Begin. + public LaunchBegin Begin(WebDestination d, long nowMs) + { + if (_pending != null) return LaunchBegin.Busy; + if (_everBegun && nowMs - _lastBeginMs < LaunchWire.MinIntervalMs) return LaunchBegin.TooSoon; + _pending = d; + _everBegun = true; + _lastBeginMs = nowMs; + _deadlineMs = nowMs + LaunchWire.ReplyTimeoutMs; + Status = "Asking DreamWeave for a login code for " + d.Label + "..."; + return LaunchBegin.Started; + } + + /// The send failed: nothing was asked, so nothing is pending. + public void Cancel(string status) + { + _pending = null; + Status = status; + } + + /// + /// NotOurs unless a request is pending and the deadline has not passed. Code and Refused clear + /// Pending and the caller eats the line. On Code, is empty and the + /// code is only in . + /// + public LaunchLine OnChatLine(string text, int chatType, long nowMs, out WebDestination? dest, out string code, out string message) + { + dest = null; + code = ""; + message = ""; + WebDestination? p = _pending; + if (p == null || nowMs >= _deadlineMs) return LaunchLine.NotOurs; + if (!LaunchWire.TryParse(text, chatType, out string parsedCode, out string refusal)) return LaunchLine.NotOurs; + + _pending = null; + dest = p; + if (parsedCode.Length > 0) + { + code = parsedCode; + Status = "Opening " + p.Label + "..."; + return LaunchLine.Code; + } + message = refusal; + Status = p.Label + ": DreamWeave said: " + refusal; + return LaunchLine.Refused; + } + + /// The pending destination once, at or after Begin + ReplyTimeoutMs; null otherwise. + public WebDestination? TakeTimedOut(long nowMs) + { + WebDestination? p = _pending; + if (p == null || nowMs < _deadlineMs) return null; + _pending = null; + Status = "No reply from DreamWeave to " + LaunchWire.RequestCommand + " within " + + (LaunchWire.ReplyTimeoutMs / 1000) + " s, so nothing was opened."; + return p; + } + + /// Login/logout: forget the request and the status. + public void Reset() + { + _pending = null; + Status = ""; + _everBegun = false; + } +} \ No newline at end of file diff --git a/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs b/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs index 509412a..5585fc0 100644 --- a/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs +++ b/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs @@ -19,8 +19,10 @@ internal static class DreamWeaveWebTests r.Add("dreamweave web: destination ids are verbs no one else uses", IdsAreFree); r.Add("dreamweave web: LaunchWire: code alphabet is strict", Alphabet); r.Add("dreamweave web: ACE's refusals parse as refusals, other lines are ignored", Refusals); + r.Add("dreamweave web: replies count only while asked, once", RepliesOnlyWhileAsked); + r.Add("dreamweave web: one request at a time, never stuck", OneAtATime); + r.Add("dreamweave web: the code never reaches a status or message", CodeNeverInStatus); } - private const string Code = "AbCdEfGhIjKlMnOpQrStUvWxYz0123456789-_AbCde"; // 43 chars private const string Line = "[WEB] Launch code: " + Code + " (single use, expires in 60 seconds)"; @@ -150,6 +152,101 @@ internal static class DreamWeaveWebTests Check.True(!Parse("[WEBX] Launch code: " + Code, 0x05, out _, out _), "lookalike lead ignored"); } + private static WebDestination Market => DreamWeaveWeb.ById("market")!; + + private static void RepliesOnlyWhileAsked() + { + var w = new WebLauncher(); + Check.True(w.OnChatLine(Line, 0x05, 0, out _, out _, out _) == LaunchLine.NotOurs, "nothing pending: not ours"); + + Check.True(w.Begin(Market, 1000) == LaunchBegin.Started, "begin"); + Check.Eq(w.Status, "Asking DreamWeave for a login code for Market...", "asking status"); + var k = w.OnChatLine(Line, 0x05, 1000 + 9_999, out var dest, out string code, out string msg); + Check.True(k == LaunchLine.Code, "line at +9,999 ms is the code"); + Check.Eq(code, Code, "the code"); + Check.Eq(msg, "", "no message on a code"); + Check.True(ReferenceEquals(dest, Market), "destination handed back"); + Check.True(w.Pending == null, "code clears pending"); + Check.True(w.OnChatLine(Line, 0x05, 1000 + 9_999, out dest, out code, out msg) == LaunchLine.NotOurs, "an identical second line is not ours"); + Check.True(dest == null && code == "" && msg == "", "no outputs on NotOurs"); + + // Timeout at exactly +10,000. + var t = new WebLauncher(); + t.Begin(Market, 50_000); + Check.True(t.OnChatLine(Line, 0x05, 60_000, out _, out _, out _) == LaunchLine.NotOurs, "line at +10,000 ms is late"); + Check.True(t.TakeTimedOut(59_999) == null, "not timed out at +9,999"); + Check.True(ReferenceEquals(t.TakeTimedOut(60_000), Market), "timed out once at +10,000"); + Check.True(t.TakeTimedOut(60_001) == null, "and only once"); + Check.True(t.Pending == null, "timeout clears pending"); + Check.Eq(t.Status, "No reply from DreamWeave to /weblogin within 10 s, so nothing was opened.", "timeout status"); + + // Refusal clears pending. + var f = new WebLauncher(); + f.Begin(Market, 0); + string refusal = "[WEB] The website sign-in service is not available right now."; + Check.True(f.OnChatLine(refusal, 0x05, 10, out dest, out code, out msg) == LaunchLine.Refused, "refusal"); + Check.Eq(msg, "The website sign-in service is not available right now.", "refusal message"); + Check.Eq(code, "", "no code on refusal"); + Check.Eq(f.Status, "Market: DreamWeave said: The website sign-in service is not available right now.", "refusal status"); + Check.True(f.Pending == null, "refusal clears pending"); + + // A player's copy and unrelated chat leave the request pending. + var p = new WebLauncher(); + p.Begin(Market, 0); + Check.True(p.OnChatLine(Line, 0x02, 5, out _, out _, out _) == LaunchLine.NotOurs, "player copy not ours"); + Check.True(p.OnChatLine("You say, \"hi\"", 0x05, 5, out _, out _, out _) == LaunchLine.NotOurs, "other chat not ours"); + Check.True(p.Pending != null, "still pending"); + + // Reset clears pending and status. + p.Reset(); + Check.True(p.Pending == null, "reset clears pending"); + Check.Eq(p.Status, "", "reset clears status"); + Check.True(p.OnChatLine(Line, 0x05, 5, out _, out _, out _) == LaunchLine.NotOurs, "after reset: not ours"); + } + + private static void OneAtATime() + { + var w = new WebLauncher(); + Check.True(w.Begin(Market, 100_000) == LaunchBegin.Started, "first begin"); + Check.True(w.Begin(DreamWeaveWeb.ById("suit")!, 100_001) == LaunchBegin.Busy, "second while pending: busy"); + Check.True(w.Begin(DreamWeaveWeb.ById("suit")!, 100_000 + 5_000) == LaunchBegin.Busy, "busy wins over too-soon/free"); + Check.True(ReferenceEquals(w.Pending, Market), "first request still the pending one"); + + w.Cancel("Could not send /weblogin."); + Check.True(w.Pending == null, "cancel clears pending"); + Check.Eq(w.Status, "Could not send /weblogin.", "cancel status"); + Check.True(w.Begin(Market, 100_000 + 2_999) == LaunchBegin.TooSoon, "within 3 s of the last begin: too soon"); + Check.True(w.Pending == null, "too soon starts nothing"); + Check.True(w.Begin(Market, 100_000 + 3_000) == LaunchBegin.Started, "at 3 s: started"); + + // Never stuck: a timeout frees the slot. + Check.True(w.TakeTimedOut(103_000 + 10_000) != null, "timeout"); + Check.True(w.Begin(Market, 113_000) == LaunchBegin.Started, "begin after timeout"); + } + + private static void CodeNeverInStatus() + { + var w = new WebLauncher(); + w.Begin(Market, 0); + Check.True(!w.Status.Contains(Code), "status while asking"); + var k = w.OnChatLine(Line, 0x05, 100, out _, out string code, out string msg); + Check.True(k == LaunchLine.Code, "code flow"); + Check.Eq(code.Length, 43, "43-char code returned"); + Check.True(!w.Status.Contains(Code), "status after code"); + Check.True(!msg.Contains(Code), "message after code"); + Check.True(!w.Status.Contains(code.Substring(0, 12)), "no code fragment in status"); + + // Timeout and refusal paths too. + var t = new WebLauncher(); + t.Begin(Market, 0); + t.TakeTimedOut(10_000); + Check.True(!t.Status.Contains(Code), "timeout status"); + var f = new WebLauncher(); + f.Begin(Market, 0); + f.OnChatLine("[WEB] nope", 0x05, 1, out _, out _, out msg); + Check.True(!f.Status.Contains(Code) && !msg.Contains(Code), "refusal status and message"); + } + private static string[] ServerPluginIds() { string? dir = AppContext.BaseDirectory; diff --git a/Tools/RynthServerPack.Tests/RynthServerPack.Tests.csproj b/Tools/RynthServerPack.Tests/RynthServerPack.Tests.csproj index a0fc97d..1fb19fa 100644 --- a/Tools/RynthServerPack.Tests/RynthServerPack.Tests.csproj +++ b/Tools/RynthServerPack.Tests/RynthServerPack.Tests.csproj @@ -37,6 +37,7 @@ +