Beispiel #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);
        }
Beispiel #2
0
 private void Start()
 {
     if (Instance == null)
     {
         Instance = this;
         DontDestroyOnLoad(this);
     }
     if (_connectedToServer == false)
     {
         ConnectToServer();
     }
     if (!SecurePlayerPrefs.HasKey("IsRegistered"))
     {
         SecurePlayerPrefs.SetInt("IsRegistered", 0);
     }
 }
Beispiel #3
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);
        }