From 447750ce7d3f41c332d9a57805a531bd069c870a Mon Sep 17 00:00:00 2001 From: Jari Petays Date: Sun, 17 Nov 2024 12:28:21 +0200 Subject: [PATCH] try to improve ServerManager SetProfileValues using ' JToken Type Conversions' best practices while keeping original semantics. Except PlayerPrefs(playerId) which is empty string (not null) if not found in JSON. JToken can be converted to its containing value using either explicit cast or explicit value conversion. IMO explicit cast is better. Even better is this was using JToken.ToObject so JSON conversion is done implicitly on one place. --- Assets/Altzone/Scripts/ServerManager.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Assets/Altzone/Scripts/ServerManager.cs b/Assets/Altzone/Scripts/ServerManager.cs index 725150e76..c5f390809 100644 --- a/Assets/Altzone/Scripts/ServerManager.cs +++ b/Assets/Altzone/Scripts/ServerManager.cs @@ -11,6 +11,7 @@ using Altzone.Scripts.Model.Poco.Clan; using Altzone.Scripts.GA; using Altzone.Scripts.Model.Poco.Game; +using UnityEngine.Assertions; /// /// ServerManager acts as an interface between the server and the game. @@ -199,18 +200,26 @@ public void LogOut() } /// - /// Sets values related to "Profile" received from server + /// Sets values related to player "Profile" received from server /// /// JSON object containing Profile info from server /// - /// Profile and Player are not the same as Profile might hold personal information! + /// Profile and Player (ServerPlayer) are not the same as Profile might hold personal information!
/// Player contains exclusively data related to in game Player. ///
public void SetProfileValues(JObject profileJSON) { - AccessToken = profileJSON["accessToken"].ToString(); - AccessTokenExpiration = int.Parse(profileJSON["tokenExpires"].ToString()); - PlayerPrefs.SetString("playerId", profileJSON["Player"]["_id"].ToString()); + var accessToken = profileJSON["accessToken"]; + Assert.IsNotNull(accessToken); + AccessToken = (string)accessToken; + + var tokenExpires = profileJSON["tokenExpires"]; + Assert.IsNotNull(tokenExpires); + AccessTokenExpiration = tokenExpires.Value(); + + var player = profileJSON["Player"]; + Assert.IsNotNull(player); + PlayerPrefs.SetString("playerId", (string)player["_id"] ?? string.Empty); //StartCoroutine(LogIn()); }