Пример #1
0
    /// <summary>
    /// Checks the registration fields.
    /// </summary>
    /// <returns><c>true</c>, if all fields cleared the check, <c>false</c> otherwise.</returns>
    /// <param name="regInfo">Reg info.</param>
    /// <param name="registrationMessage"> Success / Error message message.</param>
    public static bool CheckFields(AS_AccountInfo regInfo, ref string errorMessage)
    {
        errorMessage = "";
        // Validate the data in the fields (make sure it matches their type
        if (regInfo.GetFieldValue("email") == null)
        {
            errorMessage = "Please input your email address.";
            return(false);
        }
        if (regInfo.GetFieldValue("password") == null)
        {
            errorMessage = "Please input your password.";
            return(false);
        }
        if (regInfo.GetFieldValue("NickName") == null)
        {
            errorMessage = "Please input your Nickname.";
            return(false);
        }
        if (regInfo.GetFieldValue("Country") == null)
        {
            errorMessage = "Please choose your Country.";
            return(false);
        }
        if (!regInfo.fields.CheckMySQLFields(ref errorMessage))
        {
            return(false);
        }

        // All good..!
        return(true);
    }
Пример #2
0
    public static T GetCustomInfo <T>(this AS_AccountInfo accountInfo, string customInfoFieldName = "custominfo") where T : class, new()
    {
        // Find the custom info field
        int customInfoFieldIndex = accountInfo.fields.GetIndex(customInfoFieldName);

        if (customInfoFieldIndex < 0)
        {
            Debug.LogError("AccountSystem: Could not find custom info field " + customInfoFieldName);
            return(new T());
        }

        // Get its value
        string customInfoXMLValue = accountInfo.fields[customInfoFieldIndex].stringValue;

        // If it's never set
        if (customInfoXMLValue == "")
        {
            Debug.LogWarning("AccountSystem: Custom info field " + customInfoFieldName + " hasn't been set.");
            return(new T());
        }

        // Attempt to deserialize the text we got
        T customInfo = customInfoXMLValue.XmlDeserializeFromString <T>();

        // Debug.Log ("AccountSystem: Read info (of type " + typeof(T).FullName + ") from custom info field " + customInfoFieldName );
        return(customInfo);
    }
Пример #3
0
    public static bool DeSerializeCustomInfo(this AS_AccountInfo accountInfo)
    {
        if (accountInfo.fields.GetIndex("custominfo") < 0)
        {
            Debug.LogError("AccountSystem: There is no custom info field in account info. Is there one in your database? If not try setting up your database again or contact customer support.");
            return(false);
        }
        else
        {
            string temp = accountInfo.fields.GetFieldValue("custominfo");

            if (temp == "")
            {
                accountInfo.customInfo = new AS_CustomInfo();
                Debug.Log("AccountSystem: CustomInfo field was empty - instantiating new CustomInfo!");
            }
            else
            {
                accountInfo.customInfo = temp.XmlDeserializeFromString <AS_CustomInfo>();
                Debug.Log("AccountSystem: Successfully deserialized the downloaded custom info!");
            }

            return(true);
        }
    }
Пример #4
0
    /// <summary>
    /// Attempt to login to our database. When we are done with that attempt, return something meaningful or an error message.
    /// </summary>
    public static IEnumerator TryToDownloadAccountInfoFromDb(int id, AS_AccountInfo accountInfo, System.Action <string> callback, string phpScriptsLocation = null)
    {
        if (phpScriptsLocation == null)
        {
            phpScriptsLocation = AS_Credentials.phpScriptsLocation;
        }

        if (phpScriptsLocation == "")
        {
            Debug.LogError("PHP Scripts Location not set..! Please load the Setup scene located in ../AccountSystem/Setup/");
            yield break;
        }

        Debug.Log("AccountSystem: Downloading Account Info for user with id " + id);


        // Location of our download info script
        string url = phpScriptsLocation + getAccountInfoPhp;

        url += "&requiredForMobile=" + UnityEngine.Random.Range(0, int.MaxValue).ToString();

        // Create a new form
        WWWForm form = new WWWForm();

        // Add The required fields
        form.AddField("id", WWW.EscapeURL(id.ToString()));

        // Connect to the script
        WWW www = new WWW(url, form);

        // Wait for it to respond
        Debug.Log("AccountSystem: Awaiting response from: " + url);
        yield return(www);

        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.LogError("AccountSystem: WWW Error:\n" + www.error);
            callback("error: " + www.text);
            yield break;
        }
        if (www.text.ToLower().Contains("error"))
        {
            Debug.LogError("AccountSystem: PHP/MySQL Error:\n" + www.text);
            callback("error: " + www.text);
            yield break;
        }

        //Debug.Log(www.text);

        // Attempt to deserialize the text we got
        AS_AccountInfo temp = www.text.ToAccountInfo();

        accountInfo.fields = temp.fields;
        if (!accountInfo.DeSerializeCustomInfo())
        {
            Debug.LogWarning("AccountSystem: Could not deserialize Custom Info");
        }

        callback("Account Info downloaded successfully for user with id " + id);
    }
