TurntableManage.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { _decorator, Component, Node, Button, Sprite, tween, Vec3, Label } 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 '../UI/GetWindow/GetType';
  11. const { ccclass, property } = _decorator;
  12. @ccclass('TurntableManage')
  13. export class TurntableManage extends Component {
  14. @property({ type: Node })
  15. public turntable: Node = null;
  16. @property({ type: Sprite })
  17. public startButton: Sprite = null;
  18. @property({ type: Sprite })
  19. public closeButton: Sprite = null;
  20. @property({ type: Http })
  21. public http: Http = null;
  22. @property({ type: Label })
  23. public lastLabel: Label = null;
  24. private openWindowCom: OpenWindow = null;
  25. /**
  26. * 奖品ID:资源映射
  27. */
  28. private resourceMap: Map<number, number>;
  29. /**
  30. * ID:角度映射
  31. */
  32. private idMap: Map<number, number>;
  33. private _times: number = 0;
  34. private nextWindows: string[];
  35. set times(times: number) {
  36. this._times = times;
  37. this.lastLabel.string = this._times + "";
  38. }
  39. async start() {
  40. this.idMap = new Map();
  41. this.idMap.set(1, 0);
  42. this.idMap.set(2, 300);
  43. this.idMap.set(3, 240);
  44. this.idMap.set(4, 60);
  45. this.idMap.set(5, 120);
  46. this.idMap.set(6, 180);
  47. this.resourceMap = new Map();
  48. this.resourceMap.set(1, GetType.RedBag);
  49. this.resourceMap.set(2, GetType.Diamond);
  50. this.resourceMap.set(3, GetType.RedBag);
  51. this.resourceMap.set(4, GetType.exp);
  52. this.resourceMap.set(5, GetType.Diamond);
  53. this.resourceMap.set(6, GetType.exp);
  54. await this.queryLast();
  55. this.openWindowCom = this.node.parent.getComponent(OpenWindow);
  56. }
  57. /**
  58. * 视频播放完成开始抽奖
  59. */
  60. async onVideoOver() {
  61. //请求服务器获取的奖品ID,奖品数量
  62. let data = await this.http.send("/api/turntable/turntable", { adData: g.adData });
  63. if (data.code == 0) {
  64. platform.reportThinking("ad_end", JSON.stringify({ ad_id: "turntable", ad_time: Utils.formatDate(new Date()), id: g.userData.id, role_name: g.userData.nickName, level: g.userData.getLevel(), award: data.data.value }));
  65. platform.umUp("turntable");//转盘埋点
  66. this.startTween(data.data.id, data.data.value);
  67. }
  68. }
  69. /**
  70. * 查询剩余次数
  71. */
  72. async queryLast() {
  73. let data = await this.http.send("/api/turntable/GetTurntableTimes");
  74. if (data.code == 0) {
  75. this.times = Number(data.data);
  76. }
  77. }
  78. /**
  79. * 关闭界面
  80. */
  81. // onClose() {
  82. // // this.node.parent.destroy();
  83. // }
  84. /**
  85. * 接收参数
  86. */
  87. onParam(params: any[]) {
  88. this.nextWindows = params;
  89. }
  90. onClosing(closeCallback: () => void) {
  91. closeCallback();
  92. if (this.nextWindows && this.nextWindows.length) {
  93. WindowManager.open(this.nextWindows.shift(), WindowOpenMode.NotCloseAndAdd, this.nextWindows);
  94. }
  95. }
  96. /**
  97. * 观看视频
  98. */
  99. async onVideo() {
  100. let applyReturn = await this.http.send("/api/ad/watchVideoAD");
  101. //可以观看视频
  102. if (applyReturn.code == 0) {
  103. platform.reportThinking("ad_init", JSON.stringify({ ad_id: "turntable", ad_time: Utils.formatDate(new Date()), id: g.userData.id, role_name: g.userData.nickName, level: g.userData.getLevel() }));
  104. //播放视频
  105. let adData = await g.showRewardVideo();
  106. if (adData) {
  107. this.onVideoOver();
  108. } else {
  109. //视频观看失败
  110. WindowManager.showTips("视频观看时间不足");
  111. }
  112. } else if (applyReturn.code == 106) {
  113. WindowManager.showTips("今日视频次数已用完,请明日再继续");
  114. } else if (applyReturn.code == HttpErrorCode.VideoADCD) {
  115. WindowManager.showTips("请求频繁,请稍后再试");
  116. } else {
  117. WindowManager.showTips("视频准备中,请稍后再试");
  118. }
  119. }
  120. /**
  121. * 开始转盘
  122. * @param angle 奖品角度
  123. * @param prizedNum 奖励数
  124. */
  125. private isTurnning = false;
  126. private startTween(prizeId: number, prizedNum: string) {
  127. this.startButton.getComponent(Button).interactable = false;
  128. this.closeButton.getComponent(Button).interactable = false;
  129. let angle = this.idMap.get(prizeId);
  130. this.isTurnning = true;
  131. tween(this.turntable)
  132. .call(() => {
  133. this.turntable.eulerAngles = new Vec3(this.turntable.eulerAngles.x, this.turntable.eulerAngles.y, this.turntable.eulerAngles.z % 360);
  134. })
  135. .to(1, { eulerAngles: new Vec3(this.turntable.eulerAngles.x, this.turntable.eulerAngles.y, -360) }, { easing: "cubicIn" })
  136. .to(4, { eulerAngles: new Vec3(this.turntable.eulerAngles.x, this.turntable.eulerAngles.y, -1800 + angle) }, { easing: "cubicOut" })
  137. .call(() => {
  138. if (prizeId == 1 || prizeId == 3) {
  139. platform.reportThinking("envelope_increase", JSON.stringify({ previous_number: g.userData.bonus, increase_number: prizedNum, current_number: g.userData.bonus + prizedNum, reasons: "turntable" }));
  140. } else if (prizeId == 2 || prizeId == 5) {
  141. platform.reportThinking("diamond_increase", JSON.stringify({ previous_number: g.userData.bonus, increase_number: prizedNum, current_number: g.userData.bonus + prizedNum, reasons: "turntable" }));
  142. }
  143. this.openWindowCom.open([{ count: prizedNum, type: this.resourceMap.get(prizeId), needBezierEffect: true }], this.node);
  144. console.log(this._times);
  145. this.times = --this._times;
  146. this.startButton.getComponent(Button).interactable = true;
  147. this.closeButton.getComponent(Button).interactable = true;
  148. this.isTurnning = false;
  149. }).start();
  150. }
  151. update() {
  152. if (this._times == 0) {
  153. this.startButton.grayscale = true;
  154. this.startButton.getComponent(Button).interactable = false;
  155. } else {
  156. if (!this.isTurnning) {
  157. this.startButton.grayscale = false;
  158. this.startButton.getComponent(Button).interactable = true;
  159. }
  160. }
  161. }
  162. }