Example #1
0
    /// <summary>
    /// Using the Signup Canvas input fields to post account name, account password and email to PHP. Returns the account ID to store in GameSaveUtility
    /// </summary>
    /// <returns></returns>
    IEnumerator LoginUserRoutione()
    {
        WWWForm form = new WWWForm();

        form.AddField("account_name", loginCanvas.transform.GetChild(0).GetComponent <InputField>().text);
        form.AddField("account_pass", loginCanvas.transform.GetChild(1).GetComponent <InputField>().text);

        string[] dbText;
        bool     successfulLogin = false;

        using (UnityWebRequest www = UnityWebRequest.Post(path + "verifylogin.php", form))
        {
            yield return(www.SendWebRequest());

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Form load complete!");
                StringBuilder sb = new StringBuilder();
                foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders())
                {
                    sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n");
                }

                // Print Headers
#if (UNITY_EDITOR)
                Debug.Log(sb.ToString());
#endif

                if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("LoginSucceed"))
                {
                    if (www.GetResponseHeaders()["LoginSucceed"] == "true")
                    {
                        Debug.Log("Login succesful");
                        dbText = www.downloadHandler.text.Split('\n'); // splits all echoed text from PHP
                        status = true;
                        GameSaveUtility.SaveID(int.Parse(dbText[1]));  // know that the ID is echoed on the 2nd line of the db text body"
                        successfulLogin = true;                        // boolean trigger to retrieve all player data
                    }
                    else
                    {
                        loginPopup.transform.GetChild(0).GetComponent <Text>().text = "ERROR: Login";
                        string bodyError = "Login failed: ";
                        if (www.GetResponseHeaders().ContainsKey("PasswordFail"))
                        {
                            bodyError += "Inccorect password.\n";
                        }
                        if (www.GetResponseHeaders().ContainsKey("AccountFail"))
                        {
                            bodyError += "Inccorect account name.\n";
                        }
                        loginPopup.transform.GetChild(1).GetComponent <Text>().text = bodyError;
                        loginPopup.SetActive(true);
#if (UNITY_EDITOR)
                        Debug.Log(www.downloadHandler.text);
#endif
                    }
                }
                else
                {
                    // Failed
                    loginPopup.transform.GetChild(0).GetComponent <Text>().text = "ERROR: Login";
                    loginPopup.transform.GetChild(1).GetComponent <Text>().text = "Unable to connect to server.";
                    loginPopup.SetActive(true);
#if (UNITY_EDITOR)
                    Debug.Log(www.downloadHandler.text);
#endif
                }
            }
        }

        // if successful login, retrieve all player data and store to PlayerPrefs
        if (successfulLogin)
        {
            form = new WWWForm();
            int playerID = GameSaveUtility.GetID();
            form.AddField("player_ID", playerID);
            form.AddField("request", "getlevels");

            using (UnityWebRequest www = UnityWebRequest.Post(path + "getplayerdata.php", form))
            {
                yield return(www.SendWebRequest());

                if (www.isNetworkError || www.isHttpError)
                {
                    Debug.Log(www.error);
                }
                else
                {
                    Debug.Log("Form load complete!");
                    StringBuilder sb = new StringBuilder();
                    foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders())
                    {
                        sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n");
                    }

                    // Print Headers
#if (UNITY_EDITOR)
                    Debug.Log(sb.ToString());
#endif

                    if (www.GetResponseHeaders().Count > 0)
                    {
                        Debug.Log(www.downloadHandler.text);
                        dbText = www.downloadHandler.text.Split('\n');
                        MenuData menuData = JsonUtility.FromJson <MenuData>(dbText[0]);
                        menuData.StoreLevels();                                               // store level information back into local PlayerPrefs
                        string     parsePlayer = dbText[1].Replace("[", "").Replace("]", ""); // format the player text outside of the array to be recognized as a PlayerData object JSON
                        PlayerData playerData  = JsonUtility.FromJson <PlayerData>(parsePlayer);
                        playerData.SetPlayerInfo();                                           // store player information back into the local PlayerPrefs
                    }
                    else
                    {
                        // Failed
                        loginPopup.transform.GetChild(0).GetComponent <Text>().text = "ERROR: Login";
                        loginPopup.transform.GetChild(1).GetComponent <Text>().text = "Unable to connect to server.";
                        loginPopup.SetActive(true);
#if (UNITY_EDITOR)
                        Debug.Log(www.downloadHandler.text);
#endif
                    }
                }
            }

            if (successfulLogin)
            {
                canvasManager.SwitchCanvas("menus");
            }
        }
    }
Example #2
0
    /// <summary>
    /// Using the Signup Canvas input fields to post account name, account password and email to PHP. Returns the account ID to store in GameSaveUtility
    /// </summary>
    /// <returns></returns>
    IEnumerator CreateNewUserRoutine()
    {
        string[] dbText;

        WWWForm form = new WWWForm();

        form.AddField("account_name", signupCanvas.transform.GetChild(0).GetComponent <InputField>().text);
        form.AddField("account_pass", signupCanvas.transform.GetChild(1).GetComponent <InputField>().text);
        form.AddField("confirm_pass", signupCanvas.transform.GetChild(2).GetComponent <InputField>().text);
        form.AddField("email", signupCanvas.transform.GetChild(3).GetComponent <InputField>().text);

        using (UnityWebRequest www = UnityWebRequest.Post(path + "createaccount.php", form))
        {
            yield return(www.SendWebRequest());

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Form load complete!");
                StringBuilder sb = new StringBuilder();
                foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders())
                {
                    sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n");
                }

                // Print Headers
#if (UNITY_EDITOR)
                Debug.Log(sb.ToString());
#endif
                if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("UserCreation"))
                {
                    if (www.GetResponseHeaders()["UserCreation"] == "true")
                    {
                        Debug.Log("New account successful.");
                        dbText = www.downloadHandler.text.Split('\n');
                        status = true;
                        GameSaveUtility.SaveID(int.Parse(dbText[2])); // know that the ID is echoed on the 3rd line of the db text body
                        canvasManager.SwitchCanvas("input");
                    }
                    else
                    {
                        signupPopup.transform.GetChild(0).GetComponent <Text>().text = "ERROR: Create Account";
                        string bodyError = "Create account failed: ";
                        if (www.GetResponseHeaders().ContainsKey("PasswordMatch"))
                        {
                            bodyError += "Passwords do not match.\n";
                        }
                        if (www.GetResponseHeaders().ContainsKey("AccountFail"))
                        {
                            bodyError += "This account name has been taken. Please choose another.\n";
                        }
                        if (www.GetResponseHeaders().ContainsKey("EmailFail"))
                        {
                            bodyError += "There is already an account under this email.\n";
                        }
                        if (www.GetResponseHeaders().ContainsKey("EmailFormat"))
                        {
                            bodyError += "This is not a valid email.\n";
                        }
                        signupPopup.transform.GetChild(1).GetComponent <Text>().text = bodyError;
                        signupPopup.SetActive(true);
#if (UNITY_EDITOR)
                        Debug.Log(www.downloadHandler.text);
#endif
                    }
                }
                else
                {
                    // Failed
                    signupPopup.transform.GetChild(0).GetComponent <Text>().text = "ERROR: Create Account";
                    signupPopup.transform.GetChild(1).GetComponent <Text>().text = "Unable to connect to server.";
                    signupPopup.SetActive(true);
#if (UNITY_EDITOR)
                    Debug.Log(www.downloadHandler.text);
#endif
                }
            }
        }
    }