105 lines
2.9 KiB
C#
105 lines
2.9 KiB
C#
using MySql.Data.MySqlClient;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using System.Collections;
|
|
using UnityEngine.Networking;
|
|
|
|
public class selesai10 : MonoBehaviour
|
|
{
|
|
public TMP_Text Teks_Score, Teks_TotalScore;
|
|
public Level10SkorUploader uploader;
|
|
|
|
public string BersihkanKarakter(string input)
|
|
{
|
|
// Hilangkan karakter yang tidak bisa di-encode ke UTF-8
|
|
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input);
|
|
return System.Text.Encoding.UTF8.GetString(bytes);
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
int skorTerakhir = Data10.DataScore;
|
|
Teks_Score.text = skorTerakhir.ToString();
|
|
|
|
uploader?.SimpanSkorLevel10(() =>
|
|
{
|
|
StartCoroutine(AmbilHighScoreDariServer());
|
|
});
|
|
}
|
|
|
|
IEnumerator AmbilHighScoreDariServer()
|
|
{
|
|
if (GlobalUser.Instance == null)
|
|
{
|
|
Debug.LogWarning("GlobalUser tidak ditemukan!");
|
|
yield break;
|
|
}
|
|
|
|
int userId = GlobalUser.Instance.userId;
|
|
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("api", "get_highscore");
|
|
form.AddField("id", userId);
|
|
form.AddField("level", "level10");
|
|
|
|
string url = "https://wifiapi.wazzgroup.com/api.php";
|
|
|
|
using (UnityWebRequest www = UnityWebRequest.Post(url, form))
|
|
{
|
|
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.result != UnityWebRequest.Result.Success)
|
|
{
|
|
Debug.LogError("Gagal ambil high score: " + www.error);
|
|
Teks_TotalScore.text = "0";
|
|
}
|
|
else
|
|
{
|
|
string json = www.downloadHandler.text;
|
|
Debug.Log("Respon high score: " + json);
|
|
|
|
try
|
|
{
|
|
HighScoreResponse res = JsonUtility.FromJson<HighScoreResponse>(json);
|
|
if (res.status == "success")
|
|
{
|
|
Teks_TotalScore.text = res.level10.ToString();
|
|
}
|
|
else
|
|
{
|
|
Teks_TotalScore.text = "0";
|
|
Debug.LogWarning("Respon gagal: " + res.message);
|
|
}
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
Debug.LogError("Gagal parsing JSON high score: " + ex.Message);
|
|
Teks_TotalScore.text = "0";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class HighScoreResponse
|
|
{
|
|
public string status;
|
|
public int level10;
|
|
public string message;
|
|
}
|
|
|
|
//public void Start()
|
|
//{
|
|
// if (Data10.DataScore >= PlayerPrefs.GetInt("score"))
|
|
// {
|
|
// PlayerPrefs.SetInt("score", Data10.DataScore);
|
|
// }
|
|
|
|
// Teks_Score.text = Data10.DataScore.ToString();
|
|
// Teks_TotalScore.text = PlayerPrefs.GetInt("score").ToString();
|
|
|
|
// uploader?.SimpanSkorLevel10();
|
|
//}
|
|
}
|