AdSystem.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 = gData.gameData.configs.ServerConfig.ifShowAd == '1';
  30. }
  31. else {
  32. // this.ifShowAd = false;
  33. //test
  34. this.ifShowAd = gData.gameData.configs.ServerConfig.ifShowAd == '1';
  35. }
  36. //初始化Topon广告SDK
  37. this.adSdk.initSDK();
  38. }
  39. /**
  40. * 观看广告
  41. * @param callBack 广告相关的回调
  42. */
  43. public watchAd(callBack: Function) {
  44. if (this.isAdCooling()) {
  45. return;
  46. }
  47. this.videoAdcallBack = callBack;
  48. if (!this.ifShowAd) {
  49. this.wacthAdSuccess()
  50. }
  51. else {
  52. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  53. if (cc.sys.os == cc.sys.OS_ANDROID) {
  54. this.adSdk.showVideo();
  55. }
  56. else if (cc.sys.os == cc.sys.OS_IOS) {
  57. jsb.reflection.callStaticMethod("AppController", "showVideo", "");
  58. }
  59. }
  60. else {
  61. // WxMgr.getInstance().showRewardAd(pop)
  62. }
  63. }
  64. }
  65. /** 观看广告失败 */
  66. public watchAdFail() {
  67. mk.console.log("=== watchAdFail");
  68. this.videoAdcallBack(false);
  69. }
  70. /** 观看广告取消 */
  71. public watchAdCancel() {
  72. mk.console.log("=== watchAdCancel");
  73. this.videoAdcallBack(false);
  74. }
  75. /**
  76. * 观看广告成功
  77. * @param placementId 广告位id
  78. */
  79. public wacthAdSuccess(placementId = '') {
  80. mk.console.log("=== wacthAdSuccess");
  81. //累计视频次数
  82. mk.data.setTAEventUser(1, 'video_time', 1);
  83. //广告过后,渲染会有问题,统一下一帧再处理逻辑
  84. mk.ui.scheduleOnce(() => {
  85. if (!gData.adData.checkAdMax()) {
  86. this.videoAdcallBack(true);
  87. }
  88. this.initLastTimeStamp();
  89. }, 0)
  90. }
  91. /** 显示原生广告 */
  92. public showNative(type = 4) {
  93. if (!this.ifShowAd) {
  94. return;
  95. }
  96. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  97. if (cc.sys.os == cc.sys.OS_ANDROID) {
  98. this.adSdk.showNative(type);
  99. }
  100. else if (cc.sys.os == cc.sys.OS_IOS) {
  101. }
  102. }
  103. else {
  104. // WxMgr.getInstance().showRewardAd(pop)
  105. }
  106. }
  107. /** 销毁原生广告 */
  108. public destroyNativeAd() {
  109. if (!this.ifShowAd) {
  110. return;
  111. }
  112. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  113. if (cc.sys.os == cc.sys.OS_ANDROID) {
  114. this.adSdk.destroyNative();
  115. }
  116. else if (cc.sys.os == cc.sys.OS_IOS) {
  117. }
  118. }
  119. else {
  120. // WxMgr.getInstance().showRewardAd(pop)
  121. }
  122. }
  123. /** 上次播放视频时间戳,单位:毫秒 */
  124. private last_time_stamp: number = 0;
  125. private initLastTimeStamp() {
  126. let new_date = new Date();
  127. this.last_time_stamp = new_date.getTime();
  128. }
  129. /**
  130. * 视频是否冷却
  131. * @returns 视频冷却中
  132. */
  133. private isAdCooling(): boolean {
  134. let new_date = new Date();
  135. let new_time_stamp = new_date.getTime();
  136. let gap_time_stamp = new_time_stamp - this.last_time_stamp;
  137. if (gap_time_stamp <= 5000) {
  138. const time = 6 - Math.ceil(gap_time_stamp / 1000);
  139. mk.tip.pop('当前看视频太频繁了,请' + time + '秒后再试');
  140. return true;
  141. }
  142. return false;
  143. }
  144. public showBanner() {
  145. if (CC_DEBUG) {
  146. return;
  147. }
  148. if (!this.ifShowAd) {
  149. return;
  150. }
  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 全屏
  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. /**
  179. * 根据几率检测弹出插屏
  180. */
  181. public checkShowInterByChance() {
  182. if (!this.ifShowAd) {
  183. return;
  184. }
  185. if (!gData.gameData.adShowConfig) {
  186. return;
  187. }
  188. let chance = gData.gameData.configs.ServerConfig.interOpenChance;
  189. if (Math.random() <= chance) {
  190. this.showInterAd(0);
  191. }
  192. else {
  193. gData.moreGame.popMoreGamePopNode();
  194. }
  195. }
  196. }