AdSystem.ts 6.2 KB

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