using System; using System.Text; using GameCore; using UnityEngine; using asap.core; using System.Collections.Generic; public class EventOfferJourneyLvlTriggerData : AEventOfferJourneyData { private int _progress, _ticketCount, _roundCount; private EventOfferJourneyLvlTriggerTableContext _ctx = new EventOfferJourneyLvlTriggerTableContext(); public override int EventId => _ctx.TriggerPackId; public override int Progress => _progress; public override int TicketCount => _ticketCount; public override float InflationRate => 0; public override ShopBuyType BuyType => ShopBuyType.TriggerPack; public bool IsActive => _ctx.IsEventActive && _roundCount < _ctx.MaxRoundCount; // private int VipLvlOnTrigger => _ctx.VipLvlOnTrigger; public void Init() { _progress = 0; _ticketCount = 0; _roundCount = 0; } public override bool AddTicket(int n) { _ticketCount += n; if (_ticketCount < 0) { _ticketCount -= n; return false; } SaveData(); // GContext.container.Resolve().SaveTransitionData(_eventId, _ticketCount); GContext.Publish(new EventTicketUpdate()); return true; } public override void OnBuySuccess() { if (_progress >= _ctx.GetRewardList().Count) { Debug.LogError($"[EventOfferJourney]Trying to buy pack no.{_progress} but there are only {_ctx.GetRewardList().Count} rewards."); return; } _progress++; SaveData(); } public override void ProgressUp() { _progress++; if (_progress >= _ctx.GetRewardList().Count) { _progress = 0; _roundCount++; } SaveData(); } public override void SaveData() { ToPlayfabData().Save(); } public override bool LoadDataFromPlayfab() { var pfData = EventOfferJourneyLvlTriggerPlayfabData.Load(); if (pfData == null) return false; _progress = pfData.Progress; _ticketCount = pfData.TicketCount; _roundCount = pfData.RoundCount; return true; } private EventOfferJourneyLvlTriggerPlayfabData ToPlayfabData() { return new EventOfferJourneyLvlTriggerPlayfabData(_progress, _ticketCount, _roundCount); } public override EventOfferJourneyEntranceData ToEntranceData() { if (!IsActive) { return new EventOfferJourneyEntranceData(false, TimeSpan.Zero, "", false); } TimeSpan remainingTime = _ctx.GetEventStartTimeAndEndTime().Item2 - ZZTimeHelper.UtcNow(); // Debug.Log($"[EventOfferJourney] Remaining time: {remainingTime.TotalSeconds}s."); var isActive = IsActive && remainingTime.TotalSeconds > 0; // Debug.Log($"[EventOfferJourney] First Is active: {IsActive}."); // Debug.Log($"[EventOfferJourney] Total Is active: {isActive}."); var iconUrl = _ctx.EntranceIconUrl; // Debug.Log($"[EventOfferJourney] Icon URL: {iconUrl}."); var cost = _ctx.GetTicketCostByProgress(_progress); // Debug.Log($"[EventOfferJourney] Cost: {cost}."); var doNeedRedPoint = cost > 0 && cost <= _ticketCount; // Debug.Log($"[EventOfferJourney] Do need red point: {doNeedRedPoint}."); var res = new EventOfferJourneyEntranceData(isActive, remainingTime, iconUrl, doNeedRedPoint); return res; } public override List GetRewardList() { return _ctx.GetRewardList(); } public override ITimerContext ToTimerData(Action OnExpire) { var res = new TimerContext { StartTime = _ctx.GetEventStartTimeAndEndTime().Item1, ExpiryTime = _ctx.GetEventStartTimeAndEndTime().Item2, OnExpire = OnExpire }; return res; } public override EventOfferJourneyUiData GetUiData() { return _ctx.GetUiData(); } public override int[] GetTicketRewardArray() { return _ctx.GetTicketRewardArray(); } public override string GetTicketName() { return _ctx.GetTicketName(); } public override void OnRaid(int multiplier = 1) { int rewardCount = _ctx.GetTicketRewardArray()[0] * multiplier; GContext.Publish(new game.TargetAddData(_ctx.GetTicketId(), TicketCount, rewardCount)); AddTicket(rewardCount); } public override void OnBomb(int multiplier = 1) { int rewardCount = _ctx.GetTicketRewardArray()[1] * multiplier; GContext.Publish(new game.TargetAddData(_ctx.GetTicketId(), TicketCount, rewardCount)); AddTicket(rewardCount); } public override int GetStartPosParam() { return _ctx.GetStartPosParam(); } } public class EventOfferJourneyLvlTriggerPlayfabData { public int Progress, TicketCount, RoundCount; private const string Key = "EventOfferJourneyLvlTriggerData"; private const int TokenCount = 3; private const char Splitter = '|', Comma = ','; public EventOfferJourneyLvlTriggerPlayfabData(int progress, int ticketCount, int roundCount) { Progress = progress; TicketCount = ticketCount; RoundCount = roundCount; } private string Serialize() { var res = new StringBuilder(); res.Append(Progress).Append(Splitter) .Append(TicketCount).Append(Splitter) .Append(RoundCount).Append(Splitter); return res.ToString(); } private static EventOfferJourneyLvlTriggerPlayfabData Deserialize(string s) { if (s == null || s == "") return null; var tokens = s.Trim(Splitter).Split(Splitter); if (tokens.Length != TokenCount) { Debug.LogError($"[EventOfferJourney]Wrong amount of parameters. Expect {TokenCount}, but got {tokens.Length} in \"{s}\"."); return null; } var progress = int.Parse(tokens[0]); var ticketCount = int.Parse(tokens[1]); var roundCount = int.Parse(tokens[2]); return new EventOfferJourneyLvlTriggerPlayfabData(progress, ticketCount, roundCount); } public static EventOfferJourneyLvlTriggerPlayfabData Load() { var s = PlayFabMgr.Instance.GetLocalData(Key); return Deserialize(s); } public void Save() { var s = Serialize(); PlayFabMgr.Instance.UpdateUserDataValue(Key, s); } }