From 8e7e5c6ae4f7c1e2fa6122c22284d299710697f1 Mon Sep 17 00:00:00 2001 From: GreenHal0 Date: Tue, 14 Jan 2025 16:54:15 +0100 Subject: [PATCH] Fix score sorting and filtering --- Assets/Scripts/Scoreboard/Scoreboard.cs | 15 +++++++-------- Assets/Scripts/UI/Views/ScoreboardView.cs | 9 +++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Assets/Scripts/Scoreboard/Scoreboard.cs b/Assets/Scripts/Scoreboard/Scoreboard.cs index aa0e245..6f85884 100644 --- a/Assets/Scripts/Scoreboard/Scoreboard.cs +++ b/Assets/Scripts/Scoreboard/Scoreboard.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Linq; using UnityEngine; public class ScoreboardEntriesTable { @@ -13,12 +14,12 @@ public class ScoreboardEntriesTable public class Scoreboard : MonoBehaviour, ICommandTranslator { + [SerializeField] private ScoreboardView scoreboardView; [SerializeField] private int maxEntries; private List entries = new List(); public event Action OnEntryAdded; - [SerializeField] private ScoreboardView scoreboardView; private void Start() { @@ -29,17 +30,15 @@ public class Scoreboard : MonoBehaviour, ICommandTranslator return; if (entriesTable.entries == null) return; - List scoreboardCardDatas = new List(); for (int i = 0; i < entriesTable.entries.Count; i++) { entries.Add(entriesTable.entries[i]); OnEntryAdded?.Invoke(entriesTable.entries[i]); - PlayerScoreboardCardData cardData = new PlayerScoreboardCardData(entriesTable.entries[i].Name, entriesTable.entries[i].Score.ToString()); - scoreboardCardDatas.Add(cardData); } - SortScoreboardEntriesByHighscore(entries); - SortScoreboardCardsDatasByHighscore(scoreboardCardDatas); - scoreboardView.AddPlayerCards(scoreboardCardDatas); + + // Sort scores descending then creates cards (invert this order cause issues) + entries.Sort((x, y) => y.Score.CompareTo(x.Score)); + scoreboardView.AddEntries(entries.GetRange(0, maxEntries)); } public void AddScoreboardEntry(string entryName, int entryScore) @@ -68,11 +67,11 @@ public class Scoreboard : MonoBehaviour, ICommandTranslator public void SaveScoreboardEntriesTable() { - SortScoreboardEntriesByHighscore(entries); ScoreboardEntriesTable scoreboardEntriesTable = new ScoreboardEntriesTable(entries); string jsonScoreboardEntries = JsonUtility.ToJson(scoreboardEntriesTable); PlayerPrefs.SetString("ScoreboardEntriesTableTest", jsonScoreboardEntries); PlayerPrefs.Save(); + } public void TranslateCommand(ECommand command, PressedState state) diff --git a/Assets/Scripts/UI/Views/ScoreboardView.cs b/Assets/Scripts/UI/Views/ScoreboardView.cs index 096acb1..690bd29 100644 --- a/Assets/Scripts/UI/Views/ScoreboardView.cs +++ b/Assets/Scripts/UI/Views/ScoreboardView.cs @@ -26,6 +26,15 @@ public class ScoreboardView : BaseView AddPlayerCard(cardData); } } + public void AddEntries(List entries) + { + int rank = 1; + foreach (var entry in entries) + { + AddPlayerCard(new PlayerScoreboardCardData(rank + ".\t" + entry.Name, entry.Score.ToString())); + rank++; + } + } private void AddPlayerCard(PlayerScoreboardCardData cardData) {