Пример #5
0
    public static string GetFieldValue(this AS_AccountInfo accountInfo, string fieldKey)
    {
        if (accountInfo.fields.GetIndex(fieldKey) < 0)
        {
            return(null);
        }

        return(accountInfo.fields.GetFieldValue(fieldKey));
    }
Пример #6
0
    /// <summary>
    /// Downloads the registration form.
    /// </summary>
    /// <returns>The registration form.</returns>
    /// <param name="credentials">Credentials.</param>
    /// <param name="resultCallback">Result callback.</param>
    public static IEnumerator TryToDownloadRegistrationForm(AS_AccountInfo registrationForm, Action <string> resultCallback, string hostUrl = null)
    {
        if (hostUrl == null)
        {
            hostUrl = AS_Credentials.phpScriptsLocation;
        }

        if (hostUrl == "")
        {
            Debug.LogError("AccountSystem: Host URL not set..! Please load the Account System Setup window.");
            yield break;
        }

        // Location of the registration script
        string url = hostUrl + getRegFormPhP;

        url += "&requiredForMobile=" + UnityEngine.Random.Range(0, int.MaxValue).ToString();

        // Connect to the script
        WWW www = new WWW(url);

        // Wait for it to respond
        yield return(www);


        // Check for WWW Errors
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.LogError("AccountSystem: WWW Error:\n" + www.error);
            resultCallback(www.error);
            yield break;
        }
        // Check for PHP / MySQL Errors
        if (www.text.ToLower().Contains("error"))
        {
            Debug.LogError("AccountSystem: PHP/MySQL Error:\n" + www.text);
            resultCallback(www.text.HandleError());
            yield break;
        }
        if (www.text.ToLower().Contains("warning"))
        {
            Debug.LogWarning("AccountSystem: PHP/MySQL Warning:\n" + www.text);
            resultCallback(www.text.HandleError());
            yield break;
        }



        Debug.Log("AccountSystem: Received serialized registration form\n" + www.text);

        AS_AccountInfo temp = www.text.ToAccountInfo();

        registrationForm.fields = temp.fields;
        resultCallback("Registration Form downloaded Successfully!");

        yield break;
    }
Пример #7
0
    public static bool SetFieldValue(this AS_AccountInfo accountInfo, string fieldKey, string fieldVal)
    {
        if (accountInfo.fields.GetIndex(fieldKey) < 0)
        {
            return(false);
        }

        accountInfo.fields.SetFieldValue(fieldKey, fieldVal);
        return(true);
    }
Пример #8
0
    /// <summary>
    /// Tries to register.
    /// </summary>
    /// <param name="accountInfo">New account info.</param>
    /// <param name="callback">What to call when we are done.</param>
    /// <param name="phpScriptsLocation">Where the PHP scripts are located.</param>
    public static void TryToRegister(this AS_AccountInfo accountInfo, Action <string> callback, string phpScriptsLocation = null)
    {
        AS_CoroutineCaller caller = AS_CoroutineCaller.Create();

        caller.StartCoroutine(AS_Login.TryToRegister
                                  (accountInfo,
                                  value =>
        {
            caller.Destroy();
            callback(value);
        },
                                  phpScriptsLocation));
    }
