/*
------------------- Code Monkey -------------------
Thank you for downloading this package
I hope you find it useful in your projects
If you have any questions let me know
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using CodeMonkey.Utils;
using TMPro;
public class HighscoreTable : MonoBehaviour {
[SerializeField] private Transform entryContainer;
private Transform entryTemplate;
private List<Transform> highscoreEntryTransformList;
private TouchScreenKeyboard keyboard;
[SerializeField] private Text playerNameText;
private string stringToEdit;
public bool higscoreInGameScene;
[SerializeField] private Button gameLogs;
// [SerializeField] Player playerScript;
private void Awake() {
if (!higscoreInGameScene)
{
// entryContainer = transform.Find("highscoreEntryContainer");
entryTemplate = entryContainer.Find("highscoreEntryTemplate");
// PlayerPrefs.SetString("highscoreTable", null);
entryTemplate.gameObject.SetActive(false);
string jsonString = PlayerPrefs.GetString("highscoreTable");
Highscores highscores = JsonUtility.FromJson<Highscores>(jsonString);
if (highscores == null)
{
// There's no stored table, initialize
Debug.Log("Initializing table with default values...");
//AddHighscoreEntry(37, "Steve");
//AddHighscoreEntry(3, "Freddy");
//AddHighscoreEntry(10, "Dosadfasfn");
//AddHighscoreEntry(15, "Bob");
//AddHighscoreEntry(25, "Max");
//AddHighscoreEntry(8, "Jenny");
//AddHighscoreEntry(9, "Banan");
// Reload
gameLogs.interactable = false;
jsonString = PlayerPrefs.GetString("highscoreTable");
highscores = JsonUtility.FromJson<Highscores>(jsonString);
}
else
{
// Sort entry list by Score
for (int i = 0; i < highscores.highscoreEntryList.Count; i++)
{
for (int j = i + 1; j < highscores.highscoreEntryList.Count; j++)
{
if (highscores.highscoreEntryList[j].score > highscores.highscoreEntryList[i].score)
{
// Swap
HighscoreEntry tmp = highscores.highscoreEntryList[i];
highscores.highscoreEntryList[i] = highscores.highscoreEntryList[j];
highscores.highscoreEntryList[j] = tmp;
}
}
}
highscoreEntryTransformList = new List<Transform>();
foreach (HighscoreEntry highscoreEntry in highscores.highscoreEntryList)
{
CreateHighscoreEntryTransform(highscoreEntry, entryContainer, highscoreEntryTransformList);
}
}
}
}
private void CreateHighscoreEntryTransform(HighscoreEntry highscoreEntry, Transform container, List<Transform> transformList) {
float templateHeight = 31f;
Transform entryTransform = Instantiate(entryTemplate, container);
RectTransform entryRectTransform = entryTransform.GetComponent<RectTransform>();
// entryRectTransform.anchoredPosition = new Vector2(0, -templateHeight * transformList.Count);
entryTransform.gameObject.SetActive(true);
int rank = transformList.Count + 1;
string rankString;
switch (rank) {
default:
rankString = rank + ""; break;
case 1: rankString = "1"; break;
case 2: rankString = "2"; break;
case 3: rankString = "3"; break;
}
entryTransform.Find("posText").GetComponent<TextMeshProUGUI>().text = rankString;
int score = highscoreEntry.score;
entryTransform.Find("scoreText").GetComponent<TextMeshProUGUI>().text = score.ToString();
string name = highscoreEntry.name;
entryTransform.Find("nameText").GetComponent<TextMeshProUGUI>().text = name;
string time = highscoreEntry.time;
entryTransform.Find("timeText").GetComponent<TextMeshProUGUI>().text = time;
// Set background visible odds and evens, easier to read
// entryTransform.Find("background").gameObject.SetActive(rank % 2 == 1);
// Highlight First
//if (rank == 1) {
// entryTransform.Find("posText").GetComponent<Text>().color = Color.green;
// entryTransform.Find("scoreText").GetComponent<Text>().color = Color.green;
// entryTransform.Find("nameText").GetComponent<Text>().color = Color.green;
//}
// Set tropy
//switch (rank)
//{
// default:
// entryTransform.Find("trophy").gameObject.SetActive(false);
// break;
// case 1:
// entryTransform.Find("trophy").GetComponent<Image>().color = UtilsClass.GetColorFromString("FFD200");
// break;
// case 2:
// entryTransform.Find("trophy").GetComponent<Image>().color = UtilsClass.GetColorFromString("C6C6C6");
// break;
// case 3:
// entryTransform.Find("trophy").GetComponent<Image>().color = UtilsClass.GetColorFromString("B76F56");
// break;
//}
transformList.Add(entryTransform);
}
private void AddHighscoreEntry(int score, string name, string time) {
// Create HighscoreEntry
HighscoreEntry highscoreEntry = new HighscoreEntry { score = score, name = name, time = time };
// Load saved Highscores
string jsonString = PlayerPrefs.GetString("highscoreTable");
Highscores highscores = JsonUtility.FromJson<Highscores>(jsonString);
if (highscores == null) {
// There's no stored table, initialize
highscores = new Highscores() {
highscoreEntryList = new List<HighscoreEntry>()
};
}
// Add new entry to Highscores
highscores.highscoreEntryList.Add(highscoreEntry);
// Save updated Highscores
string json = JsonUtility.ToJson(highscores);
PlayerPrefs.SetString("highscoreTable", json);
PlayerPrefs.Save();
}
private class Highscores {
public List<HighscoreEntry> highscoreEntryList;
}
/*
* Represents a single High score entry
* */
[System.Serializable]
private class HighscoreEntry {
public int score;
public string name;
public string time;
}
//public void ShowKeyboard()
//{
// keyboard = TouchScreenKeyboard.Open("", TouchScreenKeyboardType.Default, false, false, false, true);
//}
//private void Update()
//{
// if (TouchScreenKeyboard.visible == false && keyboard != null)
// {
// if (keyboard.done)
// {
// stringToEdit = keyboard.text;
// playerNameText.text = stringToEdit;
// keyboard = null;
// }
// }
//}
public void AddDefaultPlayerInList()
{
AddHighscoreEntry(GameManager.Instance.score, System.DateTime.Now.ToString("dd/MM/yyyy"), GameManager.Instance.timeText.text);
}
}