Files
Zoomatch_unity_ios/Assets/Scripts/AdRedeemManager.cs
T
2026-05-28 15:23:36 +08:00

374 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using ZooMatch;
using DG.Tweening;
using FGUI.ZM_Common_01;
using UnityEngine;
using FairyGUI;
public class AdRedeemManager
{
public static readonly AdRedeemManager Instance = new AdRedeemManager();
AdRedeemManager()
{
}
public const string buy_one = "com.rainforestworld.space.24.99";
// public const string buy_gold_1 = "com.rainforestworld.shop.1.99";
// public const string buy_gold_2 = "com.rainforestworld.shop.3.99";
// public const string buy_gold_3 = "com.rainforestworld.shop.19.99";
// public const string buy_gold_4 = "com.rainforestworld.shop.39.99";
public const string remove_ad = "com.rainforestworld.remove.2.99";
public const string battle_pass = "com.rainforestworld.pass.9.99";
public const string pack_reward = "com.rainforestworld.reward.1.99";
public const string fail_pack = "com.rainforestworld.fail.1.99";
public const string three_days_gift = "com.rainforestworld.three.1.99";
public void Redeem(AdRedeemData _data)
{
if (Time.time - SaveData.redeem_time < 5)
{
GameHelper.ShowTips("Clicks are too frequent");
return;
}
SaveData.redeem_time = Time.time;
int myAdNum = GetLookRewardADNum();
if (myAdNum >= _data.ad_count)
{
SetLookRewardADNum(myAdNum - _data.ad_count);
SendEventClickByName(_data.type, "success");
DOVirtual.DelayedCall(0.1f, () =>
{
GameDispatcher.Instance.Dispatch(GameMsg.apple_pay_success, _data.type);
});
}
else
{
GameHelper.ShowTips("Your AD 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 == pack_reward && SaveData.GetSaveobject().is_get_packreward;
bool is_get1 = item.Key == remove_ad && SaveData.GetSaveobject().is_get_removead;
(item.Value.GetChild("img_saveingpot") as GImage).visible = false;
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;
public int GetCeilingNeedAds(string name)
{
int needAds = -1;
switch (name)
{
case buy_one:
double addspace = config.addspace;
needAds = (int)Math.Ceiling(addspace);
break;
case battle_pass:
double Passportgift = config.Passportgift;
needAds = (int)Math.Ceiling(Passportgift);
break;
case pack_reward:
double Paid_price = packdata[0].Paid_price;
needAds = (int)Math.Ceiling(Paid_price);
break;
case remove_ad:
double move_price = packdata[1].Paid_price;
needAds = (int)Math.Ceiling(move_price);
break;
case three_days_gift:
double three_price = ConfigSystem.GetConfig<MultigiftModel>().dataList[0].Paid_price;
needAds = (int)Math.Ceiling(three_price);
break;
// case buy_gold_1:
// double gold_price1 = coinList[1].Payment_amount;
// needAds = (int)Math.Ceiling(gold_price1);
// break;
// case buy_gold_2:
// double gold_price2 = coinList[2].Payment_amount;
// needAds = (int)Math.Ceiling(gold_price2);
// break;
// case buy_gold_3:
// double gold_price3 = coinList[3].Payment_amount;
// needAds = (int)Math.Ceiling(gold_price3);
// break;
// case buy_gold_4:
// double gold_price4 = coinList[4].Payment_amount;
// needAds = (int)Math.Ceiling(gold_price4);
// 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 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;
item.SetClick(() =>
{
item.enabled = false;
AdRedeemData adRedeemData = new AdRedeemData()
{
type = key,
ad_count = GetCeilingNeedAds(key)
};
Redeem(adRedeemData);
});
}
else
{
item.buy_state.selectedIndex = btn_watchAd.Buy_state_ad;
if (GameHelper.IsGiftSwitch() && ConfigSystem.GetConfig<CommonModel>().PiggyBankSwitch == 1) (item.GetChild("img_saveingpot") as GImage).visible = true;
bool is_get = key == pack_reward && SaveData.GetSaveobject().is_get_packreward;
bool is_get1 = key == remove_ad && SaveData.GetSaveobject().is_get_removead;
if (is_get || is_get1)
{
item.enabled = false;
item.SetClick(() => { });
}
else
{
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 pack_reward:
eventClickName = BuriedPointEvent.pack_click;
eventSuccName = BuriedPointEvent.pack_success;
break;
case 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 battle_pass:
eventClickName = BuriedPointEvent.pass_click;
eventSuccName = BuriedPointEvent.pass_success;
break;
case buy_one:
eventClickName = BuriedPointEvent.buy_one_click;
eventSuccName = BuriedPointEvent.buy_one_success;
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 AdRedeemData
{
public string type;
public int ad_count;
}
public class orderState
{
public string order_id;
public int status;
}
public class orderData
{
public string order_id;
public string pay_url;
}