RecruitUI.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { _decorator, Component, Node, Label, sp, Sprite, SpriteFrame, Color, EventHandler } from 'cc';
  2. import { DataSystem } from '../../core/data/DataSystem';
  3. import { Http, HttpResponseCode } from '../../core/net/Http';
  4. import { ResourceLoader } from '../../core/resourceManager/ResourceLoader';
  5. import { OpenWindow } from '../../core/ui/window/OpenWindow';
  6. import { WindowOpenMode } from '../../core/ui/window/WindowOpenMode';
  7. import { WindowSystem } from '../../core/ui/window/WindowSystem';
  8. import { ConfigData } from '../../data/ConfigData';
  9. import { HttpErrorCode } from '../../data/HttpErrorCode';
  10. import { UserData } from '../../data/UserData';
  11. import { NoviceGuideData } from '../../guide/NoviceGuideData';
  12. import { Hero_Client, UserHeroData } from '../../hero/UserHeroData';
  13. import { MarshalSkillSystem } from '../../marshal/MarshalSkillSystem';
  14. import { ReportThinking } from '../../ReportThinking';
  15. import { RecruitData } from '../RecruitData';
  16. import { UpdateRecruit } from './UpdateRecruit';
  17. const { ccclass, property } = _decorator;
  18. /**
  19. * 招募页(点将台)
  20. * @author 郑聂华
  21. */
  22. @ccclass('RecruitUI')
  23. export class RecruitUI extends Component {
  24. @property({ type: ResourceLoader, displayName: "资源加载组件", tooltip: "资源加载组件" }) res: ResourceLoader = null;
  25. @property({ type: Label, displayName: '文本_元宝', tooltip: "文本_元宝" }) txtDiamond: Label = null;
  26. @property({ type: Node, displayName: "心愿背景框", tooltip: "心愿背景框" }) wishRect: Node = null;
  27. @property({ type: Label, displayName: '文本_倍率', tooltip: "文本_倍率" }) txtRate: Label = null;
  28. @property({ type: Sprite, displayName: '图_心愿武将', tooltip: "图_心愿武将" }) imgWish: Sprite = null;
  29. @property({ type: Label, displayName: '文本_心愿武将名', tooltip: "文本_心愿武将名" }) txtName: Label = null;
  30. @property({ type: Sprite, displayName: '图_心愿武将阵营', tooltip: "图_心愿武将阵营" }) imgWishCamp: Sprite = null;
  31. @property({ type: sp.Skeleton, displayName: 'Spine_光条特效动画', tooltip: "Spine_光条特效动画" }) spineLight: sp.Skeleton = 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: "文本_十连抽原价消耗" }) txtCostTenOld: Label = null;
  35. @property({ type: OpenWindow, displayName: '打开界面', tooltip: "打开界面" }) openWnd: OpenWindow = null;
  36. private serverConfig;
  37. private recruitData: RecruitData;
  38. private isCanRecruit: boolean = true;
  39. private userData: UserData;
  40. onLoad() {
  41. this.recruitData = DataSystem.createData(RecruitData);
  42. let openEventHandler = new EventHandler();
  43. openEventHandler.target = this.node;
  44. openEventHandler.component = "RecruitUI";
  45. openEventHandler.handler = "onRecruitEvent";
  46. this.openWnd.onClosing.push(openEventHandler);
  47. }
  48. start() {
  49. this.initPanel();
  50. }
  51. update() {
  52. DataSystem.watch(RecruitData, "curWishId") && this.updateWish();
  53. DataSystem.watch(UserData, "diamond") && this.updateDiamond();
  54. DataSystem.watch(NoviceGuideData, "isRecruit") && this.recruitHero();
  55. }
  56. private async initPanel() {
  57. this.userData = DataSystem.getData(UserData);
  58. this.serverConfig = DataSystem.getData(ConfigData).get("serverConfig");
  59. this.updateDiamond();
  60. //判断是否有选中的心愿武将
  61. this.getWishGeneral();
  62. }
  63. /**查询心愿武将*/
  64. private async getWishGeneral() {
  65. let result = await this.getComponent(Http).send("/api/recruit/getWish");
  66. if (result && result.code == HttpResponseCode.Success) {
  67. this.recruitData.curWishId = result.data.heroID;
  68. this.recruitData.wishHeroRate = result.data.up;
  69. this.updateWish();
  70. }
  71. }
  72. /**
  73. * 更新心愿武将信息
  74. */
  75. private async updateWish() {
  76. let heroDataOne;
  77. if (this.recruitData.curWishId > 0) {
  78. heroDataOne = DataSystem.getData(ConfigData).get("general")[this.recruitData.curWishId] as Hero_Client;
  79. this.spineLight.setAnimation(0, "animation", false);
  80. this.imgWish.spriteFrame = await this.res.load<SpriteFrame>("images/general/texture/head_img/" + heroDataOne.hero_res + "/spriteFrame", SpriteFrame);
  81. this.imgWishCamp.spriteFrame = await this.res.load<SpriteFrame>("images/general/texture/icon_camp_" + heroDataOne.camp + "/spriteFrame", SpriteFrame);
  82. }
  83. this.imgWish.node.active = this.recruitData.curWishId > 0;
  84. this.wishRect.active = this.recruitData.curWishId <= 0;
  85. this.txtName.string = this.recruitData.curWishId > 0 ? heroDataOne.name + "" : "指定武将";
  86. this.txtRate.string = this.recruitData.curWishId > 0 ? DataSystem.getData(RecruitData).wishHeroRate + "倍" : "";
  87. }
  88. /**更新货币*/
  89. private updateDiamond() {
  90. this.txtDiamond.string = this.userData.diamond > 10000 ? (this.userData.diamond / 1000).toFixed(1) + "k" : this.userData.diamond + "";
  91. this.txtCostOne.string = this.serverConfig.recruit1 + "";
  92. this.txtCostTen.string = this.serverConfig.recruit10 + "";// * 10 * this.serverConfig.invite_discount + "";
  93. this.txtCostOne.color = this.userData.diamond < this.serverConfig.recruit1 ? Color.RED : new Color(255, 235, 60, 255);
  94. this.txtCostTen.color = this.userData.diamond < this.serverConfig.recruit10 ? Color.RED : new Color(255, 235, 60, 255);
  95. this.txtCostTenOld.string = this.serverConfig.recruit1 * 10 + "";
  96. }
  97. /**
  98. * 招募武将
  99. * @param recruitType 招募类型 1 一次 2 十次
  100. */
  101. private async recruitHero(recruitType: number = 1) {
  102. let param: any = {}; param.type = recruitType == 1 ? 1 : 2;
  103. let result = await this.getComponent(Http).send("/api/recruit/recruit", { num: recruitType == 1 ? 1 : 10 });
  104. //console.log("Recruit: ", result);
  105. if (result && result.code == HttpResponseCode.Success) {
  106. let userData = DataSystem.getData(UserData);
  107. let recruit_diamond = recruitType == 1 ? this.serverConfig.recruit1 : this.serverConfig.recruit10;
  108. ReportThinking.currency_decrease('diamond', userData.diamond, recruit_diamond, userData.diamond - recruit_diamond, 'recruit');
  109. if (recruitType == 1) DataSystem.getData(UserData).diamond -= this.serverConfig.recruit1;
  110. else DataSystem.getData(UserData).diamond -= this.serverConfig.recruit10;
  111. param.data = result.data;//[{chip:0,id:121001}]
  112. this.openWnd.open(param);
  113. this.getComponent(UpdateRecruit).addRecruitResults(result.data);
  114. this.updateUserHeroData(result.data);
  115. //this.isCanRecruit = true;
  116. }
  117. else if (result && result.code == HttpErrorCode.NoTimes) {
  118. recruitType == 1 && WindowSystem.showTips("已达今日单次招募次数上限");
  119. recruitType == 2 && WindowSystem.showTips("已达今日十连招募次数上限");
  120. this.isCanRecruit = true;
  121. }
  122. else {
  123. WindowSystem.showTips("招募失败");
  124. this.isCanRecruit = true;
  125. }
  126. }
  127. private onRecruitEvent() {
  128. console.log("Recruit General Opening");
  129. this.isCanRecruit = true;
  130. }
  131. /**更新武将数据*/
  132. private updateUserHeroData(data: any[]) {
  133. let userHeroData = DataSystem.getData(UserHeroData);
  134. for (let i = 0; i < data.length; i++) {
  135. if (data[i].chip == 0) {
  136. userHeroData.addHero(data[i].hero.id);
  137. ReportThinking.hero_activate(DataSystem.getData(ConfigData).get('general')[data[i].hero.id].name, 'recruit');
  138. } else {
  139. userHeroData.addChip(data[i].hero.id, data[i].chip);
  140. }
  141. }
  142. this.updateMarshalSkillNotice();
  143. }
  144. /**更新统帅技能数据*/
  145. private async updateMarshalSkillNotice() {
  146. await MarshalSkillSystem.updateNoticeQueue();
  147. }
  148. /**是否存在兑换元宝次数*/
  149. private async isHaveExchangeTimes() {
  150. let result = await this.getComponent(Http).send("/api/recruit/GetExchange");
  151. if (result && result.code == HttpResponseCode.Success) {
  152. //return (result.data.freeTimes + result.data.videoTimes) > 0
  153. return result.data.hadExchangedTimes < Object.keys(result.data.config).length;
  154. }
  155. return false;
  156. }
  157. private async onClickDrawOne() {
  158. if (!this.isCanRecruit) return; this.isCanRecruit = false;
  159. if (this.userData.diamond >= this.serverConfig.recruit1) {
  160. this.recruitHero(1);
  161. } else {
  162. if (await this.isHaveExchangeTimes()) {
  163. WindowSystem.open("prefabs/ui/recruit/diamondExchange", WindowOpenMode.NotCloseAndAdd, ["5"]);
  164. this.isCanRecruit = true;
  165. } else {
  166. this.isCanRecruit = true;
  167. WindowSystem.showTips("元宝不足");
  168. }
  169. }
  170. }
  171. private async onClickDrawTen() {
  172. if (!this.isCanRecruit) return; this.isCanRecruit = false;
  173. if (this.userData.diamond >= this.serverConfig.recruit10) {
  174. this.recruitHero(2);
  175. } else {
  176. if (await this.isHaveExchangeTimes()) {
  177. WindowSystem.open("prefabs/ui/recruit/diamondExchange", WindowOpenMode.NotCloseAndAdd, ["5"]);
  178. this.isCanRecruit = true;
  179. } else {
  180. this.isCanRecruit = true;
  181. WindowSystem.showTips("元宝不足");
  182. }
  183. }
  184. }
  185. }