/// <summary> /// Reads the Json response and applies it to local properties. /// </summary> /// <param name="result"></param> private AccountServiceResponse ParseResult(string result) { try { AccountServiceResponse res = JsonUtility.FromJson <AccountServiceResponse>(result); // Unity's JsonUtility does not support deserializing Dictionary, we manually parse it, dirty & ugly af, better then using a 3rd party lib if (res.ReturnCode == AccountServiceReturnCodes.Success) { string[] parts = result.Split(new[] { "\"ApplicationIds\":{" }, StringSplitOptions.RemoveEmptyEntries); parts = parts[1].Split('}'); string applicationIds = parts[0]; if (!string.IsNullOrEmpty(applicationIds)) { parts = applicationIds.Split(new[] { ',', '"', ':' }, StringSplitOptions.RemoveEmptyEntries); res.ApplicationIds = new Dictionary <string, string>(parts.Length / 2); for (int i = 0; i < parts.Length; i = i + 2) { res.ApplicationIds.Add(parts[i], parts[i + 1]); } } else { Debug.LogError("The server did not return any AppId, ApplicationIds was empty in the response."); return(null); } } return(res); } catch (Exception ex) // probably JSON parsing exception, check if returned string is valid JSON { Debug.LogException(ex); return(null); } }
public bool RegisterByEmail(AccountServiceRequest request, Action <AccountServiceResponse> callback = null, Action <string> errorCallback = null) { if (request == null) { Debug.LogError("Registration request is null"); return(false); } string fullUrl = GetUrlWithQueryStringEscaped(request); RequestHeaders["x-functions-key"] = string.IsNullOrEmpty(CustomToken) ? DefaultToken : CustomToken; //Debug.LogWarningFormat("Full URL {0}", fullUrl); PhotonEditorUtils.StartCoroutine( PhotonEditorUtils.HttpPost(fullUrl, RequestHeaders, null, s => { //Debug.LogWarningFormat("received response {0}", s); if (string.IsNullOrEmpty(s)) { if (errorCallback != null) { errorCallback( "Server's response was empty. Please register through account website during this service interruption."); } } else { AccountServiceResponse ase = this.ParseResult(s); if (ase == null) { if (errorCallback != null) { errorCallback( "Error parsing registration response. Please try registering from account website"); } } else if (callback != null) { callback(ase); } } }, e => { if (errorCallback != null) { errorCallback(e); } }) ); return(true); }
private void RegisterWithEmailSuccessCallback(AccountServiceResponse res) { EditorUtility.ClearProgressBar(); this.emailSentToAccountIsRegistered = true; // email is either registered now, or was already if (res.ReturnCode == AccountServiceReturnCodes.Success) { string key = ((int)ServiceTypes.Pun).ToString(); string appId; if (res.ApplicationIds.TryGetValue(key, out appId)) { this.mailOrAppId = appId; PhotonNetwork.PhotonServerSettings.UseCloud(this.mailOrAppId, null); key = ((int)ServiceTypes.Chat).ToString(); if (res.ApplicationIds.TryGetValue(key, out appId)) { PhotonNetwork.PhotonServerSettings.AppSettings.AppIdChat = appId; } else if (PhotonEditorUtils.HasChat) { Debug.LogWarning("Registration successful but no Chat AppId returned"); } key = ((int)ServiceTypes.Voice).ToString(); if (res.ApplicationIds.TryGetValue(key, out appId)) { PhotonNetwork.PhotonServerSettings.AppSettings.AppIdVoice = appId; } else if (PhotonEditorUtils.HasVoice) { Debug.LogWarning("Registration successful but no Voice AppId returned"); } PhotonEditor.SaveSettings(); this.photonSetupState = PhotonSetupStates.GoEditPhotonServerSettings; } else { DisplayErrorMessage("Registration successful but no PUN AppId returned"); } } else { PhotonEditor.SaveSettings(); if (res.ReturnCode == AccountServiceReturnCodes.EmailAlreadyRegistered) { this.photonSetupState = PhotonSetupStates.EmailAlreadyRegistered; } else { DisplayErrorMessage(res.Message); } } }
/// <summary> /// Reads the Json response and applies it to local properties. /// </summary> /// <param name="result"></param> private void ParseResult(string result) { if (string.IsNullOrEmpty(result)) { this.Message = "Server's response was empty. Please register through account website during this service interruption."; return; } try { AccountServiceResponse res = UnityEngine.JsonUtility.FromJson <AccountServiceResponse>(result); this.ReturnCode = res.ReturnCode; this.Message = res.Message; if (this.ReturnCode == 0) { // returnCode == 0 means: all ok. message is new AppId this.AppId = this.Message; if (PhotonEditorUtils.HasVoice) { this.AppId2 = res.MessageDetailed; } } else { // any error gives returnCode != 0 this.AppId = string.Empty; if (PhotonEditorUtils.HasVoice) { this.AppId2 = string.Empty; } } } catch (Exception ex) // probably JSON parsing exception, check if returned string is valid JSON { this.ReturnCode = -1; this.Message = ex.Message; } }
/// <summary> /// Attempts to create a Photon Cloud Account asynchronously. Blocked while RequestPendingResult is true. /// </summary> /// <remarks> /// Once your callback is called, check ReturnCode, Message and AppId to get the result of this attempt. /// </remarks> /// <param name="email">Email of the account.</param> /// <param name="serviceTypes">Defines which type of Photon-service is being requested.</param> /// <param name="callback">Called when the result is available.</param> /// <param name="errorCallback">Called when the request failed.</param> public bool RegisterByEmail(string email, List <ServiceTypes> serviceTypes, Action <AccountServiceResponse> callback = null, Action <string> errorCallback = null) { if (this.RequestPendingResult) { Debug.LogError("Registration request pending result. Not sending another."); return(false); } if (!IsValidEmail(email)) { Debug.LogErrorFormat("Email \"{0}\" is not valid", email); return(false); } string serviceTypeString = GetServiceTypesFromList(serviceTypes); if (string.IsNullOrEmpty(serviceTypeString)) { Debug.LogError("serviceTypes string is null or empty"); return(false); } string fullUrl = GetUrlWithQueryStringEscaped(email, serviceTypeString); RequestHeaders["x-functions-key"] = string.IsNullOrEmpty(CustomToken) ? DefaultToken : CustomToken; this.RequestPendingResult = true; PhotonEditorUtils.StartCoroutine( PhotonEditorUtils.HttpPost(fullUrl, RequestHeaders, null, s => { this.RequestPendingResult = false; //Debug.LogWarningFormat("received response {0}", s); if (string.IsNullOrEmpty(s)) { if (errorCallback != null) { errorCallback("Server's response was empty. Please register through account website during this service interruption."); } } else { AccountServiceResponse ase = this.ParseResult(s); if (ase == null) { if (errorCallback != null) { errorCallback("Error parsing registration response. Please try registering from account website"); } } else if (callback != null) { callback(ase); } } }, e => { this.RequestPendingResult = false; if (errorCallback != null) { errorCallback(e); } }) ); return(true); }