348 lines
11 KiB
C#
348 lines
11 KiB
C#
using cfg;
|
||
using cfg.ChargeCfg;
|
||
using Cysharp.Threading.Tasks;
|
||
using Framework;
|
||
using Gameplay;
|
||
using Gameplay.Guide;
|
||
using Gameplay.Net;
|
||
using Gameplay.PopUp;
|
||
using NLDPB;
|
||
using PhxhSDK;
|
||
using System;
|
||
using static Gameplay.CommonUtils;
|
||
|
||
public class MonthlyPassManager : Singlenton<MonthlyPassManager>,IInitable
|
||
{
|
||
public class MonthlyPassService
|
||
{
|
||
public bool IsPassUnlock { get; private set; }
|
||
|
||
public DateTime Endtime { get; private set; }
|
||
|
||
public bool TodayHasClaimed { get; private set; }
|
||
|
||
public delegate void RefreshDelegate();
|
||
public event RefreshDelegate OnRefresh;
|
||
|
||
public MonthlyPassService()
|
||
{
|
||
IsPassUnlock = false;
|
||
Endtime = DateTime.MinValue;
|
||
TodayHasClaimed = false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 数据同步
|
||
/// </summary>
|
||
public void Refresh()
|
||
{
|
||
IsPassUnlock = Actor.Instance.Logic.GetCount((uint)LogicID.MonthlyPassId) > 0;
|
||
//if (IsPassUnlock)
|
||
//{
|
||
// Instance.UpdatePassService((int)Actor.Instance.Logic.GetCount((uint)LogicID.MonthlyPassId));
|
||
//}
|
||
var EndTime = Actor.Instance.Logic.GetCount((uint)LogicID.MonthlyPassValidity);
|
||
if (EndTime > 0)
|
||
{
|
||
Endtime = CommonUtils.GetTimeByTimestamp(EndTime);
|
||
}
|
||
else
|
||
{
|
||
Endtime = DateTime.MinValue;
|
||
}
|
||
TodayHasClaimed = Actor.Instance.Logic.GetCount((uint)LogicID.MonthlyPassRewardStatus) == 1;
|
||
var RemainingDays = Endtime - CommonUtils.GetCurTime(E_TimeType.Server);
|
||
DebugUtil.Log("当前月卡状态: IsPassUnlock = " + IsPassUnlock + "\n RemainingDays = " + RemainingDays.TotalDays + "\n TodayHasClaimed = " + TodayHasClaimed);
|
||
|
||
OnRefresh?.Invoke();
|
||
}
|
||
}
|
||
|
||
private MonthlyPassService PassService;
|
||
|
||
private MonthlyPassTip_PopUp popUp;
|
||
|
||
private int ShopId = 91000;
|
||
public bool needPrompt { get; private set;}
|
||
|
||
|
||
public void AddMonthlyPassRefreshListener(MonthlyPassService.RefreshDelegate listener)
|
||
{
|
||
PassService.OnRefresh += listener;
|
||
}
|
||
public void RemoveMonthlyPassRefreshListener(MonthlyPassService.RefreshDelegate listener)
|
||
{
|
||
PassService.OnRefresh -= listener;
|
||
}
|
||
//private DataMonthlyPass CurrentPass = null;
|
||
|
||
public void Init()
|
||
{
|
||
PassService = new MonthlyPassService();
|
||
needPrompt = false;
|
||
|
||
EventManager.Instance.Register<uint>(EventManager.EventName.PartLogicUpdate, LogicListener);
|
||
EventManager.Instance.Register(EventManager.EventName.MonthlyPassBuySuccess, PassService.Refresh);
|
||
EventManager.Instance.Register(EventManager.EventName.MonthlyPassGetReward, PassService.Refresh);
|
||
}
|
||
|
||
///// <summary>
|
||
///// 根据服务器信息更新当月月卡内容
|
||
///// </summary>
|
||
///// <param name="PassID"></param>
|
||
//public void UpdatePassService(int PassID)
|
||
//{
|
||
// //var table = TableManager.Instance.Tables.MonthlyPass;
|
||
// //if (table.DataMap.TryGetValue(PassID, out DataMonthlyPass data))
|
||
// //{
|
||
// // CurrentPass = data;
|
||
// //}
|
||
// //else
|
||
// //{
|
||
// // CurrentPass = null;
|
||
// // DebugUtil.LogError("MonthlyPassManager UpdatePassService Error: {0} not found", PassID);
|
||
// //}
|
||
//}
|
||
public void Release()
|
||
{
|
||
EventManager.Instance.Unregister<uint>(EventManager.EventName.PartLogicUpdate, LogicListener);
|
||
EventManager.Instance.Unregister(EventManager.EventName.MonthlyPassBuySuccess, PassService.Refresh);
|
||
EventManager.Instance.Unregister(EventManager.EventName.MonthlyPassGetReward, PassService.Refresh);
|
||
}
|
||
|
||
void LogicListener(uint Logic)
|
||
{
|
||
if (Logic == (uint)LogicID.MonthlyPassId || Logic == (uint)LogicID.MonthlyPassRewardStatus || Logic == (uint)LogicID.MonthlyPassValidity)
|
||
{
|
||
PassService.Refresh();
|
||
if (Logic == (uint)LogicID.MonthlyPassId)
|
||
{
|
||
var tag = StorageMgr.Instance.GetStorage<MonthlyPassSaveData>("MonthlyPassUnlock");
|
||
if (PassService.IsPassUnlock)
|
||
{
|
||
if (!tag.IsPassUnlock)
|
||
{
|
||
tag.IsPassUnlock = true;
|
||
StorageMgr.Instance.SyncForce = true;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (tag.IsPassUnlock)
|
||
{
|
||
//tag.IsPassUnlock = false;
|
||
//StorageMgr.Instance.SyncForce = true;
|
||
ShowPromptPopup();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
class MonthlyPassSaveData
|
||
{
|
||
public bool IsPassUnlock;
|
||
}
|
||
|
||
void ShowPromptPopup()
|
||
{
|
||
needPrompt = true;
|
||
if (null != popUp)
|
||
PopUpManager.Instance.RemovePopUp(popUp);
|
||
|
||
popUp = new MonthlyPassTip_PopUp();
|
||
PopUpManager.Instance.AddPopUp(popUp);
|
||
DebugUtil.Log("MonthlyPass is over");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 月卡到期弹窗
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public async UniTask<bool> CheckShowPopup()
|
||
{
|
||
if (GuideManager.Instance.IsGuiding) // 引导中不弹月卡提示
|
||
return false;
|
||
|
||
if (needPrompt)
|
||
{
|
||
DebugUtil.LogP("--- 展示月卡过期界面 --- ");
|
||
needPrompt = false;
|
||
|
||
var tag = StorageMgr.Instance.GetStorage<MonthlyPassSaveData>("MonthlyPassUnlock");
|
||
tag.IsPassUnlock = false;
|
||
StorageMgr.Instance.SyncForce = true;
|
||
DebugUtil.Log("这里需要更改提示语本地化");
|
||
await UI_TipController.Open("月卡已到期,是否跳转至月卡商店界面?", () => JumpToManager.JumpFunc<int, Action>(JumpFuncType.Item_Shop, ShopId, null));
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
//DataMonthlyPass GetCurPass()
|
||
//{
|
||
// if (CurrentPass != null)
|
||
// return CurrentPass;
|
||
//
|
||
// var time = CommonUtils.GetCurTime(E_TimeType.Server);
|
||
// var year = time.Year;
|
||
// var month = time.Month;
|
||
// string date = year + "-" + month;
|
||
// Debug.Log("MonthlyPass init, Today: " + date);
|
||
//
|
||
// var table = TableManager.Instance.Tables.MonthlyPass;
|
||
// foreach (var data in table.DataList)
|
||
// {
|
||
// var startTime = CommonUtils.GetTimeByTimestamp((uint)data.StarTime);
|
||
// var endTime = CommonUtils.GetTimeByTimestamp((uint)data.EndTime);
|
||
// DebugUtil.Log("MonthlyPass init, startTime: " + startTime + " start stamp: " + data.StarTime + " endTime: " + endTime + " time: " + time);
|
||
// if (startTime <= time && endTime >= time)
|
||
// {
|
||
// CurrentPass = data;
|
||
// break;
|
||
// }
|
||
// }
|
||
// return CurrentPass;
|
||
//}
|
||
|
||
/// <summary>
|
||
/// 可领取状态
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public bool TodayCanClaim()
|
||
{
|
||
var remains = GetRemainsTime();
|
||
return !PassService.TodayHasClaimed && PassService.IsPassUnlock && remains.TotalSeconds > 0;
|
||
}
|
||
|
||
#region 交互
|
||
|
||
public bool IsPassUnlock()
|
||
{
|
||
return PassService.IsPassUnlock;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 购买月卡请求
|
||
/// </summary>
|
||
public void Purchase()
|
||
{
|
||
if (PassService.IsPassUnlock)
|
||
{
|
||
DebugUtil.Log("MonthlyPassManager Purchase Error: Pass is already unlocked");
|
||
return;
|
||
}
|
||
var currencyData = TableManager.Instance.Tables.CurrencyShopCfg.DataMap;
|
||
var currencyID = 10;
|
||
//GetCurPass();
|
||
//if (CurrentPass == null)
|
||
//{
|
||
// DebugUtil.LogError("月卡购买错误,无法获取当期月卡,使用默认内购ID");
|
||
//}
|
||
//else
|
||
// currencyID = CurrentPass.PassProductID;
|
||
|
||
if (!currencyData.TryGetValue(currencyID, out var dataCurrencyShopCfg))
|
||
{
|
||
DebugUtil.LogError("CurrencyShopCfg 没有 {0} 配置的内购商品", currencyID);
|
||
return;
|
||
}
|
||
|
||
OnPurchase(dataCurrencyShopCfg);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 领取月卡奖励服务器请求
|
||
/// </summary>
|
||
public void Claim()
|
||
{
|
||
if (TodayCanClaim())
|
||
{
|
||
var id = Actor.Instance.Logic.GetCount((uint)LogicID.MonthlyPassId);
|
||
NetHelper.MonthlyPassGetReward(id);
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.Log("Cannot claim today");
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 界面
|
||
|
||
public TimeSpan GetRemainsTime()
|
||
{
|
||
return PassService.Endtime - CommonUtils.GetCurTime(E_TimeType.Server);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取当前月卡图片路径
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public string GetPassPicPath()
|
||
{
|
||
var defaultPath = "";//TODO:后续补充默认图片路径
|
||
//GetCurPass();
|
||
//if (CurrentPass != null)
|
||
//{
|
||
// return CurrentPass.ProductPath;
|
||
//}
|
||
//else
|
||
//{
|
||
// DebugUtil.LogError("MonthlyPassManager GetPassPicPath Error: CurrentPass is null");
|
||
//}
|
||
return defaultPath;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取月卡描述
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public string GetPassDesc()
|
||
{
|
||
var defaultDesc = "";//TODO:后续补充默认描述
|
||
//GetCurPass();
|
||
//if (CurrentPass != null)
|
||
//{
|
||
// var key = CurrentPass.ProductDescription;
|
||
// return CommonUtils.GetLocalizeText(key);
|
||
//}
|
||
//else
|
||
//{
|
||
// DebugUtil.LogError("MonthlyPassManager GetPassDesc Error: CurrentPass is null");
|
||
//}
|
||
return GetLocalizeText(defaultDesc);
|
||
}
|
||
#endregion
|
||
|
||
|
||
private async void OnPurchase(DataCurrencyShopCfg cfg)
|
||
{
|
||
#if UNITY_EDITOR
|
||
NetHelper.InAppPurchaseTest((uint)cfg.Id);
|
||
return;
|
||
#endif
|
||
try
|
||
{
|
||
#if UNITY_IOS
|
||
// ios 只走苹果内购
|
||
IAPManager.Instance.SetCurrentPayChannel(IAPManager.PayChannel.Apple);
|
||
IAPManager.Instance.CheckAndBuyGoods(cfg.Id);
|
||
#elif SDK_BILIBILI
|
||
IAPManager.Instance.SetCurrentPayChannel(IAPManager.PayChannel.Bilibili);
|
||
IAPManager.Instance.CheckAndBuyGoods(cfg.Id);
|
||
#else
|
||
await UI_PayWindowController.Open(cfg);
|
||
#endif
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError("MonthlyPass OnPurchase Error: {0}", e);
|
||
|
||
CommonUtils.ShowMessageTips("Purchase failed, please try again later.");//TODO:后续补充本地化
|
||
}
|
||
}
|
||
}
|