RecruitGeneralUI.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { _decorator, Component, Node, Label, isValid, EventHandler, Color } from 'cc';
  2. import { DataSystem } from '../../core/data/DataSystem';
  3. import { Http, HttpResponseCode } from '../../core/net/Http';
  4. import { WindowOpenMode } from '../../core/ui/window/WindowOpenMode';
  5. import { WindowSystem } from '../../core/ui/window/WindowSystem';
  6. import { ConfigData } from '../../data/ConfigData';
  7. import { HttpErrorCode } from '../../data/HttpErrorCode';
  8. import { UserData } from '../../data/UserData';
  9. import { Hero_Client, UserHeroData } from '../../hero/UserHeroData';
  10. import { MarshalSkillSystem } from '../../marshal/MarshalSkillSystem';
  11. import { ReportThinking } from '../../ReportThinking';
  12. import { RecruitData } from '../RecruitData';
  13. import { RecruitItem } from './RecruitItem';
  14. import { UpdateRecruit } from './UpdateRecruit';
  15. const { ccclass, property } = _decorator;
  16. /**
  17. * 招募页 单抽与十连抽展示页
  18. * @author 郑聂华
  19. */
  20. @ccclass('RecruitGeneralUI')
  21. export class RecruitGeneralUI extends Component {
  22. @property({ type: Label, displayName: '文本_元宝', tooltip: "文本_元宝" }) txtNumDiamond: Label = null;
  23. @property({ type: Node, displayName: '单抽页', tooltip: "单抽页" }) panelOne: Node = null;
  24. @property({ type: Node, displayName: '十连抽页', tooltip: "十连抽页" }) panelTen: Node = null;
  25. @property({ type: Node, displayName: '单_开始页', tooltip: "单_开始页" }) panelOneStart: Node = null;
  26. @property({ type: Node, displayName: '单_结束页', tooltip: "单_结束页" }) panelOneEnd: Node = null;
  27. @property({ type: Node, displayName: '返回按钮', tooltip: "返回按钮" }) backNode: Node = null;
  28. @property({ type: RecruitItem, displayName: '抽奖Item_单', tooltip: "抽奖Item_单" }) recruitItemOne: RecruitItem = null;
  29. @property({ type: RecruitItem, displayName: '抽奖Item_十', tooltip: "抽奖Item_十" }) recruitItemTens: RecruitItem[] = [];
  30. @property({ type: Node, displayName: '十_开始页', tooltip: "十_开始页" }) panelTenStart: Node = null;
  31. @property({ type: Node, displayName: '十_结束页', tooltip: "十_结束页" }) panelTenEnd: Node = null;
  32. @property({ type: Label, displayName: '文本_单抽元宝消耗', tooltip: "文本_单抽元宝消耗" }) txtCostOne: Label = null;
  33. @property({ type: Label, displayName: '文本_十连抽元宝消耗', tooltip: "文本_十连抽元宝消耗" }) txtCostTen: Label = null;
  34. @property({ type: Label, displayName: '文本_单抽倒计时', tooltip: "文本_单抽倒计时" }) txtTimerOne: Label = null;
  35. @property({ type: EventHandler, displayName: '招募结束回调', tooltip: "招募结束回调" }) recruitBack: EventHandler = null;
  36. private isCanRecruit: boolean = true;
  37. private isOpenedOne: boolean = false;
  38. private isOpenAll: boolean = false;
  39. private herodatacfg: any;
  40. private serverConfig: any;
  41. private paramData: { type: number, data: [{ chip: number, hero: any }] };
  42. private userData: UserData;
  43. onLoad() {
  44. this.serverConfig = DataSystem.getData(ConfigData).get("serverConfig");
  45. }
  46. update() {
  47. DataSystem.watch(UserData, "diamond") && this.updateDiamond();
  48. }
  49. private updateDiamond() {
  50. this.txtNumDiamond.string = this.userData.diamond > 10000 ? (this.userData.diamond / 1000).toFixed(1) + "k" : this.userData.diamond + "";
  51. this.txtCostOne.string = this.serverConfig.recruit1 + "";
  52. this.txtCostTen.string = this.serverConfig.recruit10 + "";// * 10 * this.serverConfig.invite_discount + "";
  53. this.txtCostOne.color = this.userData.diamond < this.serverConfig.recruit1 ? Color.RED : new Color(255, 235, 60, 255);
  54. this.txtCostTen.color = this.userData.diamond < this.serverConfig.recruit10 ? Color.RED : new Color(255, 235, 60, 255);
  55. }
  56. /**
  57. * 打开页面回调
  58. *{type:1} 1 单抽 2 十连抽
  59. */
  60. private init(param: any) {
  61. this.herodatacfg = DataSystem.getData(ConfigData).get("general");
  62. this.paramData = param;
  63. this.serverConfig = DataSystem.getData(ConfigData).get("serverConfig");
  64. this.userData = DataSystem.getData(UserData);
  65. this.updateDiamond();
  66. this.backNode.active = false;
  67. this.panelOne.active = param.type == 1;
  68. this.panelTen.active = param.type == 2;
  69. param.type == 1 && this.initPanelOne();
  70. param.type == 2 && this.initPanelTen();
  71. this.recruitItemOne.recruitEndBack = this.recruitBack;
  72. for (let t of this.recruitItemTens) { t.recruitEndBack = this.recruitBack; }
  73. }
  74. /**单抽初始化*/
  75. initPanelOne() {
  76. this.panelTen.active = false;
  77. this.panelOneStart.active = true;
  78. this.panelOneEnd.active = false;
  79. this.recruitItemOne.init(1);
  80. this.scheduleOnce(this.openRecruitOne, 3);
  81. }
  82. /**十连抽抽初始化*/
  83. initPanelTen() {
  84. this.panelTenStart.active = true;
  85. this.panelTenEnd.active = false;
  86. this.openRecruitTen();
  87. }
  88. /**单抽翻牌*/
  89. async openRecruitOne() {
  90. if (this.isOpenedOne) return; this.isOpenedOne = true;
  91. this.unschedule(this.openRecruitOne);
  92. let tempData = this.paramData.data[0];
  93. this.recruitItemOne.openDraw(this.herodatacfg[tempData.hero.id], 1, tempData.chip > 0, tempData.chip);
  94. }
  95. /**十连抽翻牌*/
  96. async openRecruitTen() {
  97. await new Promise<void>(resolve => { setTimeout(() => resolve(), 1000) });
  98. for (let i = 0; isValid(this.node) && i < this.recruitItemTens.length; i++) {
  99. if (!this.isOpenAll) {
  100. let tempData = this.paramData.data[i];
  101. this.recruitItemTens[i].openDraw(this.herodatacfg[tempData.hero.id], 2, tempData.chip > 0, tempData.chip);
  102. await new Promise<void>(resolve => { setTimeout(() => resolve(), 300) });
  103. }
  104. }
  105. }
  106. /**
  107. * 招募
  108. * @param recruitType 招募类型 1 单抽 2 十连抽
  109. */
  110. private async recruitHero(recruitType: number = 1) {
  111. this.isCanRecruit = false;
  112. let result = await this.getComponent(Http).send("/api/recruit/recruit", { num: recruitType == 1 ? 1 : 10 });
  113. if (result && result.code == HttpResponseCode.Success) {
  114. this.backNode.active = false;
  115. let userData = DataSystem.getData(UserData);
  116. let recruit_diamond = recruitType == 1 ? this.serverConfig.recruit1 : this.serverConfig.recruit10;
  117. ReportThinking.currency_decrease('diamond', userData.diamond, recruit_diamond, userData.diamond - recruit_diamond, 'recruit');
  118. if (recruitType == 1) DataSystem.getData(UserData).diamond -= this.serverConfig.recruit1;
  119. else DataSystem.getData(UserData).diamond -= this.serverConfig.recruit10;
  120. this.paramData.data = result.data;
  121. this.isOpenedOne = false;
  122. if (recruitType == 1) this.initPanelOne(); else this.initPanelTen();
  123. this.getComponent(UpdateRecruit).addRecruitResults(result.data);
  124. this.updateUserHeroData(result.data);
  125. this.isCanRecruit = true;
  126. }
  127. else if (result && result.code == HttpErrorCode.NoTimes) {
  128. recruitType == 1 && WindowSystem.showTips("已达今日单次招募次数上限");
  129. recruitType == 2 && WindowSystem.showTips("已达今日十连招募次数上限");
  130. this.isCanRecruit = true;
  131. } else {
  132. WindowSystem.showTips("招募失败");
  133. this.isCanRecruit = true;
  134. }
  135. }
  136. /**更新武将数据*/
  137. private updateUserHeroData(data: any[]) {
  138. let userHeroData = DataSystem.getData(UserHeroData);
  139. //console.log("----Hero: ", data);
  140. for (let i = 0; i < data.length; i++) {
  141. if (data[i].chip == 0) {
  142. userHeroData.addHero(data[i].hero.id);
  143. ReportThinking.hero_activate(DataSystem.getData(ConfigData).get('general')[data[i].hero.id].name, 'recruit');
  144. } else {
  145. userHeroData.addChip(data[i].hero.id, data[i].chip);
  146. }
  147. }
  148. this.updateMarshalSkillNotice();
  149. }
  150. /**更新统帅技能数据*/
  151. private updateMarshalSkillNotice() {
  152. MarshalSkillSystem.updateNoticeQueue();
  153. }
  154. /**招募结束回调*/
  155. private recruitEndBack() {
  156. if (this.paramData.type == 1) {
  157. //await new Promise<void>(resolve => { setTimeout(() => resolve(), 1500) });//计算出的时间
  158. this.panelOneEnd.active = true; this.panelOneStart.active = false;
  159. this.backNode.active = true;
  160. } else {
  161. let isEnd = true;
  162. for (let i = 0; i < this.recruitItemTens.length; i++) { !this.recruitItemTens[i].isRecruitEnd && (isEnd = false); }
  163. if (isEnd) { this.panelTenEnd.active = true; this.panelTenStart.active = false; this.backNode.active = true; };
  164. }
  165. }
  166. /**是否存在兑换元宝次数*/
  167. private async isHaveExchangeTimes() {
  168. let result = await this.getComponent(Http).send("/api/recruit/GetExchange");
  169. if (result && result.code == HttpResponseCode.Success) {
  170. //return (result.data.freeTimes + result.data.videoTimes) > 0
  171. return result.data.hadExchangedTimes < Object.keys(result.data.config).length
  172. }
  173. return false;
  174. }
  175. /**点击开启单抽*/
  176. private onClickOpenOne() {
  177. this.openRecruitOne();
  178. }
  179. /**点击单抽*/
  180. private async onClickRecruitOneBtn() {
  181. if (!this.isCanRecruit) return;
  182. if (DataSystem.getData(UserData).diamond >= this.serverConfig.recruit1) {
  183. this.recruitHero(1);
  184. } else {
  185. if (await this.isHaveExchangeTimes()) {
  186. WindowSystem.open("prefabs/ui/recruit/diamondExchange", WindowOpenMode.NotCloseAndAdd, ["6"]);
  187. } else {
  188. WindowSystem.showTips("元宝不足");
  189. }
  190. }
  191. }
  192. /**点击开启全部十连抽*/
  193. private async onClickOpenTenAll() {
  194. this.isOpenAll = true; let awaitTime = 1500;
  195. let isFirst = true, isSameFirst = false, isOpenedLow = false, isOpenedHigh = false;
  196. for (let i = 0; i < this.recruitItemTens.length; i++) {
  197. if (!this.recruitItemTens[i].isOpened) {
  198. let tempData = this.paramData.data[i];
  199. let heroCfg = this.herodatacfg[tempData.hero.id];
  200. if (!isOpenedLow) { if (heroCfg.color < 3) { isOpenedLow = true; isSameFirst = true; } }
  201. if (!isOpenedHigh) { if (heroCfg.color >= 3) { isOpenedHigh = true; isSameFirst = true; } }
  202. let openParam = { isOpenAll: true, isFirst: isFirst, isSameFirst: isSameFirst };
  203. this.recruitItemTens[i].openDraw(heroCfg, 2, tempData.chip > 0, tempData.chip, openParam);
  204. if (heroCfg.color >= 3 && awaitTime == 1500) awaitTime = 3000;
  205. isFirst = isSameFirst = false;
  206. }
  207. }
  208. this.panelTenStart.active = false;
  209. }
  210. /**点击十连抽*/
  211. private async onClickRecruitTenBtn() {
  212. if (!this.isCanRecruit) return;
  213. if (DataSystem.getData(UserData).diamond >= this.serverConfig.recruit10) {
  214. this.isOpenAll = false;
  215. for (let t of this.recruitItemTens) {
  216. t.init();
  217. }
  218. this.recruitHero(2);
  219. } else {
  220. if (await this.isHaveExchangeTimes()) {
  221. WindowSystem.open("prefabs/ui/recruit/diamondExchange", WindowOpenMode.NotCloseAndAdd, ["6"]);
  222. } else {
  223. WindowSystem.showTips("元宝不足");
  224. }
  225. }
  226. }
  227. onDestroy() {
  228. if (DataSystem.getData(RecruitData).recruitResults && DataSystem.getData(RecruitData).recruitResults.length > 0) {
  229. WindowSystem.open("prefabs/ui/recruit/recruitResult", WindowOpenMode.NotCloseAndAdd);
  230. }
  231. }
  232. }