using UnityEngine; using asap.core; using System.Text; using cfg; using System.Linq; using GameCore; using game; namespace Assets.Scripts.UI.SocialRush { public class SocialRushArchitecture { private Model _model = new Model(); private TableContext _tableContext; public int EventId => _tableContext.EventId; public Model Model => _model; public TableContext TableContext => _tableContext; #region RedPoint private bool _isFirstTime = true; public bool IsFirstTime => _isFirstTime; private string _redPointKey = "social_rush"; public string RedPointKey => _redPointKey; #endregion #region DataStorage public static void Load(FishingEvent e) { var arc = GContext.container.Resolve(); if (arc == null) { GContext.container.Register().AsSingleton(); arc = GContext.container.Resolve(); } var res = arc.LoadPlayfab(); arc.LoadPlayerPreferences(); if (!res || arc.EventId != e.ID) arc.Init(e.ID); var urls = arc.TableContext.GetAddressableUrls(); UITypes.SocialRushPanel.SetType(urls[1]); } public void InitTest(int eventId) { _tableContext = TableContext.CreateTest(eventId); _model.Reset(); _model.Set(0, 0, GContext.container.Resolve().InflationRate); } public void Init(int eventId) { _tableContext = new TableContext(eventId); _model.Reset(); _isFirstTime = true; SavePlayfab(); SavePlayerPreferences(); } private const char Pipe = '|'; private const string PfKey = "FeastRush"; private string PpKey => "SocialRush" + GContext.container.Resolve().UserId; public void SavePlayfab() { var sb = new StringBuilder(); sb.Append(_tableContext.EventId).Append(Pipe); sb.Append(_model.ProgressDisplay).Append(Pipe); sb.Append(_model.ProgressActual).Append(Pipe); sb.Append(_model.InflationRate).Append(Pipe); PlayFabMgr.Instance.UpdateUserDataValue(PfKey, sb.ToString()); } public bool LoadPlayfab() { try { var dataString = PlayFabMgr.Instance.GetLocalData(PfKey); var tokens = dataString.Trim(Pipe).Split(Pipe); var eventId = int.Parse(tokens[0]); var progressDisplay = int.Parse(tokens[1]); var progressActual = int.Parse(tokens[2]); var inflationRate = float.Parse(tokens[3]); _tableContext = new TableContext(eventId); _model.Set(progressActual, progressDisplay, inflationRate); return true; } catch (System.Exception e) { Debug.LogWarning(e); return false; } } public void LoadPlayerPreferences() { _isFirstTime = PlayerPrefs.GetInt(PpKey, 1) == 1; } public void SavePlayerPreferences() { PlayerPrefs.SetInt(PpKey, _isFirstTime ? 1 : 0); } public void SetFirstTime() { _isFirstTime = false; SavePlayerPreferences(); } #endregion #region System public bool IsActive() { return _tableContext.IsActive() && _model.ProgressDisplay < _tableContext.GetTaskList().Sum(x => x.TaskParam) && _model.ProgressDisplay >= 0; } public bool NeedUpdate() { return _model.ProgressDisplay < _model.ProgressActual; } public bool NeedPopup() { var res = TableContext.HasUnclaimedReward(_model.ProgressDisplay, _model.ProgressActual, out var displayTaskIdx, out var actualTaskIdx); return res; } public void AddProgress(int n) { if (!IsActive()) return; _model.AddProgress(n); SavePlayfab(); } #endregion #region leyuan public void AddFishRaidProgress(int multiplier = 1) { var count = _tableContext.GetFishRaidProgress() * multiplier; GContext.Publish(new game.TargetAddData(TableContext.GetProgressItemId(), Model.ProgressActual, count)); AddProgress(count); } public void AddBombProgress(int multiplier = 1) { var count = _tableContext.GetBombProgress() * multiplier; GContext.Publish(new game.TargetAddData(TableContext.GetProgressItemId(), Model.ProgressActual, count)); AddProgress(_tableContext.GetBombProgress() * multiplier); } #endregion } }