217 lines
8.2 KiB
C#
217 lines
8.2 KiB
C#
using System;
|
|
using asap.core;
|
|
using cfg;
|
|
using GameCore;
|
|
using Newtonsoft.Json;
|
|
using System.Collections.Generic;
|
|
using game;
|
|
using Newtonsoft.Json.Linq;
|
|
using OrderManager;
|
|
using UniRx;
|
|
using UnityEngine;
|
|
|
|
namespace Assets.Scripts.UI.OfferSweep
|
|
{
|
|
public class OfferSweepManager : IDisposable,IOrderReplenishment
|
|
{
|
|
public OfferSweepManager()
|
|
{
|
|
GContext.OnEvent<TargetEvent>().Subscribe(OnEvent_TargetEvent).AddTo(Disposables);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Disposables?.Dispose();
|
|
}
|
|
|
|
private CompositeDisposable _disposable;
|
|
private CompositeDisposable Disposables => _disposable ??= new CompositeDisposable();
|
|
#region event
|
|
public void OnEvent_TargetEvent(TargetEvent targetEvent)
|
|
{
|
|
if (targetEvent.type == 4)
|
|
{
|
|
RefreshData();
|
|
}
|
|
}
|
|
#endregion
|
|
#region data
|
|
[Inject]
|
|
public Tables Tables { get; set; }
|
|
private OfferSweepPackData _sweepPackData;
|
|
public OfferSweepPackData SweepPackData => _sweepPackData ??= new OfferSweepPackData();
|
|
|
|
private OfferSweepPackData GetCachedData()
|
|
{
|
|
var dataStr = PlayFabMgr.Instance.GetLocalData(nameof(OfferSweepPackData));
|
|
#if UNITY_EDITOR
|
|
Debug.Log($"jsd OfferSweepPackData LoadData sData {dataStr}");
|
|
#endif
|
|
return dataStr != null ? JsonConvert.DeserializeObject<OfferSweepPackData>(dataStr) : new OfferSweepPackData();
|
|
}
|
|
|
|
public void RefreshData()
|
|
{
|
|
var isResetData = IsResetData(SweepPackData, out int eventId, out int sweepPackId);
|
|
if (isResetData)
|
|
{
|
|
SweepPackData.FishingEventID = eventId;
|
|
SweepPackData.SweepPackId = sweepPackId;
|
|
SweepPackData.StartTimeVipLevel = GContext.container.Resolve<PlayerData>().PriceLv;
|
|
SweepPackData.PurchasePackDir?.Clear();
|
|
SweepPackData.PurchasedCount = 0;
|
|
SaveData();
|
|
}
|
|
else
|
|
{
|
|
var cachedData = GetCachedData();
|
|
SweepPackData.SetData(cachedData);
|
|
}
|
|
|
|
|
|
if (!SweepPackData.IsActive) return;
|
|
var packManager = GetOfferSweepEventPackManager();
|
|
UITypes.OfferSweepPanel.SetType(packManager.Panel);
|
|
GContext.container.Resolve<IFaceUIService>().AddGiftFaceUI(eventId, UITypes.OfferSweepPanel, 0);
|
|
}
|
|
private bool IsResetData(OfferSweepPackData offerSweepPackData, out int eventId, out int sweepPackId)
|
|
{
|
|
eventId = sweepPackId = 0;
|
|
|
|
if (offerSweepPackData == null) return true;
|
|
|
|
var @event = GetCurrentLoopEvent();
|
|
if (@event == null) return true;
|
|
eventId = @event.ID;
|
|
|
|
sweepPackId = GetOfferSweepPackId(@event);
|
|
if (sweepPackId <= 0) return true;
|
|
|
|
var cacheData = GetCachedData();
|
|
if (cacheData == null) return false;
|
|
if (cacheData.FishingEventID != @event.ID) return true;
|
|
if (cacheData.SweepPackId == 0) cacheData.SweepPackId = sweepPackId;
|
|
|
|
return false;
|
|
}
|
|
public void SaveData()
|
|
{
|
|
#if UNITY_EDITOR
|
|
Debug.Log($"jsd OfferSweepPackData SaveData {JsonConvert.SerializeObject(SweepPackData)}");
|
|
#endif
|
|
PlayFabMgr.Instance.UpdateUserDataValue(nameof(OfferSweepPackData), JsonConvert.SerializeObject(SweepPackData));
|
|
}
|
|
|
|
public FishingEvent GetCurrentLoopEvent()
|
|
{
|
|
//4 循环活动
|
|
var events = Tables.TbFishingEvent.GetAllDataByType(4);
|
|
var fishingEventData = GContext.container.Resolve<FishingEventData>();
|
|
foreach (var e in events)
|
|
{
|
|
if (!EventStrategyFactory.IsRegisterEventBySubType(e.SubType))
|
|
continue;
|
|
var isOpen = fishingEventData.Condition(e);
|
|
if (isOpen) return e;
|
|
}
|
|
return null;
|
|
}
|
|
public EventPackManager GetOfferSweepEventPackManager(FishingEvent fishingEvent = null)
|
|
{
|
|
return Tables.TbEventPackManager.GetOrDefault(SweepPackData.SweepPackId);
|
|
}
|
|
public bool GetData(int index, out EventPackManager packManager, out Pack packConfig, out List<ItemData> itemDatas, out IAPItemList iapItem, out int packId)
|
|
{
|
|
packManager = null;
|
|
packConfig = null;
|
|
itemDatas = null;
|
|
iapItem = null;
|
|
packId = 0;
|
|
|
|
packManager = GetOfferSweepEventPackManager();
|
|
if (packManager == null) return false;
|
|
var VipLevel = this.SweepPackData.StartTimeVipLevel;
|
|
if (packManager.VIPPackList == null || packManager.VIPPackList[VipLevel].Count != 7)
|
|
{
|
|
Debug.LogError($"配置错误 eventPackManager ID {packManager.ID} VIP {VipLevel} list count {packManager.VIPPackList[VipLevel]?.Count}");
|
|
return false;
|
|
}
|
|
|
|
packId = packManager.VIPPackList?[VipLevel]?[index] ?? 0;
|
|
|
|
packConfig = Tables.TbPack?.GetOrDefault(packId);
|
|
if (packConfig == null)
|
|
{
|
|
Debug.LogError($"配置错误 eventPackManager id {packId}");
|
|
return false;
|
|
}
|
|
|
|
itemDatas = GContext.container.Resolve<PlayerItemData>().GetItemDataByDropIdLureInflation(null, packConfig.DropID);
|
|
if (itemDatas == null) return false;
|
|
|
|
iapItem = Tables.TbIAPItemList?.GetOrDefault(packConfig.IAPID);
|
|
if (iapItem == null) return false;
|
|
return true;
|
|
}
|
|
|
|
public List<ItemData> GetPurchasedItemDataByIndex(int index)
|
|
{
|
|
if (SweepPackData == null) return null;
|
|
if (index < 0) return null;
|
|
if (!SweepPackData.PurchasePackDir.ContainsKey(index)) return null;
|
|
var packId = SweepPackData.PurchasePackDir[index];
|
|
var packConfig = Tables.TbPack?.GetOrDefault(packId);
|
|
if (packConfig == null)
|
|
{
|
|
Debug.LogError($"配置错误 eventPackManager id {packId}");
|
|
return null;
|
|
}
|
|
return GContext.container.Resolve<PlayerItemData>().GetItemDataByDropId(packConfig.DropID);
|
|
}
|
|
#endregion
|
|
#region static method
|
|
public static int GetOfferSweepPackId(FishingEvent fishingEvent = null)
|
|
{
|
|
if (fishingEvent == null)
|
|
fishingEvent = GContext.container.Resolve<OfferSweepManager>().GetCurrentLoopEvent();
|
|
|
|
if (fishingEvent == null) return 0;
|
|
|
|
var strategy = EventStrategyFactory.GetStrategy(fishingEvent.SubType);
|
|
var tables = GContext.container.Resolve<Tables>();
|
|
var cycleConfig = tables.TbFishingEventCycleItem.GetOrDefault(fishingEvent.RedirectID);
|
|
return strategy.GetSweepPackId(cycleConfig.RedirectID);
|
|
}
|
|
#endregion
|
|
|
|
public void OnReplenish(ShopBuyTypeData buyType, IAPItemList iAPItemList, List<ItemData> itemDataGift, RewardType rewardShowType,
|
|
bool voucher = false)
|
|
{
|
|
if(buyType.extraPurcheData==null) return;
|
|
if (!buyType.extraPurcheData.TryGetValue(nameof(OfferSweepPackData), out var value)) return;
|
|
if (!buyType.extraPurcheData.TryGetValue(nameof(_sweepPackData.FishingEventID), out var fishingEventIdjToken))
|
|
{
|
|
Debug.LogError($"参数错误 eventPackManager id {_sweepPackData.FishingEventID}");
|
|
return;
|
|
}
|
|
var eventID= fishingEventIdjToken.ToObject<int>();
|
|
if (eventID != _sweepPackData.FishingEventID)
|
|
{
|
|
//todo 保底机制
|
|
return;
|
|
}
|
|
|
|
var packIDs = value.ToObject<Dictionary<int, int>>();
|
|
if(packIDs==null) return;
|
|
foreach (var packID in packIDs)
|
|
{
|
|
_sweepPackData.PurchasePackDir.TryAdd(packID.Key, packID.Value);
|
|
}
|
|
if (_sweepPackData.PurchasePackDir.Count == 6)
|
|
{
|
|
_sweepPackData.PurchasedCount++;
|
|
}
|
|
SaveData();
|
|
}
|
|
}
|
|
} |