using System; using asap.core; using GameCore.Events; using UniRx; using cfg; namespace Activity.EventSubscriber { /// /// 物品添加事件订阅者 /// 监听玩家获得物品事件,通过配置表反向查找活动并添加代币 /// public class ItemAddedEventSubscriber : IActivityEventSubscriber { private ActivityManager _manager; private Tables _tables; private IDisposable _subscription; public void Subscribe(ActivityManager manager) { _manager = manager; _tables = GContext.container.Resolve(); // 初始化配置表索引 _tables.TbFishingEventCycleItem.BuildItemIdIndex(); _subscription = GContext.OnEvent() .Subscribe(OnItemAdded); } private void OnItemAdded(ItemAddedEvent evt) { // 通过配置表查找物品对应的活动周期配置 var cycleItem = _tables.TbFishingEventCycleItem.FindByItemId(evt.ItemId); if (cycleItem == null) { // 不是活动代币物品,跳过 return; } // 获取活动哨兵 var sentry = _manager.GetSentry((cycleItem.Type, cycleItem.SubType)); if (sentry?.CurrentEventConfig == null) { // 活动未开启,跳过 return; } // 添加代币到活动代币服务 var tokenService = GContext.container.Resolve(); tokenService.AddToken(sentry.CurrentEventConfig.ID, evt.Count); #if UNITY_EDITOR UnityEngine.Debug.Log($"[ItemAddedEventSubscriber] Added {evt.Count} tokens to event {sentry.CurrentEventConfig.ID} for item {evt.ItemId}"); #endif } public void Unsubscribe() { _subscription?.Dispose(); _subscription = null; } } }