113 lines
3.9 KiB
C#
113 lines
3.9 KiB
C#
using Firebase.Database;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.SceneManagement;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Collections;
|
|
|
|
public class DatabaseManager : MonoBehaviour
|
|
{
|
|
public InputField Name; // Membuat kolom nama
|
|
public InputField NomorAbsen; // Membuat kolom absen
|
|
public Text notificationText; // Menampilkan notifikasi ke pengguna
|
|
|
|
private DatabaseReference dbReference;
|
|
|
|
void Start()
|
|
{
|
|
dbReference = FirebaseDatabase.DefaultInstance.RootReference;
|
|
NomorAbsen.contentType = InputField.ContentType.IntegerNumber; // Hanya input angka
|
|
Name.onValueChanged.AddListener(FilterNamaInput); // Hanya input huruf & spasi
|
|
|
|
notificationText.text = ""; // Kosongkan notifikasi di awal
|
|
}
|
|
|
|
public async void CreateUser() // Dipanggil saat tombol login ditekan
|
|
{
|
|
// Validasi input kosong
|
|
if (string.IsNullOrEmpty(Name.text) || string.IsNullOrEmpty(NomorAbsen.text))
|
|
{
|
|
Debug.LogWarning("Nama atau Nomor Absen tidak boleh kosong.");
|
|
ShowNotification("Nama atau Nomor Absen tidak boleh kosong.");
|
|
return;
|
|
}
|
|
|
|
// Validasi nama hanya huruf dan spasi
|
|
if (!Regex.IsMatch(Name.text, @"^[a-zA-Z\s]+$"))
|
|
{
|
|
Debug.LogWarning("Nama hanya boleh mengandung huruf dan spasi.");
|
|
ShowNotification("Nama hanya boleh mengandung huruf dan spasi.");
|
|
return;
|
|
}
|
|
|
|
// Validasi nomor absen harus angka
|
|
int absen;
|
|
if (!int.TryParse(NomorAbsen.text, out absen))
|
|
{
|
|
Debug.LogWarning("Nomor absen harus berupa angka.");
|
|
ShowNotification("Nomor absen harus berupa angka.");
|
|
return;
|
|
}
|
|
|
|
// Buat userID unik
|
|
string userID = Name.text.ToLower().Replace(" ", "_") + "_" + NomorAbsen.text;
|
|
|
|
// Cek apakah user sudah terdaftar di Firebase
|
|
DataSnapshot snapshot = await dbReference.Child("users").Child(userID).GetValueAsync();
|
|
|
|
if (snapshot.Exists)
|
|
{
|
|
// Ambil skor dari Firebase jika user sudah terdaftar
|
|
object scorePretestObj = snapshot.Child("score_pretest").Value;
|
|
int scorePretest = scorePretestObj != null ? int.Parse(scorePretestObj.ToString()) : 0;
|
|
|
|
object scoreQuisObj = snapshot.Child("score_quiz").Value;
|
|
int scoreQuis = scoreQuisObj != null ? int.Parse(scoreQuisObj.ToString()) : 0;
|
|
|
|
// Simpan ke PlayerPrefs
|
|
PlayerPrefs.SetString("UserID", userID);
|
|
PlayerPrefs.SetInt("ScorePretest", scorePretest);
|
|
PlayerPrefs.SetInt("ScoreQuis", scoreQuis);
|
|
PlayerPrefs.Save();
|
|
|
|
Debug.Log("Login berhasil. Data ditemukan.");
|
|
notificationText.text = ""; // Kosongkan notifikasi saat berhasil
|
|
SceneManager.LoadScene("PRETEST"); // Pindah scene
|
|
}
|
|
else
|
|
{
|
|
// User tidak ditemukan
|
|
Debug.LogWarning("User tidak ditemukan di database. Login ditolak.");
|
|
ShowNotification("Masukkan Nama dan No absen dengan benar !!");
|
|
|
|
// Kosongkan inputan nama dan absen
|
|
Name.text = "";
|
|
NomorAbsen.text = "";
|
|
}
|
|
}
|
|
|
|
private void FilterNamaInput(string input) // Menyaring input nama
|
|
{
|
|
string filtered = Regex.Replace(input, @"[^a-zA-Z\s]", "");
|
|
if (filtered != input)
|
|
{
|
|
Name.text = filtered;
|
|
}
|
|
}
|
|
|
|
// ⏳ Coroutine untuk notifikasi sementara
|
|
private void ShowNotification(string message)
|
|
{
|
|
notificationText.text = message;
|
|
StopAllCoroutines(); // Hentikan notifikasi sebelumnya jika ada
|
|
StartCoroutine(HideNotificationAfterSeconds(3f)); // Hilang setelah 3 detik
|
|
}
|
|
|
|
private IEnumerator HideNotificationAfterSeconds(float seconds)
|
|
{
|
|
yield return new WaitForSeconds(seconds);
|
|
notificationText.text = "";
|
|
}
|
|
}
|