FriendFightUI.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { _decorator, Component, Node, Sprite, Label, Animation } from 'cc';
  2. import { DataSystem } from '../../core/data/DataSystem';
  3. import { Http, HttpResponseCode } from '../../core/net/Http';
  4. import List from '../../core/ui/virtualList/List';
  5. import { WindowOpenMode } from '../../core/ui/window/WindowOpenMode';
  6. import { WindowSystem } from '../../core/ui/window/WindowSystem';
  7. import { ConfigData } from '../../data/ConfigData';
  8. import { UserData } from '../../data/UserData';
  9. import { RedPointData } from '../../main/RedPointData';
  10. import { ReportThinking } from '../../ReportThinking';
  11. import { FightData } from '../FightData';
  12. import { FriendFightItem } from '../item/FriendFightItem';
  13. const { ccclass, property } = _decorator;
  14. @ccclass('FriendFightUI')
  15. export class FriendFightUI extends Component {
  16. @property({ type: Http, displayName: "Http组件", tooltip: "Http组件" }) http: Http = null;
  17. @property({ type: List, displayName: "List列表", tooltip: "List列表" }) list: List = null;
  18. @property({ type: Sprite, displayName: "图进度", tooltip: "图进度" }) imgProgress: Sprite = null;
  19. @property({ type: Label, displayName: "文本进度", tooltip: "文本进度" }) txtProgress: Label = null;
  20. @property({ type: Animation, displayName: "宝箱动画", tooltip: "宝箱动画" }) aniBox: Animation = null;
  21. private isCanClick = true;
  22. private listData: { id: number, avator: string, nickName: string, attack: number, isChallengeWin: boolean }[];
  23. async start() {
  24. await this.getFriend();
  25. this.updateInfo();
  26. }
  27. update() {
  28. DataSystem.watch(FightData, "pvpData") && this.updateInfo();
  29. this.watchChallenge();
  30. }
  31. private watchChallenge() {
  32. if (DataSystem.watch(FightData, "challengeSuccessFriendId")) {
  33. if (DataSystem.getData(FightData).challengeSuccessFriendId > 0) {
  34. let fightData = DataSystem.getData(FightData);
  35. if (fightData.pvpData.challengeWinUserIDList.indexOf(fightData.challengeSuccessFriendId) == -1) {
  36. fightData.pvpData.challengeWinUserIDList.push(fightData.challengeSuccessFriendId);
  37. fightData.challengeSuccessFriendId = -1;
  38. fightData.pvpData = fightData.pvpData;
  39. this.updateFriendData(fightData);
  40. if (fightData.pvpData.challengeWinUserIDList.length - DataSystem.getData(ConfigData).get("battle").challengeWinTimes >= 0
  41. && !fightData.pvpData.receivedChallengeReward) {
  42. DataSystem.getData(RedPointData).canReceiveFightBox = true;
  43. this.aniBox.play();
  44. }
  45. }
  46. }
  47. }
  48. }
  49. /**获取好友数据*/
  50. private async getFriend() {
  51. let fightData = DataSystem.getData(FightData);
  52. if (fightData.friendDataList.length == 0) {
  53. let friendData = await this.http.send("/api/pvp/GetFriend");
  54. if (friendData && friendData.code == HttpResponseCode.Success) {
  55. fightData.friendDataList = friendData.data.friendDatas;
  56. }
  57. }
  58. //if (!fightData.pvpData) {
  59. let pvpData = await this.http.send("/api/pvp/GetPVPData");
  60. if (pvpData && pvpData.code == HttpResponseCode.Success) {
  61. fightData.pvpData = pvpData.data;
  62. }
  63. //}
  64. this.updateFriendData(fightData);
  65. }
  66. /**更新好友列表数据*/
  67. private updateFriendData(fightData: FightData) {
  68. let cfg = DataSystem.getData(ConfigData).get("battle");
  69. let tempFriendData = fightData.friendDataList.concat();
  70. let tempListData = [];
  71. for (let i = 0; i < tempFriendData.length; i++) {
  72. if (tempFriendData[i].campLv >= cfg.unlockLv) {
  73. let isChallengeWin = false;
  74. for (let j = 0; j < fightData.pvpData.challengeWinUserIDList.length; j++) {
  75. if (fightData.pvpData.challengeWinUserIDList[j] == tempFriendData[i].id) {
  76. isChallengeWin = true;
  77. break;
  78. }
  79. }
  80. let tempData: any = { id: tempFriendData[i].id, avator: tempFriendData[i].avator, nickName: tempFriendData[i].nickName, attack: tempFriendData[i].attack, isChallengeWin: isChallengeWin };
  81. tempListData.push(tempData);
  82. }
  83. }
  84. this.listData = tempListData.concat();
  85. this.list.numItems = this.listData.length;
  86. console.log("FriendNum: " + this.listData.length);
  87. }
  88. private updateInfo() {
  89. console.log("updateInfo");
  90. let num = DataSystem.getData(FightData).pvpData.challengeWinUserIDList ? DataSystem.getData(FightData).pvpData.challengeWinUserIDList.length : 0;
  91. this.imgProgress.fillRange = num / DataSystem.getData(ConfigData).get("battle").challengeWinTimes;
  92. this.txtProgress.string = `${num} /${DataSystem.getData(ConfigData).get("battle").challengeWinTimes}`;
  93. if (DataSystem.getData(FightData).pvpData.challengeWinUserIDList.length - DataSystem.getData(ConfigData).get("battle").challengeWinTimes >= 0
  94. && !DataSystem.getData(FightData).pvpData.receivedChallengeReward) {
  95. DataSystem.getData(RedPointData).canReceiveFightBox = true;
  96. this.aniBox.play();
  97. }
  98. }
  99. /**当列表渲染时*/
  100. public onListRender(item: Node, index: number) {
  101. if (this.listData && this.listData.length > 0) {
  102. item.getComponent(FriendFightItem).onDataChange(this.listData[index], index);
  103. }
  104. }
  105. /**获取奖励
  106. * @param rewardType 奖励类型 除了 money diamond bonus prestige 之外为武将碎片id
  107. * @param numData 数量
  108. */
  109. private getReward(rewardType: string, numData: number) {
  110. let type = 1;
  111. switch (rewardType) {
  112. case "money": type = 1; break;
  113. case "diamond": type = 2; break;
  114. case "bonus": type = 3; break;
  115. case "prestige": type = 4; break;
  116. //WindowSystem.open("prefabs/ui/turntable/prizeFly", WindowOpenMode.NotCloseAndCover, [{ type: type, num: numData }]);
  117. //break;
  118. }
  119. let userData = DataSystem.getData(UserData);
  120. ReportThinking.currency_increase(rewardType, userData[rewardType], numData, userData[rewardType] + numData, 'challengeReward');
  121. WindowSystem.open("prefabs/ui/fight/fightRewardUI", WindowOpenMode.NotCloseAndCover, [[{ rewardType: type, num: numData }]]);
  122. }
  123. private async onClickBox() {
  124. if (!this.isCanClick) return;
  125. if (!DataSystem.getData(FightData).pvpData.receivedChallengeReward) {
  126. let deltaNum = DataSystem.getData(FightData).pvpData.challengeWinUserIDList.length - DataSystem.getData(ConfigData).get("battle").challengeWinTimes;
  127. if (deltaNum >= 0) {
  128. this.isCanClick = false;
  129. let result = await this.http.send("/api/pvp/ReceiveChallengeReward");
  130. if (result && result.code == HttpResponseCode.Success) {
  131. this.getReward(result.data.rewardType, result.data.rewardNum);
  132. DataSystem.getData(FightData).pvpData.receivedChallengeReward = true;
  133. DataSystem.getData(RedPointData).canReceiveFightBox = false;
  134. this.aniBox.stop();
  135. } else {
  136. WindowSystem.showTips(`请求失败`);
  137. }
  138. this.isCanClick = true;
  139. } else {
  140. WindowSystem.showTips(`还需${Math.abs(deltaNum)}场胜利即可领取宝箱`);
  141. }
  142. } else {
  143. WindowSystem.showTips(`你已领取该奖励`);
  144. }
  145. }
  146. }