SES-120 Added scripts to populate armors and weapons

This commit is contained in:
Karol Górzyński 2021-01-02 17:34:45 +01:00
parent 809362175d
commit de3b7e3315
3 changed files with 335 additions and 0 deletions

View File

@ -1,7 +1,9 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using SessionCompanion.Database.Tables; using SessionCompanion.Database.Tables;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -33,6 +35,53 @@ namespace SessionCompanion.Database
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
protected IEnumerable<Weapon> SeedWeapon()
{
const string file = "../SessionCompanion.Database/JsonData/weapons.json";
List<Weapon> weapons = new List<Weapon>();
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<Armor> SeedArmor()
{
const string file = "../SessionCompanion.Database/JsonData/armors.json";
List<Armor> armors = new List<Armor>();
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<OtherEquipment> SeedOtherEquipment()
{
const string file = "../SessionCompanion.Database/JsonData/otherEquipments.json";
List<OtherEquipment> otherEquipment = new List<OtherEquipment>();
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) protected override void OnModelCreating(ModelBuilder builder)
{ {
base.OnModelCreating(builder); base.OnModelCreating(builder);
@ -44,6 +93,12 @@ namespace SessionCompanion.Database
builder.Entity<Background>().HasData(SeedData.SeedBackground()); builder.Entity<Background>().HasData(SeedData.SeedBackground());
builder.Entity<Biography>().HasData(SeedData.SeedBiography()); builder.Entity<Biography>().HasData(SeedData.SeedBiography());
builder.Entity<Statistics>().HasData(SeedData.SeedStatistics()); builder.Entity<Statistics>().HasData(SeedData.SeedStatistics());
builder.Entity<Weapon>().HasData(SeedWeapon());
builder.Entity<Armor>().HasData(SeedArmor());
builder.Entity<OtherEquipment>().HasData(SeedOtherEquipment());
builder.Entity<CharacterWeapon>().HasData(SeedData.SeedCharacterWeapon());
builder.Entity<CharacterArmor>().HasData(SeedData.SeedCharacterArmor());
builder.Entity<CharacterOtherEquipment>().HasData(SeedData.SeedCharacterOtherEquipment());
} }
} }
} }

View File

@ -232,5 +232,161 @@ namespace SessionCompanion.Database
}; };
return characters; return characters;
} }
static public List<CharacterWeapon> SeedCharacterWeapon()
{
List<CharacterWeapon> characterWeapons = new List<CharacterWeapon>
{
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<CharacterArmor> SeedCharacterArmor()
{
List<CharacterArmor> characterArmors = new List<CharacterArmor>
{
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<CharacterOtherEquipment> SeedCharacterOtherEquipment()
{
List<CharacterOtherEquipment> characterOtherEquipment = new List<CharacterOtherEquipment>
{
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;
}
} }
} }

View File

@ -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;
}
}
}