using System.Collections.Generic; using UnityEngine; using GameCore; using asap.core; using System.Linq; public class EventGatherCoreData : IHasChainPack { /// /// How many stages has been played. /// private int _ticketCount, _stageCount, _eventId, _chainProgress; public int StageCount => _stageCount; public int TicketCount => _ticketCount; public int EventId => _eventId; public EventGatherCoreStageData StageData; public EventGatherCoreData() { } public static EventGatherCoreData CreateNew(cfg.FishingEvent e) { var res = new EventGatherCoreData(); res._stageCount = 0; res.StageData = EventGatherCoreStageData.CreateNew(); res._eventId = e.ID; return res; } public void InitNew(int eventId) { _stageCount = 0; _ticketCount = EventGatherCoreAct.Ctx.GetWelcomeGift(); _eventId = eventId; StageData = EventGatherCoreStageData.CreateNew(); StageData.MoveToStage(0); _chainProgress = 0; } public void LoadData(EventGatherCorePlayfabData data) { _eventId = data.EventId; _ticketCount = data.TicketCount; _stageCount = data.StageCount; StageData = EventGatherCoreStageData.CreateNew(); StageData.Init(data.StageCount, data.RewardClaimStateFlag, data.TriggerItemStateFlag); _chainProgress = data.ChainProgress; } public void MoveToNextStage() { _stageCount++; StageData.MoveToStage(StageCount); ToPlayfabData().Save(); } public void AddTicket(int n) { _ticketCount += n; if (_ticketCount < 0) { _ticketCount -= n; } ToPlayfabData().Save(); GContext.container.Resolve().SaveTransitionData(_eventId, _ticketCount); GContext.Publish(new EventTicketUpdate()); } public EventGatherCorePlayfabData ToPlayfabData() { var res = new EventGatherCorePlayfabData { EventId = _eventId, TicketCount = _ticketCount, StageCount = _stageCount, RewardClaimStateFlag = StageData.RewardClaimStateFlag, TriggerItemStateFlag = StageData.TriggerItemStateFlag, ChainProgress = _chainProgress }; return res; } public EventBingoEntranceData ToEntranceData() { var res = new EventBingoEntranceData(); var ctx = EventGatherCoreAct.Ctx; var t = ctx.GetEventStartTimeAndEndTime(); res.StartTime = t.Item1; res.ExpiryTime = t.Item2; res.Icon = ctx.GetEntranceIcon(); res.NeedRedPoint = GContext.container.Resolve().TicketCount >= ctx.GetRedPointThreshold(); return res; } #region ChainPack public void SetChainProgress(int p) { _chainProgress = p; } public int GetChainProgress() { return _chainProgress; } public void Save() { ToPlayfabData().Save(); } public GenericChainPackData ToChainPackData() { return GenericChainPackData.Create(this, EventGatherCoreAct.Ctx.GetChainPackInfo()); } #endregion public TimerContext ToTimerContext(System.Action onExpire) { var ctx = EventGatherCoreAct.Ctx; var t = ctx.GetEventStartTimeAndEndTime(); var res = new TimerContext { ExpiryTime = t.Item2, StartTime = t.Item1, OnExpire = onExpire }; return res; } } public class EventGatherCoreStageData { public List RewardIdList = new List(); public long RewardClaimStateFlag, TriggerItemStateFlag; public int StageId; // public int CoreItemReceivedCount => RewardIdList.Count((id, idx) => EventGatherCoreTableContext.CoreItemIndicator == id && IsRewardClaimed(idx)); public int CoreItemReceivedCount { get { int result = 0; if (RewardIdList == null) return result; result = RewardIdList.Where((id, idx) => EventGatherCoreAct.Ctx.IsCoreItem(id) && IsRewardClaimed(idx)).Count(); // Debug.Log($"[EventGatherCore] CoreItemReceivedCount: {result}"); return result; } } private EventGatherCoreStageData() { } public static EventGatherCoreStageData CreateNew() { var res = new EventGatherCoreStageData(); res.MoveToStage(0); return res; } public int PlayCount { get { int res = 0; long flag = RewardClaimStateFlag; while (flag > 0) { if ((flag & 1) != 0) { res++; } flag >>= 1; } return res; } } public void Init(int roundCount, long rewardClaimStateFlag = 0L, long triggerItemStateFlag = 0L) { // Debug.Log($"[EventGatherCore]Move to stage {roundCount}"); RewardClaimStateFlag = rewardClaimStateFlag; TriggerItemStateFlag = triggerItemStateFlag; StageId = EventGatherCoreAct.Ctx.GetStageId(roundCount); RewardIdList = EventGatherCoreAct.Ctx.GetRewardIdList(StageId); } public void MoveToStage(int roundCount) { // Debug.Log($"[EventGatherCore]Move to stage {roundCount}"); var ctx = EventGatherCoreAct.Ctx; StageId = ctx.GetStageId(roundCount); RewardIdList = ctx.GetStageInfo(StageId); RewardClaimStateFlag = 0L; TriggerItemStateFlag = 0L; } public bool IsRewardClaimed(int rewardIdx) { return (RewardClaimStateFlag & (1L << rewardIdx)) != 0; } public bool IsTriggerUsed(int triggerIdx) { return (TriggerItemStateFlag & (1L << triggerIdx)) != 0; } public bool IsEarlyTry(EventGatherCoreTableContext ctx) { return PlayCount < ctx.GetStageMinAttemptCount(StageId) - 1; } public bool IsCloseToFinish(EventGatherCoreTableContext ctx) { return CoreItemReceivedCount == ctx.GetCoreItemTargetCount(StageId) - 1; } public void SetRewardClaimed(int rewardIdx) { RewardClaimStateFlag |= 1L << rewardIdx; } public void SetTriggerUsed(int triggerIdx) { TriggerItemStateFlag |= 1L << triggerIdx; } // public void AddCoreItem(int num = 1) // { // CoreItemReceivedCount += num; // } public bool IsStageCleared(EventGatherCoreTableContext ctx) { long fullFlag = (1L << ctx.GetStageInfo(StageId).Count) - 1; return CoreItemReceivedCount >= ctx.GetCoreItemTargetCount(StageId) || (RewardClaimStateFlag & fullFlag) == fullFlag; } public List GetRemainingRewardIds() { return RewardIdList.Where((id, idx) => ((1 << idx) & RewardClaimStateFlag) == 0).ToList(); } } public class EventGatherCoreTriggerTaken { public bool Succeeded; public int RewardIdx; public int RewardId; public Vector2 Position; } public class EventGatherCoreRewardInfo { public int Idx; public ItemData ItemData; public bool IsSpecial = false; public bool IsClaimed; } public class EventGatherCorePlayfabData { public int EventId { get; set; } public int TicketCount { get; set; } public int StageCount { get; set; } public long RewardClaimStateFlag { get; set; } public long TriggerItemStateFlag { get; set; } public int ChainProgress { get; set; } public static readonly string Key = "EventGatherCoreData"; private readonly char Splitter = '|'; private readonly int TokenCount = 6; private string Serialize() { var sb = new System.Text.StringBuilder(); sb.Append(EventId).Append(Splitter); sb.Append(TicketCount).Append(Splitter); sb.Append(StageCount).Append(Splitter); sb.Append(RewardClaimStateFlag).Append(Splitter); sb.Append(TriggerItemStateFlag).Append(Splitter); sb.Append(ChainProgress); return sb.ToString(); } private void Deserialize(string data) { var tokens = data.Trim(Splitter).Split(Splitter); if (tokens.Length != TokenCount) throw new System.Exception($"[EventGatherCore]Wrong amount of parameters. Expect {TokenCount}, but got {tokens.Length} in \"{data}\"."); EventId = int.Parse(tokens[0]); TicketCount = int.Parse(tokens[1]); StageCount = int.Parse(tokens[2]); RewardClaimStateFlag = long.Parse(tokens[3]); TriggerItemStateFlag = long.Parse(tokens[4]); ChainProgress = int.Parse(tokens[5]); } public void Save() { PlayFabMgr.Instance.UpdateUserDataValue(Key, Serialize()); } public static EventGatherCorePlayfabData Load() { var s = PlayFabMgr.Instance.GetLocalData(Key); var res = new EventGatherCorePlayfabData(); try { res.Deserialize(s); } catch (System.Exception e) { Debug.LogWarning($"[EventGatherCore]Error when deserializing EventGatherCoreData: {e}"); return null; } return res; } } public class EventGatherCoreUiData { public string MainPanelUrl { get; set; } public string InfoPanelUrl { get; set; } public string ChainPackPanelUrl { get; set; } public string NormalPackPanelUrl { get; set; } public string RewardPopupPanelUrl { get; set; } } public class EventGatherCoreRewardPopupPanelClose { public ItemData Reward{ get; set; } }