Fix score sorting and filtering

This commit is contained in:
GreenHal0 2025-01-14 16:54:15 +01:00
parent 995eff4781
commit 8e7e5c6ae4
2 changed files with 16 additions and 8 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using UnityEngine; using UnityEngine;
public class ScoreboardEntriesTable public class ScoreboardEntriesTable
{ {
@ -13,12 +14,12 @@ public class ScoreboardEntriesTable
public class Scoreboard : MonoBehaviour, ICommandTranslator public class Scoreboard : MonoBehaviour, ICommandTranslator
{ {
[SerializeField] private ScoreboardView scoreboardView;
[SerializeField] private int maxEntries; [SerializeField] private int maxEntries;
private List<ScoreboardEntry> entries = new List<ScoreboardEntry>(); private List<ScoreboardEntry> entries = new List<ScoreboardEntry>();
public event Action<ScoreboardEntry> OnEntryAdded; public event Action<ScoreboardEntry> OnEntryAdded;
[SerializeField] private ScoreboardView scoreboardView;
private void Start() private void Start()
{ {
@ -29,17 +30,15 @@ public class Scoreboard : MonoBehaviour, ICommandTranslator
return; return;
if (entriesTable.entries == null) if (entriesTable.entries == null)
return; return;
List<PlayerScoreboardCardData> scoreboardCardDatas = new List<PlayerScoreboardCardData>();
for (int i = 0; i < entriesTable.entries.Count; i++) for (int i = 0; i < entriesTable.entries.Count; i++)
{ {
entries.Add(entriesTable.entries[i]); entries.Add(entriesTable.entries[i]);
OnEntryAdded?.Invoke(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); // Sort scores descending then creates cards (invert this order cause issues)
scoreboardView.AddPlayerCards(scoreboardCardDatas); entries.Sort((x, y) => y.Score.CompareTo(x.Score));
scoreboardView.AddEntries(entries.GetRange(0, maxEntries));
} }
public void AddScoreboardEntry(string entryName, int entryScore) public void AddScoreboardEntry(string entryName, int entryScore)
@ -68,11 +67,11 @@ public class Scoreboard : MonoBehaviour, ICommandTranslator
public void SaveScoreboardEntriesTable() public void SaveScoreboardEntriesTable()
{ {
SortScoreboardEntriesByHighscore(entries);
ScoreboardEntriesTable scoreboardEntriesTable = new ScoreboardEntriesTable(entries); ScoreboardEntriesTable scoreboardEntriesTable = new ScoreboardEntriesTable(entries);
string jsonScoreboardEntries = JsonUtility.ToJson(scoreboardEntriesTable); string jsonScoreboardEntries = JsonUtility.ToJson(scoreboardEntriesTable);
PlayerPrefs.SetString("ScoreboardEntriesTableTest", jsonScoreboardEntries); PlayerPrefs.SetString("ScoreboardEntriesTableTest", jsonScoreboardEntries);
PlayerPrefs.Save(); PlayerPrefs.Save();
} }
public void TranslateCommand(ECommand command, PressedState state) public void TranslateCommand(ECommand command, PressedState state)

View File

@ -26,6 +26,15 @@ public class ScoreboardView : BaseView
AddPlayerCard(cardData); AddPlayerCard(cardData);
} }
} }
public void AddEntries(List<ScoreboardEntry> 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) private void AddPlayerCard(PlayerScoreboardCardData cardData)
{ {