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);
///
/// Starts with 0, representing the first unclaimed reward.
///
public const int SlotCount = 6;
public const string Key = "ThanksGivingPackData";
protected virtual string key => Key;
private readonly Tables _tables = GContext.container.Resolve();
private readonly TbFishingEvent _fishingEvent = GContext.container.Resolve().TbFishingEvent;
private readonly TbEventPackManager _eventPackManager = GContext.container.Resolve().TbEventPackManager;
private readonly TbPack _pack = GContext.container.Resolve().TbPack;
private readonly PlayerItemData _playerItemData = GContext.container.Resolve();
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 ChainList => _eventPackManager[_data.RedirectId].VIPPackList[0];
public int ChainListCount => _eventPackManager[_data.RedirectId].VIPPackList[0].Count;
public int EventId => _data.EventId;
private List MileStoneList => GContext.container.Resolve().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($"Updatedata {e.ID}, {e.RedirectID}, {TokenProgress}, {ChainProgress}");
_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($"VS: {progress}");
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($"Within: {scoreDisplay} / {targetDisplay}");
return;
}
i++;
}
targetDisplay = MileStoneList[^1];
scoreDisplay = targetDisplay;
// Debug.Log($"Out: {scoreDisplay} / {targetDisplay}");
}
public int GetChainProgressBySlotIdx(int slotIdx)
{
// Debug.Log($"Progress: {ChainProgress}");
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 GetItemsByPackDropId(int dropId)
{
var res = new List();
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($"AddProgress: {ChainProgress}");
// And magically, token is added already by Leyuan. So...
var dropList = GetTokenProgressRewardAfterAddingToken(tokenAdded);
// Debug.Log($"[EventPartner]Paid. Progress reward in onBuySuccess.");
// foreach (var r in dropList)
// Debug.Log($"{r}");
_playerItemData.AddItemByDropList(dropList, false);
// GContext.Publish(new ShowData(_playerItemData.GetItemDataByDropId(dropList)));
// Debug.Log($"On Buy Success");
// GContext.Publish(new ShowData());
}
public List GetTokenProgressRewardAfterAddingToken(int tokenAdded)
{
// Debug.Log($"Add {tokenAdded}");
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($"current:{currentIdx}, previous {previousIdx}");
int count = Math.Max(0, currentIdx - previousIdx);
if (count > 0)
{
var res = _eventPackManager[_data.RedirectId].MilestoneReward.GetRange(previousIdx, count);
return res;
}
return new List();
// Debug.Log($"Progress Reward Length: {res.Count}, or {count}");
}
public void AddToken(int n)
{
_data.TokenProgress += n;
UploadData();
}
public int PickRandomIndex(IList 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().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;
}
}
}