import Util from "../../before/util/Util"; import { VideoAdType } from "../../game/data/GameData"; import { IADSDKMgr } from "../sdk/ad/IADSDKMgr"; import JsbSystem from "./JsbSystem"; /** * 广告管理类脚本 * @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.Null; /** 视频广告回调 */ public videoAdcallBack: Function = null; /** 初始化广告 */ 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) { console.log("==[观看激励视频") if (this.isAdCooling()) { return; } this.videoAdcallBack = callBack; if (!this.ifShowAd) { this.wacthAdSuccess() } else { //FC;显示GroMore广告 JsbSystem.showGMoreRewardAd(); // 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); //GMore显示信息流 JsbSystem.showGMoreFeedListAd(); } 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(); //GMore销毁信息流 JsbSystem.destoryGMoreFeedListAd(); } 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 <= 5000) { const time = 6 - Math.ceil(gap_time_stamp / 1000); mk.tip.pop('当前看视频太频繁了,请' + time + '秒后再试'); return true; } return false; } public showBanner() { if (CC_DEBUG) { return; } if (!this.ifShowAd) { return; } // // 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(); // //} //GroMore banner广告 JsbSystem.showGMoreBannerAd(); } public destoryBanner() { if (CC_DEBUG) { return; } if (!this.ifShowAd) { return; } // this.adSdk.destoryBanner(); //GroMore banner广告 JsbSystem.destoryGMoreBannerAd(); } /** * showInterAd * @param type 0 插屏 1 全屏 */ public showInterAd(type: number) { if (CC_DEBUG) { return; } if (!this.ifShowAd) { return; } //this.adSdk.showInter(type); //GroMore 显示插屏 JsbSystem.showGMoreInterAd(); } /** * 根据几率检测弹出插屏 */ public checkShowInterByChance() { if (!this.ifShowAd) { return; } if (!gData.gameData.adShowConfig) { return; } let chance = gData.gameData.configs.ServerConfig.interOpenChance; if (Math.random() <= chance) { let delay = Number(gData.gameData.configs.ServerConfig.interOpenDelayMin) + Math.random() * (Number(gData.gameData.configs.ServerConfig.interOpenDelayMax) - Number(gData.gameData.configs.ServerConfig.interOpenDelayMin)); setTimeout(() => { this.showInterAd(0); }, delay * 1000); } else { gData.moreGame.popMoreGamePopNode(); } } }