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.IsAvailable && SingletonMonobehaviour.IsAvailable) { GameObjectManager instance = SingletonMonobehaviour.Instance; Dictionary unconsumedBuildings = SingletonMonobehaviour.Instance.GetUnconsumedBuildings(); Dictionary dictionary = new Dictionary(); foreach (KeyValuePair 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 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.Instance.CurrentSale.Cash); ((ShopPopupView)this.View).UpdateBadge(ShopMenuTabs.Gold, SingletonMonobehaviour.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.Instance.CanPurchaseCrane) { shopMenuTab = ShopMenuTabs.Gold; } if (shopMenuTab == ShopMenuTabs.Cash || shopMenuTab == ShopMenuTabs.Gold || shopMenuTab == ShopMenuTabs.Cranes) { SingletonMonobehaviour.Instance.AddCurrencyMenuWatched(); } ShopType shopType = ShopPopupState.TabIndexToShopTypeMapping[shopMenuTab]; if (shopType != ShopType.Shop_cash) { if (shopType == ShopType.Shop_gold) { if (SingletonMonobehaviour.Instance.CurrentSale.Gold) { shopType = ShopType.Shop_goldSale; } } } else if (SingletonMonobehaviour.Instance.CurrentSale.Cash) { shopType = ShopType.Shop_cashSale; } SingletonMonobehaviour.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 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.Instance.ShowOrUpdateGenericPopup(UISpriteType.Crane, Localization.Key("build"), localizedString, Localization.Key("ok"), null, null, null, null, true); return; } BuildingInfoPopupState buildingInfoPopupState = base.fsm.SwitchState(); buildingInfoPopupState.SetBuildingAndContent(building, BuildingPopupContent.Preview); } public void OnIAPShopItemClicked(CIG3StoreProduct product) { //SingletonMonobehaviour.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("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("payment/status", new PaymentStatusRequest() { orderno = lastOrderNo }); if (statusResponse.code == 0) { // 支付成功,清除本地存储的 orderNo //PlayerPrefs.DeleteKey("LastOrderNo"); //PlayerPrefs.Save(); SingletonMonobehaviour.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.Instance; decimal value = gameState.Balance.GetValue("Gold"); if (value < goldCost) { SingletonMonobehaviour.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.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.Instance; instance.AddCrane(false); SingletonMonobehaviour.Instance.AddVirtualCurrencySpent(); SingletonMonobehaviour.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 TabIndexToShopTypeMapping = new Dictionary { { 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 BuildingTypesWithBadgesToShopMenuTabMapping = new Dictionary { { typeof(CIGResidentialBuilding), ShopMenuTabs.Residential }, { typeof(CIGCommercialBuilding), ShopMenuTabs.Commercial }, { typeof(CIGCommunityBuilding), ShopMenuTabs.Community }, { typeof(CIGDecoration), ShopMenuTabs.Decorations } }; private ShopMenuTabs _lastOpenedTab; }