using System; using System.Collections.Generic; using CIG3.ExtensionMethods; using UnityEngine; public class IAPPack : IPack { private IAPPack(string name, IList items) { this._name = name; this._items = new List(items); } public static IAPPack Create(string packName, Dictionary values) { if (values == null || values.Count == 0) { UnityEngine.Debug.LogError(string.Format("Can't Create IAPPack with no values '{0}'.", packName)); return new IAPPack(string.Empty, new List()); } IList packItemNames = IAPPack.GetPackItemNames(values); List list = new List(); foreach (string text in packItemNames) { IPackEffect packEffect = IAPPack.CreateItemFromName(text, values); if (packEffect == null) { UnityEngine.Debug.LogError(string.Format("Can't create an item of type '{0}'.", text)); } else { list.Add(packEffect); } } return new IAPPack(packName, list); } public IList Items { get { return this._items.AsReadOnly(); } } public string Name { get { return this._name; } } public void ApplyEffects() { foreach (IPackEffect packEffect in this._items) { packEffect.ApplyEffect(); } } public override string ToString() { string text = string.Empty; foreach (IPackEffect packEffect in this._items) { text = text + packEffect.ToString() + "\n"; } return text.Trim(); } public PackItemType FindPackItem() where PackItemType : class, IPackEffect { foreach (IPackEffect packEffect in this.Items) { PackItemType packItemType = packEffect as PackItemType; if (packItemType != null) { return packItemType; } } return (PackItemType)((object)null); } private static IList GetPackItemNames(Dictionary values) { Dictionary dictionary = values.GetValue("PackItems", null) as Dictionary; if (dictionary == null) { return new List(); } return new List(dictionary.Keys); } private static IPackEffect CreateItemFromName(string name, Dictionary values) { Dictionary itemProperties = IAPPack.GetItemProperties(name, values); if (name == "FreeBuilding") { return new FreeBuildingItem(itemProperties); } if (name == "TimeBoost") { return new TimeBoostItem(itemProperties); } if (name == "LevelUp") { return new LevelUpItem(itemProperties); } if (name == "Cash") { return new CashItem(itemProperties); } if (name == "Gold") { return new GoldItem(itemProperties); } UnityEngine.Debug.LogError(string.Format("{0} is not a recognized item name. I will instantiate a DefaultItem-object.", name)); return new DefaultItem(); } private static Dictionary GetItemProperties(string itemName, Dictionary values) { if (values.ContainsKey("PackItems")) { Dictionary dictionary = (Dictionary)values["PackItems"]; if (dictionary != null && dictionary.ContainsKey(itemName)) { return dictionary[itemName] as Dictionary; } } UnityEngine.Debug.LogWarning(string.Format("Unable to find item named {0}", itemName)); return new Dictionary(); } private const string PackItemsKey = "PackItems"; private const string PackValueKey = "Value"; private List _items; private string _name; }