AdSystem.ts 7.5 KB

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