Пример #1
0
 private void Start()
 {
     if (Instance == null)
     {
         Instance = this;
         DontDestroyOnLoad(this);
     }
     if (_connectedToServer == false)
     {
         ConnectToServer();
     }
     if (!SecurePlayerPrefs.HasKey("IsRegistered"))
     {
         SecurePlayerPrefs.SetInt("IsRegistered", 0);
     }
 }
Пример #2
0
        public bool Login(string clientName, string clientPassword, bool showPopups = true)
        {
            if (!IsUsernameAllowed(clientName))
            {
                return(false);
            }

            if (clientName != SecurePlayerPrefs.GetString("ClientName") || clientPassword != SecurePlayerPrefs.GetString("ClientPassword"))
            {
                // Set new Login Credentials
                SecurePlayerPrefs.SetString("ClientName", clientName);
                SecurePlayerPrefs.SetString("ClientPassword", clientPassword);
            }

            // Send request
            string rawResponse = SendRequest(new MpRequest.Login(), 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 == "InvalidLogin" && showPopups)
                {
                    Other.Tools.CreatePopup(Other.Tools.Messages.InvalidLogin);
                }
                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);

            if (showPopups)
            {
                Other.Tools.CreatePopup(Other.Tools.Messages.LoggedIn);
            }
            return(true);
        }
Пример #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);
        }
Пример #4
0
 public bool Login(bool showPopups = true)
 {
     return(Login(SecurePlayerPrefs.GetString("ClientName"), SecurePlayerPrefs.GetString("ClientPassword"), showPopups));
 }