// Start is called before the first frame update
    void Start()
    {
        if (Application.internetReachability == NetworkReachability.NotReachable)
        {
            ErrorDataScript.setErrorText("No hay conexión a Internet");
            ErrorDataScript.setButtonMode(0);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
        else
        {
            if (!PlayerPrefs.HasKey("soundActive"))
            {
                PlayerPrefs.SetInt("soundActive", 1);
            }

            if (!PlayerPrefs.HasKey("musicActive"))
            {
                PlayerPrefs.SetInt("musicActive", 1);
            }

            SoundManager.Init();
            SoundManager.PlayMusic();

            SceneManager.LoadScene("Login Scene", LoadSceneMode.Single);
        }
    }
    //Request to the server for Login
    private IEnumerator LoginRequest(string username, string password)
    {
        UnityWebRequest requestLogin = UnityWebRequest.Get("https://unitrivia.herokuapp.com/api/login");

        requestLogin.SetRequestHeader("username", username);
        requestLogin.SetRequestHeader("password", password);
        yield return(requestLogin.SendWebRequest());

        Debug.Log("ResponseCode: " + requestLogin.responseCode);

        if (requestLogin.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.Log("ERROR CONNECTION:" + requestLogin.result);

            ErrorDataScript.setErrorText("Error de conexión");
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
        else if (requestLogin.responseCode != 200)
        {
            Debug.Log("ERROR LOGIN:"******"Error Scene", LoadSceneMode.Additive);
        }
        else
        {
            Debug.Log("EXITO LOGIN:" + requestLogin.downloadHandler.text);

            //Obtenemos la información del usuario
            StartCoroutine(ProfileRequest(requestLogin.downloadHandler.text));
        }
    }
    private IEnumerator AddCoinRequest(string token, int cantidad)
    {
        UnityWebRequest addCoinRequest = UnityWebRequest.Post("https://unitrivia.herokuapp.com/api/tienda/insertarMonedas", "");

        addCoinRequest.SetRequestHeader("jwt", token);
        addCoinRequest.SetRequestHeader("cantidad", "" + cantidad);
        yield return(addCoinRequest.SendWebRequest());

        Debug.Log("ResponseCode: " + addCoinRequest.responseCode);

        if (addCoinRequest.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.Log("ERROR CONNECTION:" + addCoinRequest.result);

            ErrorDataScript.setErrorText("Error de conexión");
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
        else if (addCoinRequest.responseCode != 200)
        {
            Debug.Log("ERROR ADDCOIN:" + addCoinRequest.downloadHandler.text);
            ErrorReturn result = ErrorReturn.CreateFromJSON(addCoinRequest.downloadHandler.text);

            ErrorDataScript.setErrorText(result.message);
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
        else
        {
            Debug.Log("EXITO ADDCOIN:" + addCoinRequest.downloadHandler.text);
            UserDataScript.addCoins(cantidad);
            Debug.Log("Insertada 1 moneda");
        }
    }
Example #4
0
    private IEnumerator RecoverQuestionRequest(string username)
    {
        UnityWebRequest recoverQuestionRequest = UnityWebRequest.Get("https://unitrivia.herokuapp.com/api/login/recover/question");

        recoverQuestionRequest.SetRequestHeader("username", username);
        yield return(recoverQuestionRequest.SendWebRequest());

        Debug.Log("ResponseCode: " + recoverQuestionRequest.responseCode);

        if (recoverQuestionRequest.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.Log("ERROR CONNECTION:" + recoverQuestionRequest.result);

            ErrorDataScript.setErrorText("Error de conexión");
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
        else if (recoverQuestionRequest.responseCode != 200)
        {
            Debug.Log("ERROR LOGIN:"******"Error Scene", LoadSceneMode.Additive);
        }
        else
        {
            Debug.Log("EXITO RECOVER:" + recoverQuestionRequest.downloadHandler.text);

            QuestionInput.text         = recoverQuestionRequest.downloadHandler.text;
            UsernameInput.interactable = false;
            SecondContainer.SetActive(true);
        }
    }
Example #5
0
    private IEnumerator DeleteProfileRequest(string token)
    {
        UnityWebRequest requestDeleteProfile = UnityWebRequest.Delete("https://unitrivia.herokuapp.com/api/profile");

        requestDeleteProfile.SetRequestHeader("jwt", token);
        yield return(requestDeleteProfile.SendWebRequest());

        //Debug.Log("ResponseCode: " + requestDeleteProfile.responseCode);

        if (requestDeleteProfile.result == UnityWebRequest.Result.ConnectionError)
        {
            //Debug.Log("ERROR CONNECTION:" + requestDeleteProfile.result);

            ErrorDataScript.setErrorText("Error de conexión");
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
        else if (requestDeleteProfile.responseCode != 200)
        {
            //Debug.Log("ERROR DELETE:" + requestDeleteProfile.downloadHandler.text);
            ServerReturn result = ServerReturn.CreateFromJSON(requestDeleteProfile.downloadHandler.text);

            ErrorDataScript.setErrorText(result.message);
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
        else
        {
            //Debug.Log("EXITO DELETE:" + requestDeleteProfile.downloadHandler.text);

            SuccessDataScript.setSuccessText("Usuario eliminado correctamente!");
            SuccessDataScript.setReturnScene("Login Scene");
            SceneManager.LoadScene("Success Scene", LoadSceneMode.Additive);
        }
    }
    private IEnumerator ProfileRequest(string token)
    {
        UnityWebRequest requestProfile = UnityWebRequest.Get("https://unitrivia.herokuapp.com/api/profile");

        requestProfile.SetRequestHeader("jwt", token);
        yield return(requestProfile.SendWebRequest());

        if (requestProfile.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.Log("ERROR CONNECTION:" + requestProfile.result);

            ErrorDataScript.setErrorText("Error de conexión");
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
        else if (requestProfile.responseCode != 200)
        {
            Debug.Log("ERROR PROFILE:" + requestProfile.downloadHandler.text);
            ErrorReturn result = ErrorReturn.CreateFromJSON(requestProfile.downloadHandler.text);

            ErrorDataScript.setErrorText(result.message);
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
        else
        {
            Debug.Log("EXITO PROFILE:" + requestProfile.downloadHandler.text);
            ProfileReturn result = ProfileReturn.CreateFromJSON(requestProfile.downloadHandler.text);

            // Save player data
            UserDataScript.setInfo("token", token);
            UserDataScript.setInfo("username", result._id);
            UserDataScript.setInfo("email", result.mail);
            UserDataScript.setInfo("question", result.preg);
            UserDataScript.setInfo("answer", result.res);
            UserDataScript.setInfo("avatar", result.avtr);
            UserDataScript.setInfo("banner", result.bnr);
            UserDataScript.setInfo("ficha", result.fich);
            UserDataScript.setCoins(result.cns);
            UserDataScript.setStats(result.nj, result.ng);
            UserDataScript.setItems(result.rfs);

            SceneManager.LoadScene("Menu Scene", LoadSceneMode.Single);
        }
    }
    void comenzarPartida(JObject res)
    {
        JValue okJV   = (JValue)res.Property("res").Value;
        string ok     = (string)okJV.Value;
        JValue infoJV = (JValue)res.Property("info").Value;
        string info   = (string)infoJV.Value;

        if (ok.Equals("ok")) //Ha ido bien
        {
            SceneManager.LoadScene("Game Scene", LoadSceneMode.Single);
        }
        else //Ha habido un error
        {
            ErrorDataScript.setErrorText(info);
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
    }
Example #8
0
    private IEnumerator RegisterRequest(string username, string password, string email, string preg, string res)
    {
        UnityWebRequest requestRegister = UnityWebRequest.Post("https://unitrivia.herokuapp.com/api/register", "");

        requestRegister.SetRequestHeader("username", username);
        requestRegister.SetRequestHeader("password", password);
        requestRegister.SetRequestHeader("email", email);
        requestRegister.SetRequestHeader("preg", preg);
        requestRegister.SetRequestHeader("res", res);
        yield return(requestRegister.SendWebRequest());

        Debug.Log("ResponseCode: " + requestRegister.responseCode);

        if (requestRegister.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.Log("ERROR CONNECTION:" + requestRegister.result);

            ErrorDataScript.setErrorText("Error de conexión");
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
        else if (requestRegister.responseCode != 200)
        {
            Debug.Log("ERROR REGISTRO:" + requestRegister.downloadHandler.text);
            ServerReturn result = ServerReturn.CreateFromJSON(requestRegister.downloadHandler.text);

            ErrorDataScript.setErrorText(result.message);
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
        else
        {
            Debug.Log("EXITO REGISTRO:" + requestRegister.downloadHandler.text);
            //Guardar Token usuario

            SuccessDataScript.setSuccessText("Registro realizado correctamente!");
            SuccessDataScript.setReturnScene("Login Scene");
            SceneManager.LoadScene("Success Scene", LoadSceneMode.Additive);
        }
    }
    //Request to the server for Login
    private IEnumerator BuyRequest(string item)
    {
        UnityWebRequest requestBuy = null;

        requestBuy = UnityWebRequest.Post("https://unitrivia.herokuapp.com/api/tienda/comprar", "");
        requestBuy.SetRequestHeader("nombre", item);
        requestBuy.SetRequestHeader("jwt", UserDataScript.getInfo("token"));
        yield return(requestBuy.SendWebRequest());

        Debug.Log("ResponseCode: " + requestBuy.responseCode);

        if (requestBuy.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.Log("ERROR CONNECTION ACTIVESHOP:" + requestBuy.result);

            ErrorDataScript.setErrorText("Error de conexión");
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
        else if (requestBuy.responseCode != 200)
        {
            Debug.Log("ERROR ACTIVESHOP:" + requestBuy.downloadHandler.text);
            ErrorReturn result = ErrorReturn.CreateFromJSON(requestBuy.downloadHandler.text);

            ErrorDataScript.setErrorText(result.message);
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
        else
        {
            Debug.Log("EXITO ACTIVESHOP:" + requestBuy.downloadHandler.text);
            SoundManager.PlayBuySound();
            UserDataScript.addItem(item);
            UserDataScript.removeCoins(100);
            checkActualItem();
        }
    }
Example #10
0
    private IEnumerator ChangePasswordRequest(string username, string answer, string password)
    {
        UnityWebRequest requestChangePassword = UnityWebRequest.Post("https://unitrivia.herokuapp.com/api/login/recover/password", "");

        requestChangePassword.SetRequestHeader("username", username);
        requestChangePassword.SetRequestHeader("res", answer);
        requestChangePassword.SetRequestHeader("newpassword", password);
        yield return(requestChangePassword.SendWebRequest());

        Debug.Log("ResponseCode: " + requestChangePassword.responseCode);

        if (requestChangePassword.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.Log("ERROR CONNECTION:" + requestChangePassword.result);

            ErrorDataScript.setErrorText("Error de conexión");
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
        else if (requestChangePassword.responseCode != 200)
        {
            Debug.Log("ERROR LOGIN:"******"Error Scene", LoadSceneMode.Additive);
        }
        else
        {
            Debug.Log("EXITO LOGIN:"******"Cambio realizado correctamente!");
            SuccessDataScript.setReturnScene("Login Scene");
            SceneManager.LoadScene("Success Scene", LoadSceneMode.Additive);
        }
    }
    //Request to the server for Guest
    private IEnumerator GuestRequest()
    {
        UnityWebRequest requestGuest = UnityWebRequest.Get("https://unitrivia.herokuapp.com/api/logAsGuest");

        yield return(requestGuest.SendWebRequest());

        Debug.Log("ResponseCode: " + requestGuest.responseCode);

        if (requestGuest.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.Log("ERROR CONNECTION:" + requestGuest.result);

            ErrorDataScript.setErrorText("Error de conexión");
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
        else
        {
            Debug.Log("EXITO GUEST:" + requestGuest.downloadHandler.text);

            // Save guest data
            StartCoroutine(ProfileRequest(requestGuest.downloadHandler.text));
        }
    }
    //Request to the server for Login
    private IEnumerator ActiveRequest(string item, string type)
    {
        UnityWebRequest requestActive = null;

        switch (type)
        {
        case "avatar":
            requestActive = UnityWebRequest.Post("https://unitrivia.herokuapp.com/api/profile/modify/avatar", "");
            requestActive.SetRequestHeader("idavatar", item);
            break;

        case "banner":
            requestActive = UnityWebRequest.Post("https://unitrivia.herokuapp.com/api/profile/modify/banner", "");
            requestActive.SetRequestHeader("idbanner", item);
            break;

        case "ficha":
            requestActive = UnityWebRequest.Post("https://unitrivia.herokuapp.com/api/profile/modify/formFicha", "");
            requestActive.SetRequestHeader("idformficha", item);
            break;

        default:
            ErrorDataScript.setErrorText("Error inesperado");
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
            break;
        }

        if (requestActive != null)
        {
            requestActive.SetRequestHeader("jwt", UserDataScript.getInfo("token"));
            yield return(requestActive.SendWebRequest());

            Debug.Log("ResponseCode: " + requestActive.responseCode);

            if (requestActive.result == UnityWebRequest.Result.ConnectionError)
            {
                Debug.Log("ERROR CONNECTION ACTIVESHOP:" + requestActive.result);

                ErrorDataScript.setErrorText("Error de conexión");
                ErrorDataScript.setButtonMode(1);
                SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
            }
            else if (requestActive.responseCode != 200)
            {
                Debug.Log("ERROR ACTIVESHOP:" + requestActive.downloadHandler.text);
                ErrorReturn result = ErrorReturn.CreateFromJSON(requestActive.downloadHandler.text);

                ErrorDataScript.setErrorText(result.message);
                ErrorDataScript.setButtonMode(1);
                SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
            }
            else
            {
                Debug.Log("EXITO ACTIVESHOP:" + requestActive.downloadHandler.text);

                switch (type)
                {
                case "avatar":
                    UserDataScript.setInfo("avatar", item);
                    break;

                case "banner":
                    UserDataScript.setInfo("banner", item);
                    break;

                case "ficha":
                    UserDataScript.setInfo("ficha", item);
                    break;

                default:
                    ErrorDataScript.setErrorText("Error. Vuelve a intentarlo.");
                    ErrorDataScript.setButtonMode(1);
                    SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
                    break;
                }

                checkActualItem();
            }
        }
        else
        {
            ErrorDataScript.setErrorText("Objeto no reconocido");
            ErrorDataScript.setButtonMode(1);
            SceneManager.LoadScene("Error Scene", LoadSceneMode.Additive);
        }
    }