From bc86e160243693dbe0e312fba4e28aa106fa24fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Sat, 1 Jun 2024 15:28:42 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20Ajout=20de=20la=20persistance=20en?= =?UTF-8?q?=20JSON=20et=20de=20commentaires=20de=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataContractJson.cs | 88 +++++++++++++++++++ .../DataContractXml.cs | 18 ++++ .../DataContractPersistence/DataToPersist.cs | 15 ++++ 3 files changed, 121 insertions(+) create mode 100644 source/Trek-12/DataContractPersistence/DataContractJson.cs diff --git a/source/Trek-12/DataContractPersistence/DataContractJson.cs b/source/Trek-12/DataContractPersistence/DataContractJson.cs new file mode 100644 index 0000000..83f745a --- /dev/null +++ b/source/Trek-12/DataContractPersistence/DataContractJson.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using System.Runtime.Serialization.Json; +using System.IO; +using System.Xml.Serialization; +using System.Collections.ObjectModel; +using System.Text.Json; +using Models.Game; +using Models.Interfaces; + +namespace DataContractPersistence +{ + public class DataContractJson : IPersistence + { + /// + /// Name of the file where the data will be saved + /// + public string FileName { get; set; } = "data.json"; + + /// + /// Path (relative to the project) + /// + public string FilePath { get; set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory); + + /// + /// Load all the data from JSON file + /// + /// A tuple with the lists of players, games, maps and best scores + public (List, List, List, List) LoadData() + { + var JsonSerializer = new DataContractJsonSerializer(typeof(DataToPersist)); + DataToPersist? data = new DataToPersist(); + + using (Stream s = File.OpenRead(Path.Combine(FilePath, FileName))) + { + data = JsonSerializer.ReadObject(s) as DataToPersist; + } + Console.WriteLine("Data loaded: " + Path.Combine(FilePath, FileName)); + return (data.Players, data.Games, data.Maps, data.BestScores); + } + + /// + /// Save all the data to a JSON file + /// + /// + /// + /// + /// + public void SaveData(List players, List games, List maps, List bestScores) + { + if (!Directory.Exists(FilePath)) + { + Debug.WriteLine("Directory created"); + Debug.WriteLine(Directory.GetDirectoryRoot(FilePath)); + Debug.WriteLine(FilePath); + Directory.CreateDirectory(FilePath); + } + + var JsonSerializer = new DataContractJsonSerializer(typeof(DataToPersist)); + DataToPersist? data = new DataToPersist(); + + data.Players = players; + data.Games = games; + data.Maps = maps; + data.BestScores = bestScores; + + var settings = new XmlWriterSettings() { Indent = true }; + using (FileStream stream = File.Create(Path.Combine(FilePath, FileName))) + { + using (var writer = JsonReaderWriterFactory.CreateJsonWriter( + stream, + Encoding.UTF8, + false, + true)) + { + JsonSerializer.WriteObject(writer, data); + writer.Flush(); + } + } + } + } +} diff --git a/source/Trek-12/DataContractPersistence/DataContractXml.cs b/source/Trek-12/DataContractPersistence/DataContractXml.cs index 81598c3..e09489d 100644 --- a/source/Trek-12/DataContractPersistence/DataContractXml.cs +++ b/source/Trek-12/DataContractPersistence/DataContractXml.cs @@ -15,9 +15,20 @@ namespace DataContractPersistence { public class DataContractXml : IPersistence { + /// + /// Name of the file where the data will be saved + /// public string FileName { get; set; } = "data.xml"; + + /// + /// Path (relative to the project) + /// public string FilePath { get; set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory); + /// + /// Load all the data from XML file + /// + /// A tuple with the lists of players, games, maps and best scores public (List, List, List, List) LoadData() { var serializer = new DataContractSerializer(typeof(DataToPersist)); @@ -31,6 +42,13 @@ namespace DataContractPersistence return (data.Players, data.Games, data.Maps, data.BestScores); } + /// + /// Save all the data to a XML file + /// + /// + /// + /// + /// public void SaveData(List players, List games, List maps, List bestScores) { if(!Directory.Exists(FilePath)) diff --git a/source/Trek-12/DataContractPersistence/DataToPersist.cs b/source/Trek-12/DataContractPersistence/DataToPersist.cs index 9a8dad5..8aca714 100644 --- a/source/Trek-12/DataContractPersistence/DataToPersist.cs +++ b/source/Trek-12/DataContractPersistence/DataToPersist.cs @@ -5,9 +5,24 @@ namespace DataContractPersistence { public class DataToPersist { + /// + /// List of players + /// public List Players { get; set; } + + /// + /// List of games (with all the data in it to be able to recreate the game) + /// public List Games { get; set; } + + /// + /// List of maps with their boards + /// public List Maps { get; set; } + + /// + /// List of best scores + /// public List BestScores { get; set; } } } \ No newline at end of file