143 lines
4.0 KiB
C#
143 lines
4.0 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using System.Collections;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class selesai9 : MonoBehaviour
|
|
{
|
|
public TMP_Text Teks_Score, Teks_TotalScore;
|
|
public Level9SkorUploader 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 = Data9.DataScore;
|
|
Teks_Score.text = skorTerakhir.ToString();
|
|
|
|
uploader?.SimpanSkorLevel9(() =>
|
|
{
|
|
StartCoroutine(AmbilHighScoreDariServer());
|
|
//unlock level
|
|
Debug.Log("Memanggil UnlockNextLevel...");
|
|
UnlockNextLevel();
|
|
});
|
|
}
|
|
|
|
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", "level9");
|
|
|
|
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.level9.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 level9;
|
|
public string message;
|
|
}
|
|
|
|
|
|
|
|
public void UnlockNextLevel()
|
|
{
|
|
if (GlobalUser.Instance == null)
|
|
{
|
|
Debug.LogWarning("GlobalUser belum tersedia!");
|
|
return;
|
|
}
|
|
|
|
int userId = GlobalUser.Instance.userId;
|
|
|
|
// Hitung level sekarang dari scene sebelumnya
|
|
int previousSceneIndex = SceneManager.GetActiveScene().buildIndex - 1;
|
|
int currentLevel = previousSceneIndex / 12;
|
|
|
|
string key = $"UnlockedLevel_{userId}";
|
|
int unlockedLevel = PlayerPrefs.GetInt(key, 1); // default Level 1
|
|
|
|
Debug.Log($"[UnlockNextLevel] CurrentLevel: {currentLevel}, Unlocked: {unlockedLevel}");
|
|
|
|
if (currentLevel + 1 > unlockedLevel)
|
|
{
|
|
PlayerPrefs.SetInt(key, currentLevel + 1);
|
|
PlayerPrefs.Save();
|
|
Debug.Log($"✅ User {userId} membuka Level {currentLevel + 1}");
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Level berikutnya sudah terbuka.");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
//public void Start()
|
|
//{
|
|
// if (Data9.DataScore >= PlayerPrefs.GetInt("score"))
|
|
// {
|
|
// PlayerPrefs.SetInt("score", Data9.DataScore);
|
|
// }
|
|
|
|
// Teks_Score.text = Data9.DataScore.ToString();
|
|
// Teks_TotalScore.text = PlayerPrefs.GetInt("score").ToString();
|
|
|
|
// uploader?.SimpanSkorLevel9();
|
|
//}
|
|
|