Files
ft/Client/Assets/Scripts/UI/PartnerStatue/EventPartnerPlayerPreferenceCache.cs
2026-06-29 21:18:33 +08:00

68 lines
2.1 KiB
C#

using System.Collections.Generic;
using asap.core;
using game;
using Newtonsoft.Json;
using EventPartner;
public class EventPartnerStatuePlayerPreferenceCache
{
public InvitationCache RecommendCache { get; set; }
public Dictionary<string, RobotPlayerPreferenceData> RobotDataCache { get; set; }
public Queue<RobotInvitation> 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<IUserService>().UserId}@{_eventId}";
public EventPartnerStatuePlayerPreferenceCache(int eventId)
{
_eventId = eventId;
RecommendCache = new InvitationCache();
RobotInvitationCache = new Queue<RobotInvitation>();
Buff = new EventPartnerStatueBuff();
RobotDataCache = new Dictionary<string, RobotPlayerPreferenceData>();
}
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<EventPartnerStatuePlayerPreferenceCache>(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());
}
}