HarvestWindow.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { _decorator, Component, EventTouch, Label, find } from 'cc';
  2. import { Http } from '../../core/net/Http';
  3. import { OpenWindow } from '../../core/ui/window/OpenWindow';
  4. import { WindowManager } from '../../core/ui/window/WindowManager';
  5. import { WindowOpenMode } from '../../core/ui/window/WindowOpenMode';
  6. import { Utils } from '../../core/utils/Utils';
  7. import { g } from '../../Data/g';
  8. import { HttpErrorCode } from '../../Data/HttpErrorCode';
  9. import { platform } from '../../Data/platform';
  10. import { GetType } from '../GetWindow/GetType';
  11. const { ccclass, property } = _decorator;
  12. @ccclass('HarvestWindow')
  13. export class HarvestWindow extends Component {
  14. @property({ tooltip: "网络请求对象", type: Http }) http: Http;
  15. private buildID: number;
  16. private cb: Function;
  17. private mult: number;
  18. public onParams(buildID: number, cb: Function): void {
  19. this.buildID = buildID;
  20. this.cb = cb;
  21. }
  22. public async onButton(e: EventTouch, mult: number) {
  23. this.mult = mult;
  24. if (mult == 1) {
  25. let result = await this.http.send("/api/ad/watchVideoAD");
  26. if (result.code == 0) {
  27. let key = this.getThankingKey(this.buildID);
  28. platform.reportThinking("ad_init", JSON.stringify({ ad_id: key, ad_time: Utils.formatDate(new Date()), id: g.userData.id, role_name: g.userData.nickName, level: g.userData.getLevel() }));
  29. //播放视频
  30. let adData = await g.showRewardVideo();
  31. if (adData) {
  32. this.onVideoOver();
  33. } else {
  34. //视频观看失败
  35. WindowManager.showTips("视频观看时间不足");
  36. }
  37. } else if (result.code == 106) {
  38. WindowManager.showTips("今日视频次数已用完,请明日再继续");
  39. } else if (result.code == HttpErrorCode.VideoADCD) {
  40. WindowManager.showTips("请求频繁,请稍后再试");
  41. } else {
  42. WindowManager.showTips("视频准备中,请稍后再试");
  43. }
  44. return;
  45. } else {
  46. this.onVideoOver();
  47. }
  48. }
  49. public async onVideoOver() {
  50. let result = await this.http.send("/api/product/harvest", { buildID: this.buildID, multiple: this.mult, adData: g.adData });
  51. if (result.code == 0) {
  52. this.upThanking(result.data.bonus);
  53. g.gameData.rankScore += result.data.addScore;
  54. this.cb(this.mult);
  55. this.cb = null;
  56. WindowManager.close();
  57. platform.reportThinking("envelope_increase", JSON.stringify({ previous_number: g.userData.bonus, increase_number: result.data.bonus, current_number: g.userData.bonus + result.data.bonus, reasons: "harvest" }));
  58. WindowManager.open("Prefabs/GetWindow", WindowOpenMode.CloseAndCover, [{ count: result.data.bonus, type: GetType.RedBag, needBezierEffect: true }, { count: result.data.exp, type: GetType.exp, needBezierEffect: false }]);
  59. } else if (result.code == HttpErrorCode.VideoADCD) {
  60. WindowManager.showTips("请求频繁,请稍后再试");
  61. }
  62. }
  63. private upThanking(award: number): void {
  64. if (this.mult == 1) {
  65. let key = this.getThankingKey(this.buildID);
  66. platform.reportThinking("ad_end", JSON.stringify({ ad_id: key, ad_time: Utils.formatDate(new Date()), id: g.userData.id, role_name: g.userData.nickName, level: g.userData.getLevel(), award: award }))
  67. } else {
  68. let data = g.gameData.getProductingList(this.buildID)[0];
  69. if (this.buildID >= 31000 && this.buildID <= 31017) {
  70. platform.reportThinking("harvest", JSON.stringify({ field_name: this.buildID, id: g.userData.id, level: g.userData.getLevel(), role_name: g.userData.nickName, harvest_time: Utils.formatDate(new Date()), award_no: award, plant_name: data.productID }));
  71. } else {
  72. let key = "";
  73. switch (this.buildID) {
  74. case 30001: key = "chick"; break;
  75. case 30002: key = "cow"; break;
  76. case 30003: key = "pig"; break;
  77. case 30004: key = "corn"; break;
  78. case 30005: key = "milk"; break;
  79. case 30006: key = "sugar"; break;
  80. case 30007: key = "cake"; break;
  81. case 30008: key = "fastfood"; break;
  82. case 30009: key = "noodle"; break;
  83. }
  84. platform.reportThinking("harvest", JSON.stringify({ field_name: key, id: g.userData.id, level: g.userData.getLevel(), role_name: g.userData.nickName, harvest_time: Utils.formatDate(new Date()), award_no: award, plant_name: data.productID }));
  85. }
  86. }
  87. }
  88. public getThankingKey(id: number): string {
  89. if (id >= 31000 && id <= 31017) {
  90. return "harvest_field";
  91. } else {
  92. switch (id) {
  93. case 30001:
  94. return "harvest_chick";
  95. case 30002:
  96. return "harvest_cow";
  97. case 30003:
  98. return "harvest_pig";
  99. case 30004:
  100. return "harvest_corn";
  101. case 30005:
  102. return "harvest_milk";
  103. case 30006:
  104. return "harvest_sugar";
  105. case 30007:
  106. return "harvest_cake";
  107. case 30008:
  108. return "harvest_fastfood";
  109. case 30009:
  110. return "harvest_noodle";
  111. }
  112. }
  113. return
  114. }
  115. onDestroy() {
  116. this.cb = null;
  117. }
  118. }