private void AddSkillsToJson(ref NpcCharacter character) { // Set the character's CR with the value from the UI, since we'll need that later to calculate a proficiency bonus. character.ChallengeRating = ChallengeRatingComboBox.SelectedItem.ToString(); // For all of the possible skills... foreach (object skillCb in SkillsGroupBox.Controls) { // If the skill is proficient... if (skillCb is CheckBox box && box.Checked) { // For all of the skill properties of the character object... foreach (PropertyInfo propertyInfo in character.Skills.GetType().GetProperties()) { // If we found the appropriate character object's skill property... IEnumerable <Attribute> attributes = propertyInfo.GetCustomAttributes(); if (box.Text.Equals(((DescriptionAttribute)attributes.ElementAt(0)).Description)) { // Set the character object's skill to the appropriate bonus. propertyInfo.SetValue(character.Skills, character.GetCrProficiencyBonus()); } } } } }
/// <summary> /// Sets the character's spells by aggregating the information from the UI. /// </summary> /// <param name="character">A reference to the character whose spells are being set.</param> private void SetCharacterSpells(ref NpcCharacter character) { string cantrips = string.Empty; foreach (RealSpell spell in CantripsListBox.Items) { cantrips += spell.Name + ","; } cantrips = cantrips.TrimEnd(','); string level1 = string.Empty; foreach (RealSpell spell in Level1ListBox.Items) { level1 += spell.Name + ","; } level1 = level1.TrimEnd(','); string level2 = string.Empty; foreach (RealSpell spell in Level2ListBox.Items) { level2 += spell.Name + ","; } level2 = level2.TrimEnd(','); string level3 = string.Empty; foreach (RealSpell spell in Level3ListBox.Items) { level3 += spell.Name + ","; } level3 = level3.TrimEnd(','); string level4 = string.Empty; foreach (RealSpell spell in Level4ListBox.Items) { level4 += spell.Name + ","; } level4 = level4.TrimEnd(','); string level5 = string.Empty; foreach (RealSpell spell in Level5ListBox.Items) { level5 += spell.Name + ","; } level5 = level5.TrimEnd(','); string level6 = string.Empty; foreach (RealSpell spell in Level6ListBox.Items) { level6 += spell.Name + ","; } level6 = level6.TrimEnd(','); string level7 = string.Empty; foreach (RealSpell spell in Level7ListBox.Items) { level7 += spell.Name + ","; } level7 = level7.TrimEnd(','); string level8 = string.Empty; foreach (RealSpell spell in Level8ListBox.Items) { level8 += spell.Name + ","; } level8 = level8.TrimEnd(','); string level9 = string.Empty; foreach (RealSpell spell in Level9ListBox.Items) { level9 += spell.Name + ","; } level9 = level9.TrimEnd(','); character.Spells = new Spells { Cantrips = cantrips, Level1 = level1, Level2 = level2, Level3 = level3, Level4 = level4, Level5 = level5, Level6 = level6, Level7 = level7, Level8 = level8, Level9 = level9, Caster = CasterTextBox.Text }; }
private void GenerateJsonBtn_Click(object sender, EventArgs e) { try { var character = new NpcCharacter() { AC = (int)ArmorClassNud.Value, Armor = ArmorComboBox.SelectedItem.ToString(), Actions = ActionsTextBox.Text, Algn = AlignmentComboBox.SelectedItem.ToString(), Personality = PersonalityTextBox.Text, Faction = FactionTextBox.Text, Faction_Leader = FactionLeaderCheckBox.Checked, First_Name = FirstNameTextBox.Text, First_Meeting = FirstMeetingTextBox.Text, Racial_Features = RacialFeaturesTextBox.Text, Size = SizeTextBox.Text, Hp = HitPointsTextBox.Text, Img = ImageTextBox.Text, Last_Name = LastNameTextBox.Text, Location = LocationTextBox.Text, Race = RaceComboBox.SelectedItem.ToString(), Appearance = AppearanceTextBox.Text, Speed = SpeedTextBox.Text, History = HistoryTextBox.Text, Stats = new Abilities { Cha = CharismaNud.Value.ToString(CultureInfo.InvariantCulture), Con = ConstitutionNud.Value.ToString(CultureInfo.InvariantCulture), Dex = DexterityNud.Value.ToString(CultureInfo.InvariantCulture), Int = IntellectNud.Value.ToString(CultureInfo.InvariantCulture), Str = StrengthNud.Value.ToString(CultureInfo.InvariantCulture), Wis = WisdomNud.Value.ToString(CultureInfo.InvariantCulture) }, Title = TitleTextBox.Text, Xp_Val = (int)XpNud.Value, Class = ClassTextBox.Text, Accolades = AccoladesTextBox.Text, Motive = MotiveTextBox.Text, Type = TypeTextBox.Text, Languages = LanguagesTextBox.Text }; // Add comma-separated spells names. SetCharacterSpells(ref character); // Add proficient skills. character.Skills = new Skills(); AddSkillsToJson(ref character); // Serialize the character into JSON. JsonTextBox.Text = JsonConvert.SerializeObject(character); // Save JSON to clipboard for easy pasting. Clipboard.SetText(JsonTextBox.Text); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }