serialisation json monstre et user commencé et quasi fini
continuous-integration/drone/push Build is failing Details

pull/32/head
Yannis DOUMIR FERNANDES 2 years ago
parent dda658d24c
commit 056d18547e

@ -215,7 +215,6 @@ int menuInscription()
n++;
continue;
}
if(userMngr.checkIfPseudoExists(pseudo))
{
Console.Clear();
@ -235,6 +234,7 @@ int menuInscription()
Console.WriteLine("\tInscription réussie !");
Console.ResetColor ();
System.Threading.Thread.Sleep(2000); // Pause de 2 secondes
return 0;
}

@ -0,0 +1,66 @@
using Model;
using Modèle;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace Modèle
{
/// <summary>
/// La classe conseil permet à un utilisateur de poster un conseil sur la page d'un monstre.
/// Il est composé d'un auteur(public), un texte(string), un monstre(Monstre) ainsi que
/// deux méthodes : addConseil et removeConseil
/// </summary>
[DataContract]
public class Conseil // J'ai l'impression d'avoir oublié de faire un truc important masi je sais pas quoi
{
[DataMember(Order = 1)]
// Faire une condition du supression il fut que la personne qui ai créé le conseil soit la même que celle qui suprime le conseil.
public int Id { get; }
int idConseil = 0; // Initialisateur de ID
[DataMember(Order = 2)]
public User Auteur { get; set; }
[DataMember(Order = 3)]
public string Texte { get; private set; }
[DataMember(Order = 4)]
public Monstre LeMonstre { get; set; }
public Conseil(User auteur, string texte, Monstre leMonstre)
{
if (string.IsNullOrWhiteSpace(texte))
{
throw new ArgumentException("Vous ne pouvez pas poster un commentaire sans texte !");
}
if (auteur == null)
{
throw new ArgumentNullException(nameof(auteur), "L'auteur ne peut pas être nul !");
}
if (leMonstre == null)
{
throw new ArgumentNullException(nameof(leMonstre), "Le monstre ne peut pas être nul !");
}
Id = idConseil;
idConseil++;
Auteur = auteur;
Texte = texte;
LeMonstre = leMonstre;
}
// Pour afficher les conseils.
public void affichConseil()
{
Console.WriteLine($"Id : {Id}");
Console.WriteLine($"Auteur : {Auteur.Pseudo}");
Console.WriteLine($"Monstre : {LeMonstre.Name}");
Console.WriteLine($"Conseil : {Texte}");
}
}
}

@ -1,4 +1,5 @@
using System.ComponentModel;
using Modèle;
using System.ComponentModel;
using System.Reflection.Metadata;
using System.Runtime.Serialization;
@ -9,33 +10,35 @@ namespace Model
public class Monstre
{
[DataMember(Order = 1)]
public int Id { get; set; }
public int Id { get; set; } // ID
[DataMember(Order = 2)]
public string Name { get; set; }
public string Name { get; set; } // Nom
[DataMember(Order = 3)]
public string Dangerosite { get; private init; }
public string Dangerosite { get; private init; } // Dangerosité
//EN FAIT IL FAUDRAIT FAIRE UN ENUM DU TYPE DE DANGEROSITÉ, pour rajouter lors de
//l'affichage de la liste des monstres une couleur selon ça,
//genre rouge dangereux, violet hyper dangereux, et vert passif
[DataMember(Order = 4)]
public string Description { get; set; }
public string Description { get; set; } // Description
[DataMember(Order = 5)]
public List<string> CharacteristicsList
public List<string> CharacteristicsList // Liste des caractéristiques
{
get; init;
}
[DataMember(Order = 6)]
public List<string> AppearanceList
public List<string> AppearanceList // Liste des apparences
{
get; init;
}
public Monstre(int id, string name, string danger, string desc, List<string> characList, List<string> appearList)
//public List<Conseil> ListeConseils { get; init; }
public Monstre(int id, string name, string danger, string desc, List<string> characList, List<string> appearList/*, List<Conseil> conseilList*/)
{
Id = id;
Name = name;
@ -43,11 +46,11 @@ namespace Model
Description = desc;
CharacteristicsList = characList;
AppearanceList = appearList;
//ListeConseils = conseilList;
if (string.IsNullOrWhiteSpace(Name) || string.IsNullOrWhiteSpace(Description) || string.IsNullOrWhiteSpace(danger))
{
throw new ArgumentException("Un monstre doit avoir un nom, une description et une dangerosité!");
}
}
}
}
}

