using System; using System.Collections.Generic; using System.Diagnostics; using CIG; using CIGEnums; using SUISS.Core; using UnityEngine; using UnityEngine.UI; public class IAPShopItemsView : ShopItemsView { //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event IAPShopItemsView.IAPShopItemClickedHandler IAPShopItemClickedEvent; private void FireIAPShopItemClickedEvent(CIG3StoreProduct storeProduct) { if (this.IAPShopItemClickedEvent != null) { this.IAPShopItemClickedEvent(storeProduct); } } //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event IAPShopItemsView.CraneShopItemClickedHandler CraneShopItemClickedEvent; private void FireCraneShopItemClickedEvent(decimal goldCost) { if (this.CraneShopItemClickedEvent != null) { this.CraneShopItemClickedEvent(goldCost); } } private void OnDestroy() { if (this._store != null) { this._store.InitializedEvent -= this.OnStoreInitialized; this._store = null; } } public void Init() { this._store = SingletonMonobehaviour.Instance.Store; IAPStoreError loadingError = this._store.LoadingError; if (loadingError == IAPStoreError.ProductsNotLoaded || loadingError == IAPStoreError.StoreInitializing) { this._store.InitializedEvent += this.OnStoreInitialized; } this.UpdateIAPItems(); } public override void Clear() { int count = this._shopItems.Count; for (int i = 0; i < count; i++) { UnityEngine.Object.Destroy(this._shopItems[i].gameObject); } this._shopItems.Clear(); } protected override void UpdateItems(ShopType category) { this._category = category; if (this._category == ShopType.Shop_cranes) { decimal currentCraneGoldCost = SingletonMonobehaviour.Instance.CurrentCraneGoldCost; if (currentCraneGoldCost > 0m) { CraneShopItem craneShopItem = UnityEngine.Object.Instantiate(this._craneShopItemPrefab, this._itemLayoutGroup.transform); craneShopItem.Init(SingletonMonobehaviour.Instance.CurrentCraneGoldCost, new Action(this.OnCraneShopItemClicked)); this._shopItems.Add(craneShopItem); } CIG3StoreProduct currentCraneIAP = SingletonMonobehaviour.Instance.CurrentCraneIAP; if (currentCraneIAP != null) { this.InitIAPShopItem(this._category, 0, currentCraneIAP, null); } } else { List list = this._sortedStoreProducts[this._category]; int count = list.Count; for (int i = count - 1; i >= 0; i--) { this.InitIAPShopItem(category, i, list[i], list[0]); } } } private void UpdateIAPItems() { foreach (KeyValuePair> keyValuePair in IAPShopItemsView.IAPShopMapping) { List list = new List(); list.AddRange(this._store.GetProducts(keyValuePair.Value)); list.Sort((CIG3StoreProduct a, CIG3StoreProduct b) => a.Price.CompareTo(b.Price)); this._sortedStoreProducts[keyValuePair.Key] = list; } } private void InitIAPShopItem(ShopType category, int index, CIG3StoreProduct storeProduct, CIG3StoreProduct cheapestStoreProduct) { IAPShopItem iapshopItem = UnityEngine.Object.Instantiate(this._iapShopItemPrefab, this._itemLayoutGroup.transform); iapshopItem.Init(storeProduct, category, index, new Action(this.OnIAPShopItemClicked), cheapestStoreProduct); this._shopItems.Add(iapshopItem); } private void OnStoreInitialized() { this._store.InitializedEvent -= this.OnStoreInitialized; this.UpdateIAPItems(); } private void OnIAPShopItemClicked(CIG3StoreProduct storeProduct) { this.FireIAPShopItemClickedEvent(storeProduct); } private void OnCraneShopItemClicked(decimal goldCost) { this.FireCraneShopItemClickedEvent(goldCost); } private static readonly Dictionary> IAPShopMapping = new Dictionary> { { ShopType.Shop_gold, (CIG3StoreProduct p) => p.Category == StoreProductCategory.Shop && p.Currencies.Contains("Gold") }, { ShopType.Shop_goldSale, (CIG3StoreProduct p) => p.Category == StoreProductCategory.ShopSale && p.Currencies.Contains("Gold") }, { ShopType.Shop_cash, (CIG3StoreProduct p) => p.Category == StoreProductCategory.Shop && p.Currencies.Contains("Cash") }, { ShopType.Shop_cashSale, (CIG3StoreProduct p) => p.Category == StoreProductCategory.ShopSale && p.Currencies.Contains("Cash") }, { ShopType.Shop_cranes, (CIG3StoreProduct p) => p.Category == StoreProductCategory.Cranes } }; [SerializeField] private IAPShopItem _iapShopItemPrefab; [SerializeField] private CraneShopItem _craneShopItemPrefab; [SerializeField] private LayoutGroup _itemLayoutGroup; private IAPStore _store; private Dictionary> _sortedStoreProducts = new Dictionary>(); private ShopType _category; private List _shopItems = new List(); public delegate void IAPShopItemClickedHandler(CIG3StoreProduct storeProduct); public delegate void CraneShopItemClickedHandler(decimal goldCost); }