Example #1
0
        /* --- Requests---
         *
         * Schedule of requests is commonly as follown:
         *
         * (1) Configure request
         * (2) Send request
         * (3) Check if request failed
         * (4) Handly response
         */

        public bool Register(string clientName, string clientPassword)
        {
            if (!IsUsernameAllowed(clientName))
            {
                return(false);
            }

            // Set new Login Credentials
            SecurePlayerPrefs.SetString("ClientName", clientName);
            SecurePlayerPrefs.SetString("ClientPassword", clientPassword);

            // Send request
            string rawResponse = SendRequest(new MpRequest.Register(), CallbackType.None, false);    // We dont need a token for this request -> ignore if token is expired

            // Check if request failed
            if (RequestFailed(rawResponse))
            {
                MpResponse.Status statusResponse = JsonUtility.FromJson <MpResponse.Status>(rawResponse);
                if (statusResponse.ErrorLevel == "UsernameAlreadyTaken")
                {
                    Debug.Log("Username already Taken");
                }
                return(false);
            }

            // Handle response
            MpResponse.Token response = JsonUtility.FromJson <MpResponse.Token>(rawResponse);
            SecurePlayerPrefs.SetString("ClientToken", response.ClientToken);         // Set new token
            SecurePlayerPrefs.SetInt("ClientTokenExpire", CurrentTimestamp() + 3600); // We add 3600 seconds because the token will be valid for one hour
            LoggedIn = true;                                                          // Set status
            SecurePlayerPrefs.SetInt("IsRegistered", 1);

            Other.Tools.CreatePopup(Other.Tools.Messages.RegisterSuccess);
            return(true);
        }
Example #2
0
        public bool Logout()
        {
            // Send request
            string rawResponse = SendRequest(new MpRequest.Logout());

            // Check if request failed
            if (RequestFailed(rawResponse))
            {
                MpResponse.Status statusResponse = JsonUtility.FromJson <MpResponse.Status>(rawResponse);
                InvalidToken(statusResponse);
                return(false);
            }

            // Clear client data
            SecurePlayerPrefs.SetString("ClientName", "");
            SecurePlayerPrefs.SetString("ClientPassword", "");
            SecurePlayerPrefs.SetInt("ClientTokenExpire", 0);
            LoggedIn = false;
            Other.Tools.CreatePopup(Other.Tools.Messages.LoggedOut);
            return(true);
        }