AdData.ts 13 KB

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