/// <summary> /// Serialize the given <see cref="Character"/> to JSON. /// </summary> /// <param name="character"> /// The <see cref="Character"/> to serialize. This cannot be null. /// </param> /// <returns> /// The character as JSON. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="character"/> cannot be null. /// </exception> public string Serialize(Character character) { if (character == null) { throw new ArgumentNullException("character"); } CharacterJsonData characterJsonData; // Serialize the base character characterJsonData = new CharacterJsonData { PrimaryOrigin = character.PrimaryOrigin, SecondaryOrigin = character.SecondaryOrigin, TrainedSkill = character.TrainedSkill, Name = character.Name, PlayerName = character.PlayerName }; foreach (ScoreType abilityScore in ScoreTypeHelper.AbilityScores) { characterJsonData.AbilityScores[abilityScore] = character[abilityScore].Total; } // Serialize levels foreach (Level level in character.Levels) { characterJsonData.Levels.Add(level); } // Serialize gear characterJsonData.MainHand = character.GetHeldItem<Item>(Hand.Main); characterJsonData.OffHand = character.GetHeldItem<Item>(Hand.Off); foreach (Slot slot in Enum.GetValues(typeof (Slot))) { if (character.GetEquippedItem<Item>(slot) != null) { characterJsonData.EquippedGear[slot] = character.GetEquippedItem<Item>(slot); } } foreach (Item item in character.Gear) { characterJsonData.OtherGear.Add(item); } // Include the ItemConverter here as there is no way to specify the // converter for collections as attributes. return JsonConvert.SerializeObject(characterJsonData, Formatting.Indented, new ItemConverter(), new LevelConverter()); }
/// <summary> /// Construct a dictionary of the items held or worn by the character. /// </summary> /// <param name="character"> /// The <see cref="Character"/> to get the gear for. /// </param> /// <returns> /// A <see cref="Dictionary<K,T>">Dictionary<Item, int></see> associating items to /// the number equipped. /// </returns> /// <exception cref="ArgumentNullException"> /// No argument can be null. /// </exception> /// <seealso cref="GetCarriedGear"/> public static Dictionary<Item, int> GetEquippedGear(Character character) { if (character == null) { throw new ArgumentNullException("character"); } Dictionary<Item, int> equipment; Slot currentSlot; equipment = new Dictionary<Item, int>(); if (character.GetHeldItem<Item>(Hand.Main) != null) { AddItem(character.GetHeldItem<Item>(Hand.Main), equipment); } if (character.GetHeldItem<Item>(Hand.Off) != null) { AddItem(character.GetHeldItem<Item>(Hand.Off), equipment); } foreach (int enumValue in Enum.GetValues(typeof(Slot))) { currentSlot = (Slot)enumValue; if (currentSlot != Slot.None && currentSlot != Slot.Hands) { if (character.GetEquippedItem<Item>(currentSlot) != null) { AddItem(character.GetEquippedItem<Item>(currentSlot), equipment); } } } return equipment; }