200 lines
5.5 KiB
C#
200 lines
5.5 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using System.Collections;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class selesai2 : MonoBehaviour
|
|
{
|
|
public TMP_Text Teks_Score, Teks_TotalScore;
|
|
public Level2SkorUploader 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 = Data2.DataScore;
|
|
Teks_Score.text = skorTerakhir.ToString();
|
|
|
|
uploader?.SimpanSkorLevel2(() =>
|
|
{
|
|
StartCoroutine(AmbilHighScoreDariServer());
|
|
|
|
//unlock level
|
|
//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", "level2");
|
|
|
|
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.level2.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 level2;
|
|
public string message;
|
|
}
|
|
|
|
//unlock level
|
|
// ✅ Fungsi untuk membuka level berikutnya
|
|
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.");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
//using MySql.Data.MySqlClient;
|
|
//using TMPro;
|
|
//using UnityEngine;
|
|
|
|
//public class selesai2 : MonoBehaviour
|
|
//{
|
|
// public TMP_Text Teks_Score, Teks_TotalScore;
|
|
// public Level2SkorUploader uploader;
|
|
|
|
|
|
// private string conn;
|
|
// private MySqlConnection koneksi;
|
|
|
|
// void Start()
|
|
// {
|
|
// conn = "Server=127.0.0.1; Database=game; User=root; Password=;";
|
|
// koneksi = new MySqlConnection(conn);
|
|
|
|
// int skorTerakhir = Data2.DataScore;
|
|
// Teks_Score.text = skorTerakhir.ToString();
|
|
|
|
// uploader?.SimpanSkorLevel2();
|
|
|
|
// AmbilHighScoreDariDatabase();
|
|
// }
|
|
|
|
// void AmbilHighScoreDariDatabase()
|
|
// {
|
|
// if (GlobalUser.Instance == null)
|
|
// {
|
|
// Debug.LogWarning("GlobalUser tidak ditemukan!");
|
|
// return;
|
|
// }
|
|
|
|
// int userId = GlobalUser.Instance.userId;
|
|
|
|
// try
|
|
// {
|
|
// koneksi.Open();
|
|
// string sql = "SELECT level2 FROM high_score WHERE id_score = @user_id";
|
|
// MySqlCommand cmd = new MySqlCommand(sql, koneksi);
|
|
// cmd.Parameters.AddWithValue("@user_id", userId);
|
|
|
|
// MySqlDataReader reader = cmd.ExecuteReader();
|
|
|
|
// if (reader.Read())
|
|
// {
|
|
// int highScore = reader.GetInt32("level2");
|
|
// Teks_TotalScore.text = highScore.ToString();
|
|
// }
|
|
// else
|
|
// {
|
|
// Teks_TotalScore.text = "0";
|
|
// Debug.LogWarning("Data high score tidak ditemukan untuk user ID: " + userId);
|
|
// }
|
|
|
|
// reader.Close();
|
|
// koneksi.Close();
|
|
// }
|
|
// catch (MySqlException ex)
|
|
// {
|
|
// Debug.LogError("Gagal mengambil high score: " + ex.Message);
|
|
// }
|
|
// }
|
|
|
|
|
|
//}
|