AdSystem.ts 6.1 KB

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