using System.Collections.Generic; using asap.core; using game; using Newtonsoft.Json; using EventPartner; public class EventPartnerStatuePlayerPreferenceCache { public InvitationCache RecommendCache { get; set; } public Dictionary RobotDataCache { get; set; } public Queue RobotInvitationCache { get; set; } public EventPartnerStatueBuff Buff { get; set; } public bool IsFirstTime { get; set; } = true; [JsonIgnore] private int _eventId; [JsonIgnore] private string Key => $"EventPartnerPlayerPreferenceCacheOf{GContext.container.Resolve().UserId}@{_eventId}"; public EventPartnerStatuePlayerPreferenceCache(int eventId) { _eventId = eventId; RecommendCache = new InvitationCache(); RobotInvitationCache = new Queue(); Buff = new EventPartnerStatueBuff(); RobotDataCache = new Dictionary(); } public static EventPartnerStatuePlayerPreferenceCache Load(int eventId) { var res = new EventPartnerStatuePlayerPreferenceCache(eventId); try { res.Deserialize(UnityEngine.PlayerPrefs.GetString(res.Key)); } catch (System.Exception e) { UnityEngine.Debug.LogError(e); } return res; } private string Serialize() { return JsonConvert.SerializeObject(this); } private void Deserialize(string s) { if (string.IsNullOrEmpty(s)) return; var data = JsonConvert.DeserializeObject(s); if (data.RecommendCache != null) RecommendCache = data.RecommendCache; if (data.RobotDataCache != null) RobotDataCache = data.RobotDataCache; if (data.RobotInvitationCache != null) RobotInvitationCache = data.RobotInvitationCache; if (data.Buff != null) Buff = data.Buff; IsFirstTime = data.IsFirstTime; } public void Save() { UnityEngine.PlayerPrefs.SetString(Key, Serialize()); } }