Пример #9
0
    public static void SetCustomInfo <T>(this AS_AccountInfo accountInfo, T customInfo, string customInfoFieldName = "custominfo")
    {
        // Serialize the custom info
        string serializedCustomInfo = customInfo.XmlSerializeToString();

        // Find the custom info field and update its value
        if (!accountInfo.fields.SetFieldValue(customInfoFieldName, serializedCustomInfo))
        {
            Debug.LogError("AccountSystem: Could not find custom info field " + customInfoFieldName);
            return;
        }

        // Debug.Log ("AccountSystem: Custom info field " + customInfoFieldName + " updated with new info (of type " + typeof(T).FullName + ")");
    }
Пример #10
0
    /// <summary>
    /// Tries to upload.
    /// </summary>
    /// <param name="accountInfo">What we're trying to upload.</param>
    /// <param name="accountId">Account identifier.</param>
    /// <param name="callback">What to call when we are done.</param>
    /// <param name="phpScriptsLocation">Where the PHP scripts are located.</param>
    public static void TryToUpload(this AS_AccountInfo accountInfo, int accountId, Action <string> callback, string phpScriptsLocation = null)
    {
        AS_CoroutineCaller caller = AS_CoroutineCaller.Create();

        caller.StartCoroutine(AS_AccountManagement.TryToUploadAccountInfoToDb
                                  (accountId,
                                  accountInfo,
                                  value =>
        {
            caller.Destroy();
            callback(value);
        },
                                  phpScriptsLocation));
    }
Пример #11
0
    public static string ToReadableString(this AS_AccountInfo accountInfo)
    {
        string readableString = "(FieldName, Value, Type, Unique?, Required?)\n";

        foreach (AS_MySQLField field in accountInfo.fields)
        {
            readableString += "(" + field.name;
            readableString += ", " + field.stringValue;
            readableString += ", " + field.type.ToString();
            readableString += (field.mustBeUnique ? ", MUST BE UNIQUE" : "");
            readableString += (field.isRequired ? ", IS REQUIRED" : "");
            readableString += ")\n";
        }
        return(readableString);
    }
Пример #12
0
    public static bool SerializeCustomInfo(this AS_AccountInfo accountInfo)
    {
        if (accountInfo.customInfo == null)
        {
            Debug.LogError("AccountSystem: The custom info field of Account Info has not been set. Uploading a null field will erase the previously stored value of custom info from the database!\nAborting. Feel free to change this.");
            return(false);
        }

        if (accountInfo.fields.GetIndex("custominfo") >= 0)
        {
            accountInfo.fields.SetFieldValue("custominfo", accountInfo.customInfo.XmlSerializeToString());
            Debug.Log("AccountSystem: Successfully serialized custom info - ready for upload");
            return(true);
        }
        else
        {
            Debug.LogError("AccountSystem: There is no custom info field in account info. Is there one in your database? If not try setting up your database again or contact customer support.");
            return(false);
        }
    }
Пример #13
0
    public static string AccInfoToString(this AS_AccountInfo accInfo, bool checkForOmmit)
    {
        string _string = "";

        for (int i = 0; i < accInfo.fields.Count; i++)
        {
            if (checkForOmmit && accInfo.fields[i].isRequired && accInfo.fields[i].stringValue == "")
            {
                Debug.LogError("AccountSystem: Failed to serialize AccountInfo - " + accInfo.fields[i].name + " field can not be empty!");
                return(null);
            }
            _string += accInfo.fields[i].name + fieldNameValueSeparator + accInfo.fields[i].stringValue + fieldsSeparator;
        }
        if (accInfo.fields.Count > 0)
        {
            _string.Remove(_string.Length - fieldsSeparator.Length);
        }

        // Debug.Log (_string);
        return(_string);
    }
