347 lines
13 KiB
C#
347 lines
13 KiB
C#
using UnityEngine;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using asap.core;
|
|
using cfg;
|
|
using GameCore;
|
|
using UnityEngine.Assertions;
|
|
using Castle.Core.Internal;
|
|
|
|
namespace GameCore
|
|
{
|
|
public partial class FishingEventData
|
|
{
|
|
public readonly ThanksGivingPackData ThanksGivingPackData = new ThanksGivingPackData();
|
|
}
|
|
|
|
}
|
|
public class ThanksGivingPackData : IProgressChainPackData
|
|
{
|
|
protected Data _data = new Data(0, 0, 0, 0);
|
|
/// <summary>
|
|
/// Starts with 0, representing the first unclaimed reward.
|
|
/// </summary>
|
|
public const int SlotCount = 6;
|
|
public const string Key = "ThanksGivingPackData";
|
|
protected virtual string key => Key;
|
|
private readonly Tables _tables = GContext.container.Resolve<Tables>();
|
|
private readonly TbFishingEvent _fishingEvent = GContext.container.Resolve<Tables>().TbFishingEvent;
|
|
private readonly TbEventPackManager _eventPackManager = GContext.container.Resolve<Tables>().TbEventPackManager;
|
|
private readonly TbPack _pack = GContext.container.Resolve<Tables>().TbPack;
|
|
private readonly PlayerItemData _playerItemData = GContext.container.Resolve<PlayerItemData>();
|
|
public const string ThanksGivingPanelName = "FestPack_ThanksGivingPanel";
|
|
public const string WinterPanelName = "FestPack_WinterSnowPanel";
|
|
public int PackId => _data == null ? 0 : _data.RedirectId;
|
|
public bool DoNeedPackRedPoint
|
|
{
|
|
get
|
|
{
|
|
if (ChainProgress < ChainListCount)
|
|
return _pack[_eventPackManager[_data.RedirectId].VIPPackList[0][ChainProgress]].IAPID == 0;
|
|
return false;
|
|
}
|
|
}
|
|
public TimeSpan RemainingTime =>
|
|
DateTime.Parse((_tables.TbFishingEvent[_data.EventId].TimeDefinition as LimitedTime)?.EndTime) -
|
|
ZZTimeHelper.UtcNow();
|
|
|
|
public bool IsWithinEventTime
|
|
{
|
|
get
|
|
{
|
|
var et = DateTime.Parse((_tables.TbFishingEvent[_data.EventId].TimeDefinition as LimitedTime).EndTime);
|
|
var st = DateTime.Parse((_tables.TbFishingEvent[_data.EventId].TimeDefinition as LimitedTime).StartTime);
|
|
return ZZTimeHelper.UtcNow() >= st && ZZTimeHelper.UtcNow() < et;
|
|
}
|
|
}
|
|
|
|
public bool IsActive => _data is not null
|
|
&& _fishingEvent.DataMap.ContainsKey(_data.EventId)
|
|
&& _eventPackManager.DataMap.ContainsKey(_data.RedirectId)
|
|
&& IsWithinEventTime
|
|
&& _data.ChainProgress < ChainListCount;
|
|
public int TokenProgress => _data.TokenProgress;
|
|
|
|
public int ChainProgress
|
|
{
|
|
get => _data.ChainProgress;
|
|
set => _data.ChainProgress = value;
|
|
}
|
|
public List<int> ChainList => _eventPackManager[_data.RedirectId].VIPPackList[0];
|
|
public int ChainListCount => _eventPackManager[_data.RedirectId].VIPPackList[0].Count;
|
|
public int EventId => _data.EventId;
|
|
|
|
private List<int> MileStoneList => GContext.container.Resolve<Tables>().TbEventPackManager[_data.RedirectId].MilestoneList;
|
|
// public string TokenIconUrl => _tables.TbItem[_tables.TbDrop[_pack[ChainList[0]].DropID].DropList.DropIDList[0]].Icon;
|
|
public string TokenIconUrl
|
|
{
|
|
get
|
|
{
|
|
int tokenId = _tables.TbDrop[_pack[ChainList[0]].DropID].DropList.DropIDList[0];
|
|
var emp = _eventPackManager[_data.RedirectId];
|
|
// if (emp != null && emp.MilestoneItem.IsNullOrEmpty() && emp.MilestoneItem.Count >= 2 && tokenId == _eventPackManager[_data.RedirectId].MilestoneItem[0])
|
|
if (emp?.MilestoneItem?.Count >= 2 && tokenId == _eventPackManager[_data.RedirectId].MilestoneItem[0])
|
|
tokenId = _eventPackManager[_data.RedirectId].MilestoneItem[1];
|
|
return _tables.TbItem[tokenId].Icon;
|
|
}
|
|
}
|
|
public bool IsEndGame => ChainProgress > ChainListCount - SlotCount;
|
|
public bool IsChainPackDepleted => TokenProgress >= MileStoneList.Sum();
|
|
// public string PanelUrl => _pack[_eventPackManager[_data.RedirectId].VIPPackList[0][0]].Panel;
|
|
// public string IconUrl => _pack[_eventPackManager[_data.RedirectId].VIPPackList[0][0]].Icon;
|
|
|
|
public string PanelUrl
|
|
{
|
|
get
|
|
{
|
|
var res = _eventPackManager[_data.RedirectId].Panel;
|
|
if (res == "")
|
|
res = _pack[_eventPackManager[_data.RedirectId].VIPPackList[0][0]].Panel;
|
|
return res;
|
|
}
|
|
}
|
|
|
|
|
|
public string IconUrl
|
|
{
|
|
get
|
|
{
|
|
var res = _eventPackManager[_data.RedirectId].Icon;
|
|
if (res == "")
|
|
res = _pack[_eventPackManager[_data.RedirectId].VIPPackList[0][0]].Icon;
|
|
return res;
|
|
}
|
|
}
|
|
|
|
public virtual string RedPointKey => "home.thanks_giving";
|
|
|
|
public void UploadData()
|
|
{
|
|
PlayFabMgr.Instance.UpdateUserDataValue(key, _data.Serialize());
|
|
}
|
|
public void LoadData(string s)
|
|
{
|
|
_data = Data.Deserialize(s);
|
|
// Debug.LogError("Debug In Action @ Thanks Giving Load Data.");
|
|
// _data.ChainProgress = 0;
|
|
// _data.TokenProgress = 0;
|
|
}
|
|
public void UpdateData(FishingEvent e)
|
|
{
|
|
// Debug.Log($"<color=#f18c0a>Updatedata {e.ID}, {e.RedirectID}, {TokenProgress}, {ChainProgress}</color>");
|
|
_data = new Data(eventId: e.ID, redirectId: _fishingEvent[e.ID].RedirectID, tokenProgress: 0, chainProgress: 0);
|
|
}
|
|
|
|
public int GetTokenProgressTargetByIdx(int idx)
|
|
{
|
|
if (idx < MileStoneList.Count) return MileStoneList.GetRange(0, idx + 1).Sum();
|
|
Debug.LogError($"Index {idx} is not within the scale of list length {MileStoneList.Count}");
|
|
return -1;
|
|
}
|
|
|
|
public void GetTokenProgressTargetByProgress(int progress, out int targetDisplay, out int scoreDisplay)
|
|
{
|
|
// var mileStoneList = _eventPackManager[_data.RedirectId].MilestoneList;
|
|
// Debug.Log($"<color=#f18c0a>VS: {progress}</color>");
|
|
int i = 0, accTarget = 0;
|
|
while (i < MileStoneList.Count)
|
|
{
|
|
accTarget += MileStoneList[i];
|
|
if (accTarget > progress)
|
|
{
|
|
targetDisplay = MileStoneList[i];
|
|
scoreDisplay = progress - MileStoneList.GetRange(0, i).Sum();
|
|
// Debug.Log($"<color=#f18c0a>Within: {scoreDisplay} / {targetDisplay}</color>");
|
|
return;
|
|
}
|
|
i++;
|
|
}
|
|
targetDisplay = MileStoneList[^1];
|
|
scoreDisplay = targetDisplay;
|
|
// Debug.Log($"<color=#f18c0a>Out: {scoreDisplay} / {targetDisplay}</color>");
|
|
}
|
|
|
|
public int GetChainProgressBySlotIdx(int slotIdx)
|
|
{
|
|
// Debug.Log($"<color=#f18c0a>Progress: {ChainProgress}</color>");
|
|
int res;
|
|
if (ChainProgress > ChainListCount - SlotCount)
|
|
res = ChainListCount - SlotCount + slotIdx;
|
|
else
|
|
res = ChainProgress + slotIdx;
|
|
Assert.IsTrue(res < ChainListCount, $"Progress {res} out of range: {ChainListCount}");
|
|
return res;
|
|
}
|
|
public Pack GetChainPackByChainProgress(int chainProgress)
|
|
{
|
|
return _pack[ChainList[chainProgress]];
|
|
}
|
|
|
|
public List<ItemData> GetItemsByPackDropId(int dropId)
|
|
{
|
|
var res = new List<ItemData>();
|
|
if (!_tables.TbDrop.DataMap.TryGetValue(dropId, out var drop))
|
|
{
|
|
Debug.Log($"Drop id {dropId} not found.");
|
|
return res;
|
|
}
|
|
if (drop.Type != DropType.Probability)
|
|
{
|
|
Debug.Log($"Unsupported drop type {drop.Type} from drop Id {dropId}.");
|
|
return res;
|
|
}
|
|
try
|
|
{
|
|
int n = drop.DropList.DropIDList.Count;
|
|
for (int i = 0; i < n; i++)
|
|
{
|
|
var itemId = drop.DropList.DropIDList[i];
|
|
var mileStoneItem = _eventPackManager[_data.RedirectId].MilestoneItem;
|
|
if (!mileStoneItem.IsNullOrEmpty() && mileStoneItem.Count >= 2 && itemId == mileStoneItem[0])
|
|
{
|
|
itemId = mileStoneItem[1];
|
|
}
|
|
res.Add(new ItemData(itemId, drop.DropList.DropCountList[i]));
|
|
_playerItemData.ItemTransition(res[i]);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"[EventBreak]Invalid drop id {drop}: {e.Message}\n{e.StackTrace}");
|
|
return res;
|
|
}
|
|
return res;
|
|
|
|
}
|
|
|
|
public int GetRewardDropByChainProgress(int chainProgress)
|
|
{
|
|
int i, sum = 0;
|
|
// var mileStoneList = _eventPackManager[_data.RedirectId].MilestoneList;
|
|
for (i = 0; i < MileStoneList.Count - 1; i++)
|
|
{
|
|
sum += MileStoneList[i];
|
|
if (chainProgress < sum)
|
|
break;
|
|
}
|
|
var mileStoneRewards = _eventPackManager[_data.RedirectId].MilestoneReward;
|
|
return mileStoneRewards[i];
|
|
}
|
|
|
|
public void OnBuySuccess()
|
|
{
|
|
if (ChainProgress >= ChainListCount)
|
|
{
|
|
Debug.LogError($"Trying to buy pack no.{ChainProgress} while there are only {ChainListCount} packs.");
|
|
return;
|
|
}
|
|
int tokenAdded = _tables.TbDrop[GetChainPackByChainProgress(_data.ChainProgress).DropID].DropList.DropCountList[0];
|
|
_data.ChainProgress++;
|
|
// Debug.Log($"<color=#f18c0a>AddProgress: {ChainProgress}</color>");
|
|
// And magically, token is added already by Leyuan. So...
|
|
var dropList = GetTokenProgressRewardAfterAddingToken(tokenAdded);
|
|
// Debug.Log($"<color=#f18c0a>[EventPartner]Paid. Progress reward in onBuySuccess.</color>");
|
|
// foreach (var r in dropList)
|
|
// Debug.Log($"<color=#f18c0a>{r}</color>");
|
|
_playerItemData.AddItemByDropList(dropList, false);
|
|
// GContext.Publish(new ShowData(_playerItemData.GetItemDataByDropId(dropList)));
|
|
// Debug.Log($"<color=#f18c0a>On Buy Success</color>");
|
|
// GContext.Publish(new ShowData());
|
|
}
|
|
|
|
public List<int> GetTokenProgressRewardAfterAddingToken(int tokenAdded)
|
|
{
|
|
// Debug.Log($"<color=#f18c0a>Add {tokenAdded}</color>");
|
|
int previousTokenProgress = _data.TokenProgress - tokenAdded, i, sum = 0, previousIdx = -1, currentIdx = -1;
|
|
for (i = 0; i < MileStoneList.Count; i++)
|
|
{
|
|
sum += MileStoneList[i];
|
|
if (previousIdx == -1 && previousTokenProgress < sum)
|
|
previousIdx = i;
|
|
if (currentIdx == -1 && _data.TokenProgress < sum)
|
|
currentIdx = i;
|
|
}
|
|
|
|
if (currentIdx == -1 && _data.TokenProgress >= sum)
|
|
currentIdx = MileStoneList.Count;
|
|
if (previousIdx == -1 && previousTokenProgress >= sum)
|
|
previousIdx = MileStoneList.Count;
|
|
// Debug.Log($"<color=#f18c0a>current:{currentIdx}, previous {previousIdx}</color>");
|
|
int count = Math.Max(0, currentIdx - previousIdx);
|
|
if (count > 0)
|
|
{
|
|
var res = _eventPackManager[_data.RedirectId].MilestoneReward.GetRange(previousIdx, count);
|
|
return res;
|
|
}
|
|
return new List<int>();
|
|
// Debug.Log($"<color=#f18c0a>Progress Reward Length: {res.Count}, or {count}</color>");
|
|
}
|
|
|
|
public void AddToken(int n)
|
|
{
|
|
_data.TokenProgress += n;
|
|
UploadData();
|
|
}
|
|
|
|
public int PickRandomIndex(IList<int> weightList)
|
|
{
|
|
// Assert.IsTrue(weightList.Count >= 1, )
|
|
return 0;
|
|
}
|
|
|
|
public void ChainProgressIncrease()
|
|
{
|
|
ChainProgress++;
|
|
}
|
|
|
|
public async void ShowChainPack()
|
|
{
|
|
if (IsChainPackDepleted)
|
|
{
|
|
//无限购买
|
|
}
|
|
else
|
|
{
|
|
UIType uIType = new UIType(PanelUrl);
|
|
GameObject go = await UIManager.Instance.ShowUI(uIType);
|
|
if (go != null)
|
|
{
|
|
go.GetComponent<ThanksGivingPopupPanel>().Init(this);
|
|
}
|
|
}
|
|
}
|
|
|
|
public class Data
|
|
{
|
|
public int TokenProgress, ChainProgress, EventId, RedirectId;
|
|
public static Data Deserialize(string s)
|
|
{
|
|
if (s == null || s == "")
|
|
{
|
|
return new Data(eventId: 0, redirectId: 0, tokenProgress: 0, chainProgress: 0);
|
|
}
|
|
string[] tokens = s.Split(",");
|
|
if (tokens.Length < 4)
|
|
{
|
|
Debug.LogError($"Wrong amount of parameters. Expect 4, but got {tokens.Length} in \"{s}\".");
|
|
return new Data(eventId: 0, redirectId: 0, tokenProgress: 0, chainProgress: 0);
|
|
}
|
|
return new Data(eventId: int.Parse(tokens[0]), redirectId: int.Parse(tokens[1]),
|
|
tokenProgress: int.Parse(tokens[2]), chainProgress: int.Parse(tokens[3]));
|
|
}
|
|
public string Serialize()
|
|
{
|
|
return $"{EventId},{RedirectId},{TokenProgress},{ChainProgress}";
|
|
}
|
|
|
|
public Data(int eventId, int redirectId, int tokenProgress, int chainProgress)
|
|
{
|
|
TokenProgress = tokenProgress;
|
|
ChainProgress = chainProgress;
|
|
EventId = eventId;
|
|
RedirectId = redirectId;
|
|
}
|
|
}
|
|
}
|