import { BannerAdType, InterAdType, InterFullAdType, VideoAdType } from "../../game/data/GameData"; import { IADSDKMgr } from "../sdk/ad/IADSDKMgr"; /** * 广告管理类脚本 * @description 显示/播放广告 * @author 冯聪 */ export default class AdSystem { /** 构造函数 * @param adSdk 传入当前使用广告sdk */ constructor(adSdk: IADSDKMgr) { this.adSdk = adSdk; // this.init(); } /** 广告sdk */ private adSdk: IADSDKMgr = null; /** 是否显示广告 */ public ifShowAd: boolean = true; /** 视频广告类型 */ public videoAdType: VideoAdType = VideoAdType.video_init_1; /** 视频广告回调 */ public videoAdcallBack: Function = null; /** 全屏插屏广告类型 */ public interFullAdType: InterFullAdType = InterFullAdType.interstitial2_init_1; /** 插屏广告类型 */ public interAdType: InterAdType = InterAdType.interstitial1_click_1; /** banner广告类型 */ public bannerAdType: BannerAdType = BannerAdType.banner_click_1; /** 初始化广告 */ public init() { //判定下平台 if (cc.sys.os == cc.sys.OS_ANDROID) { this.ifShowAd = gData.gameData.configs.ServerConfig.ifShowAd == '1'; } else { this.ifShowAd = false; //test //this.ifShowAd = gData.gameData.configs.ServerConfig.ifShowAd == '1'; } //初始化Topon广告SDK this.adSdk.initSDK(); } /** * 观看广告 * @param callBack 广告相关的回调 */ public watchAd(callBack: Function) { if (this.isAdCooling()) { return; } this.videoAdcallBack = callBack; if (!this.ifShowAd) { this.wacthAdSuccess() } else { if (cc.sys.platform != cc.sys.WECHAT_GAME) { if (cc.sys.os == cc.sys.OS_ANDROID) { this.adSdk.showVideo(); } else if (cc.sys.os == cc.sys.OS_IOS) { jsb.reflection.callStaticMethod("AppController", "showVideo", ""); } } else { // WxMgr.getInstance().showRewardAd(pop) } } } /** 观看广告失败 */ public watchAdFail() { mk.console.log("=== watchAdFail"); this.videoAdcallBack(false); } /** 观看广告取消 */ public watchAdCancel() { mk.console.log("=== watchAdCancel"); this.videoAdcallBack(false); } /** * 观看广告成功 * @param placementId 广告位id */ public wacthAdSuccess(placementId = '') { mk.console.log("=== wacthAdSuccess"); //累计视频次数 mk.data.setTAEventUser(1, 'video_time', 1); //广告过后,渲染会有问题,统一下一帧再处理逻辑 mk.ui.scheduleOnce(() => { if (!gData.adData.checkAdMax()) { this.videoAdcallBack(true); } this.initLastTimeStamp(); }, 0) } /** 显示原生广告 */ public showNative(type = 4) { if (!this.ifShowAd) { return; } if (cc.sys.platform != cc.sys.WECHAT_GAME) { if (cc.sys.os == cc.sys.OS_ANDROID) { this.adSdk.showNative(type); } else if (cc.sys.os == cc.sys.OS_IOS) { } } else { // WxMgr.getInstance().showRewardAd(pop) } } /** 销毁原生广告 */ public destroyNativeAd() { if (!this.ifShowAd) { return; } if (cc.sys.platform != cc.sys.WECHAT_GAME) { if (cc.sys.os == cc.sys.OS_ANDROID) { this.adSdk.destroyNative(); } else if (cc.sys.os == cc.sys.OS_IOS) { } } else { // WxMgr.getInstance().showRewardAd(pop) } } /** 上次播放视频时间戳,单位:毫秒 */ private last_time_stamp: number = 0; private initLastTimeStamp() { let new_date = new Date(); this.last_time_stamp = new_date.getTime(); } /** * 视频是否冷却 * @returns 视频冷却中 */ private isAdCooling(): boolean { let new_date = new Date(); let new_time_stamp = new_date.getTime(); let gap_time_stamp = new_time_stamp - this.last_time_stamp; if (gap_time_stamp <= 3000) { const time = 4 - Math.ceil(gap_time_stamp / 1000); mk.tip.pop('当前看视频太频繁了,请' + time + '秒后再试'); return true; } return false; } public showBanner(bannerAdType: BannerAdType) { if (CC_DEBUG) { return; } if (!this.ifShowAd) { return; } this.bannerAdType = bannerAdType; mk.console.logSingle('banner', 'showBanner'); // console.log("===[AdSystem gameData.adShowConfig", gData.gameData.adShowConfig.is_show_banner); // if (gData.gameData.adShowConfig && gData.gameData.adShowConfig.is_show_banner == 1) { this.adSdk.showBanner(); //} } public destoryBanner() { if (CC_DEBUG) { return; } if (!this.ifShowAd) { return; } this.adSdk.destoryBanner(); } /** * showInterAd * @param type 0 插屏 1 全屏 2 大插屏 * @param des 广告类型 */ public showInterAd(type: number, des = null) { if (CC_DEBUG) { return; } if (!this.ifShowAd) { return; } if (type == 1) { if (des as InterFullAdType) { this.interFullAdType = des; } } else { if (des as InterAdType) { this.interAdType = des; } } this.adSdk.showInter(type); } }