@ -2,30 +2,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace Persistance
{
public class LoaderJson : IUserDataManager, IMonsterDataManager
{
DataContractJsonSerializer jsonUserSerializer = new DataContractJsonSerializer(typeof(List<User>));
DataContractJsonSerializer jsonMonsterSerializer = new DataContractJsonSerializer(typeof(List<Monstre>));
MemoryStream memoryStream = new MemoryStream();
List<Monstre> IMonsterDataManager.loadMonsters()
{
throw new NotImplementedException();
List<Monstre>? monstre2;
using (FileStream s2 = File.OpenRead("monsters.json"))
{
monstre2 = jsonMonsterSerializer.ReadObject(s2) as List<Monstre>;
}
return monstre2;
//throw new NotImplementedException();
}
List<User> IUserDataManager.loadUsers()
{
List<User>? user2;
using (FileStream s2 = File.OpenRead("monsters.json"))
{
user2 = jsonMonsterSerializer.ReadObject(s2) as List<User>;
}
return user2;
//throw new NotImplementedException();
throw new NotImplementedException();
}
void IMonsterDataManager.saveMonsters(List<Monstre> monstres)
{
throw new NotImplementedException();
jsonMonsterSerializer.WriteObject(memoryStream, monstres);
using (FileStream s = File.Create("monsters.json"))
using (var writer = JsonReaderWriterFactory.CreateJsonWriter(
memoryStream,
System.Text.Encoding.UTF8,
false,
true))
{
memoryStream.WriteTo(s);
}
}
void IUserDataManager.saveUsers(List<User> users)
{
jsonMonsterSerializer.WriteObject(memoryStream, users);
using (FileStream s = File.Create("monsters.json"))
using (var writer = JsonReaderWriterFactory.CreateJsonWriter(
memoryStream,
System.Text.Encoding.UTF8,
false,
true))
{
memoryStream.WriteTo(s);
}
throw new NotImplementedException();
}
}

@ -12,13 +12,11 @@ using static System.Net.Mime.MediaTypeNames;
namespace Persistance
{
public class LoaderXML : IUserDataManager, IMonsterDataManager
{
static string path = "../../../../Persistance/saves/";
static string fichierXML = "users.xml";
static string monstreXML = "monsters.xml";
static string fichierUserXML = "users.xml";
static string fichierMonstreXML = "monsters.xml";
public static void sauvegarderListUsers()
{
@ -26,14 +24,12 @@ namespace Persistance
}
List<Monstre> IMonsterDataManager.loadMonsters()
{
#region Deserialisation
var serialiserXML = new DataContractSerializer(typeof(List<Monstre>));
List<Monstre> monsters;
using (Stream s = File.OpenRead(monstreXML))
using (Stream s = File.OpenRead(fichierMonstreXML))
{
monsters = serialiserXML.ReadObject(s) as List<Monstre>;
}
#endregion
return monsters;
}
@ -48,7 +44,7 @@ namespace Persistance
Directory.SetCurrentDirectory(Path.Combine(Directory.GetCurrentDirectory(), path)); // Setup le chemin d'accès
var serialiserXML = new DataContractSerializer(typeof(List<Monstre>));
XmlWriterSettings xmlSettings = new XmlWriterSettings() { Indent = true }; // Pour avoir le format xml dans le fichier ( indentation etc... )
using (TextWriter tw = File.CreateText(monstreXML))
using (TextWriter tw = File.CreateText(fichierMonstreXML))
{
using (XmlWriter writer = XmlWriter.Create(tw, xmlSettings))
{
@ -63,7 +59,7 @@ namespace Persistance
var serialiserXML = new DataContractSerializer(typeof(List<User>));
XmlWriterSettings xmlSettings = new XmlWriterSettings() { Indent = true }; // Pour avoir le format xml dans le fichier ( indentation etc... )
using (TextWriter tw = File.CreateText(fichierXML))
using (TextWriter tw = File.CreateText(fichierUserXML))
{
using (XmlWriter writer = XmlWriter.Create(tw, xmlSettings))
{
@ -76,7 +72,7 @@ namespace Persistance
{
var serialiserXML = new DataContractSerializer(typeof(List<User>));
List<User> users;
using (Stream s = File.OpenRead(path + fichierXML))
using (Stream s = File.OpenRead(path + fichierUserXML))
{
users = serialiserXML.ReadObject(s) as List<User>;
}

@ -1,5 +1,6 @@
using Model;
using Modèle;
namespace Tests
{

Loading…
Cancel
Save