AdData.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. }
  60. /**
  61. * 每次视频次数是否已经到达上限
  62. * @returns true 已达最大次数 false没有,可以有奖励
  63. */
  64. public checkAdMax() {
  65. this.watchNumToday++;
  66. let max = parseInt(gData.gameData.configs.ServerConfig.VideoTimesMax);
  67. mk.console.logSingle('checkAdMax watchNumToday ', this.watchNumToday);
  68. mk.console.logSingle('checkAdMax max ', max);
  69. if (this.watchNumToday > max) {
  70. mk.tip.pop("今日看广告获得奖励的次数已用完");
  71. mk.ui.closeAllUI();
  72. return true;
  73. }
  74. else {
  75. return false;
  76. }
  77. }
  78. public updateData(placementId, data) {
  79. mk.console.logSingle("updateADData:" + placementId, data);
  80. this.placementId = placementId;
  81. this.adunit_format = data.adunit_format;
  82. this.network_type = data.network_type;
  83. this.network_placement_id = data.network_placement_id;
  84. this.network_firm_id = data.network_firm_id;
  85. this.adsource_id = data.adsource_id;
  86. this.adsource_index = data.adsource_index;
  87. this.adsource_price = data.adsource_price;
  88. this.adsource_isheaderbidding = data.adsource_isheaderbidding;
  89. this.publisher_revenue = data.publisher_revenue;
  90. this.precision = data.precision;
  91. this.ecpm_level = data.ecpm_level;
  92. }
  93. public async watchVideo(type: AdFun) {
  94. this.ad_subType = type;
  95. let data = {
  96. "imei": gData.appData.machineInfo.imei,
  97. "idfa": gData.appData.machineInfo.idfa,
  98. "oaid": gData.appData.machineInfo.oaid,
  99. "uin": gData.loginData.uin,
  100. "version": gData.appData.appVersion,
  101. "tf_channel": gData.appData.tfChannel,
  102. "destoon_ad_place": this.placementId,
  103. "ad_type": 1,
  104. "adunit_format": this.adunit_format,
  105. "network_type": this.network_type,
  106. "network_placement_id": this.network_placement_id,
  107. "network_firm_id": this.network_firm_id,
  108. "adsource_id": this.adsource_id,
  109. "adsource_index": this.adsource_index,
  110. "adsource_price": this.adsource_price,
  111. "adsource_isheaderbidding": this.adsource_isheaderbidding,
  112. "publisher_revenue": this.publisher_revenue,
  113. "precision_ecpm": this.precision,
  114. "ecpm_level": this.ecpm_level,
  115. "cfg_version": gData.appData.version,
  116. "adsource_ecpm": gData.gameData.adShowConfig.average_ecpm,
  117. "ad_sbuType": this.ad_subType,
  118. }
  119. mk.console.logSingle("updateVideoData:", data);
  120. let response = await mk.http.sendData('updateVideo', data);
  121. if (response.errcode != 0) {
  122. return;
  123. }
  124. let times = gData.gameData.getProp(GameProp.videoTimes);
  125. if (!times) {
  126. times = 0;
  127. }
  128. gData.gameData.setProp(GameProp.videoTimes, ++times);
  129. if (times < 5) {
  130. mk.data.sendXYEvent("videoEnd_" + times, "第" + times + "次看视频");
  131. }
  132. let curData = this.adMap.get(this.ad_subType)
  133. if (curData) {
  134. curData.setAdData(response.data);
  135. }
  136. }
  137. /** 更新其他广告次数
  138. * @param type 1:激励视频完播、2:插屏展示、3:信息流展示、 4:激励视频展⽰、5:激励视频加载失败、6:开屏展示、7:banner展示、
  139. * 8:插屏-全屏视频 完播、9:插屏-全屏视频 展示、10:插屏-全屏视频 加载失败、11:插屏加载失败、12:信息流加载失败、13:开屏加载失败、14:banner加载失败、
  140. * 51:激励视频点击、52:插屏点击、53:信息流点击、54:开屏点击、55:banner点击、56:插屏-全屏视频点击
  141. */
  142. public updateADLog(type, placementId, data = null) {
  143. let sData = null
  144. if (!data) {
  145. sData = {
  146. "imei": gData.appData.machineInfo.imei,
  147. "idfa": gData.appData.machineInfo.idfa,
  148. "oaid": gData.appData.machineInfo.oaid,
  149. "uin": gData.loginData.uin,
  150. "version": gData.appData.appVersion,
  151. "tf_channel": gData.appData.tfChannel,
  152. "destoon_ad_place": placementId,
  153. "ad_type": type,
  154. "adunit_format": '',
  155. "network_type": '',
  156. "network_placement_id": '',
  157. "network_firm_id": '',
  158. "adsource_id": '',
  159. "adsource_index": '',
  160. "adsource_price": '',
  161. "adsource_isheaderbidding": '',
  162. "publisher_revenue": '',
  163. "precision": '',
  164. "ecpm_level": ''
  165. }
  166. }
  167. else {
  168. sData = {
  169. "imei": gData.appData.machineInfo.imei,
  170. "idfa": gData.appData.machineInfo.idfa,
  171. "oaid": gData.appData.machineInfo.oaid,
  172. "uin": gData.loginData.uin,
  173. "version": gData.appData.appVersion,
  174. "tf_channel": gData.appData.tfChannel,
  175. "destoon_ad_place": placementId,
  176. "ad_type": type,
  177. "adunit_format": data.adunit_format,
  178. "network_type": data.network_type,
  179. "network_placement_id": data.network_placement_id,
  180. "network_firm_id": data.network_firm_id,
  181. "adsource_id": data.adsource_id,
  182. "adsource_index": data.adsource_index,
  183. "adsource_price": data.adsource_price,
  184. "adsource_isheaderbidding": data.adsource_isheaderbidding,
  185. "publisher_revenue": data.publisher_revenue,
  186. "precision": data.precision,
  187. "ecpm_level": data.ecpm_level
  188. }
  189. }
  190. mk.http.sendData('updateADLog', sData);
  191. }
  192. /** 初始化ecpm及弹出广告相关数据 */
  193. initEcpmData() {
  194. gData.adData.setEcpmLevel();
  195. gData.adData.setPopClickTimes();
  196. this.TimingRedMoneyArr = gData.gameData.configs.ServerConfig.TimingRedMoney.split(',');
  197. this.ProductionButtonArr = gData.gameData.configs.ServerConfig.ProductionButton.split(',');
  198. this.FullScreenAdArr = gData.gameData.configs.ServerConfig.FullScreenAd.split(',');
  199. let arr = gData.gameData.configs.ServerConfig.ABInsertAdPercent.split(',');
  200. let per = 0;
  201. for (var i = 0; i < arr.length; i++) {
  202. per += parseFloat(arr[i]);
  203. this.ABCInsertAdPercentArr[i] = per;
  204. }
  205. this.ABCInsertAdArr = gData.gameData.configs.ServerConfig.ABInsertAd.split(',');
  206. this.FullCsahAdArr = gData.gameData.configs.ServerConfig.FullCsahAd.split(',');
  207. }
  208. /** 设置用户ecpm分级, 自动弹窗字典等 */
  209. public setEcpmLevel() {
  210. // //test
  211. // this.ecpmLevel = 1;
  212. // this.AutoRedMoneyMap.set(1, 20);
  213. // return;
  214. let forceAd = gData.gameData.configs.ForceAd;
  215. let len = forceAd.length;
  216. let ecpmlevel = -1;
  217. for (var i = 0; i < len; i++) {
  218. if (gData.gameData.adShowConfig.average_ecpm <= parseInt(forceAd[i].ecpmMax)) {
  219. if (ecpmlevel == -1) {
  220. ecpmlevel = parseInt(forceAd[i].value);
  221. }
  222. this.AutoRedMoneyMap.set(parseInt(forceAd[i].value), parseInt(forceAd[i].autoRedMoney))
  223. }
  224. }
  225. this.ecpmLevel = ecpmlevel;
  226. }
  227. /** 检查是否显示插屏、互推(概率弹出)
  228. * @type 1:弹出全屏插屏广告 2:延时弹出大插屏 3:先弹出插屏,再弹出大插屏 4:互推红包
  229. */
  230. checkShowFullInter(type, des = null) {
  231. let per = this.getPerByEcpm();
  232. let pop = Math.random() < per;
  233. if (!pop) {
  234. return;
  235. }
  236. switch (type) {
  237. case 1:
  238. if (this.FullScreenAdArr.indexOf(this.ecpmLevel.toString()) == -1) {
  239. return;
  240. }
  241. mk.ad.showInterAd(1, des);
  242. break;
  243. case 2:
  244. let delayArr = gData.gameData.configs.ServerConfig.AdDelay.split(',');
  245. let min = parseFloat(delayArr[0]);
  246. let max = parseFloat(delayArr[1]);
  247. let delay = (min + (max - min) * Math.random()) * 1000;
  248. setTimeout(() => {
  249. mk.ad.showInterAd(2, des);
  250. }, delay);
  251. break;
  252. case 3:
  253. let delayArr1 = gData.gameData.configs.ServerConfig.AdDelay.split(',');
  254. let min1 = parseFloat(delayArr1[0]);
  255. let max1 = parseFloat(delayArr1[1]);
  256. let delay1 = (min1 + (max1 - min1) * Math.random()) * 1000;
  257. let delay2 = (min1 + (max1 - min1) * Math.random()) * 1000;
  258. setTimeout(() => {
  259. mk.ad.showInterAd(0, des);
  260. setTimeout(() => {
  261. mk.ad.showInterAd(2, des);
  262. }, delay2);
  263. }, delay1);
  264. break;
  265. case 4:
  266. gData.moreGame.popNewOpenRedBag();
  267. break;
  268. }
  269. }
  270. /** 弹出插屏 */
  271. checkPopInter(des) {
  272. if (this.ABCInsertAdArr.indexOf(this.ecpmLevel.toString()) == -1) {
  273. return;
  274. }
  275. let ran = Math.random();
  276. let len = this.ABCInsertAdPercentArr.length;
  277. let index = 0;
  278. for (var i = 0; i < len; i++) {
  279. if (ran <= this.ABCInsertAdPercentArr[i]) {
  280. index = i + 2;
  281. break;
  282. }
  283. }
  284. this.checkShowFullInter(index, des);
  285. }
  286. /** 点击提现弹出全屏插屏 */
  287. checkPopCashFull() {
  288. if (this.FullCsahAdArr.indexOf(this.ecpmLevel.toString()) == -1) {
  289. return;
  290. }
  291. mk.ad.showInterAd(1);
  292. }
  293. checkShowVideoIcon() {
  294. if (this.FullCsahAdArr.indexOf(this.ecpmLevel.toString()) == -1) {
  295. return false;
  296. }
  297. return true;
  298. }
  299. //定时弹出计时
  300. private timeAd = 0;
  301. private test = true;
  302. /** 定时弹出红包 */
  303. startPopRed(dt) {
  304. if (mk.guide.isGuiding() || mk.ui.getCurOnPanel('redBagTask') || mk.ui.getCurOnPanel('productReward2')) {
  305. this.timeAd = 0;
  306. return;
  307. }
  308. if (gData.gameData.isTouchScreen) {
  309. gData.gameData.isTouchScreen = false;
  310. this.timeAd = 0;
  311. return;
  312. }
  313. if (this.TimingRedMoneyArr.indexOf(this.ecpmLevel.toString()) == -1) {
  314. return;
  315. }
  316. this.timeAd += dt;
  317. let span = this.AutoRedMoneyMap.get(this.ecpmLevel);
  318. if (this.timeAd >= span) {
  319. this.timeAd = 0;
  320. mk.ad.videoAdType = VideoAdType.video_init_19;
  321. gData.gameData.init_hideRedBag = true;
  322. mk.ui.openPanel('module/newOpenRedBag/newOpenRedBag');
  323. }
  324. }
  325. /** 需要弹出红包的点击数 */
  326. popClickTimes = 0;
  327. /** 累计点击数 */
  328. clickTimesAdd = 0;
  329. setPopClickTimes() {
  330. let arr = gData.gameData.configs.ServerConfig.ClickProduction.split(',');
  331. this.popClickTimes = Util.rnd(parseInt(arr[0]), parseInt(arr[1]));
  332. }
  333. checkPopRed() {
  334. if (this.ProductionButtonArr.indexOf(this.ecpmLevel.toString()) == -1) {
  335. return;
  336. }
  337. this.clickTimesAdd++;
  338. if (this.clickTimesAdd >= this.popClickTimes) {
  339. this.clickTimesAdd = 0;
  340. this.setPopClickTimes();
  341. if (Math.random() <= this.getPerByEcpm()) {
  342. mk.ad.videoAdType = VideoAdType.video_init_17;
  343. mk.ui.openPanel('module/newOpenRedBag/newOpenRedBag');
  344. }
  345. }
  346. }
  347. getPerByEcpm() {
  348. // //test
  349. // return 0.26;
  350. let forceAd = gData.gameData.configs.ForceAd;
  351. let len = forceAd.length;
  352. let per = 0;
  353. for (var i = 0; i < len; i++) {
  354. if (gData.gameData.adShowConfig.average_ecpm <= parseInt(forceAd[i].ecpmMax)) {
  355. per = parseFloat(forceAd[i].percent);
  356. break;
  357. }
  358. }
  359. return per;
  360. }
  361. savePreLoadEcpm(data) {
  362. let times = gData.gameData.getProp(GameProp.videoTimes);
  363. console.log('savePreLoadEcpm times ', times)
  364. if (!times) {
  365. console.log('savePreLoadEcpm data ', data.adsource_price)
  366. mk.http.sendData('user/savePreLoadEcpm', data);
  367. }
  368. }
  369. }
  370. /**
  371. * 看视频类型
  372. */
  373. export enum AdFun {
  374. bubble = 1, //气泡视频
  375. checkpoint = 2, //关卡消除视频
  376. settlement = 3, //关卡通关视频
  377. turntable = 4, //转盘视频
  378. sign = 5, //签到视频
  379. bigCash = 6, //福袋视频
  380. cashOutAddCash = 8, //钱包提现加现金视频
  381. harvest = 9, //收获超级加倍视频
  382. farmMap = 10, //图鉴领取视频
  383. }