Implements the item-state mirror + appraise round-trip infrastructure on top of Phase F.1's GameEvent dispatcher. Core layer (AcDream.Core/Items): - ItemRepository: ConcurrentDictionary-backed live item state keyed by server ObjectId. Events: ItemAdded, ItemMoved, ItemRemoved, ItemPropertiesUpdated. MoveItem handles container / slot / equip location updates atomically and fires ItemMoved with old+new container ids. UpdateProperties merges a PropertyBundle patch (for appraise results) without clobbering existing untouched keys. Wire layer (AcDream.Core.Net/Messages): - AppraiseRequest (0x00C8 C→S, inside 0xF7B1 GameAction envelope): Build(sequence, targetGuid) → 16-byte body ready for SendGameAction. - GameEvents.ParseIdentifyResponseHeader for 0x00C9 S→C — extracts (guid, appraiseFlags, success). Full PropertyBundle deserialization (the 10-flag bitfield-indexed tables) is a future pass; header alone is enough to route into the repository + surface "appraise complete" to UI. - GameEvents.ParseWieldObject (0x0023) — server-driven equip. - GameEvents.ParsePutObjInContainer (0x0022) — server-driven inventory move (item, container, placement). Tests (11 new): - ItemRepository: add/update fires correct event, move updates fields, missing-id returns false, remove, properties merge, clear. - Wire: AppraiseRequest byte-exact encoding, IdentifyResponse header round-trip, WieldObject round-trip, PutObjInContainer round-trip. Build green, 532 tests pass (up from 521). Phase F.2 unblocks the Paperdoll + Inventory UI panels and the "appraise on right-click" UX. Next pieces: PropertyBundle full deserializer (AppraiseInfo 10-flag bitfield), outbound move/drop/ pickup actions. Ref: r06 §1 (ItemType), §2 (EquipMask), §5 (appraise wire), §7 (pack depth rules). Ref: ACE GameEventIdentifyObjectResponse.cs for AppraiseInfo format. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
119 lines
3.1 KiB
C#
119 lines
3.1 KiB
C#
using AcDream.Core.Items;
|
|
using Xunit;
|
|
|
|
namespace AcDream.Core.Tests.Items;
|
|
|
|
public sealed class ItemRepositoryTests
|
|
{
|
|
private static ItemInstance MakeItem(uint id, string name = "Widget") =>
|
|
new ItemInstance
|
|
{
|
|
ObjectId = id,
|
|
WeenieClassId = 1,
|
|
Name = name,
|
|
Type = ItemType.Misc,
|
|
StackSize = 1,
|
|
Burden = 10,
|
|
Value = 5,
|
|
};
|
|
|
|
[Fact]
|
|
public void AddOrUpdate_FiresAddedEvent()
|
|
{
|
|
var repo = new ItemRepository();
|
|
ItemInstance? added = null;
|
|
repo.ItemAdded += i => added = i;
|
|
|
|
var item = MakeItem(100);
|
|
repo.AddOrUpdate(item);
|
|
|
|
Assert.Same(item, added);
|
|
Assert.Equal(1, repo.ItemCount);
|
|
Assert.Same(item, repo.GetItem(100));
|
|
}
|
|
|
|
[Fact]
|
|
public void AddOrUpdate_ExistingItem_FiresPropertiesUpdated()
|
|
{
|
|
var repo = new ItemRepository();
|
|
var item = MakeItem(100);
|
|
repo.AddOrUpdate(item);
|
|
|
|
int propUpdateCount = 0;
|
|
repo.ItemPropertiesUpdated += _ => propUpdateCount++;
|
|
|
|
repo.AddOrUpdate(item); // second call is an update
|
|
Assert.Equal(1, propUpdateCount);
|
|
}
|
|
|
|
[Fact]
|
|
public void MoveItem_UpdatesContainerAndFiresEvent()
|
|
{
|
|
var repo = new ItemRepository();
|
|
var item = MakeItem(100);
|
|
repo.AddOrUpdate(item);
|
|
|
|
uint seenOld = 999, seenNew = 999;
|
|
repo.ItemMoved += (it, oldC, newC) => { seenOld = oldC; seenNew = newC; };
|
|
|
|
repo.MoveItem(100, 42, newSlot: 3);
|
|
|
|
Assert.Equal(0u, seenOld); // was not in any container initially
|
|
Assert.Equal(42u, seenNew);
|
|
Assert.Equal(42u, item.ContainerId);
|
|
Assert.Equal(3, item.ContainerSlot);
|
|
}
|
|
|
|
[Fact]
|
|
public void MoveItem_Nonexistent_ReturnsFalse()
|
|
{
|
|
var repo = new ItemRepository();
|
|
Assert.False(repo.MoveItem(999, 42));
|
|
}
|
|
|
|
[Fact]
|
|
public void Remove_FiresEventAndRemoves()
|
|
{
|
|
var repo = new ItemRepository();
|
|
var item = MakeItem(100);
|
|
repo.AddOrUpdate(item);
|
|
|
|
ItemInstance? removed = null;
|
|
repo.ItemRemoved += i => removed = i;
|
|
|
|
Assert.True(repo.Remove(100));
|
|
Assert.Same(item, removed);
|
|
Assert.Null(repo.GetItem(100));
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateProperties_MergesIncomingBundle()
|
|
{
|
|
var repo = new ItemRepository();
|
|
var item = MakeItem(100);
|
|
item.Properties.Ints[1] = 10;
|
|
repo.AddOrUpdate(item);
|
|
|
|
var patch = new PropertyBundle();
|
|
patch.Ints[2] = 20; // new
|
|
patch.Ints[1] = 15; // overrides
|
|
patch.Strings[100] = "desc";
|
|
repo.UpdateProperties(100, patch);
|
|
|
|
Assert.Equal(15, item.Properties.Ints[1]);
|
|
Assert.Equal(20, item.Properties.Ints[2]);
|
|
Assert.Equal("desc", item.Properties.Strings[100]);
|
|
}
|
|
|
|
[Fact]
|
|
public void Clear_RemovesAllItems()
|
|
{
|
|
var repo = new ItemRepository();
|
|
repo.AddOrUpdate(MakeItem(1));
|
|
repo.AddOrUpdate(MakeItem(2));
|
|
repo.AddOrUpdate(MakeItem(3));
|
|
|
|
repo.Clear();
|
|
Assert.Equal(0, repo.ItemCount);
|
|
}
|
|
}
|