46 lines
1.1 KiB
C#
46 lines
1.1 KiB
C#
using System.Linq;
|
|
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace LobbyServer
|
|
{
|
|
class ScoresManager
|
|
{
|
|
public const string FileName = "scores.json";
|
|
public string FilePath => Path.Combine(Environment.CurrentDirectory, FileName);
|
|
|
|
private List<ScoreEntry> _scores;
|
|
public IReadOnlyList<ScoreEntry> Scores => _scores.AsReadOnly();
|
|
|
|
public ScoresManager()
|
|
{
|
|
if (File.Exists("scores.json"))
|
|
{
|
|
_scores = JsonConvert.DeserializeObject<List<ScoreEntry>>(File.ReadAllText(FilePath));
|
|
}
|
|
else
|
|
{
|
|
_scores = new List<ScoreEntry>();
|
|
_scores.Add(new ScoreEntry("QuadraLudi", 0));
|
|
}
|
|
}
|
|
|
|
public void Add(ScoreEntry entry)
|
|
{
|
|
_scores.Add(entry);
|
|
Sort();
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
File.WriteAllText(FilePath, JsonConvert.SerializeObject(_scores));
|
|
}
|
|
|
|
private void Sort()
|
|
{
|
|
_scores = _scores.OrderByDescending(x => x.Score).ToList();
|
|
}
|
|
}
|
|
} |