Files
back_cantanBuilding/Assets/Scripts/UI/Turntable/TurntableAdPopupPanel.cs
2026-05-26 16:15:54 +08:00

161 lines
5.6 KiB
C#

using System;
using asap.core;
using cfg;
using GameCore;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using game;
using UniRx;
using System.Collections.Generic;
public class TurntableAdPopupPanel : MonoBehaviour
{
[SerializeField] private Button _btnWatch, _btnClose;
[SerializeField] private TMP_Text _textWatchAd, _textRefreshTimer, _textVip, _textInfo, _textFree;
[SerializeField] private GameObject _goWatch, _goRefresh;
[SerializeField] private RewardItemNew _reward;
private Tables _tables;
private PlayerData _playerData;
// private TbWheelAd _adConfig;
private WheelAd _adConfig;
private PlayerItemData _playerItemData;
private PlayerShopData _playerShopData;
private IDisposable _ads, _timer;
private AdvertPopupType _adType;
private FishingEventData _fishingEventData;
public Action OnClosePanel;
private void Awake()
{
_tables = GContext.container.Resolve<Tables>();
_playerData = GContext.container.Resolve<PlayerData>();
_playerItemData = GContext.container.Resolve<PlayerItemData>();
_playerShopData = GContext.container.Resolve<PlayerShopData>();
_fishingEventData = GContext.container.Resolve<FishingEventData>();
}
// Start is called before the first frame update
private void Start()
{
_btnClose.onClick.AddListener(OnPanelClose);
_btnWatch.onClick.AddListener(OnClickWatch);
Init();
}
private void Init()
{
_adConfig = _playerShopData.GetTurntableAdCofig();
var itemDropPackage = _tables.TbItemDropPackage[_adConfig.Reward];
_reward.SetIcon(itemDropPackage.Icon);
_reward.text_num.text = itemDropPackage.CountDisplay[0];
SetTimer();
}
public void ResetPanel(bool doesShowWatch)
{
_goWatch.SetActive(doesShowWatch);
_goRefresh.SetActive(!doesShowWatch);
if (doesShowWatch)
{
_adConfig = _playerShopData.GetTurntableAdCofig();
int totalAdCount = _tables.TbWheelAd.DataList.Count;
int remainsCount = totalAdCount - _playerShopData.TurntableAdData.index + 1;
string textCost = LocalizationMgr.GetFormatTextValue("UI_EventAdvertPopupPanel_4", $"{remainsCount}/{totalAdCount}");
_textWatchAd.text = textCost;
_textFree.text = textCost;
_textVip.text = LocalizationMgr.GetText("UI_EventAdvertPopupPanel_5");
bool isVip = _playerData.vip >= _adConfig.VipFree;
_textVip.gameObject.SetActive(isVip);
_textFree.gameObject.SetActive(isVip);
_textWatchAd.gameObject.SetActive(!isVip);
_textInfo.gameObject.SetActive(!isVip);
}
}
void OnClickWatch()
{
int viplv = _playerData.vip;
if (viplv >= _adConfig.VipFree)
{
OnAdWatchedSuccess();
}
else
{
_btnWatch.enabled = false;
//拉起广告
DisposeAds();
_ads = GContext.OnEvent<AdsResult>().Subscribe(SetResult);
_adType = AdvertPopupType.Turntable;
GContext.container.Resolve<IAdsService>().ShowRewarded(AdvertPopupType.Turntable);
}
}
private void DisposeAds()
{
if (_ads != null)
{
_adType = AdvertPopupType.None;
_ads.Dispose();
_ads = null;
}
}
private void OnAdWatchedSuccess()
{
List<ItemData> items = _playerItemData.AddItemByDrop(_adConfig.Reward, false);
_playerShopData.UpdateTurntableAd();
if (_playerShopData.TurntableAdData.index > _tables.TbWheelAd.DataList.Count)
{
GContext.Publish(new ShowData(items));
GContext.Publish(new ShowData());
OnPanelClose();
return;
}
Init();
ResetPanel(false);
GContext.Publish(new ShowData(items));
GContext.Publish(new ShowData());
}
void SetResult(AdsResult adsResult)
{
if (adsResult.Result == 0 && adsResult.Type == _adType)
{
GContext.container.Resolve<PlayerShopData>().AddAdvertCount(_fishingEventData.wheelInit.ID);
//广告成功
OnAdWatchedSuccess();
}
DisposeAds();
_btnWatch.enabled = true;
}
private void SetTimer()
{
if (_timer != null)
{
_timer.Dispose();
}
ResetPanel(false);
DateTime nextAdTime = _playerShopData.GetTurnTableNextAdTime();
if ((nextAdTime - ZZTimeHelper.UtcNow().UtcNowOffset()).TotalSeconds <= 1.5f)
{
ResetPanel(true);
return;
}
string strTime;
TimeSpan remainingTime = nextAdTime - ZZTimeHelper.UtcNow().UtcNowOffset();
strTime = ConvertTools.ConvertTime2(remainingTime);
_textRefreshTimer.text = LocalizationMgr.GetFormatTextValue("UI_EventAdvertPopupPanel_3", strTime);
_timer = Observable.Interval(TimeSpan.FromSeconds(1.0f))
.Subscribe( _ =>{
TimeSpan remainingTime = nextAdTime - ZZTimeHelper.UtcNow().UtcNowOffset();
strTime = ConvertTools.ConvertTime2(remainingTime);
_textRefreshTimer.text = LocalizationMgr.GetFormatTextValue("UI_EventAdvertPopupPanel_3", strTime);
if (remainingTime.TotalSeconds <= 1f)
{
Init();
ResetPanel(false);
}
});
}
private void OnPanelClose()
{
_timer?.Dispose();
_ads?.Dispose();
OnClosePanel?.Invoke();
UIManager.Instance.DestroyUI(UITypes.TurntableAdPopupPanel);
}
}