| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- using Domain;
- using Domain.Weapons;
- using Domain.XML_Model;
- using Infrastructure;
- using System.Collections.Generic;
- namespace DataLayer;
- public static class DbContextInitializer
- {
- public static void Initialize(DeathwatchDbContext context, string filepath = null)
- {
- context.Database.EnsureCreated();
- // Look for any students.
- if (context.Players.Any()) return; // DB has been seeded
- var weapon = new Weapon
- {
- At = "test",
- Range = 30,
- Attribute = "CT",
- Type = "RANGE"
- };
- var players = new[]
- {
- new Player
- {
- Experience = 17400,
- CC = 52,
- CT = 40,
- F = 46,
- E = 45,
- AG = 50,
- INT = 34,
- PER = 43,
- FM = 43,
- SOC = 40,
- Weapons = new List<Weapon> { weapon }
- }
- };
- foreach (var player in players) context.Players.Add(player);
- context.SaveChanges();
- SeedDatabase(context, filepath);
- }
- public static void SeedDatabase(DeathwatchDbContext context, string filesPath = null)
- {
- // Define the paths to the XML files
- //string accessoireFilePath = Path.Combine("XML", "Accessoires.xml");
- //string ameliorationFilePath = Path.Combine("XML", "Ameliorations.xml");
- //string extensionFilePath = Path.Combine("XML", "Extensions.xml");
- if (!string.IsNullOrEmpty(filesPath))
- {
- var roots = new List<Root>();
- try
- {
- // Deserialize the XML files
- GetRootsElements(roots, filesPath);
- ExtractFromRoot(context, roots);
- }
- catch (Exception e)
- {
- throw;
- }
- }
- }
- private static void GetRootsElements(List<Root> roots, string filesPath)
- {
- var fileExist = Directory.Exists(filesPath);
- if (fileExist)
- {
- var xmlFiles = Directory.GetFiles(filesPath, "*.xml");
- foreach (var xmlFile in xmlFiles)
- {
- var root = XmlHelper.DeserializeXmlFile<Root>(xmlFile);
- roots.Add(root);
- }
- }
- }
- private static void ExtractFromRoot(DeathwatchDbContext context, List<Root> roots)
- {
- foreach (var root in roots)
- {
- if (root != null)
- {
- if (root.Extensions != null && root.Extensions != null && !context.Extensions.Any())
- {
- context.Extensions.AddRange(root.Extensions);
- context.SaveChanges();
- }
- if (root.AccessoiresArmes != null && root.AccessoiresArmes != null && !context.Accessoires.Any())
- {
- context.Accessoires.AddRange(root.AccessoiresArmes);
- context.SaveChanges();
- }
- if (root.Améliorations != null && root.Améliorations != null && !context.Améliorations.Any())
- {
- context.Améliorations.AddRange(root.Améliorations);
- context.SaveChanges();
- }
- if (root.Archétypes != null && root.Archétypes != null && !context.Archétypes.Any())
- {
- context.Archétypes.AddRange(root.Archétypes);
- context.SaveChanges();
- }
- if (root.Armes != null && root.Armes != null && !context.Armes.Any())
- {
- context.Armes.AddRange(root.Armes);
- context.SaveChanges();
- }
- if (root.Armures != null && root.Armures != null && !context.Armures.Any())
- {
- context.Armures.AddRange(root.Armures);
- context.SaveChanges();
- }
- if (root.Assermentations != null && root.Assermentations != null && !context.Assermentations.Any())
- {
- context.Assermentations.AddRange(root.Assermentations);
- context.SaveChanges();
- }
- if (root.AttributsArmes != null && root.AttributsArmes != null && !context.Attributs.Any())
- {
- context.Attributs.AddRange(root.AttributsArmes);
- context.SaveChanges();
- }
- if (root.Carrières != null && root.Carrières != null && !context.Carrières.Any())
- {
- context.Carrières.AddRange(root.Carrières);
- context.SaveChanges();
- }
- if (root.Competences != null && root.Competences != null && !context.Competences.Any())
- {
- context.Competences.AddRange(root.Competences);
- context.SaveChanges();
- }
- if (root.Coordonnées != null && root.Coordonnées != null && !context.Coordonnées.Any())
- {
- context.Coordonnées.AddRange(root.Coordonnées);
- context.SaveChanges();
- }
- if (root.Cybernétiques != null && root.Cybernétiques != null && !context.Cybernétiques.Any())
- {
- context.Cybernétiques.AddRange(root.Cybernétiques);
- context.SaveChanges();
- }
- if (root.Disciplines != null && root.Disciplines != null && !context.Disciplines.Any())
- {
- context.Disciplines.AddRange(root.Disciplines);
- context.SaveChanges();
- }
- if (root.Divinations != null && root.Divinations != null && !context.Divinations.Any())
- {
- context.Divinations.AddRange(root.Divinations);
- context.SaveChanges();
- }
- if (root.Equipements != null && root.Equipements != null && !context.Equipements.Any())
- {
- context.Equipements.AddRange(root.Equipements);
- context.SaveChanges();
- }
- if (root.Implants != null && root.Implants != null && !context.Implants.Any())
- {
- context.Implants.AddRange(root.Implants);
- context.SaveChanges();
- }
- if (root.Mondes != null && root.Mondes != null && !context.Mondes.Any())
- {
- context.Mondes.AddRange(root.Mondes);
- context.SaveChanges();
- }
- if (root.Mutations != null && root.Mutations != null && !context.Mutations.Any())
- {
- context.Mutations.AddRange(root.Mutations);
- context.SaveChanges();
- }
- if (root.Pouvoirs != null && root.Pouvoirs != null && !context.Pouvoirs.Any())
- {
- context.Pouvoirs.AddRange(root.Pouvoirs);
- context.SaveChanges();
- }
- if (root.Promotions != null && root.Promotions != null && !context.Promotions.Any())
- {
- context.Promotions.AddRange(root.Promotions);
- context.SaveChanges();
- }
- if (root.SeuilsXp != null && root.SeuilsXp != null && !context.Seuils.Any())
- {
- context.Seuils.AddRange(root.SeuilsXp);
- context.SaveChanges();
- }
- if (root.Sousmondes != null && root.Sousmondes != null && !context.Sousmondes.Any())
- {
- context.Sousmondes.AddRange(root.Sousmondes);
- context.SaveChanges();
- }
- if (root.Talents != null && root.Talents != null && !context.Talents.Any())
- {
- context.Talents.AddRange(root.Talents);
- context.SaveChanges();
- }
- if (root.Traits != null && root.Traits != null && !context.Traits.Any())
- {
- context.Traits.AddRange(root.Traits);
- context.SaveChanges();
- }
- if (root.Troubles != null && root.Troubles != null && !context.Troubles.Any())
- {
- context.Troubles.AddRange(root.Troubles);
- context.SaveChanges();
- }
- if (root.TypeMondes != null && root.TypeMondes != null && !context.TypeMondes.Any())
- {
- context.TypeMondes.AddRange(root.TypeMondes);
- context.SaveChanges();
- }
- if (root.TypesNom != null && root.TypesNom != null && !context.TypesNom.Any())
- {
- context.TypesNom.AddRange(root.TypesNom);
- context.SaveChanges();
- }
- }
- }
- }
- }
|