feat(ui+net): #16 LiveCommandBus + WorldSession.Send{Talk,Tell,Channel} + SendChatCmd wiring

Replaces NullCommandBus.Instance in PanelContext with a real
LiveCommandBus when a live session is active. Panels publish
SendChatCmd; the host routes it to the right wire opcode + emits
a ChatLog.OnSelfSent local echo (optimistic; retail-equivalent
for Talk).

Pieces:
- ChatChannelKind enum (UI.Abstractions) - mirrors holtburger's
  ChatChannelKind (references/holtburger/.../client/types.rs:35-49).
- SendChatCmd record (UI.Abstractions) - (Channel, TargetName?, Text).
- LiveCommandBus (UI.Abstractions) - single-handler-per-type;
  Register<T> throws on double-register; Publish<T> logs missing
  handler but does not throw.
- ChannelResolver (UI.Abstractions) - port of holtburger's
  resolve_legacy_channel (client/commands.rs:50-62) mapping
  ChatChannelKind to legacy ChatChannel ids verbatim from
  holtburger-protocol/.../chat/types.rs:8-24 (Fellow=0x0800,
  AllegianceBroadcast=0x02000000, Vassals=0x1000, Patron=0x2000,
  Monarch=0x4000, CoVassals=0x01000000).
- WorldSession.SendTalk / SendTell / SendChannel - 3-line wrappers
  around existing ChatRequests.Build* + SendGameAction. Internal
  GameActionCapture seam + InternalsVisibleTo for tests.
- GameWindow registers SendChatCmd handler: Say -> SendTalk +
  ChatLog echo, Tell -> SendTell + echo, channel kinds ->
  ChannelResolver.Resolve -> SendChannel + echo.

12 new tests across SendChatCmd + LiveCommandBus + ChannelResolver
+ WorldSessionChat. NullCommandBus.Instance retained for back-compat
when no live session.

Solution total: 893 green (51 + 229 + 613).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-04-25 19:27:22 +02:00
parent ff5ed9ec0b
commit 8e6e5a0b61
10 changed files with 457 additions and 1 deletions

View file

