|
- 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<CIGStoreManager>.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<CIGBuilderManager>.Instance.CurrentCraneGoldCost;
- if (currentCraneGoldCost > 0m)
- {
- CraneShopItem craneShopItem = UnityEngine.Object.Instantiate<CraneShopItem>(this._craneShopItemPrefab, this._itemLayoutGroup.transform);
- craneShopItem.Init(SingletonMonobehaviour<CIGBuilderManager>.Instance.CurrentCraneGoldCost, new Action<decimal>(this.OnCraneShopItemClicked));
- this._shopItems.Add(craneShopItem);
- }
- CIG3StoreProduct currentCraneIAP = SingletonMonobehaviour<CIGBuilderManager>.Instance.CurrentCraneIAP;
- if (currentCraneIAP != null)
- {
- this.InitIAPShopItem(this._category, 0, currentCraneIAP, null);
- }
- }
- else
- {
- List<CIG3StoreProduct> 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<ShopType, Predicate<CIG3StoreProduct>> keyValuePair in IAPShopItemsView.IAPShopMapping)
- {
- List<CIG3StoreProduct> list = new List<CIG3StoreProduct>();
- 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<IAPShopItem>(this._iapShopItemPrefab, this._itemLayoutGroup.transform);
- iapshopItem.Init(storeProduct, category, index, new Action<CIG3StoreProduct>(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<ShopType, Predicate<CIG3StoreProduct>> IAPShopMapping = new Dictionary<ShopType, Predicate<CIG3StoreProduct>>
- {
- {
- 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<CIG3StoreProduct> _store;
-
- private Dictionary<ShopType, List<CIG3StoreProduct>> _sortedStoreProducts = new Dictionary<ShopType, List<CIG3StoreProduct>>();
-
- private ShopType _category;
-
- private List<ShopItem> _shopItems = new List<ShopItem>();
-
- public delegate void IAPShopItemClickedHandler(CIG3StoreProduct storeProduct);
-
- public delegate void CraneShopItemClickedHandler(decimal goldCost);
- }
|