Subscribes to CombatState's DamageDealtAccepted / DamageTaken /
MissedOutgoing / EvadedIncoming / AttackDone / KillLanded events
and emits chat-line text into ChatLog.OnCombatLine, mirroring
holtburger's templates verbatim from references/holtburger/apps/
holtburger-cli/src/pages/game/panels/chat.rs:221-308.
Pieces:
- ChatLog: new ChatKind.Combat value; new CombatLineKind enum
(Info / Warning / Error) on ChatEntry; OnCombatLine(text, kind)
adapter.
- CombatChatTranslator (Core, IDisposable). Static formatters:
FormatDamageType (slashing/piercing/bludgeoning/fire/cold/acid/
electric/nether), FormatDamageLocation (head/chest/abdomen/
upper arm/lower arm/hand/upper leg/lower leg/foot), FormatPercent,
FormatAttackConditionsSuffix.
- ChatVM.RecentLinesDetailed() returns FormattedLine records with
kind metadata so panels can render combat lines colored.
- ChatPanel switches on Kind/CombatKind: combat-Info -> yellow,
combat-Warning -> red incoming-damage, combat-Error -> deep red,
all others -> existing renderer.Text path.
- GameWindow constructs translator after GameEventWiring.WireAll;
disposes in OnClosing + live-session failure path.
Templates landed:
Attacker: "You hit {def} for {dmg} {dtype} damage ({hp%}). [Crit]{suffix}"
Defender: "{atk} hit you for {dmg} {dtype} damage to your {loc} ({hp%})..."
Evade-out: "{def} evaded your attack."
Evade-in: "You evaded {atk}'s attack."
AttackErr: "Attack sequence finished with {error}."
Kill: synthesized "You killed {name}." + server PlayerKilled
death-message arrives separately via ChatLog.OnPlayerKilled.
Deviations from holtburger templates (documented in source):
- DamageDealt omits Critical-hit suffix until CombatState.DamageDealt
carries the flag (defender-side has it; attacker-side doesn't yet).
- DamageTaken omits (health%) until CombatState.DamageIncoming
parses the wire health-percent field.
- AttackConditions suffix is implemented but always empty until the
bitflag is plumbed into CombatState records.
18 new tests (12 translator + 4 ChatVMCombat + 2 ChatLog).
Solution total: 978 green (243 Core.Net + 639 Core + 96 UI).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
202 lines
6.4 KiB
C#
202 lines
6.4 KiB
C#
using AcDream.Core.Chat;
|
|
using AcDream.Core.Combat;
|
|
using Xunit;
|
|
|
|
namespace AcDream.Core.Tests.Chat;
|
|
|
|
public sealed class ChatLogTests
|
|
{
|
|
[Fact]
|
|
public void OnLocalSpeech_AppendsEntry_FiresEvent()
|
|
{
|
|
var log = new ChatLog();
|
|
ChatEntry? seen = null;
|
|
log.EntryAppended += e => seen = e;
|
|
|
|
log.OnLocalSpeech("Alice", "hi", 0xAA, isRanged: false);
|
|
|
|
Assert.Equal(1, log.Count);
|
|
Assert.NotNull(seen);
|
|
Assert.Equal(ChatKind.LocalSpeech, seen!.Value.Kind);
|
|
Assert.Equal("Alice", seen.Value.Sender);
|
|
Assert.Equal("hi", seen.Value.Text);
|
|
}
|
|
|
|
[Fact]
|
|
public void OnLocalSpeech_Ranged_SetsRangedKind()
|
|
{
|
|
var log = new ChatLog();
|
|
log.OnLocalSpeech("Bob", "SHOUT", 0xBB, isRanged: true);
|
|
Assert.Equal(ChatKind.RangedSpeech, log.Snapshot()[0].Kind);
|
|
}
|
|
|
|
[Fact]
|
|
public void OnChannelBroadcast_SetsChannelId()
|
|
{
|
|
var log = new ChatLog();
|
|
log.OnChannelBroadcast(channelId: 42, sender: "Alice", text: "allegiance motd");
|
|
var e = log.Snapshot()[0];
|
|
Assert.Equal(42u, e.ChannelId);
|
|
Assert.Equal(ChatKind.Channel, e.Kind);
|
|
}
|
|
|
|
[Fact]
|
|
public void OnTellReceived_SetsTellKind()
|
|
{
|
|
var log = new ChatLog();
|
|
log.OnTellReceived("Alice", "psst", 0xAA);
|
|
Assert.Equal(ChatKind.Tell, log.Snapshot()[0].Kind);
|
|
}
|
|
|
|
[Fact]
|
|
public void OnSystemMessage_EncodesChatType_AsChannelId()
|
|
{
|
|
var log = new ChatLog();
|
|
log.OnSystemMessage("Your spell fizzled!", chatType: 5);
|
|
var e = log.Snapshot()[0];
|
|
Assert.Equal(ChatKind.System, e.Kind);
|
|
Assert.Equal(5u, e.ChannelId);
|
|
}
|
|
|
|
[Fact]
|
|
public void OnSelfSent_EchoesOutbound()
|
|
{
|
|
var log = new ChatLog();
|
|
log.OnSelfSent(ChatKind.Tell, "hey", targetOrChannel: "Alice");
|
|
var e = log.Snapshot()[0];
|
|
Assert.Equal("Alice", e.Sender);
|
|
Assert.Equal("hey", e.Text);
|
|
}
|
|
|
|
[Fact]
|
|
public void RingBuffer_DropsOldestBeyondCapacity()
|
|
{
|
|
var log = new ChatLog(maxEntries: 3);
|
|
log.OnLocalSpeech("A", "1", 0, false);
|
|
log.OnLocalSpeech("B", "2", 0, false);
|
|
log.OnLocalSpeech("C", "3", 0, false);
|
|
log.OnLocalSpeech("D", "4", 0, false);
|
|
|
|
var snap = log.Snapshot();
|
|
Assert.Equal(3, snap.Length);
|
|
Assert.Equal("2", snap[0].Text); // "1" was dropped
|
|
Assert.Equal("4", snap[2].Text);
|
|
}
|
|
|
|
[Fact]
|
|
public void Clear_EmptiesBuffer()
|
|
{
|
|
var log = new ChatLog();
|
|
log.OnLocalSpeech("A", "1", 0, false);
|
|
log.Clear();
|
|
Assert.Equal(0, log.Count);
|
|
}
|
|
|
|
// ── Phase I.5: emote / soul-emote / killed / weenie-error adapters ──
|
|
|
|
[Fact]
|
|
public void OnEmote_AppendsEmoteEntry()
|
|
{
|
|
var log = new ChatLog();
|
|
log.OnEmote("Caith", "waves at you", 0xCAFE);
|
|
var e = log.Snapshot()[0];
|
|
Assert.Equal(ChatKind.Emote, e.Kind);
|
|
Assert.Equal("Caith", e.Sender);
|
|
Assert.Equal("waves at you", e.Text);
|
|
Assert.Equal(0xCAFEu, e.SenderGuid);
|
|
}
|
|
|
|
[Fact]
|
|
public void OnSoulEmote_AppendsSoulEmoteEntry()
|
|
{
|
|
var log = new ChatLog();
|
|
log.OnSoulEmote("Bob", "dances", 0xBEEF);
|
|
var e = log.Snapshot()[0];
|
|
Assert.Equal(ChatKind.SoulEmote, e.Kind);
|
|
Assert.Equal("Bob", e.Sender);
|
|
Assert.Equal("dances", e.Text);
|
|
Assert.Equal(0xBEEFu, e.SenderGuid);
|
|
}
|
|
|
|
[Fact]
|
|
public void OnPlayerKilled_AppendsSystemEntry_StoresGuids()
|
|
{
|
|
var log = new ChatLog();
|
|
log.OnPlayerKilled("Caith was killed by a Drudge.",
|
|
victimGuid: 0x12345678u, killerGuid: 0x90ABCDEFu);
|
|
var e = log.Snapshot()[0];
|
|
Assert.Equal(ChatKind.System, e.Kind);
|
|
Assert.Equal("Caith was killed by a Drudge.", e.Text);
|
|
Assert.Equal(0x12345678u, e.SenderGuid);
|
|
Assert.Equal(0x90ABCDEFu, e.ChannelId); // killer guid stashed here
|
|
}
|
|
|
|
[Fact]
|
|
public void OnWeenieError_PlainCode_AppendsSystemEntry()
|
|
{
|
|
var log = new ChatLog();
|
|
log.OnWeenieError(errorId: 0x1234, param: null);
|
|
var e = log.Snapshot()[0];
|
|
Assert.Equal(ChatKind.System, e.Kind);
|
|
Assert.Contains("0x1234", e.Text);
|
|
Assert.Equal(0x1234u, e.ChannelId);
|
|
}
|
|
|
|
[Fact]
|
|
public void OnWeenieError_WithString_AppendsInterpolation()
|
|
{
|
|
var log = new ChatLog();
|
|
log.OnWeenieError(errorId: 0x5678, param: "Mana Stone");
|
|
var e = log.Snapshot()[0];
|
|
Assert.Equal(ChatKind.System, e.Kind);
|
|
Assert.Contains("Mana Stone", e.Text);
|
|
}
|
|
|
|
[Fact]
|
|
public void OnLocalSpeech_EmptySender_SubstitutesYou()
|
|
{
|
|
// Holtburger client/messages.rs lines 476-487 — empty sender
|
|
// means the player is the speaker (echo back of their own
|
|
// ranged shout). Substitute "You" so the chat line reads
|
|
// "You: hello" instead of ": hello".
|
|
var log = new ChatLog();
|
|
log.OnLocalSpeech(sender: "", text: "hello", senderGuid: 0, isRanged: false);
|
|
var e = log.Snapshot()[0];
|
|
Assert.Equal("You", e.Sender);
|
|
Assert.Equal("hello", e.Text);
|
|
}
|
|
|
|
[Fact]
|
|
public void OnLocalSpeech_NonEmptySender_KeepsAsIs()
|
|
{
|
|
var log = new ChatLog();
|
|
log.OnLocalSpeech(sender: "Alice", text: "hi", senderGuid: 0xAA, isRanged: false);
|
|
Assert.Equal("Alice", log.Snapshot()[0].Sender);
|
|
}
|
|
|
|
// ── Phase I.7: combat-line adapter ────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void OnCombatLine_DefaultsInfoKind_TagsEntryAsCombat()
|
|
{
|
|
var log = new ChatLog();
|
|
log.OnCombatLine("You hit Mosswart for 5 slashing damage (50.0%).");
|
|
var e = log.Snapshot()[0];
|
|
Assert.Equal(ChatKind.Combat, e.Kind);
|
|
Assert.Equal(CombatLineKind.Info, e.CombatKind);
|
|
Assert.Equal("You hit Mosswart for 5 slashing damage (50.0%).", e.Text);
|
|
}
|
|
|
|
[Fact]
|
|
public void OnCombatLine_PreservesExplicitKind()
|
|
{
|
|
var log = new ChatLog();
|
|
log.OnCombatLine("Mosswart hit you for 8 fire damage to your chest.",
|
|
CombatLineKind.Warning);
|
|
Assert.Equal(CombatLineKind.Warning, log.Snapshot()[0].CombatKind);
|
|
|
|
log.OnCombatLine("Attack sequence finished with WeenieError 0x1234.",
|
|
CombatLineKind.Error);
|
|
Assert.Equal(CombatLineKind.Error, log.Snapshot()[1].CombatKind);
|
|
}
|
|
}
|