using System; using System.Collections.Generic; using System.Collections.Concurrent; namespace AcDream.Core.Chat; /// /// Unified chat log — mirrors every chat-bearing message the server /// sends (local HearSpeech, broadcast ChannelBroadcast, whispered /// Tell, system TransientMessage, PopupString). /// /// /// 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). /// /// /// /// Retail keeps ~200 lines of scrollback. Our ring buffer defaults to /// 500 and is configurable. /// /// public sealed class ChatLog { private readonly ConcurrentQueue _buffer = new(); private readonly int _maxEntries; public ChatLog(int maxEntries = 500) { if (maxEntries < 1) throw new ArgumentOutOfRangeException(nameof(maxEntries)); _maxEntries = maxEntries; } /// Fires every time a new entry is appended. public event Action? EntryAppended; /// Snapshot of all current entries, oldest first. public ChatEntry[] Snapshot() => _buffer.ToArray(); public int Count => _buffer.Count; // ── Inbound adapters ───────────────────────────────────────────────────── /// Local or ranged HearSpeech (0x02BB / 0x02BC). /// /// Phase I.5: an empty is substituted with /// "You" — the server uses this convention to indicate that the /// player is the speaker (e.g. their own ranged shouts echo back). /// Port from holtburger /// references/holtburger/.../client/messages.rs lines 476-487. /// public void OnLocalSpeech(string sender, string text, uint senderGuid, bool isRanged) { string effectiveSender = string.IsNullOrEmpty(sender) ? "You" : sender; Append(new ChatEntry( Kind: isRanged ? ChatKind.RangedSpeech : ChatKind.LocalSpeech, Sender: effectiveSender, Text: text, SenderGuid: senderGuid, ChannelId: 0)); } /// EmoteText (0x01E0) — server-driven third-person emote. public void OnEmote(string senderName, string text, uint senderGuid) { Append(new ChatEntry( Kind: ChatKind.Emote, Sender: senderName, Text: text, SenderGuid: senderGuid, ChannelId: 0)); } /// SoulEmote (0x01E2) — complex emote (chat + paired animation). public void OnSoulEmote(string senderName, string text, uint senderGuid) { Append(new ChatEntry( Kind: ChatKind.SoulEmote, Sender: senderName, Text: text, SenderGuid: senderGuid, ChannelId: 0)); } /// PlayerKilled (0x019E) — death announcement. /// /// Death messages are routed as so they /// share styling with other server announcements. The /// SenderGuid field carries the victim guid; the /// ChannelId field carries the killer guid (a small misuse /// of the field but avoids a schema change). /// public void OnPlayerKilled(string deathMessage, uint victimGuid, uint killerGuid) { Append(new ChatEntry( Kind: ChatKind.System, Sender: "", Text: deathMessage, SenderGuid: victimGuid, ChannelId: killerGuid)); } /// WeenieError (0x028A) / WeenieErrorWithString (0x028B). /// /// Phase I.5: previously-orphaned parser. The server fires this when a /// game-logic action fails (e.g. "you don't have enough mana", "you /// can't pick that up"). Routed as ; the /// ChannelId field carries the WeenieError code so plugins can /// filter or react. is the interpolated /// substring (null for plain WeenieError, set for WeenieErrorWithString). /// public void OnWeenieError(uint errorId, string? param) { // Phase I (post-launch fix): translate the wire code into the // retail-faithful template via WeenieErrorMessages. Many codes // are *informational* (e.g. 0x051B "You have entered the X // channel.", 0x051D "Turbine Chat is enabled.") not errors; // the old "WeenieError 0xNNNN" framing was misleading. Unknown // codes still fall back to the raw "WeenieError 0xNNNN[: param]" // form so nothing is silently lost. See // WeenieErrorMessages.Format for the templates + lookup table. string text = WeenieErrorMessages.Format(errorId, param); Append(new ChatEntry( Kind: ChatKind.System, Sender: "", Text: text, SenderGuid: 0, ChannelId: errorId)); } /// GameEvent ChannelBroadcast (0x0147). public void OnChannelBroadcast(uint channelId, string sender, string text) { Append(new ChatEntry( Kind: ChatKind.Channel, Sender: sender, Text: text, SenderGuid: 0, ChannelId: channelId)); } /// GameEvent Tell (0x02BD) — whisper received. public void OnTellReceived(string sender, string text, uint senderGuid) { Append(new ChatEntry( Kind: ChatKind.Tell, Sender: sender, Text: text, SenderGuid: senderGuid, ChannelId: 0)); } /// GameEvent CommunicationTransientString (0x02EB) — e.g. "Your spell fizzled!" public void OnSystemMessage(string text, uint chatType) { Append(new ChatEntry( Kind: ChatKind.System, Sender: "", Text: text, SenderGuid: 0, ChannelId: chatType)); } /// GameEvent PopupString (0x0004) — modal dialog text. public void OnPopup(string text) { Append(new ChatEntry( Kind: ChatKind.Popup, Sender: "", Text: text, SenderGuid: 0, ChannelId: 0)); } /// /// Phase I.7: combat-translator emits a pre-formatted line. The /// translator () subscribes to /// events and renders the retail /// template (e.g. "You hit Mosswart for 12 slashing damage (54.0%) /// Critical hit.") and decorates the entry with a /// so the panel can color the /// line. Maps to holtburger's info().combat() / /// warning().combat() / error().combat() tag flow at /// chat.rs:221-308. /// public void OnCombatLine(string text, Combat.CombatLineKind kind = Combat.CombatLineKind.Info) { Append(new ChatEntry( Kind: ChatKind.Combat, Sender: "", Text: text, SenderGuid: 0, ChannelId: 0) { CombatKind = kind, }); } /// Echo the player's own outbound message after local send. 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, Emote, SoulEmote, /// /// Phase I.7: a combat feedback line emitted by /// from /// events. The accompanying field /// drives panel coloring (info / warning / error per holtburger /// chat.rs:221-308). /// Combat, } public readonly record struct ChatEntry( ChatKind Kind, string Sender, string Text, uint SenderGuid, uint ChannelId) { public DateTime Received { get; init; } = DateTime.UtcNow; /// /// Phase I.7: severity bucket for /// entries. Null for every other kind. Drives the /// 's TextColored color choice. /// public Combat.CombatLineKind? CombatKind { get; init; } }