diff --git a/SessionCompanion/SessionCompanion.Database/ApplicationDbContext.cs b/SessionCompanion/SessionCompanion.Database/ApplicationDbContext.cs index 11bf6e7..a06705c 100644 --- a/SessionCompanion/SessionCompanion.Database/ApplicationDbContext.cs +++ b/SessionCompanion/SessionCompanion.Database/ApplicationDbContext.cs @@ -1,7 +1,9 @@ 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; @@ -33,6 +35,53 @@ namespace SessionCompanion.Database public ApplicationDbContext(DbContextOptions options) : base(options) { } + 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 override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); @@ -44,6 +93,12 @@ namespace SessionCompanion.Database 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()); } } } diff --git a/SessionCompanion/SessionCompanion.Database/SeedData.cs b/SessionCompanion/SessionCompanion.Database/SeedData.cs index 38fda48..2dbef97 100644 --- a/SessionCompanion/SessionCompanion.Database/SeedData.cs +++ b/SessionCompanion/SessionCompanion.Database/SeedData.cs @@ -232,5 +232,161 @@ namespace SessionCompanion.Database }; return characters; } + + static public List SeedCharacterWeapon() + { + List characterWeapons = new List + { + new CharacterWeapon + { + Id = 1, + CharacterId = 1, + WeaponId = 1, + InUse = false, + HoldInLeftHand = false, + HoldInRightHand = false + }, + new CharacterWeapon + { + Id = 2, + CharacterId = 1, + WeaponId = 4, + InUse = true, + HoldInLeftHand = true, + HoldInRightHand = false + }, + new CharacterWeapon + { + Id = 3, + CharacterId = 2, + WeaponId = 2, + InUse = true, + HoldInLeftHand = false, + HoldInRightHand = true + }, + new CharacterWeapon + { + Id = 4, + CharacterId = 2, + WeaponId = 8, + InUse = false, + HoldInLeftHand = false, + HoldInRightHand = false + }, + new CharacterWeapon + { + Id = 5, + CharacterId = 3, + WeaponId = 9, + InUse = false, + HoldInLeftHand = false, + HoldInRightHand = false + }, + new CharacterWeapon + { + Id = 7, + CharacterId = 3, + WeaponId = 7, + InUse = true, + HoldInLeftHand = false, + HoldInRightHand = true + } + }; + return characterWeapons; + } + + static public List SeedCharacterArmor() + { + List characterArmors = new List + { + new CharacterArmor + { + Id = 1, + CharacterId = 1, + ArmorId = 1, + InUse = true, + }, + new CharacterArmor + { + Id = 2, + CharacterId = 1, + ArmorId = 3, + InUse = false, + }, + new CharacterArmor + { + Id = 3, + CharacterId = 2, + ArmorId = 2, + InUse = true, + }, + new CharacterArmor + { + Id = 4, + CharacterId = 2, + ArmorId = 5, + InUse = false, + }, + new CharacterArmor + { + Id = 5, + CharacterId = 3, + ArmorId = 6, + InUse = true, + }, + new CharacterArmor + { + Id = 6, + CharacterId = 3, + ArmorId = 2, + InUse = false, + } + }; + return characterArmors; + } + + static public List SeedCharacterOtherEquipment() + { + List characterOtherEquipment = new List + { + new CharacterOtherEquipment + { + Id = 1, + CharacterId = 1, + OtherEquipmentId = 1 + }, + new CharacterOtherEquipment + { + Id = 2, + CharacterId = 1, + OtherEquipmentId = 3 + }, + new CharacterOtherEquipment + { + Id = 3, + CharacterId = 2, + OtherEquipmentId = 2 + }, + new CharacterOtherEquipment + { + Id = 4, + CharacterId = 2, + OtherEquipmentId = 5 + }, + new CharacterOtherEquipment + { + Id = 5, + CharacterId = 3, + OtherEquipmentId = 6 + }, + new CharacterOtherEquipment + { + Id = 6, + CharacterId = 3, + OtherEquipmentId = 2 + } + }; + return characterOtherEquipment; + } } } diff --git a/SessionCompanion/SessionCompanion.Database/SeedFromJsons.cs b/SessionCompanion/SessionCompanion.Database/SeedFromJsons.cs new file mode 100644 index 0000000..162e969 --- /dev/null +++ b/SessionCompanion/SessionCompanion.Database/SeedFromJsons.cs @@ -0,0 +1,124 @@ +using SessionCompanion.Database.Tables; +using SessionCompanion.ViewModels.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SessionCompanion.Database +{ + public static class SeedFromJsons + { + static public Weapon SingleWeaponSeeder(dynamic item) + { + Weapon weapon = new Weapon(); + + // Id + weapon.Id = item.index; + + // Name + weapon.Name = item.name; + + // Weapon type + weapon.WeaponType = item.category_range; + + // Cost + weapon.Cost = item.cost.quantity; + + // CurrnecyType + switch ((string)item.cost.unit) + { + case "sp": + weapon.CurrencyType = CurrencyType.sp; + break; + case "gp": + weapon.CurrencyType = CurrencyType.gp; + break; + } + + // DiceCount + weapon.DiceCount = item.damage.dice_count; + + // DiceValue + weapon.DiceValue = item.damage.dice_value; + + // RangeMeele + weapon.RangeMeele = item.range.normal; + + // RangeLong + weapon.RangeLong = item.range._long; + + // RangeThrow + if (item.throw_range != null) + { + weapon.RangeThrowNormal = item.throw_range.normal; + weapon.RangeThrowLong = item.throw_range._long; + } + + // Weight + weapon.Weight = item.weight; + + + // Twohand + if (item.two_hand_damage != null) + { + weapon.TwoHandDiceCount = item.two_hand_damage.dice_count; + weapon.TwoHandDiceValue = item.two_hand_damage.dice_value; + } + if (item.special != null) + weapon.Description = item.special[0]; + return weapon; + } + static public Armor SingleArmorSeeder(dynamic item, int id) + { + Armor armor = new Armor(); + + armor.Id = id; + armor.Name = item.name; + armor.Category = item.armor_category; + armor.ArmorClassBase = item.armor_class["base"]; + armor.HaveDexterityBonus = item.armor_class["dex_bonus"]; + armor.MinimumStrength = item.str_minimum; + armor.HaveStealthDisadvantage = item.stealth_disadvantage; + armor.Weight = item.weight; + armor.Cost = item.cost.quantity; + switch ((string)item.cost.unit) + { + case "sp": + armor.CurrencyType = CurrencyType.sp; + break; + case "gp": + armor.CurrencyType = CurrencyType.gp; + break; + } + + return armor; + } + static public OtherEquipment SingleOtherEquipmentSeeder(dynamic item, int id) + { + OtherEquipment otherEq = new OtherEquipment(); + + otherEq.Id = id; + otherEq.Name = item.name; + if (item.desc != null) + foreach (dynamic description in item.desc) + otherEq.Description += " " + description; + else + otherEq.Description = string.Empty; + + otherEq.Cost = item.cost.quantity; + switch ((string)item.cost.unit) + { + case "sp": + otherEq.CurrencyType = CurrencyType.sp; + break; + case "gp": + otherEq.CurrencyType = CurrencyType.gp; + break; + } + + return otherEq; + } + } +}