public static Weapon Clone(Weapon clonesource) { string jsonwrite = JsonConvert.SerializeObject(clonesource); Weapon clone = JsonConvert.DeserializeObject<Weapon>(jsonwrite); return clone; }
private void Weapon_Save_Click(object sender, RoutedEventArgs e) { string name = Weapon_Name_Entry.Text; if(name == "") { MessageBox.Show("You must enter a name for the weapon"); return; } int Dice = 0; if(!Int32.TryParse(Weapon_Dice_Entry.Text,out Dice)) { MessageBox.Show("You must enter a dice for the weapon"); return; } bool twohanded = false; if(Weapon_TwoHandedCheckBox.IsChecked != null) { twohanded = (bool)Weapon_TwoHandedCheckBox.IsChecked; } bool ranged = false; if (Weapon_RangedCheckBox.IsChecked != null) { ranged = (bool)Weapon_RangedCheckBox.IsChecked; } int rangedrange = 0; if(!Int32.TryParse(Weapon_RangedWeaponRange_Entry.Text,out rangedrange)) { MessageBox.Show("You must enter a Range for the Rangeweapon for the weapon"); return; } int bonusdamage = 0; if(!Int32.TryParse(Weapon_BonusDmg_Entry.Text,out bonusdamage)) { MessageBox.Show("You must enter a number bonus damage for the weapon"); return; } IEnumerable<Weapon> WeaponQuery = Weapons.Where(x => x.name == name); Weapon writeWeapon = null; if(WeaponQuery.Count() > 0) { writeWeapon = WeaponQuery.First(); writeWeapon.bonusdamage = bonusdamage; writeWeapon.Dice = Dice; writeWeapon.attackrange = rangedrange; writeWeapon.range = ranged; writeWeapon.twohanded = twohanded; } else { writeWeapon = new Weapon(name, Dice, bonusdamage, twohanded,ranged, rangedrange); } if (Weapon_WeaponsList.SelectedIndex != -1) { MessageBoxResult result = MessageBox.Show("You have selected an item do you want to override(yes),create new (no),or cancel","Attention",MessageBoxButton.YesNoCancel); if(result == MessageBoxResult.Yes) { } if (result == MessageBoxResult.No) { Weapon_WeaponsList.SelectedIndex = -1; } if (result == MessageBoxResult.Cancel) { return; } } if (WeaponQuery.Count() == 0) { Weapons.Add(writeWeapon); } Weapon_WeaponsList.Items.Refresh(); string jsonwrite = JsonConvert.SerializeObject(writeWeapon); WriteJsonStringToFile(jsonwrite, "weapon", name); //Check Characters that have this weapon to update data foreach(Character character in Characters) { int weaponoccurance = character.Weapons.Where(x => x.name == writeWeapon.name).Count(); if (weaponoccurance > 0) { character.Weapons.RemoveAll(x => x.name == writeWeapon.name); for(int i = 0; i<weaponoccurance;i++) { character.Weapons.Add(writeWeapon); UpdateUIElementsItems(); } } jsonwrite = JsonConvert.SerializeObject(character); WriteJsonStringToFile(jsonwrite, "character", character.Name); } }