AdData.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. import Util from "../../before/util/Util";
  2. import { Data } from "../../mk/data/Data";
  3. import { GameProp, VideoAdType } from "./GameData";
  4. /**
  5. * @description 广告数据
  6. * @author 邹勇
  7. */
  8. export class AdData {
  9. public placementId = '';
  10. public adunit_format = '';
  11. public network_type = '';
  12. public network_placement_id = '';
  13. public network_firm_id = '';
  14. public adsource_id = '';
  15. public adsource_index = '';
  16. public adsource_price = '100';
  17. public adsource_isheaderbidding = '';
  18. public publisher_revenue = '';
  19. public precision = '';
  20. public ecpm_level = '';
  21. /** 视频类型 */
  22. public ad_subType: AdFun;
  23. private _watchNumToday: number = 0;
  24. /** 当天看的视频次数 */
  25. public set watchNumToday(v: number) {
  26. this._watchNumToday = v;
  27. }
  28. public get watchNumToday(): number {
  29. return this._watchNumToday;
  30. }
  31. private adMap: Map<number, Data>
  32. /** 用户价值等级 1 低 2 中 3 高 等等*/
  33. public ecpmLevel = 0;
  34. /** 自动弹窗间隔时间字典 */
  35. private AutoRedMoneyMap: Map<number, number> = new Map();
  36. /** 弹出红包ecpm等级数组 */
  37. private TimingRedMoneyArr = [];
  38. /** 生产按钮和菜单里的种植按钮,概率弹出恭喜发财红包ecpm等级数组 */
  39. private ProductionButtonArr = [];
  40. /** 概率弹出全屏广告ecpm等级数组 */
  41. private FullScreenAdArr = [];
  42. /** 弹出插屏、互推概率数组 */
  43. private ABCInsertAdPercentArr = [];
  44. /** 概率弹出AB插屏广告ecpm等级数组 */
  45. private ABCInsertAdArr = [];
  46. /** 提现弹出全屏ecpm等级数据 */
  47. private FullCsahAdArr = [];
  48. public init() {
  49. this.adMap = new Map<number, Data>();
  50. this.adMap.set(AdFun.bubble, gData.reward)
  51. this.adMap.set(AdFun.checkpoint, gData.reward)
  52. this.adMap.set(AdFun.settlement, gData.reward)
  53. this.adMap.set(AdFun.turntable, gData.turnable)
  54. this.adMap.set(AdFun.sign, gData.sign)
  55. this.adMap.set(AdFun.bigCash, gData.blessingBag)
  56. this.adMap.set(AdFun.cashOutAddCash, gData.walletCashOutData);
  57. this.adMap.set(AdFun.harvest, gData.harvestData);
  58. this.adMap.set(AdFun.farmMap, gData.farmMapData);
  59. this.adMap.set(AdFun.lineUp, gData.lineUpUIData);
  60. }
  61. /**
  62. * 每次视频次数是否已经到达上限
  63. * @returns true 已达最大次数 false没有,可以有奖励
  64. */
  65. public checkAdMax() {
  66. this.watchNumToday++;
  67. let max = parseInt(gData.gameData.configs.ServerConfig.VideoTimesMax);
  68. mk.console.logSingle('checkAdMax watchNumToday ', this.watchNumToday);
  69. mk.console.logSingle('checkAdMax max ', max);
  70. if (this.watchNumToday > max) {
  71. mk.tip.pop("今日看广告获得奖励的次数已用完");
  72. mk.ui.closeAllUI();
  73. return true;
  74. }
  75. else {
  76. return false;
  77. }
  78. }
  79. public updateData(placementId, data) {
  80. mk.console.logSingle("updateADData:" + placementId, data);
  81. this.placementId = placementId;
  82. this.adunit_format = data.adunit_format;
  83. this.network_type = data.network_type;
  84. this.network_placement_id = data.network_placement_id;
  85. this.network_firm_id = data.network_firm_id;
  86. this.adsource_id = data.adsource_id;
  87. this.adsource_index = data.adsource_index;
  88. this.adsource_price = data.adsource_price;
  89. this.adsource_isheaderbidding = data.adsource_isheaderbidding;
  90. this.publisher_revenue = data.publisher_revenue;
  91. this.precision = data.precision;
  92. this.ecpm_level = data.ecpm_level;
  93. }
  94. public async watchVideo(type: AdFun, callBack: Function = null) {
  95. this.ad_subType = type;
  96. let data = {
  97. "imei": gData.appData.machineInfo.imei,
  98. "idfa": gData.appData.machineInfo.idfa,
  99. "oaid": gData.appData.machineInfo.oaid,
  100. "uin": gData.loginData.uin,
  101. "version": gData.appData.appVersion,
  102. "tf_channel": gData.appData.tfChannel,
  103. "destoon_ad_place": this.placementId,
  104. "ad_type": 1,
  105. "adunit_format": this.adunit_format,
  106. "network_type": this.network_type,
  107. "network_placement_id": this.network_placement_id,
  108. "network_firm_id": this.network_firm_id,
  109. "adsource_id": this.adsource_id,
  110. "adsource_index": this.adsource_index,
  111. "adsource_price": this.adsource_price,
  112. "adsource_isheaderbidding": this.adsource_isheaderbidding,
  113. "publisher_revenue": this.publisher_revenue,
  114. "precision_ecpm": this.precision,
  115. "ecpm_level": this.ecpm_level,
  116. "cfg_version": gData.appData.version,
  117. "adsource_ecpm": gData.gameData.adShowConfig.average_ecpm,
  118. "ad_sbuType": this.ad_subType,
  119. }
  120. mk.console.logSingle("updateVideoData:", data);
  121. let response = await mk.http.sendData('updateVideo', data);
  122. if (response.errcode != 0) {
  123. return;
  124. }
  125. let times = gData.gameData.getProp(GameProp.videoTimes);
  126. if (!times) {
  127. times = 0;
  128. }
  129. gData.gameData.setProp(GameProp.videoTimes, ++times);
  130. if (times < 5) {
  131. mk.data.sendXYEvent("videoEnd_" + times, "第" + times + "次看视频");
  132. }
  133. let curData = this.adMap.get(this.ad_subType)
  134. if (curData) {
  135. curData.setAdData(response.data);
  136. }
  137. if(callBack)
  138. {
  139. callBack(response.data);
  140. }
  141. }
  142. /** 更新其他广告次数
  143. * @param type 1:激励视频完播、2:插屏展示、3:信息流展示、 4:激励视频展⽰、5:激励视频加载失败、6:开屏展示、7:banner展示、
  144. * 8:插屏-全屏视频 完播、9:插屏-全屏视频 展示、10:插屏-全屏视频 加载失败、11:插屏加载失败、12:信息流加载失败、13:开屏加载失败、14:banner加载失败、
  145. * 51:激励视频点击、52:插屏点击、53:信息流点击、54:开屏点击、55:banner点击、56:插屏-全屏视频点击
  146. * 71:激励视频去重点击、72:插屏去重点击、73:信息流去重点击、74:开屏去重点击、75:banner去重点击、76:插屏-全屏视频去重点击
  147. */
  148. public async updateADLog(type, placementId, data = null) {
  149. let sData = null
  150. if (!data) {
  151. sData = {
  152. "imei": gData.appData.machineInfo.imei,
  153. "idfa": gData.appData.machineInfo.idfa,
  154. "oaid": gData.appData.machineInfo.oaid,
  155. "uin": gData.loginData.uin,
  156. "version": gData.appData.appVersion,
  157. "tf_channel": gData.appData.tfChannel,
  158. "destoon_ad_place": placementId,
  159. "ad_type": type,
  160. "adunit_format": '',
  161. "network_type": '',
  162. "network_placement_id": '',
  163. "network_firm_id": '',
  164. "adsource_id": '',
  165. "adsource_index": '',
  166. "adsource_price": '',
  167. "adsource_isheaderbidding": '',
  168. "publisher_revenue": '',
  169. "precision": '',
  170. "ecpm_level": '',
  171. "platformRisk": 1
  172. }
  173. }
  174. else {
  175. sData = {
  176. "imei": gData.appData.machineInfo.imei,
  177. "idfa": gData.appData.machineInfo.idfa,
  178. "oaid": gData.appData.machineInfo.oaid,
  179. "uin": gData.loginData.uin,
  180. "version": gData.appData.appVersion,
  181. "tf_channel": gData.appData.tfChannel,
  182. "destoon_ad_place": placementId,
  183. "ad_type": type,
  184. "adunit_format": data.adunit_format,
  185. "network_type": data.network_type,
  186. "network_placement_id": data.network_placement_id,
  187. "network_firm_id": data.network_firm_id,
  188. "adsource_id": data.adsource_id,
  189. "adsource_index": data.adsource_index,
  190. "adsource_price": data.adsource_price,
  191. "adsource_isheaderbidding": data.adsource_isheaderbidding,
  192. "publisher_revenue": data.publisher_revenue,
  193. "precision": data.precision,
  194. "ecpm_level": data.ecpm_level,
  195. "platformRisk": 1
  196. }
  197. }
  198. let response = await mk.http.sendData('updateADLog', sData);
  199. if (response && response.errcode == 0) {
  200. if (response.data) {
  201. mk.console.logSingle('solution ', response);
  202. //0-无处理;1-警告;2-封广告30分钟;3-封广告12小时;4-用户拉黑
  203. gData.warnTipData.setSolution(response.data.solution);
  204. gData.warnTipData.setShutdownAds(response.data.shutdown_ads);
  205. }
  206. }
  207. }
  208. /** 初始化ecpm及弹出广告相关数据 */
  209. initEcpmData() {
  210. gData.adData.setEcpmLevel();
  211. gData.adData.setPopClickTimes();
  212. this.TimingRedMoneyArr = gData.gameData.configs.ServerConfig.TimingRedMoney.split(',');
  213. this.ProductionButtonArr = gData.gameData.configs.ServerConfig.ProductionButton.split(',');
  214. this.FullScreenAdArr = gData.gameData.configs.ServerConfig.FullScreenAd.split(',');
  215. let arr = gData.gameData.configs.ServerConfig.ABInsertAdPercent.split(',');
  216. let per = 0;
  217. for (var i = 0; i < arr.length; i++) {
  218. per += parseFloat(arr[i]);
  219. this.ABCInsertAdPercentArr[i] = per;
  220. }
  221. this.ABCInsertAdArr = gData.gameData.configs.ServerConfig.ABInsertAd.split(',');
  222. this.FullCsahAdArr = gData.gameData.configs.ServerConfig.FullCsahAd.split(',');
  223. }
  224. /** 设置用户ecpm分级, 自动弹窗字典等 */
  225. public setEcpmLevel() {
  226. // //test
  227. // this.ecpmLevel = 1;
  228. // this.AutoRedMoneyMap.set(1, 20);
  229. // return;
  230. let forceAd = gData.gameData.configs.ForceAd;
  231. let len = forceAd.length;
  232. let ecpmlevel = -1;
  233. for (var i = 0; i < len; i++) {
  234. if (gData.gameData.adShowConfig.average_ecpm <= parseInt(forceAd[i].ecpmMax)) {
  235. if (ecpmlevel == -1) {
  236. ecpmlevel = parseInt(forceAd[i].value);
  237. }
  238. this.AutoRedMoneyMap.set(parseInt(forceAd[i].value), parseInt(forceAd[i].autoRedMoney))
  239. }
  240. }
  241. this.ecpmLevel = ecpmlevel;
  242. }
  243. /** 检查是否显示插屏、互推(概率弹出)
  244. * @type 1:弹出全屏插屏广告 2:延时弹出大插屏 3:先弹出插屏,再弹出大插屏 4:互推红包
  245. */
  246. checkShowFullInter(type, des = null) {
  247. let per = 0;
  248. let pop = true;
  249. switch (type) {
  250. case 1:
  251. per = this.getPerByEcpm(RateConfig.RC_fullScreen);
  252. pop = Math.random() < per;
  253. if (!pop) {
  254. return;
  255. }
  256. if (this.FullScreenAdArr.indexOf(this.ecpmLevel.toString()) == -1) {
  257. return;
  258. }
  259. mk.ad.showInterAd(1, des);
  260. break;
  261. case 2:
  262. per = this.getPerByEcpm(RateConfig.RC_tableScreen);
  263. pop = Math.random() < per;
  264. if (!pop) {
  265. return;
  266. }
  267. let delayArr = gData.gameData.configs.ServerConfig.AdDelay.split(',');
  268. let min = parseFloat(delayArr[0]);
  269. let max = parseFloat(delayArr[1]);
  270. let delay = (min + (max - min) * Math.random()) * 1000;
  271. setTimeout(() => {
  272. mk.ad.showInterAd(2, des);
  273. }, delay);
  274. break;
  275. case 3:
  276. per = this.getPerByEcpm(RateConfig.RC_tableScreen);
  277. pop = Math.random() < per;
  278. if (!pop) {
  279. return;
  280. }
  281. let delayArr1 = gData.gameData.configs.ServerConfig.AdDelay.split(',');
  282. let min1 = parseFloat(delayArr1[0]);
  283. let max1 = parseFloat(delayArr1[1]);
  284. let delay1 = (min1 + (max1 - min1) * Math.random()) * 1000;
  285. let delay2 = (min1 + (max1 - min1) * Math.random()) * 1000;
  286. setTimeout(() => {
  287. mk.ad.showInterAd(0, des);
  288. setTimeout(() => {
  289. mk.ad.showInterAd(2, des);
  290. }, delay2);
  291. }, delay1);
  292. break;
  293. case 4:
  294. per = this.getPerByEcpm(RateConfig.RC_moreGameRedBag);
  295. pop = Math.random() < per;
  296. if (!pop) {
  297. return;
  298. }
  299. gData.moreGame.popNewOpenRedBag();
  300. break;
  301. }
  302. }
  303. /** 弹出插屏 */
  304. checkPopInter(des) {
  305. if (this.ABCInsertAdArr.indexOf(this.ecpmLevel.toString()) == -1) {
  306. return;
  307. }
  308. let ran = Math.random();
  309. let len = this.ABCInsertAdPercentArr.length;
  310. let index = 0;
  311. for (var i = 0; i < len; i++) {
  312. if (ran <= this.ABCInsertAdPercentArr[i]) {
  313. index = i + 2;
  314. break;
  315. }
  316. }
  317. this.checkShowFullInter(index, des);
  318. }
  319. /** 点击提现弹出全屏插屏 */
  320. checkPopCashFull() {
  321. if (this.FullCsahAdArr.indexOf(this.ecpmLevel.toString()) == -1) {
  322. return;
  323. }
  324. mk.ad.showInterAd(1);
  325. }
  326. checkShowVideoIcon() {
  327. if (this.FullCsahAdArr.indexOf(this.ecpmLevel.toString()) == -1) {
  328. return false;
  329. }
  330. return true;
  331. }
  332. //定时弹出计时
  333. private timeAd = 0;
  334. private test = true;
  335. /** 定时弹出红包 */
  336. startPopRed(dt) {
  337. if (mk.guide.isGuiding() || mk.ui.getCurOnPanel('redBagTask') || mk.ui.getCurOnPanel('productReward2')) {
  338. this.timeAd = 0;
  339. return;
  340. }
  341. if (gData.gameData.isTouchScreen) {
  342. gData.gameData.isTouchScreen = false;
  343. this.timeAd = 0;
  344. return;
  345. }
  346. if (this.TimingRedMoneyArr.indexOf(this.ecpmLevel.toString()) == -1) {
  347. return;
  348. }
  349. this.timeAd += dt;
  350. let span = this.AutoRedMoneyMap.get(this.ecpmLevel);
  351. if (this.timeAd >= span) {
  352. this.timeAd = 0;
  353. mk.ad.videoAdType = VideoAdType.video_init_19;
  354. gData.gameData.init_hideRedBag = true;
  355. mk.ui.openPanel('module/newOpenRedBag/newOpenRedBag');
  356. }
  357. }
  358. /** 需要弹出红包的点击数 */
  359. popClickTimes = 0;
  360. /** 累计点击数 */
  361. clickTimesAdd = 0;
  362. setPopClickTimes() {
  363. let arr = gData.gameData.configs.ServerConfig.ClickProduction.split(',');
  364. this.popClickTimes = Util.rnd(parseInt(arr[0]), parseInt(arr[1]));
  365. }
  366. checkPopRed() {
  367. if (this.ProductionButtonArr.indexOf(this.ecpmLevel.toString()) == -1) {
  368. return;
  369. }
  370. if (mk.guide.isGuiding()) {
  371. return;
  372. }
  373. this.clickTimesAdd++;
  374. if (this.clickTimesAdd >= this.popClickTimes) {
  375. this.clickTimesAdd = 0;
  376. this.setPopClickTimes();
  377. if (Math.random() <= this.getPerByEcpm()) {
  378. mk.ad.videoAdType = VideoAdType.video_init_17;
  379. mk.ui.openPanel('module/newOpenRedBag/newOpenRedBag');
  380. }
  381. }
  382. }
  383. getPerByEcpm(type: RateConfig = RateConfig.RC_fullScreen) {
  384. // //test
  385. // return 0.26;
  386. let forceAd = gData.gameData.configs.ForceAd;
  387. let len = forceAd.length;
  388. let per = 0;
  389. for (var i = 0; i < len; i++) {
  390. switch (type) {
  391. case RateConfig.RC_fullScreen:
  392. if (gData.gameData.adShowConfig.average_ecpm <= parseInt(forceAd[i].ecpmMax)) {
  393. per = parseFloat(forceAd[i].qpPercent);
  394. return per;
  395. }
  396. case RateConfig.RC_tableScreen:
  397. if (gData.gameData.adShowConfig.average_ecpm <= parseInt(forceAd[i].ecpmMax)) {
  398. per = parseFloat(forceAd[i].cpPercent);
  399. return per;
  400. }
  401. case RateConfig.RC_banner:
  402. if (gData.gameData.adShowConfig.average_ecpm <= parseInt(forceAd[i].ecpmMax)) {
  403. per = parseFloat(forceAd[i].baPercent);
  404. return per;
  405. }
  406. case RateConfig.RC_infrormationFlow:
  407. if (gData.gameData.adShowConfig.average_ecpm <= parseInt(forceAd[i].ecpmMax)) {
  408. per = parseFloat(forceAd[i].xxPercent);
  409. return per;
  410. }
  411. case RateConfig.RC_splash:
  412. if (gData.gameData.adShowConfig.average_ecpm <= parseInt(forceAd[i].ecpmMax)) {
  413. per = parseFloat(forceAd[i].kpPercent);
  414. return per;
  415. }
  416. case RateConfig.RC_superDouble:
  417. if (gData.gameData.adShowConfig.average_ecpm <= parseInt(forceAd[i].ecpmMax)) {
  418. per = parseFloat(forceAd[i].jbPercent);
  419. return per;
  420. }
  421. case RateConfig.RC_moreGameRedBag:
  422. if (gData.gameData.adShowConfig.average_ecpm <= parseInt(forceAd[i].ecpmMax)) {
  423. per = parseFloat(forceAd[i].htPercent);
  424. return per;
  425. }
  426. }
  427. }
  428. return per;
  429. }
  430. savePreLoadEcpm(data) {
  431. let times = gData.gameData.getProp(GameProp.videoTimes);
  432. console.log('savePreLoadEcpm times ', times)
  433. if (!times) {
  434. console.log('savePreLoadEcpm data ', data.adsource_price)
  435. mk.http.sendData('user/savePreLoadEcpm', data);
  436. }
  437. }
  438. }
  439. /**
  440. * 看视频类型
  441. */
  442. export enum AdFun {
  443. bubble = 1, //气泡视频
  444. checkpoint = 2, //关卡消除视频
  445. settlement = 3, //关卡通关视频
  446. turntable = 4, //转盘视频
  447. sign = 5, //签到视频
  448. bigCash = 6, //福袋视频
  449. cashOutAddCash = 8, //钱包提现加现金视频
  450. harvest = 9, //收获超级加倍视频
  451. farmMap = 10, //图鉴领取视频
  452. lineUp = 11, //插队视频
  453. addProductTimes = 12,//获得生产次数
  454. }
  455. export enum RateConfig {
  456. RC_fullScreen,
  457. RC_tableScreen,
  458. RC_banner,
  459. RC_infrormationFlow,
  460. RC_splash,
  461. RC_superDouble,
  462. RC_moreGameRedBag
  463. }