From 151d63fc243fb0296a5e76e88ed9602499bb0f9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20G=C3=B3rzy=C5=84ski?= Date: Wed, 13 Jan 2021 21:14:13 +0100 Subject: [PATCH] SES-144 Add seed methods for monster seeding --- .../ApplicationDbContext.cs | 190 ++++++++++++++++++ .../SeedFromJsons.cs | 133 ++++++++++++ 2 files changed, 323 insertions(+) diff --git a/SessionCompanion/SessionCompanion.Database/ApplicationDbContext.cs b/SessionCompanion/SessionCompanion.Database/ApplicationDbContext.cs index c5359f5..38457bb 100644 --- a/SessionCompanion/SessionCompanion.Database/ApplicationDbContext.cs +++ b/SessionCompanion/SessionCompanion.Database/ApplicationDbContext.cs @@ -35,9 +35,20 @@ namespace SessionCompanion.Database 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 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"; @@ -98,6 +109,178 @@ namespace SessionCompanion.Database } 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); @@ -124,6 +307,13 @@ namespace SessionCompanion.Database 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()); } } } diff --git a/SessionCompanion/SessionCompanion.Database/SeedFromJsons.cs b/SessionCompanion/SessionCompanion.Database/SeedFromJsons.cs index b43cb4c..63d9ac3 100644 --- a/SessionCompanion/SessionCompanion.Database/SeedFromJsons.cs +++ b/SessionCompanion/SessionCompanion.Database/SeedFromJsons.cs @@ -320,5 +320,138 @@ namespace SessionCompanion.Database return spell; } + static public SpecialAbility SingleSpecialAbilitySeeder(dynamic item, int id) + { + SpecialAbility monsterSpecialAbility = new SpecialAbility(); + + monsterSpecialAbility.Id = id; + + monsterSpecialAbility.Name = item.name; + + if (item.damage_dice != null) + { + var dice = SplitDice((string)item.damage_dice); + monsterSpecialAbility.DamageDiceAmount = dice.Item1; + monsterSpecialAbility.DamageDice = dice.Item2; + } + + monsterSpecialAbility.Description = item.desc; + + return monsterSpecialAbility; + } + static public GameAction SingleActionSeeder(dynamic item, int id) + { + GameAction monsterAction = new GameAction(); + + monsterAction.Id = id; + + monsterAction.Name = item.name; + + monsterAction.AttackBonus = item.attack_bonus; + + monsterAction.DamageBonus = item.damage_bonus; + if (item.damage_dice != null) + { + var dice = SplitDice((string)item.damage_dice); + monsterAction.DamageDiceAmount = dice.Item1; + monsterAction.DamageDice = dice.Item2; + } + monsterAction.Description = item.desc; + return monsterAction; + } + static public LegendaryAction SingleLegendaryActionSeeder(dynamic item, int id) + { + LegendaryAction monsterLegendaryAction = new LegendaryAction(); + + monsterLegendaryAction.Id = id; + + monsterLegendaryAction.Name = item.name; + + monsterLegendaryAction.Description = item.desc; + + return monsterLegendaryAction; + } + static public Monster SingleMonsterSeeder(dynamic item) + { + Monster monster = new Monster(); + + monster.Id = item.index; + + monster.Name = item.name; + + monster.Size = item.size; + + monster.Type = item.type; + + monster.Subtype = item.subtype; + + monster.Alignment = item.alignment; + + monster.ArmorClass = item.armor_class; + + monster.HitPoints = item.hit_points; + + var dice = SplitDice((string)item.hit_dice); + monster.HitDiceAmount = dice.Item1; + monster.HitDiceType = dice.Item2; + + monster.WalkSpeed = item.speed.walk; + monster.FlySpeed = item.speed.fly; + monster.SwimSpeed = item.speed.swim; + + monster.Strength = item.strength; + monster.StrengthSave = item.strength_save; + + monster.Dexterity = item.dexterity; + monster.DexteritySave = item.dexterity_save; + + monster.Constitution = item.constitution; + monster.ConstitutionSave = item.constitution_save; + + monster.Wisdom = item.wisdom; + monster.WisdomSave = item.wisdom_save; + + monster.Charisma = item.charisma; + monster.CharismaSave = item.charsisma_save; + + monster.Intelligence = item.intelligence; + monster.IntelligenceSave = item.intelligence_save; + + foreach (dynamic dv in item.damage_vulnerabilities) + monster.MonsterDamageVulnerabilities += dv + ";"; + if (monster.MonsterDamageVulnerabilities != null) + monster.MonsterDamageVulnerabilities = monster.MonsterDamageVulnerabilities.Remove(monster.MonsterDamageVulnerabilities.Length - 1); + + foreach (dynamic dr in item.damage_resistances) + monster.MonsterDamageResistances += dr + ";"; + if (monster.MonsterDamageResistances != null) + monster.MonsterDamageResistances = monster.MonsterDamageResistances.Remove(monster.MonsterDamageResistances.Length - 1); + + foreach (dynamic di in item.damage_immunities) + monster.MonsterDamageImmunities += di + ";"; + if (monster.MonsterDamageImmunities != null) + monster.MonsterDamageImmunities = monster.MonsterDamageImmunities.Remove(monster.MonsterDamageImmunities.Length - 1); + + foreach (dynamic ci in item.condition_immunities) + monster.MonsterConditionImmunities += ci + ";"; + if (monster.MonsterConditionImmunities != null) + monster.MonsterConditionImmunities = monster.MonsterConditionImmunities.Remove(monster.MonsterConditionImmunities.Length - 1); + + monster.MonsterSenses = item.senses; + + monster.MonsterLanguages = item.languages; + + monster.ChallengeRating = item.challenge_rating; + + return monster; + } + static public Tuple SplitDice(string dice) + { + int stop = dice.IndexOf('d'); + var amount = int.Parse(dice.Substring(0, stop)); + stop++; + var value = int.Parse(dice.Substring(stop, dice.Length - stop)); + return new Tuple(amount, value); + } } }