// Learn TypeScript: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html // Learn Attribute: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.htmlr import { EncryptType, EncryptUtil } from "./tools/EncryptUtil"; import Utils, { LoaderListener } from "./tools/Utils"; import { InterstitialType } from "./vivo/Interstitial"; import { NativeType } from "./vivo/NativeAdComponent"; import VivoAd from "./vivo/VivoAd"; import AESUtil from "../AESUtil" import ATSDK from "../AnyThinkAds/ATJSSDK"; import ATRewardedVideoSDK from "../AnyThinkAds/ATRewardedVideoJSSDK"; import AAJS2 from "../ATAndroidJS2"; import GlobalManager from '../GlobalManager'; import ATInterstitialSDK from "../AnyThinkAds/ATInterstitialJSSDK"; import ATBannerSDK from "../AnyThinkAds/ATBannerJSSDK"; import { AdType } from '../ATAndroidJS2'; let keyS = ["eggplant_fwzoowni#","2021xianYou","Cm_touchmi"]; const { ccclass, property } = cc._decorator; //广告节点 export enum NodeAction { GAME_LOADING_IN_MAIN_UI = 1,//加载界面进入游戏 GAME_ENTER_LEVEL,//进入关卡 GAME_UNLOCK_LEVEL,//解锁关卡 BANNER_SHOW,//banner展示 BANNER_CLICK,//banner点击 BANNER_CLOSE,//banner关闭 INSERT_SHOW,//插屏展示 INSERT_CLICK,//插屏点击 INSERT_CLOSE,//插屏关闭 VIDEO_SHOW,//视频展示 VIDEO_CLOSE,//视频关闭 VIDER_CLICK,//视频点击 NATIVE_SHOW,//原生展示13 NATIVE_CLICK,//原生点击14 NATIVE_CLOSE,//原生关闭15 INIT //16 } @ccclass export default class SDK extends cc.Component implements LoaderListener { public _type: number = 1; private _config;r private static _instance: SDK = null; private _gameCode = null;//游戏id private _gameTime = 0; private _clipboardData = null; private _initCallbacks = null; public limitClick = 0; private _random = -1; public fristGame = false; private _levelTime = 0; private _utils: Utils; private _userId; private _source; private _sysytemInfo; private _netType; private _isVivo = false; private _netInitCount = 0; private _loading = false; public edType ; public ivKey=null; public static get Instance(): SDK { if (SDK._instance == null) { SDK._instance = new SDK(); } return SDK._instance; } constructor() { super(); if (cc.sys.platform == cc.sys.VIVO_GAME) { this._isVivo = true; } console.log("当前平台: " + cc.sys.platform + " " + cc.sys.VIVO_GAME +" "+ this._isVivo ); this._utils = Utils.instance; if (!this._isVivo) { return; } VivoAd.Instance.onListenHome(); VivoAd.Instance.init(); this._userId = VivoAd.Instance.getUserId(); this._source = VivoAd.Instance.getSource(); let self = this; this._sysytemInfo = VivoAd.Instance.getSystemInfoSync(); VivoAd.Instance.getNetType((netType) => { self._netType = netType; }); } public startTime() { this._levelTime = new Date().getTime(); } public endLevelTime(level: number) { let curTime = new Date().getTime(); let time = curTime - this._levelTime; this.durationLog(Math.floor(time / 1000), level); } private requestConfig() { if (!this._isVivo) { return; } let self = this; this._utils.log(self._netType); VivoAd.Instance.getClipboardData((clipboard) => { self._clipboardData = clipboard; let key = keyS[this.edType]; let bady = { key: key, gameCode: self._gameCode, clipboard: clipboard, manuId: self._type, openId: self._userId, source: JSON.stringify(self._source.query), platformVersionCode: self._sysytemInfo == null ? 0 : self._sysytemInfo.platformVersionCode, netType: self._netType, dateTime: new Date().getTime() } let body = JSON.stringify(bady); // this._utils.log(body); let u = ["pz","gg","cnf"]; this._utils.sendPost(u[self.edType], body); }); } /**日志上报 * @param type 上报日志类型 */ public reportLog(type: number, content?: any) { if (!this._isVivo) { return; } let self = this; VivoAd.Instance.getClipboardData((clipboard) => { self._clipboardData = clipboard; let key = keyS[this.edType]; let bady = { key: key, gameCode: self._gameCode, clipboard: clipboard, manuId: self._type, openId: self._userId, type: type, content: content, source: JSON.stringify(self._source.query), dateTime: new Date().getTime() } let u = ["rz","sb","rpt1"]; this._utils.sendPost(u[self.edType], JSON.stringify(bady)); }); } /**游戏上报 * @param secondes 上报日志类型 */ public durationLog(seconds: number, level: any = 0) { if (!this._isVivo) { return; } let self = this; let random = -1; if (seconds > 0) { random = this._random; } let key = keyS[this.edType]; let bady = { key: key, gameCode: self._gameCode, clipboard: self._clipboardData, manuId: self._type, openId: self._userId, level: level, seconds: seconds, source: JSON.stringify(self._source.query), random: random, dateTime: new Date().getTime() } let u = ["sc","zq","rpt2"]; this._utils.sendPost(u[self.edType], JSON.stringify(bady)); } index = 0; /** * sdk初始化 * @param gameCode 游戏id,为服务端分配 * @param callbacks 请求回调 ,返回true请求成功,false请求失败 * @param debug 调试模式 默认false * @returns */ public init(gameCode,type:EncryptType, callbacks?) { this._initCallbacks = callbacks; this.edType = type; // console.log("=============init=========type: "+type); // if(type == 0){ // console.log("=============指创========="); // }else if(type == 1){ // console.log("=============闲游========="); // }else if(type == 2){ // console.log("=============触米========="); // } // this.edType = EncryptType.ED_CM; // let de = EncryptUtil.de("961JU3BY4DjgsEsvjbH1VBWoxQLcT9dfvgGThac0WLgxl72I7V3+b3Uxcs3adY8gzkPvmsBxT4Os5hu9YwlqZXS6A+NA1HbZCjGdwqRHWZPOKWtOhCnKz1Pe+41YpGGj+9GKjBK5TTgKaZRSvQ9HznLtOT7RSLUZqaIdnSGObmI8AKMYcYnx4PAa9q/I5ksA5lB8/yWo1i6IR7hZxK3qXYPfDu9cD5IutZxvqm5xzxJOnjoiIvRCceKmxpqd66AumUjLGAcV2mBMMUe0B5NrO/X1bWUQgi2yKE6EB0ipQE9A7921RANGfTax5zIpSfnv8gLiv8/fc7aDoxFrVpPIhUyBp18G5kVXOwNftWje+4s0dTc67HKSXTEEMBbWIhSqfe5/Nrp306Ls8QsqO50L3WNCfEC+THO5XQgn57AN5X0j2FSz7Exy1fdscRFAf2q4Zx7R9NEMBnaDoxFrVpPIgQ0u4UQOeRSg7W/nQgRYhW0j2FSz7Exy3rToMyhSJesZqaIdnSGObmI8AKMYcYnx4PAa9q/I5ksA5lB8/yWo1i6IR7hZxK3qXYPfDu9cD5Irt3RlAGZCE7EO0auILwjWEU/QovnDWQ+Ng98O71wPkiEv31hBKtl9FNWSzD5W93HcDzUSiQz1irsEXvTx9t7m95U5Rex0B121a0BvjXmhTQiAOfhB+pVUXwkoJol/0gCBvO8RHqt7exx6XqgOxXAujWip285CirBY9CFOJ/tqxQAQSUaGUjFBwiZmiGzrKcT0PSEyYOvZdJkE1KFziq1+mwbLd9pa6vxAsS9XscBkB4OxA0tJkvk2g9sL4eyHHZmEFScfIMeTcJkJPHgxbNLOcEChcfQmIii3z0y2D6PwBm8JKCaJf9IAgDkci7sTftYMbTK9rLir8vJkjHiLQcxFIbzvER6re3scel6oDsVwLo1oqdvOQoqwWos3giRKK3UJlWj2zdEEu4sYvcfqEqZ655sGCl96/upxraKdWT6ToZyeTNJ+WQkjE426FLcuWc/xgV9dRmnQWy2OWwbd0Cii26yZlSKBRPSn/eAtmqX1D7DOQ++awHFPhzV2zakldbpcWrzj0NtQXyexdz/gt25eaQqlLcrTDPuuh2nUL7SKZLET28fSVMvwGxt/uXy2IdBBjW9l/GHWh5bx0XlWHIJfLZay2hRwOBHnaYRXBj9jyCAyGQn7Kzbsh4Uf2C3K3mJ45yZTMLFMDUQVqMUC3E/XUFaJp6XWZwBRPC7qG9aomgmUbnuVJToziBjQb7AiD939KiAXt7qh3/RgB3U4+ioxycRaYuNvMgjcRthfoVguHRt1G/yLCqyIRKWxMLNnxwQrxD1C1xS+iojp6m+OhdQ1mjaA6q9wcf6CEN+A6FbDVnBIJyVeLC1AdTUs7H53DApXn7RaSL/R5bdK7N8VJ0xh0B1pn2DNUCiIqBwmf+Vg40QVqMUC3E/XWaaiEuqhB/GjhXFMcYfeTuAWciCGbM2Rs="); // console.log("=============触米de: "+de); if (!this._isVivo) { if (callbacks) { callbacks(true); } return; } let self = this; if (this._random <= 0) { //随机8位 this._random = this._utils.getRandomInt(0, 10000); } this._gameCode = gameCode; this._utils.addListener(this); this.requestConfig(); self._gameTime = new Date().getTime(); VivoAd.Instance.onHide(() => { let curTime = new Date().getTime(); let time = curTime - self._gameTime; self.durationLog(Math.floor(time / 1000), 0); self._gameTime = 0; this._utils.log("********用户退出游戏或退到后台: " + (time / 1000)); }); VivoAd.Instance.onShow(() => { self._gameTime = new Date().getTime(); this._utils.log("*******用户回到游戏"); }); // this.index++; // if(this.index>2){ // this.index = 0; // } // this.scheduleOnce(()=>{ // SDK.Instance.init(gameCode,this.index,null); // },10); } //自动刷新广告 private flushConfig() { if (!this._isVivo) { return; } let self = this; let go = () => { if (self._loading) { self.unschedule(go); return; } if (self._netInitCount > 2) { self._utils.ok(null); self.unschedule(go); return; } self._netInitCount++; self.requestConfig(); } this.schedule(go, 10); } //监听网络回调 onOk(config: any) { if (!this._isVivo) { return; } this._config = config; let self = this; if (this._config) { this._loading = true; VivoAd.Instance.initConfig(this._config, () => { if (self._initCallbacks) { self._initCallbacks(true); } }); } else { self._utils.log("********* initConfig is faill : " + JSON.stringify(this._config)); if (self._initCallbacks && !this._loading) { self._initCallbacks(false); this.flushConfig(); } } } /** * 展示banner * @param type 原生广告类型 * @param top 是否展示顶部 * @param x * @param y */ public showBanner(type: NativeType = NativeType.BANNER, top: boolean = false, y: number = 0, x: number = 0) { if (!this._isVivo) { return; } this.hideBanner(); VivoAd.Instance.showBanner(type, top, y, x); } /** * 隐藏banner */ public hideBanner() { if (!this._isVivo) { return; } VivoAd.Instance.hideBanner(); } public hideIcon() { if (!this._isVivo) { return; } VivoAd.Instance.hideIcon(); } /** * 展示原生插屏 * 默认id节点为第一个0,有多个时,按服务端配置为准,进行传参 * @param node 默认0 * @param isforever true时,每次调用都会展示,false时,受服务器调用次数控制,默认false * @param id */ public showInterstitial(id: InterstitialType = InterstitialType.INTERSTITIAL_GAME_OVER) { if (!this._isVivo) { return; } VivoAd.Instance.showInterstitial(id); } /** * 展示原生icon广告 * 默认id节点为第一个0,有多个时,按服务端配置为准,进行传参 * @param node * @param x * @param y * @param id */ public showIconAd(x: number, y: number, id: number = 0) { if (!this._isVivo) { return; } this.hideIcon(); VivoAd.Instance.showIconAd(id, x, y); } /*0显示视频*/ public showRewardVideo(callBacks: Function, fail: Function, noAd?: Function) { console.log('a'); if (!this._isVivo) { console.log('b'); return; } console.log('c'); VivoAd.Instance.showRewardVideo(callBacks, fail, noAd); console.log('d'); } //toast提示框 public showToast(message: string) { if (!this._isVivo) { return; } if (VivoAd.Instance) { VivoAd.Instance.showToast(message); } } /** * 游戏结算界面数分延迟 * @param lable * @param score * @param cb */ public gameResultDelayedTime(lable: cc.Label, score: number, cb: Function) { if (this._config && score > 0) { let dalayedTime = this._config.maxClickCount; if (dalayedTime > 0) { let count = 0; let s = Math.floor((dalayedTime / score) * 100) / 100; let add = score / 200; if (add < 1) { add = 1; } this._utils.log("----time: " + dalayedTime + " s: " + s + " add: " + add); let go = () => { count = Math.floor(count + add); if (count > score) { this.unschedule(go); lable.string = "" + score if (cb) { cb(); } return; } if (lable) { lable.string = "" + count } } this.schedule(go, s); } else { lable.string = "" + score if (cb) { this.scheduleOnce(() => { cb(); }, 0.2); } } } else { lable.string = "" + score if (cb) { this.scheduleOnce(() => { cb(); }, 0.2); } } } /** * 激励AD 加载 */ public initAdForPage() { if (cc.sys.os === cc.sys.OS_ANDROID) { let deviceId = AAJS2.getDeviceUserId(); console.log("zh:checkstatus:", ATRewardedVideoSDK.checkAdStatus(AAJS2.getPlacementId())); var setting = {}; setting[ATRewardedVideoSDK.userIdKey] = deviceId; ATRewardedVideoSDK.loadRewardedVideo(AAJS2.getPlacementId(), setting); } } } cc["SDK"] = SDK.Instance;