AdSystem.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { IADSDKMgr } from "../sdk/ad/IADSDKMgr";
  2. import JsbSystem from "./JsbSystem";
  3. /**
  4. * 广告管理类脚本
  5. * @description 显示/播放广告
  6. * @author 冯聪
  7. */
  8. export default class AdSystem {
  9. /** 构造函数
  10. * @param adSdk 传入当前使用广告sdk
  11. */
  12. constructor(adSdk: IADSDKMgr) {
  13. this.adSdk = adSdk;
  14. // this.init();
  15. }
  16. /** 广告sdk */
  17. private adSdk: IADSDKMgr = null;
  18. /** 是否显示广告 */
  19. public ifShowAd: boolean = true;
  20. /** 视频广告回调 */
  21. public videoAdcallBack: Function = null;
  22. /** 初始化广告 */
  23. public init() {
  24. //判定下平台
  25. if (cc.sys.os == cc.sys.OS_ANDROID) {
  26. this.ifShowAd = true;
  27. }
  28. else {
  29. this.ifShowAd = false;
  30. }
  31. //初始化Topon广告SDK
  32. this.adSdk.initSDK();
  33. }
  34. /**
  35. * 观看广告
  36. * @param callBack 广告相关的回调
  37. */
  38. public watchAd(callBack: Function) {
  39. if (this.isAdCooling()) {
  40. return;
  41. }
  42. this.videoAdcallBack = callBack;
  43. if (!this.ifShowAd) {
  44. this.wacthAdSuccess()
  45. }
  46. else {
  47. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  48. if (cc.sys.os == cc.sys.OS_ANDROID) {
  49. // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop);
  50. this.adSdk.showVideo();
  51. }
  52. else if (cc.sys.os == cc.sys.OS_IOS) {
  53. jsb.reflection.callStaticMethod("AppController", "showVideo", "");
  54. }
  55. }
  56. else {
  57. // WxMgr.getInstance().showRewardAd(pop)
  58. }
  59. }
  60. }
  61. /** 观看广告失败 */
  62. public watchAdFail() {
  63. mk.console.log("=== watchAdFail");
  64. this.videoAdcallBack(false);
  65. }
  66. /** 观看广告取消 */
  67. public watchAdCancel() {
  68. mk.console.log("=== watchAdCancel");
  69. this.videoAdcallBack(false);
  70. }
  71. /**
  72. * 观看广告成功
  73. * @param placementId 广告位id
  74. */
  75. public wacthAdSuccess(placementId = '') {
  76. mk.console.log("=== wacthAdSuccess");
  77. this.videoAdcallBack(true);
  78. this.initLastTimeStamp();
  79. }
  80. /** 显示原生广告 */
  81. public showNative(type = 4) {
  82. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  83. if (cc.sys.os == cc.sys.OS_ANDROID) {
  84. // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop);
  85. this.adSdk.showNative(type);
  86. }
  87. else if (cc.sys.os == cc.sys.OS_IOS) {
  88. }
  89. }
  90. else {
  91. // WxMgr.getInstance().showRewardAd(pop)
  92. }
  93. }
  94. /** 销毁原生广告 */
  95. public destroyNativeAd() {
  96. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  97. if (cc.sys.os == cc.sys.OS_ANDROID) {
  98. // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop);
  99. this.adSdk.destroyNative();
  100. }
  101. else if (cc.sys.os == cc.sys.OS_IOS) {
  102. }
  103. }
  104. else {
  105. // WxMgr.getInstance().showRewardAd(pop)
  106. }
  107. }
  108. /** 上次播放视频时间戳,单位:毫秒 */
  109. private last_time_stamp: number = 0;
  110. private initLastTimeStamp() {
  111. let new_date = new Date();
  112. this.last_time_stamp = new_date.getTime();
  113. }
  114. /**
  115. * 视频是否冷却
  116. * @returns 视频冷却中
  117. */
  118. private isAdCooling(): boolean {
  119. let new_date = new Date();
  120. let new_time_stamp = new_date.getTime();
  121. let gap_time_stamp = new_time_stamp - this.last_time_stamp;
  122. if (gap_time_stamp <= 5000) {
  123. const time = 6 - Math.ceil(gap_time_stamp / 1000);
  124. mk.tip.pop('当前看视频太频繁了,请' + time + '秒后再试');
  125. return true;
  126. }
  127. return false;
  128. }
  129. public showBanner() {
  130. if (!this.ifShowAd) {
  131. return;
  132. }
  133. // console.log("===[AdSystem gameData.adShowConfig", gData.gameData.adShowConfig.is_show_banner);
  134. // if (gData.gameData.adShowConfig && gData.gameData.adShowConfig.is_show_banner == 1) {
  135. this.adSdk.showBanner();
  136. //}
  137. }
  138. public destoryBanner() {
  139. this.adSdk.destoryBanner();
  140. }
  141. /**
  142. * showInterAd
  143. * @param type 0 插屏 1 全屏
  144. */
  145. public showInterAd(type: number) {
  146. if (!this.ifShowAd) {
  147. return;
  148. }
  149. this.adSdk.showInter(type);
  150. }
  151. /**
  152. * 根据几率检测弹出插屏
  153. */
  154. public checkShowInterByChance() {
  155. if (!gData.gameData.adShowConfig) {
  156. return;
  157. }
  158. if (Math.random() <= gData.gameData.adShowConfig.insert_probability) {
  159. this.showInterAd(0);
  160. }
  161. else {
  162. gData.moreGame.popMoreGamePopNode();
  163. }
  164. }
  165. }