acdream/src/AcDream.UI.Abstractions/LiveCommandBus.cs
Erik 8e6e5a0b61 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>
2026-04-25 19:27:22 +02:00

64 lines
2.5 KiB
C#

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.");
}
}
}