AdSystem.ts 8.1 KB

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