acdream/src/AcDream.Core/Items/ItemManaState.cs
Erik dcb61efb5a refactor(runtime): expose canonical gameplay state
Move character options and movement skills into the Runtime-owned character graph, expose borrowed inventory, character, and social views, and route retained UI state commands through generation-gated typed Runtime contracts. Preserve the existing synchronous wire path while deleting the App-owned option and skill mirrors and extending normalized parity checkpoints.

Co-authored-by: Codex <codex@openai.com>
2026-07-26 09:12:30 +02:00

46 lines
1.6 KiB
C#

using System;
using System.Collections.Concurrent;
namespace AcDream.Core.Items;
/// <summary>
/// Last server-reported mana fraction for queried inventory items.
/// Retail routes <c>QueryItemManaResponse (0x0264)</c> through
/// <c>ClientUISystem::Handle_Item__QueryItemManaResponse @ 0x00563FE0</c>, including
/// the response's validity flag, before notifying the selected-object toolbar.
/// </summary>
public sealed class ItemManaState
{
private readonly ConcurrentDictionary<uint, float> _manaByGuid = new();
private long _revision;
/// <summary>Fires for every valid or invalid query response.</summary>
public event Action<uint /*guid*/, float /*fraction*/, bool /*valid*/>? ItemManaChanged;
public float GetManaPercent(uint guid) =>
_manaByGuid.TryGetValue(guid, out float percent) ? percent : 0f;
public bool HasMana(uint guid) => _manaByGuid.ContainsKey(guid);
public int Count => _manaByGuid.Count;
public long Revision => Interlocked.Read(ref _revision);
public bool TryGetManaPercent(uint guid, out float fraction) =>
_manaByGuid.TryGetValue(guid, out fraction);
public void OnQueryItemManaResponse(uint itemGuid, float manaPercent, bool valid)
{
if (valid)
_manaByGuid[itemGuid] = manaPercent;
else
_manaByGuid.TryRemove(itemGuid, out _);
Interlocked.Increment(ref _revision);
ItemManaChanged?.Invoke(itemGuid, manaPercent, valid);
}
public void Clear()
{
_manaByGuid.Clear();
Interlocked.Increment(ref _revision);
}
}