Пример #14
0
    // Name ~ Value ~ Type ~ Must Be Unique ~ Can Be Ommited
    public static AS_AccountInfo ToAccountInfo(this string _string)
    {
        // Debug.Log (_string);
        AS_AccountInfo accInfo = new AS_AccountInfo();

        string[] _fieldsSeparator         = new string[] { fieldsSeparator };
        string[] _fieldNameValueSeparator = new string[] { fieldNameValueSeparator };
        string[] fields = _string.Split(_fieldsSeparator, StringSplitOptions.RemoveEmptyEntries);
        foreach (string field in fields)
        {
            string[]          words      = field.Split(_fieldNameValueSeparator, StringSplitOptions.None);
            string            fieldName  = words[0];
            string            fieldValue = words[1];
            AS_MySQLFieldType fieldType  = words[2].GetEnumType();
            bool mustBeUnique            = (words[3].ToLower() == "true");
            bool isRequired = (words[4].ToLower() == "true");

            // Debug.Log("Name: " + fieldName + " ~ Value: " + fieldValue + " ~ Type: " + words[2] + " (" + fieldType + ") ~ Must Be Unique: " + mustBeUnique.ToString() + " ~ Can Be Ommited: " + canBeOmmited.ToString());
            accInfo.fields.Add(new AS_MySQLField(fieldName, fieldValue, fieldType, mustBeUnique, isRequired, ""));
        }

        return(accInfo);
    }
    void OnGUI()
    {
        GUILayout.BeginArea(new Rect(10, 10, Screen.width - 10, Screen.height - 10));

        // ------------ BUTTONS ------------------

        // Prompt to Download Info from the Database
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Download Account Info", GUILayout.Width(200)))
        {
            guiMessage = "Downloading your info..";
            accountInfo.TryToDownload(accountId, AccountInfoDownloaded);
        }

        // Prompt to Upload Info to the Database
        if (GUILayout.Button("Upload Account Info", GUILayout.Width(200)))
        {
            string errorMessage = "";
            if (!accountInfo.fields.CheckMySQLFields(ref errorMessage))
            {
                Debug.LogError("AccountSystem: Invalid MySQL Field Value:\n" + errorMessage);
                guiMessage = errorMessage;
            }
            else
            {
                // If the user set a new password
                if (tempPasswordVal != "")
                {
                    Debug.Log("AccountSystem: Updated Password!");
                    accountInfo.fields.SetFieldValue("password", tempPasswordVal.Hash());
                }
                guiMessage = "Uploading your info..";
                accountInfo.TryToUpload(accountId, AccountInfoUploaded);
            }
        }

        GUILayout.EndHorizontal();


        GUILayout.Label(" ", GUILayout.Width(100));

        if (currentPage == ManagementPage.AccountInfo)
        {
            GUILayout.Label("~~~==== Account Management ====~~~", GUILayout.Width(300));
            if (GUILayout.Button("Go to Custom Info", GUILayout.Width(200)))
            {
                currentPage = ManagementPage.CustomInfo;
            }

            // ------------ ACCOUNT INFO ------------------
            accountInfo = AccountInfoOnGUI(accountInfo);
        }
        else if (currentPage == ManagementPage.CustomInfo)
        {
            // ------------ CUSTOM INFO ------------------
            // Note that upon altering the CustomInfo class,
            // this part will seize to work -
            // Although don't worry, you will still
            // be able to upload / download and see the custom
            // Info class in the default inspector

            GUILayout.Label("~~~==== Custom Info ====~~~", GUILayout.Width(300));
            if (GUILayout.Button("Go to Account Management", GUILayout.Width(200)))
            {
                currentPage = ManagementPage.AccountInfo;
            }

            accountInfo.customInfo = accountInfo.customInfo.CustomInfoOnGUI();
        }


        GUILayout.Label("", GUILayout.Height(10));

        GUILayout.Label(guiMessage);

        // Tutorial
#if UNITY_EDITOR
        GUILayout.Label("", GUILayout.Height(10));
        GUILayout.Label("\bHow To Manage your Account:" +
                        "\n1) Alter your Account Info" +
                        "\n2) Alter your Custom Info" +
                        "\n3) Hit Upload" +
                        "\n\nThis message was printed from AS_AccountManagementGUI.cs", GUILayout.Width(500));
#endif

        GUILayout.EndArea();
    }
    // Called by OnGUI and provides a basic account information management GUI
    public AS_AccountInfo AccountInfoOnGUI(AS_AccountInfo accountInfo)
    {
        GUILayout.BeginVertical();

        // Title


        GUILayout.Label("Account Id: " + accountId, GUILayout.Width(300));
        if (accountInfo.fields.GetIndex("isactive") > 0)
        {
            GUILayout.Label("Status: " + accountInfo.fields.GetFieldValue("isactive"), GUILayout.Width(300));
        }


        // For each field in Account Info
        foreach (AS_MySQLField field in accountInfo.fields)
        {
            // Id is an auto-increment unique identifier
            // and custom info is not specified during registration
            if (field.name.ToLower() == "id" | field.name.ToLower() == "custominfo" | field.name.ToLower() == "isactive")
            {
                continue;
            }



            // For any given field
            GUILayout.BeginHorizontal();

            // Prompt the user to input the value
            string tempVal = "";

            // Print the name


            // If it's the password,
            if (field.name.ToLower() == "password")
            {
                GUILayout.Label("New Password", GUILayout.Width(200));
                tempPasswordVal = GUILayout.TextField(tempPasswordVal,
                                                      new GUILayoutOption[] { GUILayout.Width(200), GUILayout.Height(20) });
                // Don't store it on the account Info.
                // If on Upload the tempPasswordVal is not empty, we hash it and upload the hashed pass
            }
            else
            {
                GUILayout.Label(field.name.UppercaseFirst(), GUILayout.Width(200));
                tempVal = GUILayout.TextField(accountInfo.fields.GetFieldValue(field.name),
                                              new GUILayoutOption[] { GUILayout.Width(200), GUILayout.Height(20) });

                // Store the value
                accountInfo.fields.SetFieldValue(field.name, tempVal);
            }

            GUILayout.EndHorizontal();
        }

        GUILayout.EndVertical();

        return(accountInfo);
    }
