|
|
|
@ -1,91 +0,0 @@
|
|
|
|
|
using CoreLibrary.Joueurs;
|
|
|
|
|
using System.Runtime.Serialization.Json;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
|
|
namespace CoreLibrary.Persistance
|
|
|
|
|
{
|
|
|
|
|
public class Persistance : IPersistance
|
|
|
|
|
{
|
|
|
|
|
private readonly string joueursFilePath = "joueurs.json";
|
|
|
|
|
private readonly string partiesFilePath = "parties.json";
|
|
|
|
|
private readonly string ancienRepertoire = Directory.GetCurrentDirectory();
|
|
|
|
|
public bool EstEnregistre { get; private set; }
|
|
|
|
|
|
|
|
|
|
public List<Joueur> ChargerJoueurs()
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(joueursFilePath))
|
|
|
|
|
{
|
|
|
|
|
return new List<Joueur>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(List<Joueur>));
|
|
|
|
|
List<Joueur> joueurs;
|
|
|
|
|
|
|
|
|
|
using (FileStream s = File.OpenRead(joueursFilePath))
|
|
|
|
|
{
|
|
|
|
|
joueurs = jsonSerializer.ReadObject(s) as List<Joueur>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return joueurs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Partie> ChargerParties()
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(partiesFilePath))
|
|
|
|
|
{
|
|
|
|
|
return new List<Partie>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(List<Partie>));
|
|
|
|
|
List<Partie> parties;
|
|
|
|
|
|
|
|
|
|
using (FileStream s = File.OpenRead(partiesFilePath))
|
|
|
|
|
{
|
|
|
|
|
parties = jsonSerializer.ReadObject(s) as List<Partie>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parties;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void EnregistrerJoueurs(List<Joueur> joueurs)
|
|
|
|
|
{
|
|
|
|
|
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(List<Joueur>));
|
|
|
|
|
|
|
|
|
|
using (FileStream s = File.Create(Path.Combine(joueursFilePath, "../..")))
|
|
|
|
|
{
|
|
|
|
|
using (var writer = JsonReaderWriterFactory.CreateJsonWriter(
|
|
|
|
|
s,
|
|
|
|
|
Encoding.UTF8,
|
|
|
|
|
false,
|
|
|
|
|
true))
|
|
|
|
|
{
|
|
|
|
|
jsonSerializer.WriteObject(writer, joueurs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EstEnregistre = true;
|
|
|
|
|
Directory.SetCurrentDirectory(ancienRepertoire);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void EnregistrerParties(List<Partie> parties)
|
|
|
|
|
{
|
|
|
|
|
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(List<Partie>));
|
|
|
|
|
|
|
|
|
|
using (FileStream s = File.Create(Path.Combine(partiesFilePath, "../..")))
|
|
|
|
|
{
|
|
|
|
|
using (var writer = JsonReaderWriterFactory.CreateJsonWriter(
|
|
|
|
|
s,
|
|
|
|
|
Encoding.UTF8,
|
|
|
|
|
false,
|
|
|
|
|
true))
|
|
|
|
|
{
|
|
|
|
|
jsonSerializer.WriteObject(writer, parties);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EstEnregistre = true;
|
|
|
|
|
Directory.SetCurrentDirectory(ancienRepertoire);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|