AdData.ts 14 KB

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