35 lines
1.4 KiB
C#
35 lines
1.4 KiB
C#
namespace Game.Data {
|
|
internal class Leaderboard {
|
|
public static void WriteScore(string playerName, int score) {
|
|
var oldScores = ReadScores();
|
|
for (int i = 0; i < oldScores.Count; ++i) {
|
|
if (oldScores[i].Item1 == playerName) {
|
|
oldScores.RemoveAt(i);
|
|
break;
|
|
}
|
|
}
|
|
oldScores.Add((playerName, score));
|
|
oldScores.Sort((a, b) => a.Item2.CompareTo(b.Item2));
|
|
File.WriteAllLines("leaders.txt", oldScores.Select(score => score.Item1 + "\t" + score.Item2.ToString()));
|
|
}
|
|
|
|
public static List<(string, int)> ReadScores() {
|
|
if (!File.Exists("leaders.txt")) return new List<(string, int)>();
|
|
using StreamReader rd = new StreamReader("leaders.txt");
|
|
List<(string, int)> scores = new List<(string, int)>();
|
|
string? line = null;
|
|
while ((line = rd.ReadLine()) != null) {
|
|
if (line.Length > 0) {
|
|
string[] fields = line.Split('\t');
|
|
if (fields.Length < 2) continue;
|
|
int score;
|
|
if (!int.TryParse(fields[1], out score)) continue;
|
|
scores.Add((fields[0], score));
|
|
}
|
|
}
|
|
scores.Sort((a, b) => a.Item2.CompareTo(b.Item2));
|
|
return scores;
|
|
}
|
|
}
|
|
}
|