Example #1
0
        private void FinishMoveCallback()
        {
            string rawResponse = _callbackResponse;

            // Check if request failed
            if (RequestFailed(rawResponse))
            {
                MpResponse.Status statusResponse = JsonUtility.FromJson <MpResponse.Status>(rawResponse);
                InvalidToken(statusResponse);
                if (statusResponse.ErrorLevel == "NotInGame")
                {
                    Other.Tools.CreatePopup(Other.Tools.Messages.NotInGame);
                }
                if (statusResponse.ErrorLevel == "InvalidMass")
                {
                    Other.Tools.CreatePopup(Other.Tools.Messages.Error);
                }
            }

            // Handle response
            MpResponse.Move moveResponse = JsonUtility.FromJson <MpResponse.Move>(rawResponse);
            if (GameManager.Instance.State == GameManager.GameState.WaitForOpponent)
            {
                GameManager.Instance.OpponentMove(moveResponse.Mass);
            }
        }
Example #2
0
        public bool JoinPrivateGame(string gameName, string gamePassword)
        {
            // Configure request
            MpRequest.JoinPrivateGame request = new MpRequest.JoinPrivateGame();
            request.GameName     = gameName;
            request.GamePassword = gamePassword;

            // Send request
            string rawResponse = SendRequest(request);

            // Check if request failed
            if (RequestFailed(rawResponse))
            {
                MpResponse.Status statusResponse = JsonUtility.FromJson <MpResponse.Status>(rawResponse);
                InvalidToken(statusResponse);
                if (statusResponse.ErrorLevel == "InvalidGameName")
                {
                    Other.Tools.CreatePopup(Other.Tools.Messages.CouldntFindGame);
                }
                if (statusResponse.ErrorLevel == "InvalidPassword")
                {
                    Other.Tools.CreatePopup(Other.Tools.Messages.InvalidGamePassword);
                }
                return(false);
            }

            // Handle response
            Scenes.SetString("GameMode", "Online");
            Scenes.SetString("OpponentName", gameName);
            Scenes.Load("Game");
            return(true);
        }
Example #3
0
        public bool CreatePrivateGame(string gamePassword)
        {
            // Configure request
            MpRequest.CreatePrivateGame request = new MpRequest.CreatePrivateGame();
            request.GamePassword = gamePassword;

            // Send request
            string rawResponse = SendRequest(request);

            // Check if request failed
            if (RequestFailed(rawResponse))
            {
                MpResponse.Status statusResponse = JsonUtility.FromJson <MpResponse.Status>(rawResponse);
                InvalidToken(statusResponse);
                if (statusResponse.ErrorLevel == "AlreadyCreatedGame")
                {
                    Other.Tools.CreatePopup(Other.Tools.Messages.AlreadyCreatedGame);
                }
                return(false);
            }

            // Handle response
            Other.Tools.CreatePopup(Other.Tools.Messages.CreatePrivateGame);
            return(true);
        }
Example #4
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 #5
0
 private bool InvalidToken(MpResponse.Status response)
 {
     if (response.ErrorLevel != "InvalidToken")
     {
         return(false);
     }
     Debug.Log("Invalid Token. Requesting new one");
     Other.Tools.CreatePopup(Other.Tools.Messages.Error);
     Login(false);
     return(true);
 }
Example #6
0
        public List <string> GetAvaibleGames()
        {
            // Send request
            string rawResponse = SendRequest(new MpRequest.GetAvaibleGames());

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

            // Handle response
            MpResponse.AvaibleGames avaibleGamesResponse = JsonUtility.FromJson <MpResponse.AvaibleGames>(rawResponse);
            return(avaibleGamesResponse.GameNames);
        }
Example #7
0
        public bool CancelQuickMatch()
        {
            // Send request
            string rawResponse = SendRequest(new MpRequest.CancelQuickMatch());

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

            // Handle request
            return(true);
        }
Example #8
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);
        }
Example #9
0
        private void QuickMatchCallback()
        {
            string rawResponse = _callbackResponse;

            // Check if request failed
            if (RequestFailed(rawResponse))
            {
                MpResponse.Status statusResponse = JsonUtility.FromJson <MpResponse.Status>(rawResponse);
                InvalidToken(statusResponse);
                if (statusResponse.ErrorLevel == "AlreadyInQueue")
                {
                    Other.Tools.CreatePopup(Other.Tools.Messages.AlreadyInQueue);
                }
            }
            else
            {
                // Handle request
                MpResponse.Player playerResponse = JsonUtility.FromJson <MpResponse.Player>(rawResponse);
                Scenes.SetString("GameMode", "Online");
                Scenes.SetString("OpponentName", playerResponse.PlayerName);
                Scenes.Load("Game");
                Other.Tools.CreatePopup(Other.Tools.Messages.SearchingGames);
            }
        }