Files
RedHotRoast-ios/Assets/Scripts/AdRedeemManager.cs
T

402 lines
13 KiB
C#
Raw Normal View History

2026-04-22 09:52:55 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using DG.Tweening;
using FGUI.P01_Common;
using FlowerPower;
using UnityEngine;
public class AdRdManager
{
public static readonly AdRdManager Instance = new AdRdManager();
AdRdManager()
{
}
public const string MakeupAd = "MakeupAd";
public const string SavingPotMakeupAd = "SavingPotMakeupAd";
public const string MakeupLVAd = "MakeupLVAd";
public void Rd(AdRdData _data)
{
if (Time.time - SaveData.rd_time < 2)
{
GameHelper.ShowTips("Clicks are too frequent");
return;
}
SaveData.rd_time = Time.time;
int myAdNum = GetLookRewardADNum();
if (myAdNum >= _data.ad_count)
{
if (_data.type == PurchasingManager.pack_reward || _data.type == PurchasingManager.remove_ad || _data.type.StartsWith("buy_gold"))
{
UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.BuyConfirmUI_Open, _data);
}
else
{
SetLookRewardADNum(myAdNum - _data.ad_count);
string name = _data.type.Contains("shop") ? _data.shopName : _data.type;
SendEventClickByName(name, "success");
DOVirtual.DelayedCall(0.1f, () =>
{
GameDispatcher.Instance.Dispatch(GameMsg.apple_pay_success, name);
});
}
}
else
{
GameHelper.ShowTips("Your ADs count is insufficient!");
}
}
public void ShowVideoAd(string adName, Action _action)
{
GameHelper.ShowVideoAd(adName, isSuccess =>
{
if (isSuccess)
{
_action?.Invoke();
}
});
}
// private btn_watchAd btn_WatchAd = null;
private Dictionary<string, btn_watchAd> btn_WatchAd = new Dictionary<string, btn_watchAd>();
private Dictionary<string, Action> ActionSetText = new Dictionary<string, Action>(); //广告数量的显示
public void SetWatchAd(string key, btn_watchAd _btn, Action action)
{
if (!btn_WatchAd.ContainsKey(key))
{
btn_WatchAd.Add(key, _btn);
}
else
{
btn_WatchAd[key] = _btn;
}
if (!ActionSetText.ContainsKey(key))
{
ActionSetText.Add(key, action);
}
else
{
ActionSetText[key] = action;
}
}
private void removeWatchAd()
{
btn_WatchAd.Clear();
}
private int ad_cool_down = ConfigSystem.GetConfig<CommonModel>().exchangeCD;
private void updateWatchCD()
{
var lastTimes = SaveData.GetSaveobject()._watch_ad_cd;
foreach (var item in btn_WatchAd)
{
bool is_get = item.Key == PurchasingManager.pack_reward && SaveData.GetSaveobject().is_get_packreward;
bool is_get1 = item.Key == PurchasingManager.remove_ad && SaveData.GetSaveobject().is_get_removead;
if (is_get || is_get1)
{
item.Value.enabled = false;
}
else if (GameHelper.GetNowTime() < lastTimes && !checkIsCanReceive(item.Key))
{
item.Value.enabled = false;
item.Value.can_buy.selectedIndex = btn_watchAd.Can_buy_cd;
item.Value.btn_text.text = CommonHelper.TimeFormat(lastTimes - Convert.ToInt32(GameHelper.GetNowTime()), CountDownType.Hour);
}
else
{
// stopAction();
item.Value.enabled = true;
item.Value.can_buy.selectedIndex = btn_watchAd.Can_buy_can;
checkBtnState(item.Value, item.Key);
}
}
}
private CommonModel config = ConfigSystem.GetConfig<CommonModel>();
private List<Paidgift> packdata = ConfigSystem.GetConfig<PaidgiftModel>().dataList;
private List<Paidcoins> coinList = ConfigSystem.GetConfig<PaidcoinsModel>().dataList;
public int GetCeilingNeedAds(string name)
{
int needAds = -1;
switch (name)
{
case PurchasingManager.buy_one:
double addspace = config.addspace;
needAds = (int)Math.Ceiling(addspace);
break;
case PurchasingManager.buy_one_off:
double addspace1 = config.AddDiscount;
needAds = (int)Math.Ceiling(addspace1);
break;
case PurchasingManager.battle_pass:
double Passportgift = config.Passportgift;
needAds = (int)Math.Ceiling(Passportgift);
break;
case PurchasingManager.pack_reward:
double Paid_price = packdata[0].Paid_price;
needAds = (int)Math.Ceiling(Paid_price);
break;
case PurchasingManager.remove_ad:
double move_price = packdata[1].Paid_price;
needAds = (int)Math.Ceiling(move_price);
break;
case PurchasingManager.buy_gold_1:
needAds = (int)Math.Ceiling(getCoinNeedAds(name));
break;
case PurchasingManager.buy_gold_2:
needAds = (int)Math.Ceiling(getCoinNeedAds(name));
break;
case PurchasingManager.buy_gold_3:
needAds = (int)Math.Ceiling(getCoinNeedAds(name));
break;
case PurchasingManager.buy_gold_4:
needAds = (int)Math.Ceiling(getCoinNeedAds(name));
break;
case PurchasingManager.buy_gold_5:
needAds = (int)Math.Ceiling(getCoinNeedAds(name));
break;
case MakeupAd:
needAds = 99999;
break;
case SavingPotMakeupAd:
needAds = 99999;
break;
case MakeupLVAd:
needAds = 99999;
break;
}
// if (name.StartsWith("buy_gold"))
// {
// List<Paidcoins> coinList = ConfigSystem.GetConfig<PaidcoinsModel>().dataList;
//
// int startIndex = "buy_gold".Length;
// string suffix = name[startIndex..]; // 截取 "gold" 后的所有字符
// int suffix_num = int.Parse(suffix);
//
// double gold_price = coinList[suffix_num].Payment_amount;
// needAds = (int)Math.Ceiling(gold_price);
// }
return needAds;
}
private double getCoinNeedAds(string sku)
{
Paidcoins result = coinList.FirstOrDefault(c => c.SKU == sku);
if (result != null)
{
return result.Payment_amount;
}
return 0;
}
private bool checkIsCanReceive(string name)
{
int myAd = GetLookRewardADNum();
return myAd >= GetCeilingNeedAds(name);
}
private void checkBtnState(btn_watchAd item, string key)
{
// stopAction();
// foreach (var item in btn_WatchAd)
// {
item.SetClick(() => { });
if (checkIsCanReceive(key))
{
item.buy_state.selectedIndex = btn_watchAd.Buy_state_buy;
if (item.GetChild("img_saveingpot") != null) {
item.GetChild("img_saveingpot").visible = false;
}
item.SetClick(() =>
{
item.enabled = false;
AdRdData adRdData = new AdRdData()
{
type = key,
ad_count = GetCeilingNeedAds(key)
};
Rd(adRdData);
});
}
else
{
item.buy_state.selectedIndex = btn_watchAd.Buy_state_ad;
bool is_get = key == PurchasingManager.pack_reward && SaveData.GetSaveobject().is_get_packreward;
bool is_get1 = key == PurchasingManager.remove_ad && SaveData.GetSaveobject().is_get_removead;
if (is_get || is_get1)
{
item.enabled = false;
item.SetClick(() => { });
}
else
{
if (item.GetChild("img_saveingpot") != null && GameHelper.IsGiftSwitch() && ConfigSystem.GetConfig<CommonModel>().PiggyBankSwitch == 1) {
item.GetChild("img_saveingpot").visible = true;
}
item.SetClick(() =>
{
SendEventClickByName(key, "click");
ShowVideoAd("buy_add_one", () =>
{
RunAllAction();
item.enabled = false;
TimerHelper.mEasy.AddTimer(0.1f, () =>
{
var ad_times = Convert.ToInt32(GameHelper.GetNowTime());
SaveData.GetSaveobject()._watch_ad_cd = ad_times + ad_cool_down;
startAction();
});
});
});
}
// }
}
}
private void RunAllAction()
{
foreach (var item in ActionSetText)
{
item.Value?.Invoke();
}
}
private void startAction()
{
stopAction();
HallManager.Instance.UpdateSecondEvent += updateWatchCD;
}
private void stopAction()
{
HallManager.Instance.UpdateSecondEvent -= updateWatchCD;
}
public int GetLookRewardADNum()
{
int nums = PlayerPrefs.GetInt("get_look_reward_ad_num", 0);
return nums;
}
public void SetLookRewardADNum(int nums)
{
PlayerPrefs.SetInt("get_look_reward_ad_num", nums);
}
public void Start()
{
startAction();
updateWatchCD();
}
public void Destroy()
{
stopAction();
removeWatchAd();
ActionSetText.Clear();
}
public void SendEventClickByName(string name, string type)
{
string eventClickName = "";
string eventSuccName = "";
switch (name)
{
case PurchasingManager.pack_reward:
eventClickName = BuriedPointEvent.pack_click;
eventSuccName = BuriedPointEvent.pack_success;
break;
case PurchasingManager.remove_ad:
eventClickName = BuriedPointEvent.remove_ad_click;
eventSuccName = BuriedPointEvent.remove_ad_success;
break;
// case buy_gold_1:
// eventClickName = BuriedPointEvent.gold_click_1;
// eventSuccName = BuriedPointEvent.gold_success_1;
// break;
// case buy_gold_2:
// eventClickName = BuriedPointEvent.gold_click_2;
// eventSuccName = BuriedPointEvent.gold_success_2;
// break;
// case buy_gold_3:
// eventClickName = BuriedPointEvent.gold_click_3;
// eventSuccName = BuriedPointEvent.gold_success_3;
// break;
// case buy_gold_4:
// eventClickName = BuriedPointEvent.gold_click_4;
// eventSuccName = BuriedPointEvent.gold_success_4;
// break;
case PurchasingManager.battle_pass:
eventClickName = BuriedPointEvent.pass_click;
eventSuccName = BuriedPointEvent.pass_success;
break;
case PurchasingManager.buy_one:
eventClickName = BuriedPointEvent.buy_one_click;
eventSuccName = BuriedPointEvent.buy_one_success;
break;
case PurchasingManager.buy_one_off:
eventClickName = BuriedPointEvent.BuyOneOffClick;
eventSuccName = BuriedPointEvent.BuyOneOffSuccess;
break;
}
if (name.StartsWith("buy_gold"))
{
int startIndex = "buy_gold".Length;
string suffix = name[startIndex..]; // 截取 "gold" 后的所有字符
eventClickName = $"gold_click_{suffix}";
eventSuccName = $"gold_success_{suffix}";
}
if (type == "success" && eventSuccName != "")
{
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_AD_event, eventSuccName, 1);
}
else if (type == "click" && eventClickName != "")
{
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_AD_event, eventClickName, 1);
}
}
}
public class AdRdData
{
public string type;
public int ad_count;
public string shopName;
}
public class orderState
{
public string order_id;
public int status;
}
public class orderData
{
public string order_id;
public string pay_url;
public int code;
}