using System.Runtime.Serialization.Json;
using QwirkleClassLibrary.Players;
namespace QwirkleClassLibrary.Persistences
{
///
/// This is the persistence class for the leaderboard : it is in charge of managing all the parameters necessary for the backup and recovery of data concerning the leaderboard.
///
public class LeaderboardPersistenceJson : ILeaderboardPersistence
{
///
/// As the name suggest, this class is used to save the data from the leaderboard.
///
/// The current leaderboard we want to save data from.
public void SaveLeaderboard(Leaderboard leaderboard)
{
var serializer = new DataContractJsonSerializer(typeof(Leaderboard));
using (Stream writer = File.Create("Leaderboard.json"))
{
serializer.WriteObject(writer, leaderboard);
}
}
///
/// This method is used to load the leaderboard into the app when the application starts.
///
/// Leaderboard
///
public Leaderboard LoadLeaderboard()
{
var serializer = new DataContractJsonSerializer(typeof(Leaderboard));
using (Stream reader = File.OpenRead("Leaderboard.json"))
{
return serializer.ReadObject(reader) as Leaderboard ?? throw new InvalidOperationException();
}
}
}
}