@ -300,6 +300,12 @@ public sealed class GameWindow : IDisposable
private static readonly bool DevToolsEnabled = private static readonly bool DevToolsEnabled =
Environment.GetEnvironmentVariable("ACDREAM_DEVTOOLS") == "1"; Environment.GetEnvironmentVariable("ACDREAM_DEVTOOLS") == "1";
// Phase I.3 — real ICommandBus for live sessions. Constructed when
// the live session spins up (so SendChatCmd handlers can close over
// _liveSession + Chat). Null when offline; PanelContext then falls
// back to NullCommandBus.Instance.
private AcDream.UI.Abstractions.LiveCommandBus? _commandBus;
// Phase G.1-G.2 world lighting/time state. // Phase G.1-G.2 world lighting/time state.
public readonly AcDream.Core.World.WorldTimeService WorldTime = public readonly AcDream.Core.World.WorldTimeService WorldTime =
new AcDream.Core.World.WorldTimeService( new AcDream.Core.World.WorldTimeService(
@ -1221,6 +1227,41 @@ public sealed class GameWindow : IDisposable
senderGuid: speech.SenderGuid, senderGuid: speech.SenderGuid,
isRanged: speech.IsRanged); isRanged: speech.IsRanged);
// Phase I.3: real ICommandBus. Panels publish SendChatCmd here
// and we route it to the right wire opcode (Talk / Tell / ChatChannel)
// plus a local echo into ChatLog so the player sees their own
// message immediately. Closes over _liveSession + Chat so this
// wiring only exists for the lifetime of the live session.
var liveSession = _liveSession;
var chat = Chat;
_commandBus = new AcDream.UI.Abstractions.LiveCommandBus();
_commandBus.Register<AcDream.UI.Abstractions.SendChatCmd>(cmd =>
{
if (string.IsNullOrEmpty(cmd.Text)) return;
switch (cmd.Channel)
{
case AcDream.UI.Abstractions.ChatChannelKind.Say:
liveSession.SendTalk(cmd.Text);
chat.OnSelfSent(AcDream.Core.Chat.ChatKind.LocalSpeech, cmd.Text);
break;
case AcDream.UI.Abstractions.ChatChannelKind.Tell:
if (string.IsNullOrEmpty(cmd.TargetName)) return;
liveSession.SendTell(cmd.TargetName, cmd.Text);
chat.OnSelfSent(
AcDream.Core.Chat.ChatKind.Tell, cmd.Text,
targetOrChannel: cmd.TargetName);
break;
default:
var resolved = AcDream.UI.Abstractions.ChannelResolver.Resolve(cmd.Channel);
if (resolved is null) return;
liveSession.SendChannel(resolved.Value.ChannelId, cmd.Text);
chat.OnSelfSent(
AcDream.Core.Chat.ChatKind.Channel, cmd.Text,
targetOrChannel: resolved.Value.DisplayName);
break;
}
});
// Issue #5: feed PrivateUpdateVital + PrivateUpdateVitalCurrent // Issue #5: feed PrivateUpdateVital + PrivateUpdateVitalCurrent
// into LocalPlayer so VitalsPanel can draw Stam / Mana bars. // into LocalPlayer so VitalsPanel can draw Stam / Mana bars.
_liveSession.VitalUpdated += v => _liveSession.VitalUpdated += v =>
@ -4116,9 +4157,15 @@ public sealed class GameWindow : IDisposable
// today) would need manual protection. // today) would need manual protection.
if (DevToolsEnabled && _imguiBootstrap is not null && _panelHost is not null) if (DevToolsEnabled && _imguiBootstrap is not null && _panelHost is not null)
{ {
// Phase I.3 — prefer the live command bus when a live session
// is up so panel-emitted SendChatCmd actually flows server-ward.
// Fall back to NullCommandBus for offline / pre-connect renders.
AcDream.UI.Abstractions.ICommandBus bus =
_commandBus ?? (AcDream.UI.Abstractions.ICommandBus)
AcDream.UI.Abstractions.NullCommandBus.Instance;
var ctx = new AcDream.UI.Abstractions.PanelContext( var ctx = new AcDream.UI.Abstractions.PanelContext(
(float)deltaSeconds, (float)deltaSeconds,
AcDream.UI.Abstractions.NullCommandBus.Instance); bus);
_panelHost.RenderAll(ctx); _panelHost.RenderAll(ctx);
_imguiBootstrap.Render(); _imguiBootstrap.Render();
} }

View file

@ -10,4 +10,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\AcDream.Core\AcDream.Core.csproj" /> <ProjectReference Include="..\AcDream.Core\AcDream.Core.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="AcDream.Core.Net.Tests" />
</ItemGroup>
</Project> </Project>

View file

