Resolve WES-132-Common-Abstract-Minigame
This commit is contained in:
committed by
Jelle De Geest
parent
4fdb8f95cb
commit
966475455a
@@ -7,8 +7,16 @@ using UnityEngine.UI;
|
||||
/// <summary>
|
||||
/// The hangman-variant of the ScoreBoard
|
||||
/// </summary>
|
||||
public class HangmanGameEndedPanel : MonoBehaviour
|
||||
public class HangmanGameEndedPanel : AbstractGameEndedPanel
|
||||
{
|
||||
/// <summary>
|
||||
/// Tell the scoreboard that the scoreboard is for HangMan
|
||||
/// </summary>
|
||||
protected override MinigameIndex minigameIndex
|
||||
{
|
||||
get { return MinigameIndex.HANGMAN; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// "VERLOREN" or "GEWONNEN"
|
||||
/// </summary>
|
||||
@@ -40,28 +48,11 @@ public class HangmanGameEndedPanel : MonoBehaviour
|
||||
/// </summary>
|
||||
public TMP_Text scoreText;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the scoreboard entries container
|
||||
/// </summary>
|
||||
public Transform scoreboardEntriesContainer;
|
||||
|
||||
/// <summary>
|
||||
/// The GameObjects representing the letters
|
||||
/// </summary>
|
||||
private List<GameObject> scoreboardEntries = new List<GameObject>();
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the ScoreboardEntry prefab
|
||||
/// </summary>
|
||||
public GameObject scoreboardEntry;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the end result image
|
||||
/// </summary>
|
||||
public Image image;
|
||||
|
||||
public List<Sprite> sprites;
|
||||
|
||||
/// <summary>
|
||||
/// Generate the content of the GameEnded panel
|
||||
/// </summary>
|
||||
@@ -99,105 +90,4 @@ public class HangmanGameEndedPanel : MonoBehaviour
|
||||
scoreText.text = $"Score: {score}";
|
||||
SetScoreBoard();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sets the scoreboard
|
||||
/// </summary>
|
||||
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<Tuple<string, Score>> allScores = new List<Tuple<string, Score>>();
|
||||
foreach (User user in UserList.GetUsers())
|
||||
{
|
||||
// Get user's progress for this minigame
|
||||
var progress = user.GetMinigameProgress(MinigameIndex.HANGMAN);
|
||||
if (progress != null)
|
||||
{
|
||||
// Add scores to dictionary
|
||||
List<Score> scores = progress.highestScores;
|
||||
foreach (Score score in scores)
|
||||
{
|
||||
allScores.Add(new Tuple<string, Score>(user.GetUsername(), 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<string, Score> 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<Image>().sprite = UserList.GetUserByUsername(username).GetAvatar();
|
||||
|
||||
// Set the player name
|
||||
entry.transform.Find("PlayerName").GetComponent<TMP_Text>().text = username;
|
||||
|
||||
// Set the score
|
||||
entry.transform.Find("Score").GetComponent<TMP_Text>().text = score.scoreValue.ToString();
|
||||
|
||||
// Set the rank
|
||||
entry.transform.Find("Rank").GetComponent<TMP_Text>().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<TMP_Text>().text = formatted;
|
||||
|
||||
|
||||
// Alternating colors looks nice
|
||||
if (rank % 2 == 0)
|
||||
{
|
||||
Image image = entry.transform.GetComponent<Image>();
|
||||
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>();
|
||||
image.color = new Color(0, 229, 255, 233);
|
||||
}
|
||||
|
||||
rank++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user