Merge pull request ' Ajout de la persistance en JSON' (#93) from persistence into dev
continuous-integration/drone/push Build is passing Details

Reviewed-on: #93
pull/99/head
Lucas DUFLOT 11 months ago
commit a7214c03af

@ -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
{
/// <summary>
/// Name of the file where the data will be saved
/// </summary>
public string FileName { get; set; } = "data.json";
/// <summary>
/// Path (relative to the project)
/// </summary>
public string FilePath { get; set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
/// <summary>
/// Load all the data from JSON file
/// </summary>
/// <returns>A tuple with the lists of players, games, maps and best scores</returns>
public (List<Player>, List<Game>, List<Map>, List<BestScore>) 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);
}
/// <summary>
/// Save all the data to a JSON file
/// </summary>
/// <param name="players"></param>
/// <param name="games"></param>
/// <param name="maps"></param>
/// <param name="bestScores"></param>
public void SaveData(List<Player> players, List<Game> games, List<Map> maps, List<BestScore> 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();
}
}
}
}
}

@ -15,9 +15,20 @@ namespace DataContractPersistence
{
public class DataContractXml : IPersistence
{
/// <summary>
/// Name of the file where the data will be saved
/// </summary>
public string FileName { get; set; } = "data.xml";
/// <summary>
/// Path (relative to the project)
/// </summary>
public string FilePath { get; set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
/// <summary>
/// Load all the data from XML file
/// </summary>
/// <returns>A tuple with the lists of players, games, maps and best scores</returns>
public (List<Player>, List<Game>, List<Map>, List<BestScore>) LoadData()
{
var serializer = new DataContractSerializer(typeof(DataToPersist));
@ -31,6 +42,13 @@ namespace DataContractPersistence
return (data.Players, data.Games, data.Maps, data.BestScores);
}
/// <summary>
/// Save all the data to a XML file
/// </summary>
/// <param name="players"></param>
/// <param name="games"></param>
/// <param name="maps"></param>
/// <param name="bestScores"></param>
public void SaveData(List<Player> players, List<Game> games, List<Map> maps, List<BestScore> bestScores)
{
if(!Directory.Exists(FilePath))

@ -5,9 +5,24 @@ namespace DataContractPersistence
{
public class DataToPersist
{
/// <summary>
/// List of players
/// </summary>
public List<Player> Players { get; set; }
/// <summary>
/// List of games (with all the data in it to be able to recreate the game)
/// </summary>
public List<Game> Games { get; set; }
/// <summary>
/// List of maps with their boards
/// </summary>
public List<Map> Maps { get; set; }
/// <summary>
/// List of best scores
/// </summary>
public List<BestScore> BestScores { get; set; }
}
}
Loading…
Cancel
Save