using System; using System.Collections.Generic; using System.Linq; using TMPro; using UnityEngine; using UnityEngine.UI; public class GameEndedPanel : MonoBehaviour { public UserList userList; /// /// "VERLOREN" or "GEWONNEN" /// public TMP_Text endText; /// /// LPM /// public TMP_Text lpmText; /// /// Letters ( right | wrong ) /// public TMP_Text lettersRightText; public TMP_Text lettersWrongText; /// /// Letters /// public TMP_Text lettersTotalText; /// /// Accuracy /// public TMP_Text accuracyText; /// /// Words /// public TMP_Text wordsText; /// /// Time /// public TMP_Text timeText; /// /// Score /// public TMP_Text scoreText; /// /// Reference to the scoreboard entries container /// public Transform scoreboardEntriesContainer; /// /// The GameObjects representing the letters /// private List scoreboardEntries = new List(); /// /// Reference to the ScoreboardEntry prefab /// public GameObject scoreboardEntry; /// /// Generate the content of the GameEnded panel /// /// Time of starting the minigame /// Total number of words /// Total number of correctly spelled letters /// Total number of incorrectly spelled letters /// "VERLOREN" or "GEWONNEN" /// Final score public void GenerateContent(DateTime startTime, int totalWords, int correctLetters, int incorrectLetters, string result, int score) { // Final result endText.text = result; // LPM TimeSpan duration = DateTime.Now.Subtract(startTime); lpmText.text = (60f * correctLetters / duration.TotalSeconds).ToString("#") + " LPM"; // Letters ( right | wrong ) total lettersRightText.text = correctLetters.ToString(); lettersWrongText.text = incorrectLetters.ToString(); lettersTotalText.text = (correctLetters + incorrectLetters).ToString(); // Accuracy if (correctLetters + incorrectLetters > 0) { accuracyText.text = ((correctLetters) * 100f / (correctLetters + incorrectLetters)).ToString("#.##") + "%"; } else { accuracyText.text = "-"; } // Words wordsText.text = $"{totalWords}"; // Time timeText.text = duration.ToString(@"mm\:ss"); // Score scoreText.text = $"Score: {score}"; SetScoreBoard(); } /// /// Sets the scoreboard /// private void SetScoreBoard() { // Clean the previous scoreboard entries for (int i = 0; i < scoreboardEntries.Count; i++) { Destroy(scoreboardEntries[i]); } scoreboardEntries.Clear(); // Instantiate new entries // Get all scores from all users List> allScores = new List>(); foreach (User user in userList.GetUsers()) { // Get user's progress for this minigame Progress progress = user.GetMinigameProgress(MinigameIndex.SPELLING_BEE); if (progress != null) { // Add scores to dictionary List scores = progress.Get>("highestScores"); foreach (Score score in scores) { allScores.Add(new Tuple(user.username, score)); } } } // Sort allScores based on Score.scoreValue allScores.Sort((a, b) => b.Item2.scoreValue.CompareTo(a.Item2.scoreValue)); // Instantiate scoreboard entries int rank = 1; foreach (Tuple tup in allScores.Take(10)) { string username = tup.Item1; Score score = tup.Item2; GameObject entry = Instantiate(scoreboardEntry, scoreboardEntriesContainer); scoreboardEntries.Add(entry); // Set the player icon entry.transform.Find("Image").GetComponent().sprite = userList.GetUserByUsername(username).avatar; // Set the player name entry.transform.Find("PlayerName").GetComponent().text = username; // Set the score entry.transform.Find("Score").GetComponent().text = score.scoreValue.ToString(); // Set the rank entry.transform.Find("Rank").GetComponent().text = rank.ToString(); // Set the ago // Convert the score.time to Datetime DateTime time = DateTime.Parse(score.time); DateTime currentTime = DateTime.Now; TimeSpan diff = currentTime.Subtract(time); string formatted; if (diff.Days > 0) { formatted = $"{diff.Days}d "; } else if (diff.Hours > 0) { formatted = $"{diff.Hours}h "; } else if (diff.Minutes > 0) { formatted = $"{diff.Minutes}m "; } else { formatted = "now"; } entry.transform.Find("Ago").GetComponent().text = formatted; // Alternating colors looks nice if (rank % 2 == 0) { Image image = entry.transform.GetComponent(); image.color = new Color(image.color.r, image.color.g, image.color.b, 0f); } // Make new score stand out if (diff.TotalSeconds < 1) { Image image = entry.transform.GetComponent(); image.color = new Color(0, 229, 255, 233); } rank++; } } }