Пример #17
0
    //Facebook
    public static IEnumerator TryToRegisterFB(AS_AccountInfo newAccountInfo, Action <string> resultCallback, string email, string nickname, string hostUrl = null)
    {
        if (hostUrl == null)
        {
            hostUrl = AS_Credentials.phpScriptsLocation;
        }

        if (hostUrl == "")
        {
            Debug.LogError("AccountSystem: Host URL not set..! Please load the Account System Setup window.");
            yield break;
        }

        // Location of the registration script
        string url = hostUrl + registerPhp;


        url += "&requiredForMobile=" + UnityEngine.Random.Range(0, int.MaxValue).ToString();

        // Create a new form
        WWWForm form = new WWWForm();

        // Hash the password
        string password       = newAccountInfo.fields.GetFieldValue("password");
        string hashedPassword = newAccountInfo.fields.GetFieldValue("password").Hash();

        newAccountInfo.fields.SetFieldValue("password", hashedPassword);

        // If there should be an account activation, make sure we require it
        //bool requireEmailActivation = false;// (newAccountInfo.fields.GetIndex("isactive") >= 0);
        // if (requireEmailActivation)
        //     newAccountInfo.fields.SetFieldValue("isactive", "0");
        newAccountInfo.fields.SetFieldValue("isactive", "1");

        // Serialize the custom info field
        newAccountInfo.SerializeCustomInfo();

        // Serialize the account info
        string serializedAccountInfo = newAccountInfo.AccInfoToString(false);

        if (serializedAccountInfo == null)
        {
            Debug.LogError("AccountSystem: Could not serialize account info - check previous Debug.Log for errors");
            yield break;
        }

        form.AddField("newAccountInfo", serializedAccountInfo);

        form.AddField("requireEmailActivation", 1);

        // Connect to the script
        WWW www = new WWW(url, form);

        // Wait for it to respond
        yield return(www);


        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.LogError("AccountSystem: WWW Error:\n" + www.error);
            resultCallback("Error: Could not connect. Please try again later!");
            yield break;
        }
        if (www.text.ToLower().Contains("error"))
        {
            Debug.LogError("AccountSystem: PHP/MySQL Error:\n" + www.text);
            resultCallback(www.text.HandleError());
            yield break;
        }

        if (www.text.ToLower().Contains("success"))
        {
            Net.instance.OnLoginFB(email, password, nickname);
            //Debug.Log("AccountSystem: New account registered successfully!\n" + www.text);
            //resultCallback("New account registered successfully!");
        }
        else
        {
            Debug.LogError("AccountSystem: Could not register new account - Check Message:\n" + www.text);
            resultCallback("Error: Could not register new account");
        }


        yield break;
    }