You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sae201_qwirkle/Qwirkle/QwirkleClassLibrary/Persistences/LeaderboardPersistenceJson.cs

39 lines
1.6 KiB

using System.Runtime.Serialization.Json;
using QwirkleClassLibrary.Players;
namespace QwirkleClassLibrary.Persistences
{
/// <summary>
/// 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.
/// </summary>
public class LeaderboardPersistenceJson : ILeaderboardPersistence
{
/// <summary>
/// As the name suggest, this class is used to save the data from the leaderboard.
/// </summary>
/// <param name="leaderboard">The current leaderboard we want to save data from.</param>
public void SaveLeaderboard(Leaderboard leaderboard)
{
var serializer = new DataContractJsonSerializer(typeof(Leaderboard));
using (Stream writer = File.Create("Leaderboard.json"))
{
serializer.WriteObject(writer, leaderboard);
}
}
/// <summary>
/// This method is used to load the leaderboard into the app when the application starts.
/// </summary>
/// <returns>Leaderboard</returns>
/// <exception cref="InvalidOperationException"></exception>
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();
}
}
}
}