feat(chat): Phase H.1 Talk/Tell/ChatChannel + HearSpeech + ChatLog

Completes the chat-wire layer end-to-end: outbound Talk (/say), Tell
(/tell), ChatChannel, + inbound HearSpeech (0x02BB) / HearRangedSpeech
(0x02BC) routed into a unified ChatLog that also consumes the already-
parsed GameEvent ChannelBroadcast / Tell / TransientMessage / Popup.

Wire layer (AcDream.Core.Net/Messages):
- ChatRequests.BuildTalk (0x0015, inside 0xF7B1): gameActionSequence
  + string16L message. PackString16L helper with 4-byte pad.
- ChatRequests.BuildTell (0x005D): targetName + message, each
  string16L with its own padding.
- ChatRequests.BuildChatChannel (0x0147): channelId + message.
- HearSpeech.TryParse handles BOTH 0x02BB local AND 0x02BC ranged —
  single parser with IsRanged flag in the returned record. Standalone
  GameMessage (NOT wrapped in 0xF7B0).

WorldSession integration:
- ProcessDatagram branch for HearSpeech.LocalOpcode /
  HearSpeech.RangedOpcode; fires new SpeechHeard event.
- Places the new branch before the 0xF7B0 GameEvent branch so ordering
  stays stable.

Core layer (AcDream.Core/Chat):
- ChatEntry record: (Kind, Sender, Text, SenderGuid, ChannelId, Received).
- ChatKind enum: LocalSpeech, RangedSpeech, Channel, Tell, System, Popup.
- ChatLog: ring-buffer (default 500) of entries; adapters for every
  inbound source (OnLocalSpeech, OnChannelBroadcast, OnTellReceived,
  OnSystemMessage, OnPopup) plus OnSelfSent for echoing outbound.
  Fires EntryAppended so UI panel can scroll / highlight.

Tests (15 new):
- ChatRequests: Talk / Tell / ChatChannel byte-exact encoding (including
  string16L padding edge cases).
- HearSpeech: local + ranged round-trip, wrong-opcode returns null.
- ChatLog: local / ranged / channel / tell / system / self echo,
  ring-buffer drops oldest, Clear empties.

Build green, 570 tests pass (up from 555).

With the chat wire layer in place, Phase H.1's "chat window panel"
(UI slice 05) is purely a UI task: instantiate ChatLog, bind to
EntryAppended, feed rows into the retail-UI widget toolkit. No more
protocol gaps.

Ref: r08 §3 (opcodes 0x0015, 0x005D, 0x0147), §2 (0x02BB, 0x02BC).
Ref: ACE GameMessageHearSpeech.cs + GameActionChannelBroadcast.cs.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-04-18 17:03:45 +02:00
parent c95aedcd4a
commit 404cab55ba
6 changed files with 568 additions and 0 deletions

View file

@ -0,0 +1,142 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
namespace AcDream.Core.Chat;
/// <summary>
/// Unified chat log — mirrors every chat-bearing message the server
/// sends (local HearSpeech, broadcast ChannelBroadcast, whispered
/// Tell, system TransientMessage, PopupString).
///
/// <para>
/// Sits behind the UI chat panel (Phase D.2) and the scripting
/// plugin API so plugins can react to chat (e.g. auto-reply, loot
/// logging).
/// </para>
///
/// <para>
/// Retail keeps ~200 lines of scrollback. Our ring buffer defaults to
/// 500 and is configurable.
/// </para>
/// </summary>
public sealed class ChatLog
{
private readonly ConcurrentQueue<ChatEntry> _buffer = new();
private readonly int _maxEntries;
public ChatLog(int maxEntries = 500)
{
if (maxEntries < 1) throw new ArgumentOutOfRangeException(nameof(maxEntries));
_maxEntries = maxEntries;
}
/// <summary>Fires every time a new entry is appended.</summary>
public event Action<ChatEntry>? EntryAppended;
/// <summary>Snapshot of all current entries, oldest first.</summary>
public ChatEntry[] Snapshot() => _buffer.ToArray();
public int Count => _buffer.Count;
// ── Inbound adapters ─────────────────────────────────────────────────────
/// <summary>Local or ranged HearSpeech (0x02BB / 0x02BC).</summary>
public void OnLocalSpeech(string sender, string text, uint senderGuid, bool isRanged)
{
Append(new ChatEntry(
Kind: isRanged ? ChatKind.RangedSpeech : ChatKind.LocalSpeech,
Sender: sender,
Text: text,
SenderGuid: senderGuid,
ChannelId: 0));
}
/// <summary>GameEvent ChannelBroadcast (0x0147).</summary>
public void OnChannelBroadcast(uint channelId, string sender, string text)
{
Append(new ChatEntry(
Kind: ChatKind.Channel,
Sender: sender,
Text: text,
SenderGuid: 0,
ChannelId: channelId));
}
/// <summary>GameEvent Tell (0x02BD) — whisper received.</summary>
public void OnTellReceived(string sender, string text, uint senderGuid)
{
Append(new ChatEntry(
Kind: ChatKind.Tell,
Sender: sender,
Text: text,
SenderGuid: senderGuid,
ChannelId: 0));
}
/// <summary>GameEvent CommunicationTransientString (0x02EB) — e.g. "Your spell fizzled!"</summary>
public void OnSystemMessage(string text, uint chatType)
{
Append(new ChatEntry(
Kind: ChatKind.System,
Sender: "",
Text: text,
SenderGuid: 0,
ChannelId: chatType));
}
/// <summary>GameEvent PopupString (0x0004) — modal dialog text.</summary>
public void OnPopup(string text)
{
Append(new ChatEntry(
Kind: ChatKind.Popup,
Sender: "",
Text: text,
SenderGuid: 0,
ChannelId: 0));
}
/// <summary>Echo the player's own outbound message after local send.</summary>
public void OnSelfSent(ChatKind kind, string text, string targetOrChannel = "")
{
Append(new ChatEntry(
Kind: kind,
Sender: targetOrChannel, // used as "to whom" for Tell / channel name for Channel
Text: text,
SenderGuid: 0,
ChannelId: 0));
}
private void Append(ChatEntry entry)
{
_buffer.Enqueue(entry);
while (_buffer.Count > _maxEntries)
_buffer.TryDequeue(out _);
EntryAppended?.Invoke(entry);
}
public void Clear()
{
while (_buffer.TryDequeue(out _)) { /* drain */ }
}
}
public enum ChatKind
{
LocalSpeech,
RangedSpeech,
Channel,
Tell,
System,
Popup,
}
public readonly record struct ChatEntry(
ChatKind Kind,
string Sender,
string Text,
uint SenderGuid,
uint ChannelId)
{
public DateTime Received { get; init; } = DateTime.UtcNow;
}