|
- using System;
- using System.Collections.Generic;
- using System.Net.Sockets;
- using System.Net;
- using CIG;
- using CIG.Extensions;
- using CIG.Translation;
- using CIG3.ExtensionMethods;
- using CIGEnums;
- using Cysharp.Threading.Tasks;
- using SUISS.Core;
- using SUISSEngine;
- using UnityEngine;
- using UnityEngine.Purchasing;
-
- public class ShopPopupState : PopupBaseState
- {
- public override void Enter(State oldState)
- {
- base.Enter(oldState);
- if (SingletonMonobehaviour<CIGPurchasedBuildingsManager>.IsAvailable && SingletonMonobehaviour<GameObjectManager>.IsAvailable)
- {
- GameObjectManager instance = SingletonMonobehaviour<GameObjectManager>.Instance;
- Dictionary<string, int> unconsumedBuildings = SingletonMonobehaviour<CIGPurchasedBuildingsManager>.Instance.GetUnconsumedBuildings();
- Dictionary<ShopMenuTabs, int> dictionary = new Dictionary<ShopMenuTabs, int>();
- foreach (KeyValuePair<string, int> keyValuePair in unconsumedBuildings)
- {
- if (keyValuePair.Value > 0)
- {
- CIGBuilding building = instance.GetBuilding(keyValuePair.Key);
- if (building != null)
- {
- Type type = building.GetType();
- if (ShopPopupState.BuildingTypesWithBadgesToShopMenuTabMapping.ContainsKey(type))
- {
- ShopMenuTabs shopMenuTabs = ShopPopupState.BuildingTypesWithBadgesToShopMenuTabMapping[type];
- if (!dictionary.ContainsKey(shopMenuTabs))
- {
- dictionary[shopMenuTabs] = 0;
- }
- (dictionary)[shopMenuTabs] = dictionary[shopMenuTabs] + keyValuePair.Value;
- }
- }
- }
- }
- foreach (KeyValuePair<Type, ShopMenuTabs> keyValuePair2 in ShopPopupState.BuildingTypesWithBadgesToShopMenuTabMapping)
- {
- ShopMenuTabs value = keyValuePair2.Value;
- ((ShopPopupView)this.View).UpdateBadge(value, (!dictionary.ContainsKey(value)) ? 0 : dictionary[value]);
- }
- }
- ((ShopPopupView)this.View).UpdateBadge(ShopMenuTabs.Cash, SingletonMonobehaviour<CIGSaleManager>.Instance.CurrentSale.Cash);
- ((ShopPopupView)this.View).UpdateBadge(ShopMenuTabs.Gold, SingletonMonobehaviour<CIGSaleManager>.Instance.CurrentSale.Gold);
- }
-
- public void OpenPopup(ShopMenuTabs shopMenuTab, bool reopenLastTab = false)
- {
- this.InvokeNextFrame(delegate
- {
- this.SwitchTab(shopMenuTab, true, reopenLastTab);
- });
- }
-
- public void SwitchTab(ShopMenuTabs shopMenuTab, bool forceUpdate = true, bool reopenLastTab = false)
- {
- if (!forceUpdate && this._lastOpenedTab == shopMenuTab)
- {
- return;
- }
- if (reopenLastTab)
- {
- shopMenuTab = this._lastOpenedTab;
- }
- if (shopMenuTab == ShopMenuTabs.Cranes && !SingletonMonobehaviour<CIGBuilderManager>.Instance.CanPurchaseCrane)
- {
- shopMenuTab = ShopMenuTabs.Gold;
- }
- if (shopMenuTab == ShopMenuTabs.Cash || shopMenuTab == ShopMenuTabs.Gold || shopMenuTab == ShopMenuTabs.Cranes)
- {
- SingletonMonobehaviour<CIGGameStats>.Instance.AddCurrencyMenuWatched();
- }
- ShopType shopType = ShopPopupState.TabIndexToShopTypeMapping[shopMenuTab];
- if (shopType != ShopType.Shop_cash)
- {
- if (shopType == ShopType.Shop_gold)
- {
- if (SingletonMonobehaviour<CIGSaleManager>.Instance.CurrentSale.Gold)
- {
- shopType = ShopType.Shop_goldSale;
- }
- }
- }
- else if (SingletonMonobehaviour<CIGSaleManager>.Instance.CurrentSale.Cash)
- {
- shopType = ShopType.Shop_cashSale;
- }
- SingletonMonobehaviour<CIGGameStats>.Instance.AddScreenViewed("shop_" + shopType.ToString());
- this._lastOpenedTab = shopMenuTab;
- ((ShopPopupView)this.View).SwitchTab(shopMenuTab, shopType);
- }
-
- public void OnBuildingShopItemClicked(CIGBuilding building)
- {
- ILocalizedString localizedString = null;
- int requiredGridType = building.tile.requiredGridType;
- if (requiredGridType > 0 && requiredGridType != 10)
- {
- string str = "surfacetype_";
- SurfaceType surfaceType = (SurfaceType)requiredGridType;
- string key = str + surfaceType.ToString().ToLower().Replace("driedswamp", "swamp");
- ILocalizedString localizedString2 = Localization.Key(key);
- bool[] availableSurfaceTypes = IsometricIsland.Current.grid.AvailableSurfaceTypes;
- if (requiredGridType >= availableSurfaceTypes.Length || !availableSurfaceTypes[requiredGridType])
- {
- List<ILocalizedString> list = IslandExtensions.IslandsWithElementType(requiredGridType).Apply(delegate (Island island)
- {
- if (island.IsAvailable())
- {
- return Localization.Concat(new ILocalizedString[]
- {
- Localization.Literal("- "),
- island.GetDisplayName()
- });
- }
- return null;
- }, true);
- ILocalizedString localizedString3 = Localization.EmptyLocalizedString;
- int count = list.Count;
- for (int i = 0; i < count; i++)
- {
- localizedString3 = Localization.Concat(new ILocalizedString[]
- {
- localizedString3,
- list[i],
- (i != count - 1) ? Localization.LiteralNewLineString : Localization.EmptyLocalizedString
- });
- }
- localizedString = Localization.Format(Localization.Key("cannot_build_here"), new ILocalizedString[]
- {
- localizedString2,
- localizedString3
- });
- }
- else if (CityIsland.Current.expansions.GetUnlockedElementCount(requiredGridType) == 0)
- {
- localizedString = Localization.Format(Localization.Key("type_not_unlocked"), new ILocalizedString[]
- {
- localizedString2
- });
- }
- }
- if (localizedString != null)
- {
- SingletonMonobehaviour<PopupManager>.Instance.ShowOrUpdateGenericPopup(UISpriteType.Crane, Localization.Key("build"), localizedString, Localization.Key("ok"), null, null, null, null, true);
- return;
- }
- BuildingInfoPopupState buildingInfoPopupState = base.fsm.SwitchState<BuildingInfoPopupState>();
- buildingInfoPopupState.SetBuildingAndContent(building, BuildingPopupContent.Preview);
- }
-
- public void OnIAPShopItemClicked(CIG3StoreProduct product)
- {
- //SingletonMonobehaviour<CIGStoreManager>.Instance.PurchaseHandler.InitiatePurchase(product, new Action(this.RefreshTab));
- ClickPayButton_Task(product).Forget();
- }
-
- private async UniTask ClickPayButton_Task(CIG3StoreProduct product)
- {
- string localIP = GetLocalIP();
- Debug.Log("Local IP Address: " + localIP);
- int money = (int)(product.DollarPrice * 100); // 将 DollarPrice 乘以 100
- PayResponse payResponse = await HttpManager.Instance.RequestPost<PayRequest, PayResponse>("payment/pay", new PayRequest() { phone = HttpManager.Instance.phoneNum, money = money, clientIp = localIP });
- if (payResponse.code == 0)
- {
- PayResponseData payResponseData = payResponse.data;
-
- string orderNo = payResponseData.orderno;
- HttpManager.Instance.LastOrderNo = orderNo;
- HttpManager.Instance.LastProduct = product;
- //// 存储 orderNo
- //PlayerPrefs.SetString("LastOrderNo", orderNo);
- //PlayerPrefs.Save();
- // 解码 URL
- string decodedUrl = Uri.UnescapeDataString(payResponseData.pay_url);
- Application.OpenURL(decodedUrl);
- }
- // else
- // {
- // ErrorText.text = loginResponse.message;
- // }
- }
-
- void OnApplicationFocus(bool hasFocus)
- {
- if (hasFocus && !isCheckingPaymentStatus)
- {
- CheckPaymentStatus().Forget();
- }
- }
-
- private bool isCheckingPaymentStatus = false;
- private async UniTaskVoid CheckPaymentStatus()
- {
- if (isCheckingPaymentStatus)
- {
- return;
- }
- isCheckingPaymentStatus = true;
-
- //string lastOrderNo = PlayerPrefs.GetString("LastOrderNo", string.Empty);
- string lastOrderNo = HttpManager.Instance.LastOrderNo;
- if (!string.IsNullOrEmpty(lastOrderNo))
- {
- // 查询支付状态
- PaymentStatusResponse statusResponse = await HttpManager.Instance.RequestPost<PaymentStatusRequest, PaymentStatusResponse>("payment/status", new PaymentStatusRequest() { orderno = lastOrderNo });
- if (statusResponse.code == 0)
- {
- // 支付成功,清除本地存储的 orderNo
- //PlayerPrefs.DeleteKey("LastOrderNo");
- //PlayerPrefs.Save();
- SingletonMonobehaviour<CIGStoreManager>.Instance.PurchaseHandler.InitiatePurchase(HttpManager.Instance.LastProduct, new Action(this.RefreshTab));
- HttpManager.Instance.LastOrderNo = "";
- HttpManager.Instance.LastProduct = null;
- Debug.Log("Payment successful for orderNo: " + lastOrderNo);
- }
- else
- {
- Debug.Log("Payment not successful for orderNo: " + lastOrderNo);
- }
- }
- isCheckingPaymentStatus = false;
- }
-
- string GetLocalIP()
- {
- string localIP = string.Empty;
- try
- {
- var host = Dns.GetHostEntry(Dns.GetHostName());
- foreach (var ip in host.AddressList)
- {
- // 检查是否是 IPv4 地址,并且排除回环地址
- if (ip.AddressFamily == AddressFamily.InterNetwork)
- {
- localIP = ip.ToString();
- break;
- }
- }
- }
- catch (System.Exception ex)
- {
- Debug.LogError("Error getting local IP: " + ex.Message);
- }
-
- return string.IsNullOrEmpty(localIP) ? "Unable to determine local IP" : localIP;
- }
-
- public void OnCraneShopItemClicked(decimal goldCost)
- {
- CIGGameState gameState = SingletonMonobehaviour<CIGGameState>.Instance;
- decimal value = gameState.Balance.GetValue("Gold");
- if (value < goldCost)
- {
- SingletonMonobehaviour<PopupManager>.Instance.ShowOrUpdateGenericPopup(UISpriteType.GoldLarge, Localization.Key("oops_three_dots"), Localization.Format(Localization.Key("you_need_more_gold"), new ILocalizedString[]
- {
- Localization.Integer(goldCost - value)
- }), Localization.Key("ok"), null, null, null, null, true);
- }
- else
- {
- SingletonMonobehaviour<PopupManager>.Instance.ShowOrUpdateGenericPopup(UISpriteType.Crane, Localization.Key("crane"), Localization.Format(Localization.Key("crane_purchase_confirm"), new ILocalizedString[]
- {
- Localization.Integer(goldCost)
- }), Localization.Key("ok"), Localization.Key("confirmspend.no_thanks"), delegate ()
- {
- gameState.SpendCurrencies(new Currencies("Gold", goldCost), false, delegate (bool success, Currencies spent)
- {
- if (success)
- {
- CIGBuilderManager instance = SingletonMonobehaviour<CIGBuilderManager>.Instance;
- instance.AddCrane(false);
- SingletonMonobehaviour<CIGGameStats>.Instance.AddVirtualCurrencySpent();
- SingletonMonobehaviour<PopupManager>.Instance.ShowOrUpdateGenericPopup(UISpriteType.Crane, Localization.Key("build_purchase_succeeded_title"), Localization.Format(Localization.Key("crane_purchased"), new ILocalizedString[]
- {
- Localization.Integer(instance.MaxBuildCount)
- }), Localization.Key("ok"), null, new Action(this.RefreshTab), null, null, true);
- }
- });
- }, null, null, true);
- }
- }
-
- private void RefreshTab()
- {
- ((ShopPopupView)this.View).UpdateCraneTab();
- this.SwitchTab(this._lastOpenedTab, true, false);
- }
-
- private static readonly Dictionary<ShopMenuTabs, ShopType> TabIndexToShopTypeMapping = new Dictionary<ShopMenuTabs, ShopType>
- {
- {
- ShopMenuTabs.Gold,
- ShopType.Shop_gold
- },
- {
- ShopMenuTabs.Cash,
- ShopType.Shop_cash
- },
- {
- ShopMenuTabs.Cranes,
- ShopType.Shop_cranes
- },
- {
- ShopMenuTabs.Residential,
- ShopType.Shop_residential
- },
- {
- ShopMenuTabs.Commercial,
- ShopType.Shop_commercial
- },
- {
- ShopMenuTabs.Community,
- ShopType.Shop_community
- },
- {
- ShopMenuTabs.Decorations,
- ShopType.Shop_decorations
- }
- };
-
- private static readonly Dictionary<Type, ShopMenuTabs> BuildingTypesWithBadgesToShopMenuTabMapping = new Dictionary<Type, ShopMenuTabs>
- {
- {
- typeof(CIGResidentialBuilding),
- ShopMenuTabs.Residential
- },
- {
- typeof(CIGCommercialBuilding),
- ShopMenuTabs.Commercial
- },
- {
- typeof(CIGCommunityBuilding),
- ShopMenuTabs.Community
- },
- {
- typeof(CIGDecoration),
- ShopMenuTabs.Decorations
- }
- };
-
- private ShopMenuTabs _lastOpenedTab;
- }
|