RedEnvelopes.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import { _decorator, Component, Node, Label, Animation, Button } from 'cc';
  2. import { ADHelper } from '../ad/ADHelper';
  3. import { RewardVideoSystem } from '../ad/RewardVideoSystem';
  4. import { DataSystem } from '../core/data/DataSystem';
  5. import { Http, HttpResponseCode } from '../core/net/Http';
  6. import { HttpSystem } from '../core/net/HttpSystem';
  7. import { ResourceLoader } from '../core/resourceManager/ResourceLoader';
  8. import { Sound } from '../core/sound/Sound';
  9. import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
  10. import { WindowSystem } from '../core/ui/window/WindowSystem';
  11. import { HttpErrorCode } from '../data/HttpErrorCode';
  12. import { platform } from '../data/jsb/platform';
  13. import { UserData } from '../data/UserData';
  14. import { NoviceGuideData } from '../guide/NoviceGuideData';
  15. import { ReportThinking } from '../ReportThinking';
  16. import { Task } from '../task/Task';
  17. import { TaskData } from '../task/TaskData';
  18. import { LevelBonusData } from './LevelBonusData';
  19. const { ccclass, property, requireComponent } = _decorator;
  20. @ccclass('RedEnvelopes')
  21. @requireComponent(ResourceLoader)
  22. export class RedEnvelopes extends Component {
  23. @property({ tooltip: "奖励金额", type: Label }) private prizeNum: Label;
  24. @property({ tooltip: "普通领取", type: Node }) private toGet: Node;
  25. @property({ tooltip: "视频领取", type: Node }) private videoGet: Node;
  26. @property({ tooltip: "视频icon", type: Node }) private videoIcon: Node;
  27. @property({ tooltip: "视频按钮文本", type: Label }) private videoBtnTxt: Label;
  28. @property({ tooltip: "任务红包普通领取按钮", type: Node }) private txtGetBtn: Node;
  29. @property({ tooltip: "插屏广告", type: ADHelper }) private adNode: ADHelper;
  30. @property({ tooltip: "普通红包文本(开封前)", type: Node }) private envelopesTxt: Node;
  31. @property({ tooltip: "新手红包文本(开封前)", type: Node }) private newEnvelopesTxt: Node;
  32. @property({ tooltip: "新手红包图标", type: Node }) private newIcon: Node;
  33. @property({ tooltip: "开红包动画", type: Animation }) private openAnim: Animation;
  34. @property({ tooltip: "背景", type: Node }) private bg: Node;
  35. @property({ tooltip: "关卡红包关闭按钮", type: Node }) private closeBtn: Node;
  36. @property({ tooltip: "手指动画", type: Node }) private handAnim: Node;
  37. @property({ tooltip: "自动领取关卡红包倒计时", type: Label }) private countDownTxt: Label;
  38. private prizenum = 0;//奖励数值
  39. private prizeType = 0;//奖励类型
  40. private activeId = 0;//任务红包id
  41. private closeTime = 10 * 1000;//倒计时自动关闭时间,单位毫秒
  42. private isCountDown = false;//是否倒计时
  43. private endTime = 0;//倒计时结束实际点
  44. start() {
  45. }
  46. /**
  47. * 窗口打开回调
  48. * @param data 数据,type:(0:直接领取,1:任务红包普通领取,2:任务红包二倍领取,3:任务红包三倍领取,4:新手红包,5:关卡红包掉落直接弹窗),num:红包币数目,isShowBtn:任务红包普通领取按钮是否显示
  49. */
  50. public setData(data: { type: number, num: number, id: number, isShowBtn: boolean }) {
  51. data.id && (this.activeId = data.id);
  52. this.prizeNum.string = "X" + data.num;
  53. this.prizeType = data.type;
  54. switch (data.type) {
  55. case 0://关卡红包
  56. this.toGet.active = true;
  57. this.prizenum = data.num;
  58. this.openAnim.play();
  59. break;
  60. case 1:
  61. this.videoGet.active = true;
  62. this.videoIcon.active = false;
  63. this.videoBtnTxt.string = "领取奖励";
  64. this.openAnim.play();
  65. break;
  66. case 2:
  67. this.videoGet.active = true;
  68. this.videoBtnTxt.string = "双倍领取";
  69. this.openAnim.play();
  70. break;
  71. case 3:
  72. this.videoGet.active = true;
  73. this.videoBtnTxt.string = "三倍领取";
  74. this.openAnim.play();
  75. break;
  76. case 4://新手红包
  77. this.toGet.active = true;
  78. this.envelopesTxt.active = false;
  79. this.newEnvelopesTxt.active = true;
  80. this.newIcon.active = true;
  81. break;
  82. case 5:
  83. this.toGet.active = true;
  84. // this.bg.getComponent(Button).interactable = true;
  85. this.closeBtn.active = true;
  86. this.handAnim.active = true;
  87. this.handAnim.getComponent(Animation).play();
  88. //倒计时
  89. this.startCountDown();
  90. break;
  91. default:
  92. WindowSystem.close(this.node);
  93. break;
  94. }
  95. data.isShowBtn && (this.txtGetBtn.active = data.isShowBtn);
  96. }
  97. private isPlay = false;//是否正在播放开红包动画
  98. //手动打开红包
  99. public async openRed() {
  100. if (this.prizeType == 4 && !this.isPlay) {
  101. platform.reportThinking("guide", JSON.stringify({ guide_id: 'first_gift' }));
  102. this.isPlay = true;
  103. this.openAnim && this.openAnim.play();
  104. }
  105. if (this.prizeType == 5 && !this.isPlay) {
  106. this.isCountDown = false;
  107. this.isPlay = true;
  108. this.closeBtn.active = false;
  109. // this.bg.getComponent(Button).interactable = false;
  110. let result = await this.getComponent(Http).send("/api/ad/watchVideoAD");
  111. if (result && result.code == 0) {
  112. ReportThinking.ad_init('red_envelope');
  113. let adData = await RewardVideoSystem.show();
  114. if (adData) {//观看视频成功
  115. let _result = await this.getComponent(Http).send("/api/battle/receiveBonus", { adData: adData.obj });
  116. if (_result && _result.code == 0) {//打开红包成功
  117. ReportThinking.ad_end('red_envelope');
  118. this.prizeNum.string = "X" + _result.data;
  119. this.prizenum = _result.data;
  120. this.prizeType = 0;
  121. this.handAnim.getComponent(Animation).stop();
  122. this.handAnim.active = false;
  123. this.openAnim && this.openAnim.play();
  124. this.countDownTxt.string = "";
  125. } else if (_result && _result.code == HttpErrorCode.VideoADCD) {
  126. WindowSystem.showTips('视频观看时间不足');
  127. this.closeBtn.active = true;
  128. // this.bg.getComponent(Button).interactable = true;
  129. this.isPlay = false;
  130. this.startCountDown();
  131. } else {
  132. this.closeBtn.active = true;
  133. // this.bg.getComponent(Button).interactable = true;
  134. this.isPlay = false;
  135. this.startCountDown();
  136. WindowSystem.showTips('红包打开失败');
  137. }
  138. } else {//观看视频失败
  139. WindowSystem.showTips('视频观看时间不足');
  140. this.closeBtn.active = true;
  141. // this.bg.getComponent(Button).interactable = true;
  142. this.isPlay = false;
  143. this.startCountDown();
  144. }
  145. } else {//返回错误信息,无法观看视频
  146. WindowSystem.showTips('视频次数不足');
  147. this.closeBtn.active = true;
  148. // this.bg.getComponent(Button).interactable = true;
  149. this.isPlay = false;
  150. this.startCountDown();
  151. }
  152. }
  153. }
  154. //视频双倍领取
  155. public async openVideo() {
  156. let result = await this.getComponent(Http).send("/api/ad/watchVideoAD");
  157. if (result && result.code == 0) {
  158. let userData = DataSystem.getData(UserData);
  159. ReportThinking.ad_init('taskredpackt_get');
  160. let adData = await RewardVideoSystem.show();
  161. if (adData) {//观看视频成功
  162. this.onGetGift(true, adData.obj);
  163. } else {//观看视频失败
  164. WindowSystem.showTips('视频观看时间不足');
  165. }
  166. }
  167. }
  168. //普通领取
  169. public async toGetPrize() {
  170. this.onGetGift();
  171. }
  172. //背景点击事件
  173. private bgOnTouch() {
  174. if (this.prizeType == 1 || this.prizeType == 2 || this.prizeType == 3) {
  175. WindowSystem.close(this.node);
  176. return;
  177. }
  178. this.onGetGift();
  179. }
  180. private async onGetGift(isDouble = false, adData?: any) {
  181. this.isCountDown = false;//停止倒计时
  182. if (this.prizeType && this.prizeType != 4 && this.prizeType != 5) {
  183. let data;
  184. if (adData) {
  185. data = { id: this.activeId, isDouble: isDouble, adData: adData };
  186. } else {
  187. data = { id: this.activeId, isDouble: isDouble };
  188. }
  189. let result = await this.getComponent(Http).send('/api/task/ReceiveTaskActive', data);
  190. if (result && result.code == HttpResponseCode.Success) {
  191. if (result.data.rewardType == 'bonus') {
  192. this.prizenum = result.data.rewardNum;
  193. let userData = DataSystem.getData(UserData);
  194. adData && ReportThinking.ad_end('taskredpackt_get', this.prizenum);
  195. let activitys = DataSystem.getData(TaskData).activitys;
  196. for (let i = 0; i < activitys.length; i++) {
  197. if (activitys[i].id == this.activeId) {
  198. !isDouble && (activitys[i].isReceiveSingle = true);
  199. isDouble && (activitys[i].isReceiveDouble = true);
  200. }
  201. }
  202. DataSystem.getData(TaskData).getActiveBoxGiftNum++;
  203. }
  204. WindowSystem.close(this.node);
  205. }
  206. } else {
  207. if (this.prizeType == 4) {
  208. let result = await this.getComponent(Http).send("/api/user/ReceiveNewUserBonus");
  209. if (result && result.code == 0) {
  210. platform.reportThinking("guide", JSON.stringify({ guide_id: 'first_claim' }));
  211. this.prizenum = result.data;
  212. await WindowSystem.open("prefabs/ui/firstHero/firstHero", WindowOpenMode.NotCloseAndAdd);
  213. } else if (result && result.code == HttpErrorCode.HadReceive) {
  214. WindowSystem.showTips('已领取');
  215. await WindowSystem.open("prefabs/ui/firstHero/firstHero", WindowOpenMode.NotCloseAndAdd);
  216. } else {
  217. return;
  218. }
  219. }
  220. if (this.prizeType == 5) {
  221. DataSystem.getData(LevelBonusData).saveTimes++;
  222. adData && ReportThinking.ad_end('taskredpackt_get', this.prizenum);
  223. await WindowSystem.open("prefabs/ui/turntable/prizeFly", WindowOpenMode.NotCloseAndAdd, [{ type: 4, num: 1 }]);
  224. }
  225. WindowSystem.close(this.node);
  226. }
  227. }
  228. async onDestroy() {
  229. this.prizenum && await WindowSystem.open("prefabs/ui/turntable/prizeFly", WindowOpenMode.NotCloseAndAdd, [{ type: 3, num: this.prizenum }]);
  230. !this.prizeType && (Math.random() * 100) <= 20 && this.adNode.show();
  231. }
  232. update() {
  233. if (this.isCountDown) {
  234. let time = Date.now();
  235. if (time < this.endTime) {
  236. this.closeTime = this.endTime - time;
  237. this.countDownTxt.string = Math.round(this.closeTime / 1000) + "秒自动关闭";
  238. } else if (this.isCountDown) {
  239. this.isCountDown = false;
  240. this.countDownTxt.string = "自动关闭";
  241. this.toGetPrize();
  242. }
  243. //新手引导自动关闭红包
  244. if (DataSystem.watch(NoviceGuideData, "isShow")) {
  245. DataSystem.getData(NoviceGuideData).isShow && this.toGetPrize();
  246. }
  247. }
  248. //新手引导自动关闭红包
  249. if (this.bg.getComponent(Button).interactable && DataSystem.watch(NoviceGuideData, "isShow")) {
  250. DataSystem.getData(NoviceGuideData).isShow && this.bgOnTouch();
  251. }
  252. }
  253. /**
  254. * 开始倒计时
  255. */
  256. private startCountDown() {
  257. this.endTime = Date.now() + this.closeTime;
  258. this.isCountDown = true;
  259. }
  260. }