From 0e6b28cd0e0c0ecdb3b56042756e938566aaf7c1 Mon Sep 17 00:00:00 2001 From: tudor <7089284+tudddorrr@users.noreply.github.com> Date: Tue, 19 Aug 2025 23:21:24 +0100 Subject: [PATCH] cache the identified alias, use cached alias when offline --- .../Talo/Runtime/APIs/PlayersAPI.cs | 64 +++++++++++++++---- .../Talo/Runtime/APIs/SavesAPI.cs | 2 +- .../Talo Game Services/Talo/Runtime/Talo.cs | 15 ++--- .../SavesDemo/Scripts/MenuUIController.cs | 3 +- 4 files changed, 61 insertions(+), 23 deletions(-) diff --git a/Assets/Talo Game Services/Talo/Runtime/APIs/PlayersAPI.cs b/Assets/Talo Game Services/Talo/Runtime/APIs/PlayersAPI.cs index b0f38ff..0d52799 100644 --- a/Assets/Talo Game Services/Talo/Runtime/APIs/PlayersAPI.cs +++ b/Assets/Talo Game Services/Talo/Runtime/APIs/PlayersAPI.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using UnityEngine; +using System.IO; namespace TaloGameServices { @@ -10,6 +11,8 @@ public class PlayersAPI : BaseAPI public event Action OnIdentificationStarted; public event Action OnIdentificationFailed; + private readonly string _offlineDataPath = Application.persistentDataPath + "/ta.bin"; + public PlayersAPI() : base("v1/players") { } public void InvokeIdentifiedEvent() @@ -17,9 +20,43 @@ public void InvokeIdentifiedEvent() OnIdentified?.Invoke(Talo.CurrentPlayer); } + private async Task HandleIdentifySuccess(PlayerAlias alias, string socketToken = "") + { + if (!Talo.IsOffline()) + { + await Talo.Socket.ResetConnection(); + } + + Talo.CurrentAlias = alias; + if (!string.IsNullOrEmpty(socketToken)) + { + Talo.Socket.SetSocketToken(socketToken); + } + + WriteOfflineAlias(); + InvokeIdentifiedEvent(); + + return alias.player; + } + public async Task Identify(string service, string identifier) { OnIdentificationStarted?.Invoke(); + + if (Talo.IsOffline()) + { + var offlineAlias = GetOfflineAlias(); + if (offlineAlias != null) + { + return await HandleIdentifySuccess(offlineAlias); + } + else + { + OnIdentificationFailed?.Invoke(); + throw new Exception("No offline player alias found."); + } + } + var uri = new Uri($"{baseUrl}/identify?service={service}&identifier={identifier}"); try @@ -27,21 +64,11 @@ public async Task Identify(string service, string identifier) var json = await Call(uri, "GET"); var res = JsonUtility.FromJson(json); - await Talo.Socket.ResetConnection(); - - Talo.CurrentAlias = res.alias; - Talo.Socket.SetSocketToken(res.socketToken); - InvokeIdentifiedEvent(); - - return Talo.CurrentPlayer; + return await HandleIdentifySuccess(res.alias, res.socketToken); } catch { - if (!Talo.IsOffline()) - { - await Talo.PlayerAuth.SessionManager.ClearSession(); - } - + await Talo.PlayerAuth.SessionManager.ClearSession(); OnIdentificationFailed?.Invoke(); throw; } @@ -69,6 +96,7 @@ public async Task Update() var res = JsonUtility.FromJson(json); Talo.CurrentPlayer = res.player; + WriteOfflineAlias(); return Talo.CurrentPlayer; } @@ -91,5 +119,17 @@ public async Task Find(string playerId) var res = JsonUtility.FromJson(json); return res.player; } + + private void WriteOfflineAlias() + { + var content = JsonUtility.ToJson(Talo.CurrentAlias); + Talo.Crypto.WriteFileContent(_offlineDataPath, content); + } + + private PlayerAlias GetOfflineAlias() + { + if (!File.Exists(_offlineDataPath)) return null; + return JsonUtility.FromJson(Talo.Crypto.ReadFileContent(_offlineDataPath)); + } } } diff --git a/Assets/Talo Game Services/Talo/Runtime/APIs/SavesAPI.cs b/Assets/Talo Game Services/Talo/Runtime/APIs/SavesAPI.cs index cd5e1f1..3be278e 100644 --- a/Assets/Talo Game Services/Talo/Runtime/APIs/SavesAPI.cs +++ b/Assets/Talo Game Services/Talo/Runtime/APIs/SavesAPI.cs @@ -209,7 +209,7 @@ public void UnloadCurrentSave() public async Task DeleteSave(int saveId) { - var save = savesManager.FindSaveByID(saveId); + var _ = savesManager.FindSaveByID(saveId); if (!Talo.IsOffline()) { diff --git a/Assets/Talo Game Services/Talo/Runtime/Talo.cs b/Assets/Talo Game Services/Talo/Runtime/Talo.cs index e47cd61..67280f8 100644 --- a/Assets/Talo Game Services/Talo/Runtime/Talo.cs +++ b/Assets/Talo Game Services/Talo/Runtime/Talo.cs @@ -35,11 +35,7 @@ public static PlayerAlias CurrentAlias public static Player CurrentPlayer { - get - { - IdentityCheck(); - return _currentAlias.player; - } + get => _currentAlias?.player ?? null; set => _currentAlias.player = value; } @@ -58,7 +54,8 @@ public static Player CurrentPlayer public static LiveConfig LiveConfig { - get { + get + { if (_liveConfig == null) { throw new Exception("Live config needs to be initialised first - use Talo.GameConfig.Get() to fetch it."); @@ -196,12 +193,12 @@ public static void IdentityCheck() public static bool IsOffline() { if (TestMode) return RequestMock.Offline; - return Application.internetReachability == NetworkReachability.NotReachable; + return Application.internetReachability == NetworkReachability.NotReachable || Settings.offlineMode; } internal static bool CheckTestMode() { - #if UNITY_EDITOR || DEBUG +#if UNITY_EDITOR || DEBUG var assembly = AppDomain.CurrentDomain.GetAssemblies() .FirstOrDefault((assembly) => assembly.FullName.ToLowerInvariant().StartsWith("nunit.framework")); @@ -217,7 +214,7 @@ internal static bool CheckTestMode() return false; } } - #endif +#endif _testMode = false; return _testMode; } diff --git a/Assets/Talo Game Services/Talo/Samples/SavesDemo/Scripts/MenuUIController.cs b/Assets/Talo Game Services/Talo/Samples/SavesDemo/Scripts/MenuUIController.cs index 8c14221..ada16f5 100644 --- a/Assets/Talo Game Services/Talo/Samples/SavesDemo/Scripts/MenuUIController.cs +++ b/Assets/Talo Game Services/Talo/Samples/SavesDemo/Scripts/MenuUIController.cs @@ -53,7 +53,8 @@ private async void OnNewGameClick() Talo.Saves.UnloadCurrentSave(); var date = DateTime.Now.ToString("ddd dd MMM HH:mm:ss"); - await Talo.Saves.CreateSave($"Save created {date}"); + var save = await Talo.Saves.CreateSave($"Save created {date}"); + Talo.Saves.ChooseSave(save.id); } private void OnLoadClick()