345 lines
9.9 KiB
C#
345 lines
9.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
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;
|
|
}
|
|
|
|
[Serializable]
|
|
public class GetItens
|
|
{
|
|
public int status;
|
|
public GetItensResponse[] response;
|
|
}
|
|
[Serializable]
|
|
public class GetItensResponse
|
|
{
|
|
public string appearName, resourceName, group;
|
|
public int cost;
|
|
}
|
|
#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;
|
|
public bool checkItensOnStart = true;
|
|
|
|
// Start is called before the first frame update
|
|
void Awake()
|
|
{
|
|
instance = this;
|
|
token = PlayerPrefs.GetString("token");
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (checkItensOnStart)
|
|
{
|
|
StartCoroutine(GetItensCustomize());
|
|
}
|
|
else
|
|
{
|
|
StartCoroutine(checkItens());
|
|
}
|
|
}
|
|
|
|
IEnumerator GetItensCustomize()
|
|
{
|
|
while (!ReadingFile.canGo)
|
|
{
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
|
|
string path = ReadingFile.route + "/api/customize/getItensCustomize";
|
|
|
|
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);
|
|
|
|
yield return new WaitForEndOfFrame();
|
|
StartCoroutine(GetItensCustomize());
|
|
}
|
|
else
|
|
{
|
|
string Json = www.downloadHandler.text;
|
|
|
|
GlobalFunctions.CheckStatus(Json);
|
|
|
|
GetItens getItens = JsonUtility.FromJson<GetItens>(Json); Debug.Log(Json);
|
|
|
|
if (getItens.status == 200)
|
|
{
|
|
if (CustomizeZoe_Screen.instance != null)
|
|
{
|
|
CustomizeZoe_Screen.instance.SetListOfOptions(getItens.response);
|
|
}
|
|
|
|
yield return new WaitForEndOfFrame();
|
|
|
|
StartCoroutine(checkItens());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public IEnumerator checkItens()
|
|
{
|
|
while (!ReadingFile.canGo)
|
|
{
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
|
|
string path = ReadingFile.route + "/api/customize/getInfo/" + ReadingFile.idmatricula +
|
|
"/" + ReadingFile.idUnit;
|
|
|
|
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;
|
|
|
|
GlobalFunctions.CheckStatus(Json);
|
|
|
|
GetInfo getInfo = JsonUtility.FromJson<GetInfo>(Json); Debug.Log(Json);
|
|
|
|
if (getInfo.status == 200 && getInfo.response.Length > 0)
|
|
{
|
|
actualItens = getInfo.response[0].actualItens;
|
|
boughtItens = getInfo.response[0].boughtItens;
|
|
coins = getInfo.response[0].coins;
|
|
|
|
checkdItens = true;
|
|
}
|
|
else
|
|
{
|
|
if (getInfo.error == "")
|
|
{
|
|
StartCoroutine(InsertNewProfile());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
GlobalFunctions.CheckStatus(Json);
|
|
|
|
coins = 0;
|
|
actualItens = "Eyes_Blue;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;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void BuyItem(string itemToBuy, int coinsValue)
|
|
{
|
|
StartCoroutine(BuyItemCor(itemToBuy, coinsValue));
|
|
}
|
|
|
|
IEnumerator BuyItemCor(string itemToBuy, int coinsValue)
|
|
{
|
|
string newBoughtItens = boughtItens + itemToBuy + ";";
|
|
|
|
string path = ReadingFile.route + "/api/customize/updateItens";
|
|
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("coinsToRemove", coinsValue);
|
|
form.AddField("itemToAdd", newBoughtItens);
|
|
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;
|
|
|
|
GlobalFunctions.CheckStatus(Json);
|
|
|
|
BasicResponseToCheckStatus checkStatus = JsonUtility.FromJson<BasicResponseToCheckStatus>(Json);
|
|
|
|
if (checkStatus.status == 200)
|
|
{
|
|
boughtItens = newBoughtItens;
|
|
coins = coins - coinsValue;
|
|
CustomizeBuyScreen.instance.BoughtItemReady();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UpdateActual(List<string> itens)
|
|
{
|
|
string itensInString = "";
|
|
|
|
foreach (string item in itens)
|
|
{
|
|
itensInString += item + ";";
|
|
}
|
|
Debug.Log("Got Here");
|
|
StartCoroutine(UpdateActualCor(itensInString));
|
|
}
|
|
|
|
IEnumerator UpdateActualCor(string newActual)
|
|
{
|
|
string path = ReadingFile.route + "/api/customize/updateActualItens";
|
|
Debug.Log("Got There");
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("newActuals", newActual);
|
|
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;
|
|
GlobalFunctions.CheckStatus(Json);
|
|
|
|
BasicResponseToCheckStatus checkStatus = JsonUtility.FromJson<BasicResponseToCheckStatus>(Json);
|
|
|
|
if (checkStatus.status == 200)
|
|
{
|
|
SceneManager.LoadSceneAsync("SampleScene", LoadSceneMode.Single);
|
|
}
|
|
else
|
|
{
|
|
StartCoroutine(UpdateActualCor(newActual));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
|
|
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;
|
|
GlobalFunctions.CheckStatus(Json);
|
|
}
|
|
}
|
|
}
|
|
}
|