RynthDreamWeave: launch request state
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5e732cecb7
commit
1eb8bf1e8a
3 changed files with 189 additions and 1 deletions
|
|
@ -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
|
||||
{
|
||||
/// <summary>Held by the chat pump and the render thread around every call.</summary>
|
||||
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; } = "";
|
||||
|
||||
/// <summary>Busy while a request is pending (checked first); else TooSoon within MinIntervalMs of the last Begin.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>The send failed: nothing was asked, so nothing is pending.</summary>
|
||||
public void Cancel(string status)
|
||||
{
|
||||
_pending = null;
|
||||
Status = status;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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, <paramref name="message"/> is empty and the
|
||||
/// code is only in <paramref name="code"/>.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>The pending destination once, at or after Begin + ReplyTimeoutMs; null otherwise.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Login/logout: forget the request and the status.</summary>
|
||||
public void Reset()
|
||||
{
|
||||
_pending = null;
|
||||
Status = "";
|
||||
_everBegun = false;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@
|
|||
<!-- DreamWeave's web destinations and the launch reply's wire text (pure). -->
|
||||
<Compile Include="..\..\Plugins\RynthCore.Plugin.RynthDreamWeave\DreamWeave\DreamWeaveWeb.cs" Link="DreamWeave\DreamWeaveWeb.cs" />
|
||||
<Compile Include="..\..\Plugins\RynthCore.Plugin.RynthDreamWeave\DreamWeave\LaunchWire.cs" Link="DreamWeave\LaunchWire.cs" />
|
||||
<Compile Include="..\..\Plugins\RynthCore.Plugin.RynthDreamWeave\DreamWeave\WebLauncher.cs" Link="DreamWeave\WebLauncher.cs" />
|
||||
<!-- RynthConquest's own features (pure parts) and the RynthOracle sources it links. -->
|
||||
<Compile Include="..\..\Plugins\RynthCore.Plugin.RynthConquest\Conquest\ConquestData.cs" Link="Conquest\ConquestData.cs" />
|
||||
<Compile Include="..\..\Plugins\RynthCore.Plugin.RynthConquest\Conquest\ConquestFeature.cs" Link="Conquest\ConquestFeature.cs" />
|
||||
|
|
|
|||
Loading…
Reference in a new issue