using CodeJay.Classes; using CodeJay.Defines; using System; using System.Collections; using System.Text; using UnityEngine; using UnityEngine.UI; public class ProductSlot : CardBase { [SerializeField] private GameObject[] gos_glow; // 0 파랑, 1 빨강 [SerializeField] private Image icon; [SerializeField] private TMPro.TextMeshProUGUI NameTMP; [SerializeField] private TMPro.TextMeshProUGUI ProductTMP; [SerializeField] private TMPro.TextMeshProUGUI PriceTMP; [SerializeField] private TMPro.TextMeshProUGUI t_cashrewardamount; [Space(20)] [SerializeField] private GameObject AdButton; [SerializeField] private TMPro.TextMeshProUGUI AdNumberTMP; [Space(20)] [SerializeField] private GameObject PurchaseButton; [Space(20)] [SerializeField] private GameObject SaleGO; [SerializeField] private TMPro.TextMeshProUGUI SaleTMP; [Space(20)] [SerializeField] private GameObject BuyWithCashBtn; [SerializeField] private GameObject go_i_dia; [SerializeField] private GameObject go_i_heart; [SerializeField] private TMPro.TextMeshProUGUI t_buycash; private ProductData _data; private StringBuilder sb = new StringBuilder(); #if UNITY_EDITOR private void OnApplicationFocus(bool focusStatus) { if (focusStatus == true) { DateTime loadTime = GameManager.Timer.LoadTime(); if (loadTime.Day != DateTime.Now.Day) { GameManager.DB.ResetADSRewardActionCount(); GameManager.DB.ResetFreeHeartCount(); GameManager.DB.ResetFreeKeyCount(); GameManager.Timer.DeactivateTimer(name); //buyButton.onClick.AddListener(OnBuyButtonClicked); } if (GameManager.DB.Get_SaveData().Month != InternetTime.Ins.Time.Month) { GameManager.DB.Set_Month(InternetTime.Ins.Time.Month); GameManager.DB.ResetTotalCashPerMonth(); } } } #else private void OnApplicationPause(bool pauseStatus) { if (pauseStatus == false) { DateTime loadTime = GameManager.Timer.LoadTime(); if (loadTime.Day != InternetTime.Ins.Time.Day) { GameManager.DB.ResetADSRewardActionCount(); GameManager.DB.ResetFreeHeartCount(); GameManager.DB.ResetFreeKeyCount(); GameManager.Timer.DeactivateTimer(name); //buyButton.onClick.AddListener(OnBuyButtonClicked); } if (GameManager.DB.Get_SaveData().Month != InternetTime.Ins.Time.Month) { GameManager.DB.Set_Month(InternetTime.Ins.Time.Month); GameManager.DB.ResetTotalCashPerMonth(); } } } #endif private void OnDestroy() { if (GameManager.Instance != null && GameManager.ADS != null) { GameManager.ADS.OnCompletedRewardedAd -= OnCompletedRewardedAd; } if (GameManager.Instance != null && GameManager.DB != null) { GameManager.DB.OnResetFreeHeartCountEvent -= OnResetFreeHeartCountEvent; GameManager.DB.OnResetADSRewardActionCountEvent -= OnResetADSRewardActionCountEvent; } } public override void Set(T _base, int iLoop = -1, int idata = -1) { base.Set(_base, iLoop, idata); SetData(_base as ProductData); } public void SetData(ProductData data) { _data = data; icon.sprite = _data.sprite_item; NameTMP.text = _data.productName; ProductTMP.text = _data.productDescription; PriceTMP.text = _data.price.ToString("c", new System.Globalization.CultureInfo("ko-KR", false)); t_cashrewardamount.text = ""; BuyWithCashBtn.SetActive(false); DSUtil.InActivateGameObjects(gos_glow, data.rewardType == CodeJay.Enum.EProductReward.Key ? 0 : 1); if (data.productID == "heart_free") { GameManager.DB.OnResetFreeHeartCountEvent += OnResetFreeHeartCountEvent; SaleGO.SetActive(false); AdButton.SetActive(false); PriceTMP.transform.parent.gameObject.SetActive(true); PriceTMP.text = "무료"; if (GameManager.DB.FreeHeartCount <= 0) { if (PurchaseButton != null) { Color c = PurchaseButton.GetComponent().color; c.a = 0.5f; PurchaseButton.GetComponent().color = c; } } else { if (PurchaseButton != null) { Color c = PurchaseButton.GetComponent().color; c.a = 1.0f; PurchaseButton.GetComponent().color = c; } } } else if (data.productID == "heart_ads") { GameManager.DB.OnResetADSRewardActionCountEvent += OnResetADSRewardActionCountEvent; SaleGO.SetActive(false); AdButton.SetActive(true); PriceTMP.transform.parent.gameObject.SetActive(false); AdNumberTMP.text = $"{GameManager.DB.ProductAdWatchedNumber} / 3"; SetADSButton(); if (GameManager.DB.ProductAdWatchedNumber <= 0) { if (AdButton != null) { Color c = AdButton.GetComponent().color; c.a = 0.5f; AdButton.GetComponent().color = c; } } else { if (AdButton != null) { Color c = AdButton.GetComponent().color; c.a = 1.0f; AdButton.GetComponent().color = c; } } } else if (data.productID == "key_ads") { GameManager.DB.OnResetADSRewardActionCountEvent += OnResetADSRewardActionCountEvent; SaleGO.SetActive(false); AdButton.SetActive(true); PriceTMP.transform.parent.gameObject.SetActive(false); AdNumberTMP.text = $"{GameManager.DB.ShopADDia} / 3"; SetDiaADSButton(); if (GameManager.DB.ShopADDia <= 0) { if (AdButton != null) { Color c = AdButton.GetComponent().color; c.a = 0.5f; AdButton.GetComponent().color = c; } } else { if (AdButton != null) { Color c = AdButton.GetComponent().color; c.a = 1.0f; AdButton.GetComponent().color = c; } } } else if (data.productID == "com.fgb.adsremove") { SaleGO.SetActive(false); AdButton.SetActive(false); PriceTMP.transform.parent.gameObject.SetActive(true); if (GameManager.DB.IsRemoveADS == true) { Color c = PurchaseButton.GetComponent().color; c.a = 0.5f; PurchaseButton.GetComponent().color = c; } } else if (data.productID == "key_free") { GameManager.DB.OnResetFreeKeyCountEvent += OnResetFreeHeartCountEvent; SaleGO.SetActive(false); AdButton.SetActive(false); PriceTMP.transform.parent.gameObject.SetActive(true); PriceTMP.text = "무료"; if (GameManager.DB.FreeKeyCount <= 0) { if (PurchaseButton != null) { Color c = PurchaseButton.GetComponent().color; c.a = 0.5f; PurchaseButton.GetComponent().color = c; } } else { if (PurchaseButton != null) { Color c = PurchaseButton.GetComponent().color; c.a = 1.0f; PurchaseButton.GetComponent().color = c; } } } else if (data.productID == "heart_60") { SaleGO.SetActive(true); AdButton.SetActive(false); PriceTMP.transform.parent.gameObject.SetActive(true); SaleTMP.text = "▼8%"; } else if (data.productID == "heart_130") { SaleGO.SetActive(true); AdButton.SetActive(false); PriceTMP.transform.parent.gameObject.SetActive(true); SaleTMP.text = "▼15%"; } else if (data.productID == "key_45") { SaleGO.SetActive(true); AdButton.SetActive(false); PriceTMP.transform.parent.gameObject.SetActive(true); SaleTMP.text = "▼11%"; } else if (data.productID == "key_75") { SaleGO.SetActive(true); AdButton.SetActive(false); PriceTMP.transform.parent.gameObject.SetActive(true); SaleTMP.text = "▼20%"; } else if (data.productID == "heart_cash") { BuyWithCashBtn.SetActive(true); go_i_dia.SetActive(true); go_i_heart.SetActive(false); t_buycash.text = _data.price.ToString(); } else if (data.productID == "cash_heart") { BuyWithCashBtn.SetActive(true); go_i_dia.SetActive(false); go_i_heart.SetActive(true); t_buycash.text = _data.price.ToString(); } else { SaleGO.SetActive(false); AdButton.SetActive(false); PriceTMP.transform.parent.gameObject.SetActive(true); } if (data.type == CodeJay.Enum.EProductType.IAP) { PriceTMP.text = $"\\{data.price}"; t_cashrewardamount.text = $"x{data.rewardAmount}"; } } private void SetADSButton() { StopAllCoroutines(); GameManager.ADS.OnCompletedRewardedAd += OnCompletedRewardedAd; DateTime loadTime = GameManager.Timer.LoadTime(); if (loadTime.Day != InternetTime.Ins.Time.Day) { GameManager.DB.ResetADSRewardActionCount(); GameManager.DB.ResetFreeHeartCount(); GameManager.DB.ResetFreeKeyCount(); } else { if (GameManager.DB.ProductAdWatchedNumber <= 0) StartCoroutine(Co_Time()); else { if (AdButton != null) { Color c = AdButton.GetComponent().color; c.a = 1.0f; AdButton.GetComponent().color = c; } SetADSRewardActionCountText(GameManager.DB.ProductAdWatchedNumber); } //GameManager.Timer.ActivateLoadSimpleTimer(name, Constants.ADS_TIMER_COUNT, OnStartTimer, OnPauseTimer, OnStopTimer, OnPassedTime); } if (GameManager.DB.Get_SaveData().Month != InternetTime.Ins.Time.Month) { GameManager.DB.Set_Month(InternetTime.Ins.Time.Month); GameManager.DB.ResetTotalCashPerMonth(); } } IEnumerator Co_Time() { while (true) { yield return new WaitForSeconds(1f); var cal = GameManager.DB.ProductAdWatchedTime - InternetTime.Ins.Time; if (cal.TotalSeconds > 0) AdNumberTMP.text = DSUtil.Get_TimeText_MS(cal); else { GameManager.DB.ProductAdWatchedNumber_Plus(); SetData(_data); } } } private void SetDiaADSButton() { StopAllCoroutines(); // 광고 이벤트 연결하기 GameManager.ADS.OnCompletedRewardedAd += OnCompletedRewardedAd; DateTime loadTime = GameManager.Timer.LoadTime(); if (loadTime.Day != InternetTime.Ins.Time.Day) { GameManager.DB.ResetADSRewardActionCount(); GameManager.DB.ResetFreeHeartCount(); GameManager.DB.ResetFreeKeyCount(); } else { if (GameManager.DB.ShopADDia <= 0) StartCoroutine(Co_DiaTime()); else { if (AdButton != null) { Color c = AdButton.GetComponent().color; c.a = 1.0f; AdButton.GetComponent().color = c; } SetADSRewardActionCountText(GameManager.DB.ShopADDia); } //GameManager.Timer.ActivateLoadSimpleTimer(name, Constants.ADS_TIMER_COUNT, OnStartTimer, OnPauseTimer, OnStopTimer, OnPassedTime); } if (GameManager.DB.Get_SaveData().Month != InternetTime.Ins.Time.Month) { GameManager.DB.Set_Month(InternetTime.Ins.Time.Month); GameManager.DB.ResetTotalCashPerMonth(); } } IEnumerator Co_DiaTime() { while (true) { yield return new WaitForSeconds(1f); var cal = GameManager.DB.ShopADDiaTime - InternetTime.Ins.Time; if (cal.TotalSeconds > 0) AdNumberTMP.text = DSUtil.Get_TimeText_MS(cal); else { GameManager.DB.ProductAdDia_Plus(); SetData(_data); } } } public void ClickButton() { rewardGranted = false; GameManager.Sound.PlaySFX(ESFXType.Button_Hit); if (CheckConditionForState() == false) { return; } if (_data.productID == "heart_free") { if (GameManager.DB.FreeHeartCount > 0) { GameManager.DB.AddHeart(1, this.name); GameManager.DB.MinFreeHeartCount(1); if (GameManager.DB.FreeHeartCount <= 0 && PurchaseButton != null) { Color c = PurchaseButton.GetComponent().color; c.a = 0.5f; PurchaseButton.GetComponent().color = c; } } else { // 무료 하트 횟수가 없다는 팝업 or 가만히 두기 GameManager.UI.ShowNStackPopup(EPopupType.NotVaildFreeHeartPopup); } } else if (_data.productID == "key_free") { if (GameManager.DB.FreeKeyCount > 0) { GameManager.DB.AddKey(50, this.name); GameManager.DB.MinFreeKeyCount(1); if (GameManager.DB.FreeKeyCount <= 0 && PurchaseButton != null) { Color c = PurchaseButton.GetComponent().color; c.a = 0.5f; PurchaseButton.GetComponent().color = c; } } else { // 무료 하트 횟수가 없다는 팝업 or 가만히 두기 //GameManager.UI.ShowNStackPopup(EPopupType.NotVaildFreeHeartPopup); } } else if (_data.productID == "heart_ads") { // 광고 출력 // 광고 횟수 - if (GameManager.DB.ProductAdWatchedNumber > 0) { GameManager.ADS.ShowHeartRewardedAd(this.name); } else { // 광고 횟수가 없다는 팝업 or 가만히 두기 GameManager.UI.ShowNStackPopup(EPopupType.NotVaildFreeHaert_ADS_Popup); } } else if (_data.productID == "key_ads") { // 광고 출력 // 광고 횟수 - if (GameManager.DB.ShopADDia > 0) { GameManager.ADS.ShowResultRewardedAd(this.name); } else { // 광고 횟수가 없다는 팝업 or 가만히 두기 //GameManager.UI.ShowNStackPopup(EPopupType.NotVaildFreeHaert_ADS_Popup); } } else if (_data.productID == "heart_cash") { if (GameManager.DB.Key >= _data.price) { GameManager.DB.SubKey(_data.price, this.name); GameManager.DB.AddHeart(_data.rewardAmount, this.name); GameManager.DB.AddBuyKeyCount(1); GameManager.DB.SaveDatas(); } else { // 팝업 출력 //GameManager.UI.ShowNStackPopup(EPopupType.NotEnoughGoldPopup); } } else if (_data.productID == "cash_heart") { if (GameManager.DB.Heart >= _data.price) { GameManager.DB.SubHeart(_data.price, this.name); GameManager.DB.AddKey(_data.rewardAmount, this.name); GameManager.DB.SaveDatas(); } else { // 팝업 출력 //GameManager.UI.ShowNStackPopup(EPopupType.NotEnoughGoldPopup); } } else if (_data.productID == "com.fgb.adsremove") { if (GameManager.DB.IsRemoveADS == true) return; if (GameManager.DB.TotalCashPerMonth + _data.price > 700000) { GameManager.UI.ShowNStackPopup(EPopupType.CashOverPopup); return; } GameManager.IAP.OnProcessPurchase += OnProcessPurchase; GameManager.IAP.Purchase(_data.productID); } else { if (GameManager.DB.TotalCashPerMonth + _data.price > 700000) { GameManager.UI.ShowNStackPopup(EPopupType.CashOverPopup); return; } GameManager.IAP.OnProcessPurchase += OnProcessPurchase; GameManager.IAP.Purchase(_data.productID); } //##################################################################### } private bool CheckConditionForState() { switch (_data.type) { case CodeJay.Enum.EProductType.ADS: // 타이머 체크 if (IsRunningTimer() == false) { if (GameManager.Network.IsOnline == true) { // 광고 준비 체크 if (true) { return true; } else { // 팝업 출력(광고 준비 X) GameManager.UI.ShowNStackPopup(EPopupType.FailLoadADSPopup); return false; } } else { // 팝업 출력 GameManager.UI.ShowNStackPopup(EPopupType.NotOnline_ClosePopup); return false; } } else { return false; } break; case CodeJay.Enum.EProductType.IAP: //if (GameManager.Network.IsOnline == true) //{ // // 구글 로그인에 관련된 처리는 필요없을듯 하다 // // 결제 실패에 관한 처리가 있어어야 하는가? // // StorePanel에서 결제 실패에 관한 처리를 하면 되는가? // // 결제 실패시 이 팝업 로드 EPopupType.FailIAPPopup // // 70만원 이상이면 결제 금지(한달 기간) // if (GameManager.DB.BuyShopAccumulatedMoney <= 600000) // { // return true; // } // else // { // GameManager.UI.ShowNStackPopup(EPopupType.MonthlyPurchaselimitPopup); // return false; // } //} //else //{ // // 팝업 출력 // GameManager.UI.ShowNStackPopup(EPopupType.NotOnline_ClosePopup); // return false; //} return true; case CodeJay.Enum.EProductType.Gold: // 위에서 처리하기 때문에 비여있는 상태 return true; break; } return false; } private void OnStartTimer(int timerCount) { if (timerCount <= 0) { GameManager.Timer.DeactivateTimer(name); } else { SetTimerText(timerCount); // buyButton.onClick.RemoveListener(OnBuyButtonClicked); } } private void OnPauseTimer(int timerCount) { if (timerCount <= 0) { SetADSRewardActionCountText(GameManager.DB.ProductAdWatchedNumber); GameManager.Timer.DeactivateTimer(name); //buyButton.onClick.AddListener(OnBuyButtonClicked); } else { SetTimerText(timerCount); //buyButton.onClick.RemoveListener(OnBuyButtonClicked); } } private void OnStopTimer(int timerCount) { if (timerCount <= 0) { if (GameManager.DB.ProductAdWatchedNumber == 0) GameManager.DB.ProductAdWatchedNumber_Plus(); SetADSRewardActionCountText(GameManager.DB.ProductAdWatchedNumber); GameManager.Timer.DeactivateTimer(name); //buyButton.onClick.AddListener(OnBuyButtonClicked); } } private void OnPassedTime(int timerCount) { SetTimerText(timerCount); if (timerCount <= 0) { SetADSRewardActionCountText(GameManager.DB.ProductAdWatchedNumber); } } private bool IsRunningTimer() { return GameManager.Timer.IsVaildTimer(name); } private void SetADSRewardActionCountText(int count) { sb.Clear(); sb.Append(count.ToString()).Append("/").Append(Constants.ADS_REWARD_COUNT.ToString()); AdNumberTMP.text = sb.ToString(); } private void SetTimerText(int time) { int minute = time / 60; int second = time % 60; sb.Clear(); sb.Append(GetDoubleCharNumber(minute)).Append(":").Append(GetDoubleCharNumber(second)); AdNumberTMP.text = sb.ToString(); } private string GetDoubleCharNumber(int num) { string text; if (num < 10) { text = "0" + num.ToString(); } else { text = num.ToString(); } return text; } bool rewardGranted = false; private void OnCompletedRewardedAd(bool isCompleted, string name) { if (rewardGranted) return; rewardGranted = true; if (name == this.name) { if (isCompleted) { if (name == "ProductSlot (2)") { GameManager.DB.AddHeart(_data.rewardAmount, this.name); GameManager.DB.MinProductAdWatchedNumber(1); GameManager.ADS.OnCompletedRewardedAd -= OnCompletedRewardedAd; SetData(_data); if (GameManager.DB.ProductAdWatchedNumber <= 0 && AdButton != null) { Color c = AdButton.GetComponent().color; c.a = 0.5f; AdButton.GetComponent().color = c; } } else { GameManager.DB.AddKey(_data.rewardAmount, this.name); GameManager.DB.MinProductAdDia(1); GameManager.ADS.OnCompletedRewardedAd -= OnCompletedRewardedAd; SetData(_data); if (GameManager.DB.ShopADDia <= 0 && AdButton != null) { Color c = AdButton.GetComponent().color; c.a = 0.5f; AdButton.GetComponent().color = c; } } } } } private void OnProcessPurchase(string id) { if (id == "FailPurchase") { // 구매 실패 GameManager.UI.ShowNStackPopup(EPopupType.FailIAPPopup); GameManager.IAP.OnProcessPurchase -= OnProcessPurchase; return; } if (_data.productID == id) { if (_data.productID == "com.fgb.adsremove") { Color c = PurchaseButton.GetComponent().color; c.a = 0.5f; PurchaseButton.GetComponent().color = c; } GameManager.DB.AddBuyShopAccumulatedMoney(_data.price); } GameManager.IAP.OnProcessPurchase -= OnProcessPurchase; } private void OnResetFreeHeartCountEvent() { Color c = PurchaseButton.GetComponent().color; c.a = 1.0f; PurchaseButton.GetComponent().color = c; } private void OnResetADSRewardActionCountEvent() { Color c = AdButton.GetComponent().color; c.a = 1.0f; AdButton.GetComponent().color = c; AdNumberTMP.text = $"{GameManager.DB.ProductAdWatchedNumber} / 3"; } }