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");
        }
    }
    //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 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);
        }
    }
    //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();
        }
    }
Exemplo n.º 5
0
        private void PerformUpload()
        {
            if (!treeView1.Enabled)
                return;

            if (_lastSkin == null)
                return;

            if (_lastSkin.Width != 64 || _lastSkin.Height != 32)
            {
                MessageBox.Show(this, GetLanguageString("B_MSG_UPLOADRES"));
                return;
            }

            login.Username = GlobalSettings.LastUsername;
            login.Password = GlobalSettings.LastPassword;

            bool dialogRes = true;
            bool didShowDialog = false;

            if ((ModifierKeys & Keys.Shift) != 0 || !GlobalSettings.RememberMe || !GlobalSettings.AutoLogin)
            {
                login.Remember = GlobalSettings.RememberMe;
                login.AutoLogin = GlobalSettings.AutoLogin;
                dialogRes = login.ShowDialog() == DialogResult.OK;
                didShowDialog = true;
            }

            if (!dialogRes)
                return;

            _pleaseWaitForm = new PleaseWait();
            _pleaseWaitForm.FormClosed += _pleaseWaitForm_FormClosed;

            _uploadThread = new Thread(UploadThread);
            var ret = new ErrorReturn();
            _uploadThread.Start(new object[] {login.Username, login.Password, _lastSkin.File.FullName, ret});

            _pleaseWaitForm.DialogResult = DialogResult.OK;
            _pleaseWaitForm.ShowDialog();
            _uploadThread = null;
            bool didError = true;

            if (ret.ReportedError != null)
            {
                MessageBox.Show(this, GetLanguageString("B_MSG_UPLOADERROR") + "\r\n" + ret.ReportedError, "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (ret.Exception != null)
            {
                MessageBox.Show(this, GetLanguageString("B_MSG_UPLOADERROR") + "\r\n" + ret.Exception.Message, "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (_pleaseWaitForm.DialogResult != DialogResult.Abort)
            {
                MessageBox.Show(this, GetLanguageString("B_MSG_UPLOADSUCCESS"), "Woo!", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                GlobalSettings.LastSkin = _lastSkin.File.ToString();
                if (_uploadedSkin != null)
                    _uploadedSkin.IsLastSkin = false;
                _uploadedSkin = _lastSkin;
                _uploadedSkin.IsLastSkin = true;
                treeView1.Invalidate();

                didError = false;
            }

            if (didShowDialog)
            {
                GlobalSettings.RememberMe = login.Remember;
                GlobalSettings.AutoLogin = login.AutoLogin;

                if (GlobalSettings.RememberMe == false)
                    GlobalSettings.LastUsername = GlobalSettings.LastPassword = "";
                else
                {
                    GlobalSettings.LastUsername = login.Username;
                    GlobalSettings.LastPassword = login.Password;
                }

                if (didError && GlobalSettings.AutoLogin)
                {
                    GlobalSettings.AutoLogin = false;
                    PerformUpload();
                }
            }
        }
    //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);
        }
    }
Exemplo n.º 7
0
 // ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
 private void DebugResults(UnityEngine.Networking.UnityWebRequest webReq)
 {
     Debug.Log(webReq.downloadHandler.text);
     statusReturn = JsonUtility.FromJson <ErrorReturn>(webReq.downloadHandler.text);
 }