ZoeAppClass/Assets/Scripts/Customize/APICustomize.cs

178 lines
5.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class APICustomize : MonoBehaviour
{
public static APICustomize instance;
string token;
#region ClassesRegion
[Serializable]
public class GetInfo
{
public int status;
public string error;
public GetInfoResponse[] response;
}
[Serializable]
public class GetInfoResponse
{
public int coins;
public string actualItens, boughtItens;
}
[Serializable]
public class BasicResponseToCheckStatus
{
public int status;
}
#endregion
public class BypassCertificate : CertificateHandler
{
protected override bool ValidateCertificate(byte[] certificateData)
{
//Simply return true no matter what
return true;
}
}
public string actualItens, boughtItens;
public int coins;
public bool checkdItens = false;
// Start is called before the first frame update
void Awake()
{
instance = this;
token = PlayerPrefs.GetString("token");
}
public IEnumerator checkItens()
{
while (!ReadingFile.instance.canGo)
{
yield return new WaitForEndOfFrame();
}
string path = ReadingFile.route + "/api/customize/getInfo/" + ReadingFile.idmatricula +
"/" + ReadingFile.idUnit;
Debug.Log(path);
Debug.Log(ReadingFile.idmatricula + ", " + ReadingFile.idUnit);
using (UnityWebRequest www = UnityWebRequest.Get(path))
{
www.certificateHandler = new BypassCertificate();
www.downloadHandler = new DownloadHandlerBuffer();
www.SetRequestHeader("token", Uri.EscapeUriString(token));
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError(www.error);
}
else
{
string Json = www.downloadHandler.text;
Debug.Log(Json);
GetInfo getInfo = JsonUtility.FromJson<GetInfo>(Json);
if (getInfo.status == 200 && getInfo.response.Length > 0 && getInfo.response[0].actualItens != null)
{
actualItens = getInfo.response[0].actualItens;
boughtItens = getInfo.response[0].boughtItens;
coins = getInfo.response[0].coins;
checkdItens = true;
}
else
{
if (getInfo.error == "")
{
yield return StartCoroutine(InsertNewProfile());
}
}
}
}
}
public IEnumerator GiveCoins(int coinsToAdd)
{
string path = ReadingFile.route + "/api/customize/updateCoins";
WWWForm form = new WWWForm();
form.AddField("idUser", ReadingFile.idmatricula);
form.AddField("idUnit", ReadingFile.idUnit);
form.AddField("coinsToAdd", coinsToAdd);
Debug.Log(path);
using (UnityWebRequest www = UnityWebRequest.Post(path, form))
{
www.certificateHandler = new BypassCertificate();
www.downloadHandler = new DownloadHandlerBuffer();
www.SetRequestHeader("token", Uri.EscapeUriString(token));
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError(www.error);
}
else
{
string Json = www.downloadHandler.text; Debug.Log(Json);
coins += coinsToAdd;
GameObject.Find("Canvas - GoOut").GetComponent<Animator>().SetTrigger("coins");
GameObject.Find("txtCoinsBack").GetComponent<Text>().text = coins.ToString();
GameObject.Find("txtCoins").GetComponent<Text>().text = coins.ToString();
Debug.Log(Json);
}
}
}
IEnumerator InsertNewProfile()
{
string path = ReadingFile.route + "/api/customize/insertNew";
WWWForm form = new WWWForm();
form.AddField("idUser", ReadingFile.idmatricula);
form.AddField("idUnit", ReadingFile.idUnit);
using (UnityWebRequest www = UnityWebRequest.Post(path, form))
{
www.certificateHandler = new BypassCertificate();
www.downloadHandler = new DownloadHandlerBuffer();
www.SetRequestHeader("token", Uri.EscapeUriString(token));
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError(www.error);
}
else
{
string Json = www.downloadHandler.text;
Debug.Log(Json);
coins = 0;
actualItens = "Eyes_White;Cheek_Basica;Face_0;Glasses_0;";
boughtItens = "Eyes_Blue;Eyes_Green;Eyes_Red;Eyes_Yellow;Eyes_White;Cheek_Basica;Face_0;Glasses_0";
checkdItens = true;
}
}
}
}