add Leaderboard class
This commit is contained in:
parent
feba6acc36
commit
82ed5fb784
33
App/Data/Leaderboard.cs
Normal file
33
App/Data/Leaderboard.cs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
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() {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user