135 lines
4.2 KiB
C#
135 lines
4.2 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
using System;
|
|
using Firebase.Database;
|
|
using Firebase.Extensions;
|
|
|
|
public class PosttestControlller : MonoBehaviour
|
|
{
|
|
public string posttestSceneName = "Posttest";
|
|
public Button tombolPosttest;
|
|
public GameObject panelKunciPosttest;
|
|
|
|
private string kodeLogin;
|
|
private string currentMateri;
|
|
private string posttestKey;
|
|
private bool posttestUnlocked = false;
|
|
|
|
|
|
void Start()
|
|
{
|
|
kodeLogin = TestManager.Instance.kodeLogin;
|
|
currentMateri = LevelControl.Instance.currentMateriName;
|
|
|
|
tombolPosttest.interactable = false; // Disable dulu
|
|
CheckAllMateriUnlockedFromFirebase();
|
|
}
|
|
|
|
public void OpenPosttest()
|
|
{
|
|
if (posttestUnlocked)
|
|
{
|
|
SceneManager.LoadScene(posttestSceneName);
|
|
if (panelKunciPosttest != null)
|
|
panelKunciPosttest.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
tombolPosttest.interactable = false;
|
|
Debug.Log("Posttest belum terbuka.");
|
|
|
|
if (panelKunciPosttest != null)
|
|
panelKunciPosttest.SetActive(true);
|
|
StartCoroutine(ClosePanel());
|
|
}
|
|
}
|
|
|
|
|
|
IEnumerator ClosePanel()
|
|
{
|
|
panelKunciPosttest.SetActive(true);
|
|
yield return new WaitForSeconds(2f);
|
|
panelKunciPosttest.SetActive(false);
|
|
}
|
|
|
|
private void CheckAllMateriUnlockedFromFirebase()
|
|
{
|
|
var materiList = LevelControl.Instance.materiLevelPairs;
|
|
|
|
int totalCek = materiList.Count;
|
|
int selesai = 0;
|
|
bool semuaLulus = true;
|
|
|
|
foreach (var pair in materiList)
|
|
{
|
|
string path = $"users/{kodeLogin}/progress/{pair.materiName}";
|
|
int levelTotal = pair.levelNames.Count;
|
|
|
|
FirebaseDatabase.DefaultInstance.GetReference(path)
|
|
.GetValueAsync().ContinueWithOnMainThread(task =>
|
|
{
|
|
selesai++;
|
|
|
|
if (task.IsCompleted && task.Result.Exists)
|
|
{
|
|
//int unlocked = int.Parse(task.Result.Value.ToString());
|
|
int unlocked = 1;
|
|
int.TryParse(task.Result.Value.ToString(), out unlocked);
|
|
if (unlocked < levelTotal)
|
|
{
|
|
semuaLulus = false;
|
|
Debug.Log($"[Posttest] {pair.materiName} belum lengkap ({unlocked}/{levelTotal})");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
semuaLulus = false;
|
|
Debug.Log($"[Posttest] Gagal cek {pair.materiName}");
|
|
}
|
|
|
|
// Setelah semua data dicek
|
|
if (selesai == totalCek)
|
|
{
|
|
OnCekSemuaLevelSelesai(semuaLulus);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
private void OnCekSemuaLevelSelesai(bool semuaSelesai)
|
|
{
|
|
string posttestPath = $"users/{kodeLogin}/posttestUnlocked/{currentMateri}";
|
|
|
|
if (semuaSelesai)
|
|
{
|
|
posttestUnlocked = true;
|
|
FirebaseDatabase.DefaultInstance.GetReference(posttestPath).SetValueAsync(true);
|
|
|
|
tombolPosttest.interactable = true;
|
|
Debug.Log("[Posttest] Semua materi selesai! Tombol posttest aktif.");
|
|
}
|
|
else
|
|
{
|
|
FirebaseDatabase.DefaultInstance.GetReference(posttestPath)
|
|
.GetValueAsync().ContinueWithOnMainThread(task =>
|
|
{
|
|
if (task.IsCompleted && task.Result.Exists && task.Result.Value.ToString() == "true")
|
|
{
|
|
posttestUnlocked = true;
|
|
tombolPosttest.interactable = true;
|
|
Debug.Log("[Posttest] Sudah pernah unlock sebelumnya (Firebase).");
|
|
}
|
|
else
|
|
{
|
|
posttestUnlocked = false;
|
|
tombolPosttest.interactable = false;
|
|
Debug.Log("[Posttest] Belum selesai semua materi.");
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
}
|