Reputation: 361
I have a quiz game and at the end of the game I want to display a leaderboard of the top 10 people with their names and score for example: Ioue 500.
Right now I'm adding score every time user scores, and I'm checking the high score. However I'm not sure how to do the leaderboard. can someone guide me?
This script is where the user registers, it's a different scene.
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Linq;
using System.Text.RegularExpressions;
using System.IO;
using System;
public class Registration : MonoBehaviour
{
public Text name;
public static Registration _instace;
private void Awake()
{
_instace = this;
}
/// <summary>
/// Performs data validation and registers user
/// </summary>
public void Register()
{
string data;
string currentUser = string.Empty;
string userName;
data = GetData(name);
if (data == null || string.IsNullOrEmpty(data))
{
Recolor(name, errorColor);
return;
}
else
{
userName = data;
currentUser += data + ";";
}
string previousDirectory = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(Application.persistentDataPath);
if (File.Exists("Users.csv"))
{
string[] users = File.ReadAllLines("Users.csv");
foreach (string user in users)
{
string[] parts = user.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
}
}
File.AppendAllText("Users.csv", currentUser + Environment.NewLine);
Directory.SetCurrentDirectory(previousDirectory);
SceneManager.LoadScene(nextScene);
}
// Gets input data from control group provided
string GetData(Graphic group)
{
InputField inputField = group.GetComponentInChildren<InputField>();
if (inputField != null)
{
return inputField.text;
}
return null;
}
// Gets input data from control group provided
string GetDataD(Graphic group)
{
Dropdown inputField = group.GetComponentInChildren<Dropdown>();
if (inputField != null)
{
return inputField.captionText.text;
}
return null;
}
}
Where i add score is when player get's the right answer
if (isCorrect)
{
theAnswerIsCorrect = true;
playerScore += 20;
scoreDisplayText.text = "Score: " + playerScore.ToString();
}
This is where I compare the old and new score:
public void submitNewPlayerScore(int newScore)
{
if (newScore > playerProgress.highestScore)
{
playerProgress.highestScore = newScore;
SavePlayerProgress();
}
}
Now what I need is I will have many users playing, I just want to display the top 10 users with their names and high scores. Thank you!
Upvotes: 1
Views: 1791
Reputation: 5920
Okay first thing : Why do you even need registration when it's offline?
You can omit the registration and just ask for a player name after quiz ends ( if you want to make offline highscores ).
Since you already have kind of highestScore
you can just order it using Linq
. I assume your code has access to all players ( because it's in csv file ) so I can recommend doing something like this :
List<Player> playerList = new List<Player>();
foreach(string user in File.ReadAllLines("Users.csv"))
{
playersList.Add(Player.Parse(user)); // If you have such method...
}
Now since you have all Player
s in one list you can use Linq
to order it and take only x
to display :
int x = 10; // how many records to take
IEnumerable<Player> highestX = playerList.OrderBy(player => player.highestScore).Take(x);
highestX
is now holding 10 records with the highest score. You can just iterate through them and display details like highestX.ElementAt(0).Name
or something like this.
Upvotes: 1