AdSystem.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { VideoAdType } from "../../game/data/GameData";
  2. import { IADSDKMgr } from "../sdk/ad/IADSDKMgr";
  3. import JsbSystem from "./JsbSystem";
  4. /**
  5. * 广告管理类脚本
  6. * @description 显示/播放广告
  7. * @author 冯聪
  8. */
  9. export default class AdSystem {
  10. /** 构造函数
  11. * @param adSdk 传入当前使用广告sdk
  12. */
  13. constructor(adSdk: IADSDKMgr) {
  14. this.adSdk = adSdk;
  15. // this.init();
  16. }
  17. /** 广告sdk */
  18. private adSdk: IADSDKMgr = null;
  19. /** 是否显示广告 */
  20. public ifShowAd: boolean = true;
  21. /** 视频广告类型 */
  22. public videoAdType: VideoAdType = VideoAdType.Null;
  23. /** 视频广告回调 */
  24. public videoAdcallBack: Function = null;
  25. /** 初始化广告 */
  26. public init() {
  27. //判定下平台
  28. if (cc.sys.os == cc.sys.OS_ANDROID) {
  29. this.ifShowAd = true;
  30. }
  31. else {
  32. this.ifShowAd = false;
  33. }
  34. //初始化Topon广告SDK
  35. this.adSdk.initSDK();
  36. }
  37. /**
  38. * 观看广告
  39. * @param callBack 广告相关的回调
  40. */
  41. public watchAd(callBack: Function) {
  42. if (this.isAdCooling()) {
  43. return;
  44. }
  45. this.videoAdcallBack = callBack;
  46. if (!this.ifShowAd) {
  47. this.wacthAdSuccess()
  48. }
  49. else {
  50. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  51. if (cc.sys.os == cc.sys.OS_ANDROID) {
  52. // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop);
  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.ui.scheduleOnce(() => {
  82. if (!gData.adData.checkAdMax()) {
  83. this.videoAdcallBack(true);
  84. }
  85. this.initLastTimeStamp();
  86. }, 0)
  87. }
  88. /** 显示原生广告 */
  89. public showNative(type = 4) {
  90. if (!this.ifShowAd) {
  91. return;
  92. }
  93. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  94. if (cc.sys.os == cc.sys.OS_ANDROID) {
  95. // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop);
  96. this.adSdk.showNative(type);
  97. }
  98. else if (cc.sys.os == cc.sys.OS_IOS) {
  99. }
  100. }
  101. else {
  102. // WxMgr.getInstance().showRewardAd(pop)
  103. }
  104. }
  105. /** 销毁原生广告 */
  106. public destroyNativeAd() {
  107. if (!this.ifShowAd) {
  108. return;
  109. }
  110. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  111. if (cc.sys.os == cc.sys.OS_ANDROID) {
  112. // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop);
  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 (!this.ifShowAd) {
  145. return;
  146. }
  147. // console.log("===[AdSystem gameData.adShowConfig", gData.gameData.adShowConfig.is_show_banner);
  148. // if (gData.gameData.adShowConfig && gData.gameData.adShowConfig.is_show_banner == 1) {
  149. this.adSdk.showBanner();
  150. //}
  151. }
  152. public destoryBanner() {
  153. if (!this.ifShowAd) {
  154. return;
  155. }
  156. this.adSdk.destoryBanner();
  157. }
  158. /**
  159. * showInterAd
  160. * @param type 0 插屏 1 全屏
  161. */
  162. public showInterAd(type: number) {
  163. if (!this.ifShowAd) {
  164. return;
  165. }
  166. this.adSdk.showInter(type);
  167. }
  168. /**
  169. * 根据几率检测弹出插屏
  170. */
  171. public checkShowInterByChance() {
  172. if (!this.ifShowAd) {
  173. return;
  174. }
  175. if (!gData.gameData.adShowConfig) {
  176. return;
  177. }
  178. let chance = gData.gameData.configs.ServerConfig.interOpenChance;
  179. if (!chance) {
  180. chance = 0.4;
  181. }
  182. if (Math.random() <= chance) {
  183. this.showInterAd(0);
  184. }
  185. else {
  186. gData.moreGame.popMoreGamePopNode();
  187. }
  188. }
  189. }