AdSystem.ts 7.6 KB

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