using Microsoft.EntityFrameworkCore; using Newtonsoft.Json.Linq; using SessionCompanion.Database.Tables; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SessionCompanion.Database { public class ApplicationDbContext : DbContext { public virtual DbSet Alignments { get; set; } public virtual DbSet Backgrounds { get; set; } public virtual DbSet Biographies { get; set; } public virtual DbSet Characters { get; set; } public virtual DbSet Charismas { get; set; } public virtual DbSet Classes { get; set; } public virtual DbSet Constitutions { get; set; } public virtual DbSet Dexterities { get; set; } public virtual DbSet Intelligences { get; set; } public virtual DbSet Races { get; set; } public virtual DbSet Statistics { get; set; } public virtual DbSet Strengths { get; set; } public virtual DbSet Users { get; set; } public virtual DbSet Wisdoms { get; set; } public virtual DbSet Spells { get; set; } public virtual DbSet CharacterSpells { get; set; } public virtual DbSet CharacterSpellSlots { get; set; } public virtual DbSet Armors { get; set; } public virtual DbSet CharacterArmors { get; set; } public virtual DbSet Weapons { get; set; } public virtual DbSet CharacterWeapons { get; set; } public virtual DbSet OtherEquipment { get; set; } public virtual DbSet CharacterOtherEquipment { get; set; } public virtual DbSet Monsters { get; set; } public virtual DbSet MonsterActions { get; set; } public virtual DbSet MonsterLegendaryActions { get; set; } public virtual DbSet MonsterSpecialAbilities { get; set; } public virtual DbSet GameActions { get; set; } public virtual DbSet LegendaryActions { get; set; } public virtual DbSet SpecialAbilities { get; set; } public virtual DbSet Shopkeepers { get; set; } public virtual DbSet ShopkeeperItems { get; set; } public ApplicationDbContext(DbContextOptions options) : base(options) { } public List SpecialAbilitiesList { get; set; } public List LegendaryActionsList { get; set; } public List GameActionsList { get; set; } protected IEnumerable SeedWeapon() { const string file = "../SessionCompanion.Database/JsonData/weapons.json"; List weapons = new List(); using (StreamReader reader = new StreamReader(file)) { var json = reader.ReadToEnd(); dynamic jweapons = JArray.Parse(json); foreach (dynamic item in jweapons) weapons.Add(SeedFromJsons.SingleWeaponSeeder(item)); } return weapons; } protected IEnumerable SeedArmor() { const string file = "../SessionCompanion.Database/JsonData/armors.json"; List armors = new List(); using (StreamReader reader = new StreamReader(file)) { var json = reader.ReadToEnd(); dynamic jactions = JArray.Parse(json); int id = 1; foreach (dynamic item in jactions) { armors.Add(SeedFromJsons.SingleArmorSeeder(item, id)); id++; } } return armors; } protected IEnumerable SeedOtherEquipment() { const string file = "../SessionCompanion.Database/JsonData/otherEquipments.json"; List otherEquipment = new List(); using (StreamReader reader = new StreamReader(file)) { var json = reader.ReadToEnd(); dynamic jactions = JArray.Parse(json); int id = 1; foreach (dynamic item in jactions) { otherEquipment.Add(SeedFromJsons.SingleOtherEquipmentSeeder(item, id)); id++; } } return otherEquipment; } protected IEnumerable SeedSpell() { const string file = "../SessionCompanion.Database/JsonData/spells.json"; List spells = new List(); using (StreamReader reader = new StreamReader(file)) { var json = reader.ReadToEnd(); dynamic jspells = JArray.Parse(json); foreach (dynamic item in jspells) spells.Add(SeedFromJsons.SingleSpellSeeder(item)); } return spells; } protected IEnumerable SeedSpecialAbilities() { const string file = "../SessionCompanion.Database/JsonData/monsters.json"; List specAbilities = new List(); using (StreamReader reader = new StreamReader(file)) { var json = reader.ReadToEnd(); dynamic jabilites = JArray.Parse(json); int id = 1; foreach (dynamic item in jabilites) { if (item.special_abilities != null) { foreach (dynamic sp in item.special_abilities) { if (specAbilities.Find(x => x.Name == (string)sp.name) is null) { specAbilities.Add(SeedFromJsons.SingleSpecialAbilitySeeder(sp, id)); id++; } } } } } SpecialAbilitiesList = specAbilities; return specAbilities; } protected IEnumerable SeedActions() { const string file = "../SessionCompanion.Database/JsonData/monsters.json"; List gameActions = new List(); using (StreamReader reader = new StreamReader(file)) { var json = reader.ReadToEnd(); dynamic jactions = JArray.Parse(json); int id = 1; foreach (dynamic item in jactions) { if (item.actions != null) { foreach (dynamic a in item.actions) { if (gameActions.Find(x => x.Name == (string)a.name) is null) { gameActions.Add(SeedFromJsons.SingleActionSeeder(a, id)); id++; } } } } } GameActionsList = gameActions; return gameActions; } protected IEnumerable SeedLegendaryActions() { const string file = "../SessionCompanion.Database/JsonData/monsters.json"; List legendaryActions = new List(); using (StreamReader reader = new StreamReader(file)) { var json = reader.ReadToEnd(); dynamic jlegendaryactions = JArray.Parse(json); int id = 1; foreach (dynamic item in jlegendaryactions) { if (item.legendary_actions != null) { foreach (dynamic la in item.legendary_actions) { if (legendaryActions.Find(x => x.Name == (string)la.name) is null) { legendaryActions.Add(SeedFromJsons.SingleLegendaryActionSeeder(la, id)); id++; } } } } } LegendaryActionsList = legendaryActions; return legendaryActions; } protected IEnumerable SeedMonster() { const string file = "../SessionCompanion.Database/JsonData/monsters.json"; List monsters = new List(); using (StreamReader reader = new StreamReader(file)) { var json = reader.ReadToEnd(); dynamic jmonsters = JArray.Parse(json); foreach (dynamic item in jmonsters) monsters.Add(SeedFromJsons.SingleMonsterSeeder(item)); } return monsters; } protected IEnumerable SeedMonsterSpecialAbilities() { const string file = "../SessionCompanion.Database/JsonData/monsters.json"; List monsterSpecialAbilities = new List(); using (StreamReader reader = new StreamReader(file)) { var json = reader.ReadToEnd(); dynamic jabilities = JArray.Parse(json); int id = 1; foreach (dynamic item in jabilities) { if (item.special_abilities != null) { foreach (dynamic a in item.special_abilities) { var abilityId = SpecialAbilitiesList.Find(x => x.Name == (string)a.name).Id; monsterSpecialAbilities.Add(new MonsterSpecialAbility { Id = id, MonsterId = (int)item.index, SpecialAbilityId = abilityId }); id++; } } } } return monsterSpecialAbilities; } protected IEnumerable SeedMonsterActions() { const string file = "../SessionCompanion.Database/JsonData/monsters.json"; List monsterActions = new List(); using (StreamReader reader = new StreamReader(file)) { var json = reader.ReadToEnd(); dynamic jactions = JArray.Parse(json); int id = 1; foreach (dynamic item in jactions) { if (item.actions != null) { foreach (dynamic a in item.actions) { var abilityId = GameActionsList.Find(x => x.Name == (string)a.name).Id; monsterActions.Add(new MonsterAction { Id = id, MonsterId = (int)item.index, GameActionId = abilityId }); id++; } } } } return monsterActions; } protected IEnumerable SeedMonsterLegendaryActions() { const string file = "../SessionCompanion.Database/JsonData/monsters.json"; List monsterLegendaryActions = new List(); using (StreamReader reader = new StreamReader(file)) { var json = reader.ReadToEnd(); dynamic jactions = JArray.Parse(json); int id = 1; foreach (dynamic item in jactions) { if (item.legendary_actions != null) { foreach (dynamic a in item.legendary_actions) { var actionId = LegendaryActionsList.Find(x => x.Name == (string)a.name).Id; monsterLegendaryActions.Add(new MonsterLegendaryAction { Id = id, MonsterId = (int)item.index, LegendaryActionId = actionId }); id++; } } } } return monsterLegendaryActions; } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity().HasData(SeedData.SeedUsers()); builder.Entity().HasData(SeedData.SeedCharacter()); builder.Entity().HasData(SeedData.SeedRace()); builder.Entity().HasData(SeedData.SeedClass()); builder.Entity().HasData(SeedData.SeedAlignment()); builder.Entity().HasData(SeedData.SeedBackground()); builder.Entity().HasData(SeedData.SeedBiography()); builder.Entity().HasData(SeedData.SeedStatistics()); builder.Entity().HasData(SeedWeapon()); builder.Entity().HasData(SeedArmor()); builder.Entity().HasData(SeedOtherEquipment()); builder.Entity().HasData(SeedData.SeedCharacterWeapon()); builder.Entity().HasData(SeedData.SeedCharacterArmor()); builder.Entity().HasData(SeedData.SeedCharacterOtherEquipment()); builder.Entity().HasData(SeedData.SeedCharisma()); builder.Entity().HasData(SeedData.SeedConstitution()); builder.Entity().HasData(SeedData.SeedDexterity()); builder.Entity().HasData(SeedData.SeedIntelligence()); builder.Entity().HasData(SeedData.SeedStrength()); builder.Entity().HasData(SeedData.SeedWisdom()); builder.Entity().HasData(SeedSpell()); builder.Entity().HasData(SeedData.SeedCharacterSpell()); builder.Entity().HasData(SeedData.SeedCharacterSpellSlot()); builder.Entity().HasData(SeedMonster()); builder.Entity().HasData(SeedSpecialAbilities()); builder.Entity().HasData(SeedLegendaryActions()); builder.Entity().HasData(SeedActions()); builder.Entity().HasData(SeedMonsterSpecialAbilities()); builder.Entity().HasData(SeedMonsterActions()); builder.Entity().HasData(SeedMonsterLegendaryActions()); builder.Entity().HasData(SeedData.SeedSchopkeepers()); builder.Entity().HasData(SeedData.SeedShopkeeperItems()); } } }