From 6f7b71ad706a7d9def616383aed49a591efb3e70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20G=C3=B3rzy=C5=84ski?= Date: Sun, 3 Jan 2021 16:28:13 +0100 Subject: [PATCH] Modify scripts to seed data --- .../ApplicationDbContext.cs | 16 ++ .../SessionCompanion.Database/SeedData.cs | 143 +++++++++++++ .../SeedFromJsons.cs | 200 ++++++++++++++++++ 3 files changed, 359 insertions(+) diff --git a/SessionCompanion/SessionCompanion.Database/ApplicationDbContext.cs b/SessionCompanion/SessionCompanion.Database/ApplicationDbContext.cs index a8e842d..4a9eaec 100644 --- a/SessionCompanion/SessionCompanion.Database/ApplicationDbContext.cs +++ b/SessionCompanion/SessionCompanion.Database/ApplicationDbContext.cs @@ -85,6 +85,19 @@ namespace SessionCompanion.Database } 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 override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); @@ -102,6 +115,9 @@ namespace SessionCompanion.Database builder.Entity().HasData(SeedData.SeedCharacterWeapon()); builder.Entity().HasData(SeedData.SeedCharacterArmor()); builder.Entity().HasData(SeedData.SeedCharacterOtherEquipment()); + builder.Entity().HasData(SeedSpell()); + builder.Entity().HasData(SeedData.SeedCharacterSpell()); + builder.Entity().HasData(SeedData.SeedCharacterSpellSlot()); } } } diff --git a/SessionCompanion/SessionCompanion.Database/SeedData.cs b/SessionCompanion/SessionCompanion.Database/SeedData.cs index 2dbef97..07542db 100644 --- a/SessionCompanion/SessionCompanion.Database/SeedData.cs +++ b/SessionCompanion/SessionCompanion.Database/SeedData.cs @@ -388,5 +388,148 @@ namespace SessionCompanion.Database }; return characterOtherEquipment; } + static public List SeedCharacterSpell() + { + List characterSpells = new List + { + new CharacterSpell + { + Id = 1, + CharacterId = 1, + SpellId = 2 + }, + new CharacterSpell + { + Id = 2, + CharacterId = 1, + SpellId = 55 + }, + new CharacterSpell + { + Id = 3, + CharacterId = 1, + SpellId = 17 + }, + new CharacterSpell + { + Id = 4, + CharacterId = 1, + SpellId = 78 + }, + new CharacterSpell + { + Id = 5, + CharacterId = 2, + SpellId = 43 + }, + new CharacterSpell + { + Id = 6, + CharacterId = 2, + SpellId = 154 + }, + new CharacterSpell + { + Id = 7, + CharacterId = 2, + SpellId = 123 + }, + new CharacterSpell + { + Id = 8, + CharacterId = 2, + SpellId = 67 + }, + new CharacterSpell + { + Id = 9, + CharacterId = 3, + SpellId = 90 + }, + new CharacterSpell + { + Id = 10, + CharacterId = 2, + SpellId = 44 + }, + }; + return characterSpells; + } + static public List SeedCharacterSpellSlot() + { + List characterSpellSlots = new List + { + new CharacterSpellSlots + { + Id = 1, + CharacterId = 1, + FirstLevelSlots = 4, + FirstLevelSlotsUsed = 0, + SecondLevelSlots = 5, + SecondLevelSlotsUsed = 4, + ThirdLevelSlots = 2, + ThirdLevelSlotsUsed = 1, + FourthLevelSlots = 3, + FourthLevelSlotsUsed = 1, + FifthLevelSlots = 1, + FifthLevelSlotsUsed = 0, + SixthLevelSlots = 0, + SixthLevelSlotsUsed = 0, + SeventhLevelSlots = 0, + SeventhLevelSlotsUsed = 0, + EightLevelSlots = 0, + EightLevelSlotsUsed = 0, + NinthLevelSlots = 0, + NinthLevelSlotsUsed = 0 + }, + new CharacterSpellSlots + { + Id = 2, + CharacterId = 2, + FirstLevelSlots = 6, + FirstLevelSlotsUsed = 2, + SecondLevelSlots = 3, + SecondLevelSlotsUsed = 2, + ThirdLevelSlots = 3, + ThirdLevelSlotsUsed = 1, + FourthLevelSlots = 2, + FourthLevelSlotsUsed = 2, + FifthLevelSlots = 1, + FifthLevelSlotsUsed = 1, + SixthLevelSlots = 0, + SixthLevelSlotsUsed = 0, + SeventhLevelSlots = 0, + SeventhLevelSlotsUsed = 0, + EightLevelSlots = 0, + EightLevelSlotsUsed = 0, + NinthLevelSlots = 0, + NinthLevelSlotsUsed = 0 + }, + new CharacterSpellSlots + { + Id = 3, + CharacterId = 3, + FirstLevelSlots = 2, + FirstLevelSlotsUsed = 0, + SecondLevelSlots = 3, + SecondLevelSlotsUsed = 2, + ThirdLevelSlots = 2, + ThirdLevelSlotsUsed = 0, + FourthLevelSlots = 1, + FourthLevelSlotsUsed = 1, + FifthLevelSlots = 1, + FifthLevelSlotsUsed = 0, + SixthLevelSlots = 0, + SixthLevelSlotsUsed = 0, + SeventhLevelSlots = 0, + SeventhLevelSlotsUsed = 0, + EightLevelSlots = 0, + EightLevelSlotsUsed = 0, + NinthLevelSlots = 0, + NinthLevelSlotsUsed = 0 + } + }; + return characterSpellSlots; + } } } diff --git a/SessionCompanion/SessionCompanion.Database/SeedFromJsons.cs b/SessionCompanion/SessionCompanion.Database/SeedFromJsons.cs index 162e969..b43cb4c 100644 --- a/SessionCompanion/SessionCompanion.Database/SeedFromJsons.cs +++ b/SessionCompanion/SessionCompanion.Database/SeedFromJsons.cs @@ -120,5 +120,205 @@ namespace SessionCompanion.Database return otherEq; } + static public Spell SingleSpellSeeder(dynamic item) + { + Spell spell = new Spell(); + + // Id + spell.Id = item.index; + + // Name + spell.Name = item.name; + + // Description + foreach (dynamic des in item.desc) + spell.Description += des + ";"; + if (spell.Description != null) + spell.Description = spell.Description.Remove(spell.Description.Length - 1); + + // HigherLevel + if (item.higher_level != null) + { + foreach (dynamic hl in item.higher_level) + spell.HigherLevel += (string)hl + ";"; + if (spell.HigherLevel != null) + spell.HigherLevel = spell.HigherLevel.Remove(spell.HigherLevel.Length - 1); + } + + // Range + switch ((string)item.range) + { + case "90 feet": + spell.Range = SpellsEnums.Ranges.Feet_90; + break; + case "60 feet": + spell.Range = SpellsEnums.Ranges.Feet_60; + break; + case "30 feet": + spell.Range = SpellsEnums.Ranges.Feet_30; + break; + case "10 feet": + spell.Range = SpellsEnums.Ranges.Feet_10; + break; + case "120 feet": + spell.Range = SpellsEnums.Ranges.Feet_120; + break; + case "150 feet": + spell.Range = SpellsEnums.Ranges.Feet_150; + break; + case "1 mile": + spell.Range = SpellsEnums.Ranges.Miles_1; + break; + case "300 feet": + spell.Range = SpellsEnums.Ranges.Feet_300; + break; + case "500 feet": + spell.Range = SpellsEnums.Ranges.Feet_500; + break; + case "100 feet": + spell.Range = SpellsEnums.Ranges.Feet_100; + break; + case "500 miles": + spell.Range = SpellsEnums.Ranges.Miles_500; + break; + case "5 feet": + spell.Range = SpellsEnums.Ranges.Feet_5; + break; + default: + if (Enum.TryParse((string)item.range, true, out SpellsEnums.Ranges RangeType)) + if (Enum.IsDefined(typeof(SpellsEnums.Ranges), RangeType)) + spell.Range = RangeType; + break; + } + + // Components + foreach (dynamic component in item.components) + { + if (Enum.TryParse((string)component, true, out SpellsEnums.Components ComponentType)) + if (Enum.IsDefined(typeof(SpellsEnums.Components), ComponentType)) + spell.Components += ComponentType + ";"; + } + if (spell.Components != null) + spell.Components = spell.Components.Remove(spell.Components.Length - 1); + + if (item.material != null) + spell.Material = item.material; + + if ((string)item.ritual == "no") + spell.Ritual = false; + else + spell.Ritual = true; + + // Duration + switch ((string)item.duration) + { + case "8 hours": + spell.Duration = SpellsEnums.Durations.Hours_8; + break; + case "1 hour": + spell.Duration = SpellsEnums.Durations.Hours_1; + break; + case "24 hours": + spell.Duration = SpellsEnums.Durations.Hours_24; + break; + case "1 minute": + spell.Duration = SpellsEnums.Durations.Minutes_1; + break; + case "10 days": + spell.Duration = SpellsEnums.Durations.Days_10; + break; + case "10 minutes": + spell.Duration = SpellsEnums.Durations.Minutes_10; + break; + case "1 round": + spell.Duration = SpellsEnums.Durations.Round_1; + break; + case "7 days": + spell.Duration = SpellsEnums.Durations.Days_7; + break; + case "30 days": + spell.Duration = SpellsEnums.Durations.Days_30; + break; + case "2 hours": + spell.Duration = SpellsEnums.Durations.Hours_2; + break; + case "Until dispelled": + spell.Duration = SpellsEnums.Durations.Until_dispelled; + break; + default: + if (Enum.TryParse((string)item.duration, true, out SpellsEnums.Durations DurationType)) + if (Enum.IsDefined(typeof(SpellsEnums.Durations), DurationType)) + spell.Duration = DurationType; + break; + } + + // Concentration + if ((string)item.concentration == "no") + spell.Concentration = false; + else + spell.Concentration = true; + + // CastingTime + switch ((string)item.casting_time) + { + case "1 action": + spell.CastingTime = SpellsEnums.CastingTimes.Action_1; + break; + case "1 minute": + spell.CastingTime = SpellsEnums.CastingTimes.Minutes_1; + break; + case "1 hour": + spell.CastingTime = SpellsEnums.CastingTimes.Hours_1; + break; + case "8 hours": + spell.CastingTime = SpellsEnums.CastingTimes.Hours_8; + break; + case "1 bonus action": + spell.CastingTime = SpellsEnums.CastingTimes.Bonus_Action_1; + break; + case "10 minutes": + spell.CastingTime = SpellsEnums.CastingTimes.Minutes_10; + break; + case "1 reaction": + spell.CastingTime = SpellsEnums.CastingTimes.Reaction_1; + break; + case "24 hours": + spell.CastingTime = SpellsEnums.CastingTimes.Hours_24; + break; + case "12 hours": + spell.CastingTime = SpellsEnums.CastingTimes.Hours_12; + break; + } + + // Level + spell.Level = (int)item.level; + + // School + if (Enum.TryParse((string)item.school.name, true, out SpellsEnums.Schools SchoolType)) + if (Enum.IsDefined(typeof(SpellsEnums.Schools), SchoolType)) + spell.School = SchoolType; + + // Classes + foreach (dynamic c in item.classes) + { + if (Enum.TryParse((string)c.name, true, out SpellsEnums.Classes ClassType)) + if (Enum.IsDefined(typeof(SpellsEnums.Classes), ClassType)) + spell.Classes += ClassType + ";"; + } + if (spell.Classes != null) + spell.Classes = spell.Classes.Remove(spell.Classes.Length - 1); + + // Subclasses + foreach (dynamic s in item.subclasses) + { + if (Enum.TryParse((string)s.name, true, out SpellsEnums.Subclass SubClassType)) + if (Enum.IsDefined(typeof(SpellsEnums.Subclass), SubClassType)) + spell.Subclasses += SubClassType + ";"; + } + if (spell.Subclasses != null) + spell.Subclasses = spell.Subclasses.Remove(spell.Subclasses.Length - 1); + + return spell; + } } }