You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

256 lines
11 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using CIG;
  4. using CIG.Extensions;
  5. using CIG.Translation;
  6. using CIG3.ExtensionMethods;
  7. using CIGEnums;
  8. using SUISS.Core;
  9. using SUISSEngine;
  10. public class ShopPopupState : PopupBaseState
  11. {
  12. public override void Enter(State oldState)
  13. {
  14. base.Enter(oldState);
  15. if (SingletonMonobehaviour<CIGPurchasedBuildingsManager>.IsAvailable && SingletonMonobehaviour<GameObjectManager>.IsAvailable)
  16. {
  17. GameObjectManager instance = SingletonMonobehaviour<GameObjectManager>.Instance;
  18. Dictionary<string, int> unconsumedBuildings = SingletonMonobehaviour<CIGPurchasedBuildingsManager>.Instance.GetUnconsumedBuildings();
  19. Dictionary<ShopMenuTabs, int> dictionary = new Dictionary<ShopMenuTabs, int>();
  20. foreach (KeyValuePair<string, int> keyValuePair in unconsumedBuildings)
  21. {
  22. if (keyValuePair.Value > 0)
  23. {
  24. CIGBuilding building = instance.GetBuilding(keyValuePair.Key);
  25. if (building != null)
  26. {
  27. Type type = building.GetType();
  28. if (ShopPopupState.BuildingTypesWithBadgesToShopMenuTabMapping.ContainsKey(type))
  29. {
  30. ShopMenuTabs shopMenuTabs = ShopPopupState.BuildingTypesWithBadgesToShopMenuTabMapping[type];
  31. if (!dictionary.ContainsKey(shopMenuTabs))
  32. {
  33. dictionary[shopMenuTabs] = 0;
  34. }
  35. (dictionary)[shopMenuTabs] = dictionary[shopMenuTabs] + keyValuePair.Value;
  36. }
  37. }
  38. }
  39. }
  40. foreach (KeyValuePair<Type, ShopMenuTabs> keyValuePair2 in ShopPopupState.BuildingTypesWithBadgesToShopMenuTabMapping)
  41. {
  42. ShopMenuTabs value = keyValuePair2.Value;
  43. ((ShopPopupView)this.View).UpdateBadge(value, (!dictionary.ContainsKey(value)) ? 0 : dictionary[value]);
  44. }
  45. }
  46. ((ShopPopupView)this.View).UpdateBadge(ShopMenuTabs.Cash, SingletonMonobehaviour<CIGSaleManager>.Instance.CurrentSale.Cash);
  47. ((ShopPopupView)this.View).UpdateBadge(ShopMenuTabs.Gold, SingletonMonobehaviour<CIGSaleManager>.Instance.CurrentSale.Gold);
  48. }
  49. public void OpenPopup(ShopMenuTabs shopMenuTab, bool reopenLastTab = false)
  50. {
  51. this.InvokeNextFrame(delegate
  52. {
  53. this.SwitchTab(shopMenuTab, true, reopenLastTab);
  54. });
  55. }
  56. public void SwitchTab(ShopMenuTabs shopMenuTab, bool forceUpdate = true, bool reopenLastTab = false)
  57. {
  58. if (!forceUpdate && this._lastOpenedTab == shopMenuTab)
  59. {
  60. return;
  61. }
  62. if (reopenLastTab)
  63. {
  64. shopMenuTab = this._lastOpenedTab;
  65. }
  66. if (shopMenuTab == ShopMenuTabs.Cranes && !SingletonMonobehaviour<CIGBuilderManager>.Instance.CanPurchaseCrane)
  67. {
  68. shopMenuTab = ShopMenuTabs.Gold;
  69. }
  70. if (shopMenuTab == ShopMenuTabs.Cash || shopMenuTab == ShopMenuTabs.Gold || shopMenuTab == ShopMenuTabs.Cranes)
  71. {
  72. SingletonMonobehaviour<CIGGameStats>.Instance.AddCurrencyMenuWatched();
  73. }
  74. ShopType shopType = ShopPopupState.TabIndexToShopTypeMapping[shopMenuTab];
  75. if (shopType != ShopType.Shop_cash)
  76. {
  77. if (shopType == ShopType.Shop_gold)
  78. {
  79. if (SingletonMonobehaviour<CIGSaleManager>.Instance.CurrentSale.Gold)
  80. {
  81. shopType = ShopType.Shop_goldSale;
  82. }
  83. }
  84. }
  85. else if (SingletonMonobehaviour<CIGSaleManager>.Instance.CurrentSale.Cash)
  86. {
  87. shopType = ShopType.Shop_cashSale;
  88. }
  89. SingletonMonobehaviour<CIGGameStats>.Instance.AddScreenViewed("shop_" + shopType.ToString());
  90. this._lastOpenedTab = shopMenuTab;
  91. ((ShopPopupView)this.View).SwitchTab(shopMenuTab, shopType);
  92. }
  93. public void OnBuildingShopItemClicked(CIGBuilding building)
  94. {
  95. ILocalizedString localizedString = null;
  96. int requiredGridType = building.tile.requiredGridType;
  97. if (requiredGridType > 0 && requiredGridType != 10)
  98. {
  99. string str = "surfacetype_";
  100. SurfaceType surfaceType = (SurfaceType)requiredGridType;
  101. string key = str + surfaceType.ToString().ToLower().Replace("driedswamp", "swamp");
  102. ILocalizedString localizedString2 = Localization.Key(key);
  103. bool[] availableSurfaceTypes = IsometricIsland.Current.grid.AvailableSurfaceTypes;
  104. if (requiredGridType >= availableSurfaceTypes.Length || !availableSurfaceTypes[requiredGridType])
  105. {
  106. List<ILocalizedString> list = IslandExtensions.IslandsWithElementType(requiredGridType).Apply(delegate (Island island)
  107. {
  108. if (island.IsAvailable())
  109. {
  110. return Localization.Concat(new ILocalizedString[]
  111. {
  112. Localization.Literal("- "),
  113. island.GetDisplayName()
  114. });
  115. }
  116. return null;
  117. }, true);
  118. ILocalizedString localizedString3 = Localization.EmptyLocalizedString;
  119. int count = list.Count;
  120. for (int i = 0; i < count; i++)
  121. {
  122. localizedString3 = Localization.Concat(new ILocalizedString[]
  123. {
  124. localizedString3,
  125. list[i],
  126. (i != count - 1) ? Localization.LiteralNewLineString : Localization.EmptyLocalizedString
  127. });
  128. }
  129. localizedString = Localization.Format(Localization.Key("cannot_build_here"), new ILocalizedString[]
  130. {
  131. localizedString2,
  132. localizedString3
  133. });
  134. }
  135. else if (CityIsland.Current.expansions.GetUnlockedElementCount(requiredGridType) == 0)
  136. {
  137. localizedString = Localization.Format(Localization.Key("type_not_unlocked"), new ILocalizedString[]
  138. {
  139. localizedString2
  140. });
  141. }
  142. }
  143. if (localizedString != null)
  144. {
  145. SingletonMonobehaviour<PopupManager>.Instance.ShowOrUpdateGenericPopup(UISpriteType.Crane, Localization.Key("build"), localizedString, Localization.Key("ok"), null, null, null, null, true);
  146. return;
  147. }
  148. BuildingInfoPopupState buildingInfoPopupState = base.fsm.SwitchState<BuildingInfoPopupState>();
  149. buildingInfoPopupState.SetBuildingAndContent(building, BuildingPopupContent.Preview);
  150. }
  151. public void OnIAPShopItemClicked(CIG3StoreProduct product)
  152. {
  153. SingletonMonobehaviour<CIGStoreManager>.Instance.PurchaseHandler.InitiatePurchase(product, new Action(this.RefreshTab));
  154. }
  155. public void OnCraneShopItemClicked(decimal goldCost)
  156. {
  157. CIGGameState gameState = SingletonMonobehaviour<CIGGameState>.Instance;
  158. decimal value = gameState.Balance.GetValue("Gold");
  159. if (value < goldCost)
  160. {
  161. SingletonMonobehaviour<PopupManager>.Instance.ShowOrUpdateGenericPopup(UISpriteType.GoldLarge, Localization.Key("oops_three_dots"), Localization.Format(Localization.Key("you_need_more_gold"), new ILocalizedString[]
  162. {
  163. Localization.Integer(goldCost - value)
  164. }), Localization.Key("ok"), null, null, null, null, true);
  165. }
  166. else
  167. {
  168. SingletonMonobehaviour<PopupManager>.Instance.ShowOrUpdateGenericPopup(UISpriteType.Crane, Localization.Key("crane"), Localization.Format(Localization.Key("crane_purchase_confirm"), new ILocalizedString[]
  169. {
  170. Localization.Integer(goldCost)
  171. }), Localization.Key("ok"), Localization.Key("confirmspend.no_thanks"), delegate ()
  172. {
  173. gameState.SpendCurrencies(new Currencies("Gold", goldCost), false, delegate (bool success, Currencies spent)
  174. {
  175. if (success)
  176. {
  177. CIGBuilderManager instance = SingletonMonobehaviour<CIGBuilderManager>.Instance;
  178. instance.AddCrane(false);
  179. SingletonMonobehaviour<CIGGameStats>.Instance.AddVirtualCurrencySpent();
  180. SingletonMonobehaviour<PopupManager>.Instance.ShowOrUpdateGenericPopup(UISpriteType.Crane, Localization.Key("build_purchase_succeeded_title"), Localization.Format(Localization.Key("crane_purchased"), new ILocalizedString[]
  181. {
  182. Localization.Integer(instance.MaxBuildCount)
  183. }), Localization.Key("ok"), null, new Action(this.RefreshTab), null, null, true);
  184. }
  185. });
  186. }, null, null, true);
  187. }
  188. }
  189. private void RefreshTab()
  190. {
  191. ((ShopPopupView)this.View).UpdateCraneTab();
  192. this.SwitchTab(this._lastOpenedTab, true, false);
  193. }
  194. private static readonly Dictionary<ShopMenuTabs, ShopType> TabIndexToShopTypeMapping = new Dictionary<ShopMenuTabs, ShopType>
  195. {
  196. {
  197. ShopMenuTabs.Gold,
  198. ShopType.Shop_gold
  199. },
  200. {
  201. ShopMenuTabs.Cash,
  202. ShopType.Shop_cash
  203. },
  204. {
  205. ShopMenuTabs.Cranes,
  206. ShopType.Shop_cranes
  207. },
  208. {
  209. ShopMenuTabs.Residential,
  210. ShopType.Shop_residential
  211. },
  212. {
  213. ShopMenuTabs.Commercial,
  214. ShopType.Shop_commercial
  215. },
  216. {
  217. ShopMenuTabs.Community,
  218. ShopType.Shop_community
  219. },
  220. {
  221. ShopMenuTabs.Decorations,
  222. ShopType.Shop_decorations
  223. }
  224. };
  225. private static readonly Dictionary<Type, ShopMenuTabs> BuildingTypesWithBadgesToShopMenuTabMapping = new Dictionary<Type, ShopMenuTabs>
  226. {
  227. {
  228. typeof(CIGResidentialBuilding),
  229. ShopMenuTabs.Residential
  230. },
  231. {
  232. typeof(CIGCommercialBuilding),
  233. ShopMenuTabs.Commercial
  234. },
  235. {
  236. typeof(CIGCommunityBuilding),
  237. ShopMenuTabs.Community
  238. },
  239. {
  240. typeof(CIGDecoration),
  241. ShopMenuTabs.Decorations
  242. }
  243. };
  244. private ShopMenuTabs _lastOpenedTab;
  245. }