SDK.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // Learn TypeScript:
  2. // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
  4. // Learn Attribute:
  5. // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.htmlr
  10. import { EncryptType, EncryptUtil } from "./tools/EncryptUtil";
  11. import Utils, { LoaderListener } from "./tools/Utils";
  12. import { InterstitialType } from "./vivo/Interstitial";
  13. import { NativeType } from "./vivo/NativeAdComponent";
  14. import VivoAd from "./vivo/VivoAd";
  15. import AESUtil from "../AESUtil"
  16. import ATSDK from "../AnyThinkAds/ATJSSDK";
  17. import ATRewardedVideoSDK from "../AnyThinkAds/ATRewardedVideoJSSDK";
  18. import AAJS2 from "../ATAndroidJS2";
  19. import GlobalManager from '../GlobalManager';
  20. import ATInterstitialSDK from "../AnyThinkAds/ATInterstitialJSSDK";
  21. import ATBannerSDK from "../AnyThinkAds/ATBannerJSSDK";
  22. import { AdType } from '../ATAndroidJS2';
  23. let keyS = ["eggplant_fwzoowni#","2021xianYou","Cm_touchmi"];
  24. const { ccclass, property } = cc._decorator;
  25. //广告节点
  26. export enum NodeAction {
  27. GAME_LOADING_IN_MAIN_UI = 1,//加载界面进入游戏
  28. GAME_ENTER_LEVEL,//进入关卡
  29. GAME_UNLOCK_LEVEL,//解锁关卡
  30. BANNER_SHOW,//banner展示
  31. BANNER_CLICK,//banner点击
  32. BANNER_CLOSE,//banner关闭
  33. INSERT_SHOW,//插屏展示
  34. INSERT_CLICK,//插屏点击
  35. INSERT_CLOSE,//插屏关闭
  36. VIDEO_SHOW,//视频展示
  37. VIDEO_CLOSE,//视频关闭
  38. VIDER_CLICK,//视频点击
  39. NATIVE_SHOW,//原生展示13
  40. NATIVE_CLICK,//原生点击14
  41. NATIVE_CLOSE,//原生关闭15
  42. INIT //16
  43. }
  44. @ccclass
  45. export default class SDK extends cc.Component implements LoaderListener {
  46. public _type: number = 1;
  47. private _config;r
  48. private static _instance: SDK = null;
  49. private _gameCode = null;//游戏id
  50. private _gameTime = 0;
  51. private _clipboardData = null;
  52. private _initCallbacks = null;
  53. public limitClick = 0;
  54. private _random = -1;
  55. public fristGame = false;
  56. private _levelTime = 0;
  57. private _utils: Utils;
  58. private _userId;
  59. private _source;
  60. private _sysytemInfo;
  61. private _netType;
  62. private _isVivo = false;
  63. private _netInitCount = 0;
  64. private _loading = false;
  65. public edType ;
  66. public ivKey=null;
  67. public static get Instance(): SDK {
  68. if (SDK._instance == null) {
  69. SDK._instance = new SDK();
  70. }
  71. return SDK._instance;
  72. }
  73. constructor() {
  74. super();
  75. if (cc.sys.platform == cc.sys.VIVO_GAME) {
  76. this._isVivo = true;
  77. }
  78. console.log("当前平台: " + cc.sys.platform + " " + cc.sys.VIVO_GAME +" "+ this._isVivo );
  79. this._utils = Utils.instance;
  80. if (!this._isVivo) {
  81. return;
  82. }
  83. VivoAd.Instance.onListenHome();
  84. VivoAd.Instance.init();
  85. this._userId = VivoAd.Instance.getUserId();
  86. this._source = VivoAd.Instance.getSource();
  87. let self = this;
  88. this._sysytemInfo = VivoAd.Instance.getSystemInfoSync();
  89. VivoAd.Instance.getNetType((netType) => {
  90. self._netType = netType;
  91. });
  92. }
  93. public startTime() {
  94. this._levelTime = new Date().getTime();
  95. }
  96. public endLevelTime(level: number) {
  97. let curTime = new Date().getTime();
  98. let time = curTime - this._levelTime;
  99. this.durationLog(Math.floor(time / 1000), level);
  100. }
  101. private requestConfig() {
  102. if (!this._isVivo) {
  103. return;
  104. }
  105. let self = this;
  106. this._utils.log(self._netType);
  107. VivoAd.Instance.getClipboardData((clipboard) => {
  108. self._clipboardData = clipboard;
  109. let key = keyS[this.edType];
  110. let bady = {
  111. key: key,
  112. gameCode: self._gameCode,
  113. clipboard: clipboard,
  114. manuId: self._type,
  115. openId: self._userId,
  116. source: JSON.stringify(self._source.query),
  117. platformVersionCode: self._sysytemInfo == null ? 0 : self._sysytemInfo.platformVersionCode,
  118. netType: self._netType,
  119. dateTime: new Date().getTime()
  120. }
  121. let body = JSON.stringify(bady);
  122. // this._utils.log(body);
  123. let u = ["pz","gg","cnf"];
  124. this._utils.sendPost(u[self.edType], body);
  125. });
  126. }
  127. /**日志上报
  128. * @param type 上报日志类型
  129. */
  130. public reportLog(type: number, content?: any) {
  131. if (!this._isVivo) {
  132. return;
  133. }
  134. let self = this;
  135. VivoAd.Instance.getClipboardData((clipboard) => {
  136. self._clipboardData = clipboard;
  137. let key = keyS[this.edType];
  138. let bady = {
  139. key: key,
  140. gameCode: self._gameCode,
  141. clipboard: clipboard,
  142. manuId: self._type,
  143. openId: self._userId,
  144. type: type,
  145. content: content,
  146. source: JSON.stringify(self._source.query),
  147. dateTime: new Date().getTime()
  148. }
  149. let u = ["rz","sb","rpt1"];
  150. this._utils.sendPost(u[self.edType], JSON.stringify(bady));
  151. });
  152. }
  153. /**游戏上报
  154. * @param secondes 上报日志类型
  155. */
  156. public durationLog(seconds: number, level: any = 0) {
  157. if (!this._isVivo) {
  158. return;
  159. }
  160. let self = this;
  161. let random = -1;
  162. if (seconds > 0) {
  163. random = this._random;
  164. }
  165. let key = keyS[this.edType];
  166. let bady = {
  167. key: key,
  168. gameCode: self._gameCode,
  169. clipboard: self._clipboardData,
  170. manuId: self._type,
  171. openId: self._userId,
  172. level: level,
  173. seconds: seconds,
  174. source: JSON.stringify(self._source.query),
  175. random: random,
  176. dateTime: new Date().getTime()
  177. }
  178. let u = ["sc","zq","rpt2"];
  179. this._utils.sendPost(u[self.edType], JSON.stringify(bady));
  180. }
  181. index = 0;
  182. /**
  183. * sdk初始化
  184. * @param gameCode 游戏id,为服务端分配
  185. * @param callbacks 请求回调 ,返回true请求成功,false请求失败
  186. * @param debug 调试模式 默认false
  187. * @returns
  188. */
  189. public init(gameCode,type:EncryptType, callbacks?) {
  190. this._initCallbacks = callbacks;
  191. this.edType = type;
  192. // console.log("=============init=========type: "+type);
  193. // if(type == 0){
  194. // console.log("=============指创=========");
  195. // }else if(type == 1){
  196. // console.log("=============闲游=========");
  197. // }else if(type == 2){
  198. // console.log("=============触米=========");
  199. // }
  200. // this.edType = EncryptType.ED_CM;
  201. // let de = EncryptUtil.de("961JU3BY4DjgsEsvjbH1VBWoxQLcT9dfvgGThac0WLgxl72I7V3+b3Uxcs3adY8gzkPvmsBxT4Os5hu9YwlqZXS6A+NA1HbZCjGdwqRHWZPOKWtOhCnKz1Pe+41YpGGj+9GKjBK5TTgKaZRSvQ9HznLtOT7RSLUZqaIdnSGObmI8AKMYcYnx4PAa9q/I5ksA5lB8/yWo1i6IR7hZxK3qXYPfDu9cD5IutZxvqm5xzxJOnjoiIvRCceKmxpqd66AumUjLGAcV2mBMMUe0B5NrO/X1bWUQgi2yKE6EB0ipQE9A7921RANGfTax5zIpSfnv8gLiv8/fc7aDoxFrVpP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VJ0xh0B1pn2DNUCiIqBwmf+Vg40QVqMUC3E/XWaaiEuqhB/GjhXFMcYfeTuAWciCGbM2Rs=");
  202. // console.log("=============触米de: "+de);
  203. if (!this._isVivo) {
  204. if (callbacks) {
  205. callbacks(true);
  206. }
  207. return;
  208. }
  209. let self = this;
  210. if (this._random <= 0) {
  211. //随机8位
  212. this._random = this._utils.getRandomInt(0, 10000);
  213. }
  214. this._gameCode = gameCode;
  215. this._utils.addListener(this);
  216. this.requestConfig();
  217. self._gameTime = new Date().getTime();
  218. VivoAd.Instance.onHide(() => {
  219. let curTime = new Date().getTime();
  220. let time = curTime - self._gameTime;
  221. self.durationLog(Math.floor(time / 1000), 0);
  222. self._gameTime = 0;
  223. this._utils.log("********用户退出游戏或退到后台: " + (time / 1000));
  224. });
  225. VivoAd.Instance.onShow(() => {
  226. self._gameTime = new Date().getTime();
  227. this._utils.log("*******用户回到游戏");
  228. });
  229. // this.index++;
  230. // if(this.index>2){
  231. // this.index = 0;
  232. // }
  233. // this.scheduleOnce(()=>{
  234. // SDK.Instance.init(gameCode,this.index,null);
  235. // },10);
  236. }
  237. //自动刷新广告
  238. private flushConfig() {
  239. if (!this._isVivo) {
  240. return;
  241. }
  242. let self = this;
  243. let go = () => {
  244. if (self._loading) {
  245. self.unschedule(go);
  246. return;
  247. }
  248. if (self._netInitCount > 2) {
  249. self._utils.ok(null);
  250. self.unschedule(go);
  251. return;
  252. }
  253. self._netInitCount++;
  254. self.requestConfig();
  255. }
  256. this.schedule(go, 10);
  257. }
  258. //监听网络回调
  259. onOk(config: any) {
  260. if (!this._isVivo) {
  261. return;
  262. }
  263. this._config = config;
  264. let self = this;
  265. if (this._config) {
  266. this._loading = true;
  267. VivoAd.Instance.initConfig(this._config, () => {
  268. if (self._initCallbacks) {
  269. self._initCallbacks(true);
  270. }
  271. });
  272. } else {
  273. self._utils.log("********* initConfig is faill : " + JSON.stringify(this._config));
  274. if (self._initCallbacks && !this._loading) {
  275. self._initCallbacks(false);
  276. this.flushConfig();
  277. }
  278. }
  279. }
  280. /**
  281. * 展示banner
  282. * @param type 原生广告类型
  283. * @param top 是否展示顶部
  284. * @param x
  285. * @param y
  286. */
  287. public showBanner(type: NativeType = NativeType.BANNER, top: boolean = false, y: number = 0, x: number = 0) {
  288. if (!this._isVivo) {
  289. return;
  290. }
  291. this.hideBanner();
  292. VivoAd.Instance.showBanner(type, top, y, x);
  293. }
  294. /**
  295. * 隐藏banner
  296. */
  297. public hideBanner() {
  298. if (!this._isVivo) {
  299. return;
  300. }
  301. VivoAd.Instance.hideBanner();
  302. }
  303. public hideIcon() {
  304. if (!this._isVivo) {
  305. return;
  306. }
  307. VivoAd.Instance.hideIcon();
  308. }
  309. /**
  310. * 展示原生插屏
  311. * 默认id节点为第一个0,有多个时,按服务端配置为准,进行传参
  312. * @param node 默认0
  313. * @param isforever true时,每次调用都会展示,false时,受服务器调用次数控制,默认false
  314. * @param id
  315. */
  316. public showInterstitial(id: InterstitialType = InterstitialType.INTERSTITIAL_GAME_OVER) {
  317. if (!this._isVivo) {
  318. return;
  319. }
  320. VivoAd.Instance.showInterstitial(id);
  321. }
  322. /**
  323. * 展示原生icon广告
  324. * 默认id节点为第一个0,有多个时,按服务端配置为准,进行传参
  325. * @param node
  326. * @param x
  327. * @param y
  328. * @param id
  329. */
  330. public showIconAd(x: number, y: number, id: number = 0) {
  331. if (!this._isVivo) {
  332. return;
  333. }
  334. this.hideIcon();
  335. VivoAd.Instance.showIconAd(id, x, y);
  336. }
  337. /*0显示视频*/
  338. public showRewardVideo(callBacks: Function, fail: Function, noAd?: Function) {
  339. console.log('a');
  340. if (!this._isVivo) {
  341. console.log('b');
  342. return;
  343. }
  344. console.log('c');
  345. VivoAd.Instance.showRewardVideo(callBacks, fail, noAd);
  346. console.log('d');
  347. }
  348. //toast提示框
  349. public showToast(message: string) {
  350. if (!this._isVivo) {
  351. return;
  352. }
  353. if (VivoAd.Instance) {
  354. VivoAd.Instance.showToast(message);
  355. }
  356. }
  357. /**
  358. * 游戏结算界面数分延迟
  359. * @param lable
  360. * @param score
  361. * @param cb
  362. */
  363. public gameResultDelayedTime(lable: cc.Label, score: number, cb: Function) {
  364. if (this._config && score > 0) {
  365. let dalayedTime = this._config.maxClickCount;
  366. if (dalayedTime > 0) {
  367. let count = 0;
  368. let s = Math.floor((dalayedTime / score) * 100) / 100;
  369. let add = score / 200;
  370. if (add < 1) {
  371. add = 1;
  372. }
  373. this._utils.log("----time: " + dalayedTime + " s: " + s + " add: " + add);
  374. let go = () => {
  375. count = Math.floor(count + add);
  376. if (count > score) {
  377. this.unschedule(go);
  378. lable.string = "" + score
  379. if (cb) {
  380. cb();
  381. }
  382. return;
  383. }
  384. if (lable) {
  385. lable.string = "" + count
  386. }
  387. }
  388. this.schedule(go, s);
  389. } else {
  390. lable.string = "" + score
  391. if (cb) {
  392. this.scheduleOnce(() => {
  393. cb();
  394. }, 0.2);
  395. }
  396. }
  397. } else {
  398. lable.string = "" + score
  399. if (cb) {
  400. this.scheduleOnce(() => {
  401. cb();
  402. }, 0.2);
  403. }
  404. }
  405. }
  406. /**
  407. * 激励AD 加载
  408. */
  409. public initAdForPage() {
  410. if (cc.sys.os === cc.sys.OS_ANDROID) {
  411. let deviceId = AAJS2.getDeviceUserId();
  412. console.log("zh:checkstatus:", ATRewardedVideoSDK.checkAdStatus(AAJS2.getPlacementId()));
  413. var setting = {};
  414. setting[ATRewardedVideoSDK.userIdKey] = deviceId;
  415. ATRewardedVideoSDK.loadRewardedVideo(AAJS2.getPlacementId(), setting);
  416. }
  417. }
  418. }
  419. cc["SDK"] = SDK.Instance;