AdSystem.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { VideoAdType } from "../../game/data/GameData";
  2. import { IADSDKMgr } from "../sdk/ad/IADSDKMgr";
  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 videoAdType: VideoAdType = VideoAdType.video_init_1;
  22. /** 视频广告回调 */
  23. public videoAdcallBack: Function = null;
  24. /** 初始化广告 */
  25. public init() {
  26. //判定下平台
  27. if (cc.sys.os == cc.sys.OS_ANDROID) {
  28. this.ifShowAd = gData.gameData.configs.ServerConfig.ifShowAd == '1';
  29. }
  30. else {
  31. this.ifShowAd = false;
  32. //test
  33. //this.ifShowAd = gData.gameData.configs.ServerConfig.ifShowAd == '1';
  34. }
  35. //初始化Topon广告SDK
  36. this.adSdk.initSDK();
  37. }
  38. /**
  39. * 观看广告
  40. * @param callBack 广告相关的回调
  41. */
  42. public watchAd(callBack: Function) {
  43. if (this.isAdCooling()) {
  44. return;
  45. }
  46. this.videoAdcallBack = callBack;
  47. if (!this.ifShowAd) {
  48. this.wacthAdSuccess()
  49. }
  50. else {
  51. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  52. if (cc.sys.os == cc.sys.OS_ANDROID) {
  53. this.adSdk.showVideo();
  54. }
  55. else if (cc.sys.os == cc.sys.OS_IOS) {
  56. jsb.reflection.callStaticMethod("AppController", "showVideo", "");
  57. }
  58. }
  59. else {
  60. // WxMgr.getInstance().showRewardAd(pop)
  61. }
  62. }
  63. }
  64. /** 观看广告失败 */
  65. public watchAdFail() {
  66. mk.console.log("=== watchAdFail");
  67. this.videoAdcallBack(false);
  68. }
  69. /** 观看广告取消 */
  70. public watchAdCancel() {
  71. mk.console.log("=== watchAdCancel");
  72. this.videoAdcallBack(false);
  73. }
  74. /**
  75. * 观看广告成功
  76. * @param placementId 广告位id
  77. */
  78. public wacthAdSuccess(placementId = '') {
  79. mk.console.log("=== wacthAdSuccess");
  80. //累计视频次数
  81. mk.data.setTAEventUser(1, 'video_time', 1);
  82. //广告过后,渲染会有问题,统一下一帧再处理逻辑
  83. mk.ui.scheduleOnce(() => {
  84. if (!gData.adData.checkAdMax()) {
  85. this.videoAdcallBack(true);
  86. }
  87. this.initLastTimeStamp();
  88. }, 0)
  89. }
  90. /** 显示原生广告 */
  91. public showNative(type = 4) {
  92. if (!this.ifShowAd) {
  93. return;
  94. }
  95. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  96. if (cc.sys.os == cc.sys.OS_ANDROID) {
  97. this.adSdk.showNative(type);
  98. }
  99. else if (cc.sys.os == cc.sys.OS_IOS) {
  100. }
  101. }
  102. else {
  103. // WxMgr.getInstance().showRewardAd(pop)
  104. }
  105. }
  106. /** 销毁原生广告 */
  107. public destroyNativeAd() {
  108. if (!this.ifShowAd) {
  109. return;
  110. }
  111. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  112. if (cc.sys.os == cc.sys.OS_ANDROID) {
  113. this.adSdk.destroyNative();
  114. }
  115. else if (cc.sys.os == cc.sys.OS_IOS) {
  116. }
  117. }
  118. else {
  119. // WxMgr.getInstance().showRewardAd(pop)
  120. }
  121. }
  122. /** 上次播放视频时间戳,单位:毫秒 */
  123. private last_time_stamp: number = 0;
  124. private initLastTimeStamp() {
  125. let new_date = new Date();
  126. this.last_time_stamp = new_date.getTime();
  127. }
  128. /**
  129. * 视频是否冷却
  130. * @returns 视频冷却中
  131. */
  132. private isAdCooling(): boolean {
  133. let new_date = new Date();
  134. let new_time_stamp = new_date.getTime();
  135. let gap_time_stamp = new_time_stamp - this.last_time_stamp;
  136. if (gap_time_stamp <= 5000) {
  137. const time = 6 - Math.ceil(gap_time_stamp / 1000);
  138. mk.tip.pop('当前看视频太频繁了,请' + time + '秒后再试');
  139. return true;
  140. }
  141. return false;
  142. }
  143. public showBanner() {
  144. if (CC_DEBUG) {
  145. return;
  146. }
  147. if (!this.ifShowAd) {
  148. return;
  149. }
  150. mk.console.logSingle('banner', 'showBanner');
  151. // console.log("===[AdSystem gameData.adShowConfig", gData.gameData.adShowConfig.is_show_banner);
  152. // if (gData.gameData.adShowConfig && gData.gameData.adShowConfig.is_show_banner == 1) {
  153. this.adSdk.showBanner();
  154. //}
  155. }
  156. public destoryBanner() {
  157. if (CC_DEBUG) {
  158. return;
  159. }
  160. if (!this.ifShowAd) {
  161. return;
  162. }
  163. this.adSdk.destoryBanner();
  164. }
  165. /**
  166. * showInterAd
  167. * @param type 0 插屏 1 全屏 2 大插屏
  168. */
  169. public showInterAd(type: number) {
  170. if (CC_DEBUG) {
  171. return;
  172. }
  173. if (!this.ifShowAd) {
  174. return;
  175. }
  176. this.adSdk.showInter(type);
  177. }
  178. }