import { RateConfig } from "../../game/data/AdData"; 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 = false; /** 视频广告类型 */ public videoAdType: VideoAdType = VideoAdType.video_init_1; /** 视频广告回调 */ public videoAdcallBack: Function = null; /** 全屏插屏广告类型 */ public interFullAdType: InterFullAdType = InterFullAdType.interstitial2_init_1; /** 全屏插屏广告类型关闭回调 */ public interFullAdCallBack: Function = null; /** 插屏广告类型 */ 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(); let value = mk.storage.getStorage('last_time_stamp'); if (value) { this.last_time_stamp = parseInt(value); } value = mk.storage.getStorage('solution'); if (value) { gData.warnTipData.curSolution = parseInt(value); } } /** * 观看广告 * @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(); gData.gameData.uploadAverageEcpm(); }, 0) } /** * 全屏插屏关闭回调 */ public closeInterFullAdCallBack(){ setTimeout(()=>{ if(this.interFullAdCallBack){ this.interFullAdCallBack(); this.interFullAdCallBack = null; console.log("closeInterFullAdCallBack==="); } }, 1000); } /** 显示原生广告 */ public showNative(type = 4) { if (!this.ifShowAd) { return; } let per = gData.adData.getPerByEcpm(RateConfig.RC_infrormationFlow); let pop = Math.random() < per; if (!pop) { 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(); mk.storage.setStorage('last_time_stamp', this.last_time_stamp); } /** * 视频是否冷却 * @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; let needTime = 0; switch (gData.warnTipData.curSolution) { case 0: case 1: case 2: case 3: case 5: needTime = 5 * 1000; break; case 4: mk.ui.openPanel('module/warnTip/WarnTipPanel'); return true; } if (gap_time_stamp < needTime) { const time = Math.ceil((needTime - gap_time_stamp) / 1000); const leftTime = mk.time.formatWithUnit(time); mk.tip.pop('看广告太频繁,请' + leftTime + '后再试'); return true; } gData.warnTipData.setSolution(0); return false; } public showBanner(bannerAdType: BannerAdType) { if (CC_DEBUG) { return; } if (!this.ifShowAd) { return; } if (gData.warnTipData.curSolution >= 2) { return; } let per = gData.adData.getPerByEcpm(RateConfig.RC_banner); let pop = Math.random() < per; if (!pop) { 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, callBack = null) { if (CC_DEBUG) { return; } if (!this.ifShowAd) { return; } if (gData.warnTipData.curSolution >= 2) { return; } if (type == 1) { if (des as InterFullAdType) { this.interFullAdType = des; } this.interFullAdCallBack = callBack; } else { if (des as InterAdType) { this.interAdType = des; } } this.adSdk.showInter(type); } }