|
|
|
@ -7,16 +7,22 @@ namespace CoreLibrary.Persistance
|
|
|
|
|
{
|
|
|
|
|
public class Persistance : IPersistance
|
|
|
|
|
{
|
|
|
|
|
public List<Joueur> joueurs = new List<Joueur>();
|
|
|
|
|
public List<Partie> parties = new List<Partie>();
|
|
|
|
|
|
|
|
|
|
public string ancienRepertoire = Directory.GetCurrentDirectory();
|
|
|
|
|
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("joueurs.json"))
|
|
|
|
|
using (FileStream s = File.OpenRead(joueursFilePath))
|
|
|
|
|
{
|
|
|
|
|
joueurs = jsonSerializer.ReadObject(s) as List<Joueur>;
|
|
|
|
|
}
|
|
|
|
@ -26,9 +32,15 @@ namespace CoreLibrary.Persistance
|
|
|
|
|
|
|
|
|
|
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("parties.json"))
|
|
|
|
|
using (FileStream s = File.OpenRead(partiesFilePath))
|
|
|
|
|
{
|
|
|
|
|
parties = jsonSerializer.ReadObject(s) as List<Partie>;
|
|
|
|
|
}
|
|
|
|
@ -40,7 +52,7 @@ namespace CoreLibrary.Persistance
|
|
|
|
|
{
|
|
|
|
|
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(List<Joueur>));
|
|
|
|
|
|
|
|
|
|
using (FileStream s = File.Create("joueurs.json"))
|
|
|
|
|
using (FileStream s = File.Create(Path.Combine(joueursFilePath, "../..")))
|
|
|
|
|
{
|
|
|
|
|
using (var writer = JsonReaderWriterFactory.CreateJsonWriter(
|
|
|
|
|
s,
|
|
|
|
@ -51,6 +63,8 @@ namespace CoreLibrary.Persistance
|
|
|
|
|
jsonSerializer.WriteObject(writer, joueurs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EstEnregistre = true;
|
|
|
|
|
Directory.SetCurrentDirectory(ancienRepertoire);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -58,7 +72,7 @@ namespace CoreLibrary.Persistance
|
|
|
|
|
{
|
|
|
|
|
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(List<Partie>));
|
|
|
|
|
|
|
|
|
|
using (FileStream s = File.Create("parties.json"))
|
|
|
|
|
using (FileStream s = File.Create(Path.Combine(partiesFilePath, "../..")))
|
|
|
|
|
{
|
|
|
|
|
using (var writer = JsonReaderWriterFactory.CreateJsonWriter(
|
|
|
|
|
s,
|
|
|
|
@ -69,7 +83,9 @@ namespace CoreLibrary.Persistance
|
|
|
|
|
jsonSerializer.WriteObject(writer, parties);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EstEnregistre = true;
|
|
|
|
|
Directory.SetCurrentDirectory(ancienRepertoire);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|