@ -784,9 +784,27 @@ public sealed class WorldSession : IDisposable
/// </summary> /// </summary>
public void SendGameAction(byte[] gameActionBody) public void SendGameAction(byte[] gameActionBody)
{ {
// Phase I.3 test seam: when set, intercept the body before the
// wire-write path runs (which would otherwise NPE on an unseeded
// ISAAC keystream during unit tests). Production callers leave
// this null and the body proceeds to the framed/encrypted UDP send.
if (GameActionCapture is not null)
{
GameActionCapture(gameActionBody);
return;
}
SendGameMessage(gameActionBody); SendGameMessage(gameActionBody);
} }
/// <summary>
/// Phase I.3: test-only hook. When non-null, <see cref="SendGameAction"/>
/// invokes this instead of writing to the wire. Lets unit tests verify
/// that <see cref="SendTalk"/>/<see cref="SendTell"/>/<see cref="SendChannel"/>
/// produce the bytes they should without standing up a full handshake +
/// ISAAC keystream. Production sites never set this.
/// </summary>
internal Action<byte[]>? GameActionCapture { get; set; }
/// <summary> /// <summary>
/// Phase B.2: get and increment the game-action sequence counter. /// Phase B.2: get and increment the game-action sequence counter.
/// Call once per outbound movement message; pass the returned value /// Call once per outbound movement message; pass the returned value
@ -795,6 +813,44 @@ public sealed class WorldSession : IDisposable
/// </summary> /// </summary>
public uint NextGameActionSequence() => ++_gameActionSequence; public uint NextGameActionSequence() => ++_gameActionSequence;
/// <summary>
/// Phase I.3: send a local /say message (heard within ~20m).
/// Wraps <see cref="ChatRequests.BuildTalk"/>.
/// </summary>
public void SendTalk(string text)
{
ArgumentNullException.ThrowIfNull(text);
uint seq = NextGameActionSequence();
byte[] body = ChatRequests.BuildTalk(seq, text);
SendGameAction(body);
}
/// <summary>
/// Phase I.3: send a /tell (whisper) by target character name.
/// Wraps <see cref="ChatRequests.BuildTell"/>.
/// </summary>
public void SendTell(string targetName, string text)
{
ArgumentNullException.ThrowIfNull(targetName);
ArgumentNullException.ThrowIfNull(text);
uint seq = NextGameActionSequence();
byte[] body = ChatRequests.BuildTell(seq, targetName, text);
SendGameAction(body);
}
/// <summary>
/// Phase I.3: send to a chat channel (allegiance, fellowship, etc.) by
/// the legacy <c>ChatChannel</c> bitflag id.
/// Wraps <see cref="ChatRequests.BuildChatChannel"/>.
/// </summary>
public void SendChannel(uint channelId, string text)
{
ArgumentNullException.ThrowIfNull(text);
uint seq = NextGameActionSequence();
byte[] body = ChatRequests.BuildChatChannel(seq, channelId, text);
SendGameAction(body);
}
private void SendGameMessage(byte[] gameMessageBody) private void SendGameMessage(byte[] gameMessageBody)
{ {
var fragment = GameMessageFragment.BuildSingleFragment( var fragment = GameMessageFragment.BuildSingleFragment(

View file

@ -0,0 +1,47 @@
namespace AcDream.UI.Abstractions;
/// <summary>
/// Maps a <see cref="ChatChannelKind"/> to the legacy <c>ChatChannel</c>
/// bitflag id used by the 0x0147 ChatChannel GameAction. Ported from
/// holtburger's <c>resolve_legacy_channel</c>
/// (<c>references/holtburger/crates/holtburger-core/src/client/commands.rs</c>
/// lines 50-62) cross-referenced against the <c>ChatChannel</c> enum
/// (<c>references/holtburger/crates/holtburger-protocol/src/messages/chat/types.rs</c>
/// lines 8-24) for the actual numeric ids.
///
/// <para>
/// Returns <c>null</c> for non-legacy kinds (Say, Tell, Unknown, and the
/// Turbine-routed General/Trade/etc.) — those callers handle dispatch
/// themselves. <see cref="ChatChannelKind.Say"/> rides Talk (0x0015) and
/// <see cref="ChatChannelKind.Tell"/> rides Tell (0x005D); the Turbine
/// channels need TurbineChat wiring not yet in place (Phase I.6).
/// </para>
/// </summary>
public static class ChannelResolver
{
/// <summary>Result of a successful legacy-channel resolution.</summary>
public readonly record struct Resolved(uint ChannelId, string DisplayName);
/// <summary>
/// Resolve <paramref name="kind"/> to a legacy ChatChannel id + a
/// human-readable display name for chat-log echo. Returns <c>null</c>
/// for non-legacy kinds; the caller routes those separately.
/// </summary>
public static Resolved? Resolve(ChatChannelKind kind) => kind switch
{
// ChatChannel values from holtburger-protocol/src/messages/chat/types.rs:
// Fellow = 0x00000800
// Vassals = 0x00001000
// Patron = 0x00002000
// Monarch = 0x00004000
// CoVassals = 0x01000000
// AllegianceBroadcast = 0x02000000
ChatChannelKind.Fellowship => new Resolved(0x00000800u, "Fellowship"),
ChatChannelKind.Allegiance => new Resolved(0x02000000u, "Allegiance"),
ChatChannelKind.Vassals => new Resolved(0x00001000u, "Vassals"),
ChatChannelKind.Patron => new Resolved(0x00002000u, "Patron"),
ChatChannelKind.Monarch => new Resolved(0x00004000u, "Monarch"),
ChatChannelKind.CoVassals => new Resolved(0x01000000u, "CoVassals"),
_ => null,
};
}

View file

@ -0,0 +1,40 @@
namespace AcDream.UI.Abstractions;
/// <summary>
/// Outbound chat channel selector. Mirrors holtburger's <c>ChatChannelKind</c>
/// (<c>references/holtburger/crates/holtburger-core/src/client/types.rs</c>
/// lines 35-49) plus a synthetic <see cref="Say"/> + <see cref="Tell"/> for
/// the two non-channel cases — local speech and whispers — so a single
/// <see cref="SendChatCmd"/> can carry every outbound flavour the chat
/// panel emits.
///
/// <para>
/// Channels split into:
/// <list type="bullet">
/// <item><b>Legacy</b> (Fellowship..CoVassals): map to a fixed <c>ChatChannel</c>
/// bitflag id via <see cref="ChannelResolver"/> and ride 0x0147 ChatChannel.</item>
/// <item><b>Turbine</b> (General..Olthoi): require a TurbineChat channel id
/// resolved at runtime — not yet wired (Phase I.6 owns TurbineChat).</item>
/// <item><b>Say</b> / <b>Tell</b>: route to the dedicated 0x0015 / 0x005D
/// opcodes — no channel id needed.</item>
/// </list>
/// </para>
/// </summary>
public enum ChatChannelKind
{
Say,
Tell,
Fellowship,
Allegiance,
Vassals,
Patron,
Monarch,
CoVassals,
General,
Trade,
Lfg,
Roleplay,
Society,
Olthoi,
Unknown,
}

View file

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
namespace AcDream.UI.Abstractions;
/// <summary>
/// Real <see cref="ICommandBus"/> implementation — single-handler-per-type
/// dispatch keyed by <c>typeof(T)</c>. Replaces <see cref="NullCommandBus"/>
/// in live sessions; <see cref="NullCommandBus"/> persists for tests and
/// non-live UI scenarios where no command flow is wanted.
///
/// <para>
/// <b>Threading.</b> Both <see cref="Register{T}"/> and <see cref="Publish{T}"/>
/// run on the render thread today (panels render on the render thread, and
/// host wiring happens at startup). The internal handler dictionary is
/// not synchronized — register all handlers during host setup before the
/// panel host starts rendering.
/// </para>
///
/// <para>
/// Phase I.3 of the chat/UI consolidation plan
/// (<c>~/.claude/plans/ticklish-conjuring-cake.md</c>): primary client of
/// the bus is the <see cref="SendChatCmd"/> handler wired by GameWindow
/// against <c>WorldSession.SendTalk/SendTell/SendChannel</c> + the local
/// <c>ChatLog</c> echo.
/// </para>
/// </summary>
public sealed class LiveCommandBus : ICommandBus
{
private readonly Dictionary<Type, Delegate> _handlers = new();
/// <summary>
/// Register a single handler for commands of type <typeparamref name="T"/>.
/// Throws <see cref="InvalidOperationException"/> if a handler is already
/// registered for this type — single-handler-per-type is intentional so
/// command routing is unambiguous.
/// </summary>
public void Register<T>(Action<T> handler) where T : notnull
{
ArgumentNullException.ThrowIfNull(handler);
if (_handlers.ContainsKey(typeof(T)))
throw new InvalidOperationException(
$"A handler for command type {typeof(T).FullName} is already registered.");
_handlers[typeof(T)] = handler;
}
/// <inheritdoc />
public void Publish<T>(T command) where T : notnull
{
ArgumentNullException.ThrowIfNull(command);
if (_handlers.TryGetValue(typeof(T), out var handler))
{
((Action<T>)handler).Invoke(command);
}
else
{
// Soft-warn: command published with no registered handler.
// Don't throw — the host may publish optional commands a non-
// live build doesn't wire (e.g. inventory pre-Phase I.7).
Console.WriteLine(
$"[LiveCommandBus] no handler registered for {typeof(T).FullName}; dropping.");
}
}
}

View file

@ -0,0 +1,14 @@
namespace AcDream.UI.Abstractions;
/// <summary>
/// Command published by chat panels to send a message. The host resolves
/// <see cref="Channel"/> + <see cref="TargetName"/> + <see cref="Text"/>
/// into the right wire opcode (Talk, Tell, or ChatChannel) and echoes
/// locally via <c>ChatLog.OnSelfSent</c>.
///
/// <para>
/// <see cref="TargetName"/> is meaningful only for
/// <see cref="ChatChannelKind.Tell"/>; ignored otherwise.
/// </para>
/// </summary>
public sealed record SendChatCmd(ChatChannelKind Channel, string? TargetName, string Text);

View file

@ -0,0 +1,79 @@
using System.Net;
using AcDream.Core.Net;
using AcDream.Core.Net.Messages;
namespace AcDream.Core.Net.Tests;
/// <summary>
/// Phase I.3 — verifies that <see cref="WorldSession.SendTalk"/>,
/// <see cref="WorldSession.SendTell"/>, and <see cref="WorldSession.SendChannel"/>
/// produce the same wire bytes that <see cref="ChatRequests"/> builders do,
/// using a sequence number drawn from <see cref="WorldSession.NextGameActionSequence"/>.
///
/// <para>
/// Uses the internal <c>GameActionCapture</c> test seam to intercept the
/// game-action body before it hits the (unseeded) ISAAC-encrypted wire path.
/// </para>
/// </summary>
public sealed class WorldSessionChatTests
{
private static WorldSession NewSession()
{
// Bind to a throwaway loopback endpoint; we never actually
// exchange packets — the capture hook intercepts the body.
var ep = new IPEndPoint(IPAddress.Loopback, 65000);
return new WorldSession(ep);
}
[Fact]
public void SendTalk_EmitsBytesIdenticalToChatRequestsBuildTalk()
{
using var session = NewSession();
byte[]? captured = null;
session.GameActionCapture = body => captured = body;
session.SendTalk("hello");
// After SendTalk, the sequence counter has been incremented to 1.
// ChatRequests.BuildTalk(seq=1, "hello") should match exactly.
byte[] expected = ChatRequests.BuildTalk(1, "hello");
Assert.NotNull(captured);
Assert.Equal(expected, captured);
}
[Fact]
public void SendTell_EmitsBytesIdenticalToChatRequestsBuildTell()
{
using var session = NewSession();
byte[]? captured = null;
session.GameActionCapture = body => captured = body;
session.SendTell("Alice", "hey");
byte[] expected = ChatRequests.BuildTell(1, "Alice", "hey");
Assert.NotNull(captured);
Assert.Equal(expected, captured);
}
[Fact]
public void SendChannel_IncrementsSequence_AndMatchesBuildChatChannel()
{
using var session = NewSession();
var captured = new System.Collections.Generic.List<byte[]>();
session.GameActionCapture = body => captured.Add(body);
session.SendChannel(channelId: 0x00000800u, "raid plan");
session.SendChannel(channelId: 0x02000000u, "allegiance ping");
Assert.Equal(2, captured.Count);
Assert.Equal(ChatRequests.BuildChatChannel(1, 0x00000800u, "raid plan"), captured[0]);
Assert.Equal(ChatRequests.BuildChatChannel(2, 0x02000000u, "allegiance ping"), captured[1]);
}
[Fact]
public void SendTalk_NullText_Throws()
{
using var session = NewSession();
Assert.Throws<ArgumentNullException>(() => session.SendTalk(null!));
}
}

View file

@ -0,0 +1,73 @@
namespace AcDream.UI.Abstractions.Tests;
public sealed class LiveCommandBusTests
{
private sealed record FakeCmd(int Value);
private sealed record OtherCmd(string Tag);
[Fact]
public void Publish_InvokesRegisteredHandler_ForMatchingType()
{
var bus = new LiveCommandBus();
FakeCmd? captured = null;
bus.Register<FakeCmd>(c => captured = c);
bus.Publish(new FakeCmd(42));
Assert.NotNull(captured);
Assert.Equal(42, captured!.Value);
}
[Fact]
public void Publish_DoesNotThrow_WhenNoHandlerRegistered()
{
var bus = new LiveCommandBus();
// No handler for OtherCmd — must not throw.
bus.Publish(new OtherCmd("x"));
}
[Fact]
public void Register_Twice_ForSameType_Throws()
{
var bus = new LiveCommandBus();
bus.Register<FakeCmd>(_ => { });
Assert.Throws<InvalidOperationException>(() => bus.Register<FakeCmd>(_ => { }));
}
}
public sealed class ChannelResolverTests
{
[Fact]
public void Resolve_LegacyChannels_MatchesHoltburgerIds()
{
// Holtburger references/holtburger/crates/holtburger-protocol/src/messages/chat/types.rs:8-24
// and holtburger-core/src/client/commands.rs:50-62. Six legacy channels
// map to known ChatChannel ids; the rest fall through to TurbineChat.
Assert.Equal((0x00000800u, "Fellowship"), AsTuple(ChannelResolver.Resolve(ChatChannelKind.Fellowship)));
Assert.Equal((0x02000000u, "Allegiance"), AsTuple(ChannelResolver.Resolve(ChatChannelKind.Allegiance)));
Assert.Equal((0x00001000u, "Vassals"), AsTuple(ChannelResolver.Resolve(ChatChannelKind.Vassals)));
Assert.Equal((0x00002000u, "Patron"), AsTuple(ChannelResolver.Resolve(ChatChannelKind.Patron)));
Assert.Equal((0x00004000u, "Monarch"), AsTuple(ChannelResolver.Resolve(ChatChannelKind.Monarch)));
Assert.Equal((0x01000000u, "CoVassals"), AsTuple(ChannelResolver.Resolve(ChatChannelKind.CoVassals)));
}
[Fact]
public void Resolve_NonLegacyChannels_ReturnsNull()
{
// Say + Tell are handled separately by the SendChatCmd dispatcher.
// General/Trade/etc. require a TurbineChat channel id we don't yet wire.
Assert.Null(ChannelResolver.Resolve(ChatChannelKind.Say));
Assert.Null(ChannelResolver.Resolve(ChatChannelKind.Tell));
Assert.Null(ChannelResolver.Resolve(ChatChannelKind.General));
Assert.Null(ChannelResolver.Resolve(ChatChannelKind.Trade));
Assert.Null(ChannelResolver.Resolve(ChatChannelKind.Unknown));
}
private static (uint ChannelId, string DisplayName) AsTuple(ChannelResolver.Resolved? r)
{
Assert.NotNull(r);
return (r!.Value.ChannelId, r.Value.DisplayName);
}
}

View file

@ -0,0 +1,33 @@
namespace AcDream.UI.Abstractions.Tests;
public sealed class SendChatCmdTests
{
[Fact]
public void Construct_DefaultsTargetNameToNull_ForNonTellChannels()
{
var cmd = new SendChatCmd(ChatChannelKind.Say, TargetName: null, Text: "hello");
Assert.Equal(ChatChannelKind.Say, cmd.Channel);
Assert.Null(cmd.TargetName);
Assert.Equal("hello", cmd.Text);
}
[Fact]
public void Equality_HoldsForRecordsWithSameValues()
{
var a = new SendChatCmd(ChatChannelKind.Tell, "Alice", "hi");
var b = new SendChatCmd(ChatChannelKind.Tell, "Alice", "hi");
Assert.Equal(a, b);
Assert.Equal(a.GetHashCode(), b.GetHashCode());
}
[Fact]
public void Equality_DiffersWhenChannelDiffers()
{
var a = new SendChatCmd(ChatChannelKind.Fellowship, null, "raid time");
var b = new SendChatCmd(ChatChannelKind.Allegiance, null, "raid time");
Assert.NotEqual(a, b);
}
}