FightUI.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import { _decorator, Component, Node, Label, Toggle } 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 { BitmapFont } from '../../core/ui/BitmapFont';
  6. import List from '../../core/ui/virtualList/List';
  7. import { WindowSystem } from '../../core/ui/window/WindowSystem';
  8. import { ConfigData } from '../../data/ConfigData';
  9. import { platform } from '../../data/jsb/platform';
  10. import { UserData } from '../../data/UserData';
  11. import { FormationData } from '../../hero/formation/FormationData';
  12. import { UserHeroData } from '../../hero/UserHeroData';
  13. import { ReportThinking } from '../../ReportThinking';
  14. import { FightData, FightItemData, FriendData } from '../FightData';
  15. import { FightItem } from '../item/FightItem';
  16. import { FightPlaceItem } from '../item/FightPlaceItem';
  17. const { ccclass, property } = _decorator;
  18. @ccclass('FightUI')
  19. export class FightUI extends Component {
  20. @property({ type: ResourceLoader, displayName: "资源加载组件", tooltip: "资源加载组件" }) res: ResourceLoader = null;
  21. @property({ type: Http, displayName: "Http组件", tooltip: "Http组件" }) http: Http = null;
  22. @property({ type: Label, displayName: '文本_总秒伤', tooltip: "文本_总秒伤" }) txtTotalAttack: Label = null;
  23. @property({ type: BitmapFont, displayName: '今日剩余切磋次数', tooltip: "今日剩余切磋次数" }) txtFightNum: BitmapFont = null;
  24. @property({ type: [FightPlaceItem], displayName: '站位', tooltip: "站位" }) fightPlaceItems: FightPlaceItem[] = [];
  25. @property({ type: List, displayName: '列表', tooltip: "列表" }) public list: List;
  26. private isLoaded: boolean = false;
  27. private listData: FightItemData[];
  28. private toggleIndex = 0;
  29. async start() {
  30. console.log("Data: ", this.listData);
  31. await this.loadData();
  32. this.loadItemData(1);
  33. this.updatePlaceItem();
  34. this.updateInfo();
  35. console.log("UserData: ", DataSystem.getData(UserData));
  36. //console.log("FightUI World Pos: (" + this.node.worldPosition.x + "," + this.node.worldPosition.y + ")");
  37. }
  38. update() {
  39. DataSystem.watch(FightData, "totalAttack") && this.updateInfo();
  40. DataSystem.watch(FightData, "pvpData") && this.updateInfo();
  41. (DataSystem.watch(FightData, "pvpData")
  42. || DataSystem.watch(FightData, "beAttackBonusList")) && this.loadItemData(this.toggleIndex == 0 ? 1 : 2);
  43. this.watchRevenge();
  44. this.watchAttackRedbag();
  45. }
  46. private watchAttackRedbag() {
  47. if (DataSystem.watch(FightData, "attackBonusList")) {
  48. console.log("Atk Redbag: ", DataSystem.getData(FightData).attackBonusList);
  49. this.loadItemData(this.toggleIndex == 0 ? 1 : 2);
  50. }
  51. }
  52. /**观察复仇数据变化*/
  53. private watchRevenge() {
  54. if (DataSystem.watch(FightData, "revengeSuccessEnemyId")) {
  55. if (DataSystem.getData(FightData).revengeSuccessEnemyId > 0) {
  56. let fightData = DataSystem.getData(FightData);
  57. for (let i = 0; i < fightData.beAttackBonusList.length; i++) {
  58. if (fightData.beAttackBonusList[i].enemyID == fightData.revengeSuccessEnemyId) {
  59. fightData.beAttackBonusList[i].revenged = true;
  60. }
  61. }
  62. fightData.revengeSuccessEnemyId = -1;
  63. this.toggleIndex == 1 && this.loadItemData(2);
  64. }
  65. }
  66. }
  67. /**标签切换时*/
  68. onToggle(toggle: Toggle) {
  69. this.toggleIndex = toggle._toggleContainer.toggleItems.indexOf(toggle);
  70. this.loadItemData(toggle._toggleContainer.toggleItems.indexOf(toggle) + 1);
  71. }
  72. /**更新武将位置信息显示*/
  73. private updatePlaceItem() {
  74. let fightData = DataSystem.getData(FightData);
  75. let formationData = DataSystem.getData(FormationData);
  76. let userHeroData = DataSystem.getData(UserHeroData);
  77. let userData = DataSystem.getData(UserData);
  78. let cfg = DataSystem.getData(ConfigData).get("battle");
  79. let selfHeroId = formationData.get(5) ? formationData.get(5) : -1;
  80. let selfAttack = selfHeroId > 0 ? userHeroData.get(selfHeroId).client.attack : 0;
  81. for (let i = 0; i < 4; i++) {
  82. if (selfHeroId < 0)
  83. selfHeroId = formationData.get(i + 1) ? formationData.get(i + 1) : -1;
  84. selfAttack += formationData.get(i + 1) ? userHeroData.get(formationData.get(i + 1)).client.attack : 0;
  85. }
  86. let selfData: FriendData = { id: 0, heroID: selfHeroId, attack: selfAttack, campLv: userData.campLv, avator: userData.avator, nickName: userData.nickName, contribution: userData.contribution };
  87. this.fightPlaceItems[0].init(selfData);
  88. for (let i = 1; i < this.fightPlaceItems.length; i++) {
  89. let friendData = fightData.friendDataList[i - 1] ? fightData.friendDataList[i - 1] : null;
  90. friendData = friendData ? (friendData.campLv >= cfg.unlockLv ? friendData : null) : null;
  91. this.fightPlaceItems[i].init(friendData);
  92. }
  93. }
  94. private updateInfo() {
  95. this.txtTotalAttack.string = DataSystem.getData(FightData).totalAttack > 10000 ? (DataSystem.getData(FightData).totalAttack / 1000).toFixed(1) + "k" : DataSystem.getData(FightData).totalAttack + "";
  96. this.txtFightNum.string = (DataSystem.getData(FightData).pvpData.times + DataSystem.getData(FightData).pvpData.videoTimes) + "";
  97. }
  98. /**加载数据*/
  99. private async loadData() {
  100. let fightData = DataSystem.getData(FightData);
  101. await fightData.pull(this.http);
  102. this.isLoaded = true;
  103. console.log("FightData: " + JSON.stringify(fightData));
  104. }
  105. /**加载列表数据
  106. * @param itemType item类型 1 切磋 2 复仇
  107. */
  108. private async loadItemData(itemType: number) {
  109. if (this.isLoaded) {
  110. let fightData = DataSystem.getData(FightData);
  111. let tempListData = [];
  112. let attackRbList = fightData.attackBonusList.concat();
  113. let beAttackRbList = fightData.beAttackBonusList.concat();
  114. let cfg = DataSystem.getData(ConfigData).get("battle");
  115. if (itemType == 1) {//切磋
  116. let randomPlayer = new FightItemData();
  117. randomPlayer.id = 0;
  118. randomPlayer.attack = 0;
  119. randomPlayer.avator = "";
  120. randomPlayer.isRandomPlayer = true;
  121. tempListData.push(randomPlayer);
  122. for (let i = 0; i < fightData.friendDataList.length; i++) {
  123. if (fightData.friendDataList[i].campLv >= cfg.unlockLv) {
  124. let tempFriendData = new FightItemData();
  125. tempFriendData.attack = fightData.friendDataList[i].attack;
  126. tempFriendData.avator = fightData.friendDataList[i].avator;
  127. tempFriendData.id = fightData.friendDataList[i].id;
  128. tempFriendData.itemType = itemType;
  129. tempFriendData.nickName = fightData.friendDataList[i].nickName;
  130. tempFriendData.redbagType = -1;
  131. let isHaveAttackRb = false, isHaveBeAttackRb = false;
  132. for (let j = 0; j < fightData.attackBonusList.length; j++) {
  133. if (fightData.attackBonusList[j].enemyID == tempFriendData.id) {
  134. isHaveAttackRb = true;
  135. tempFriendData.redbagType = 1;
  136. tempFriendData.redbagKey = fightData.attackBonusList[j].key;
  137. tempFriendData.redbagData = fightData.attackBonusList[j];
  138. tempFriendData.canOpenTime = fightData.attackBonusList[j].canOpenTime;
  139. attackRbList.splice(j, 1);
  140. break;
  141. }
  142. }
  143. if (!isHaveAttackRb) {
  144. for (let j = 0; j < fightData.beAttackBonusList.length; j++) {
  145. if (fightData.beAttackBonusList[j].enemyID == tempFriendData.id) {
  146. isHaveBeAttackRb = true;
  147. tempFriendData.redbagType = 2;
  148. tempFriendData.redbagKey = fightData.beAttackBonusList[j].key;
  149. tempFriendData.redbagData = fightData.beAttackBonusList[j];
  150. tempFriendData.canOpenTime = fightData.beAttackBonusList[j].canOpenTime;
  151. beAttackRbList.splice(j, 1);
  152. break;
  153. }
  154. }
  155. }
  156. tempListData.push(tempFriendData);
  157. }
  158. }
  159. for (let i = 0; i < attackRbList.length; i++) {
  160. let tempFriendData = new FightItemData();
  161. tempFriendData.attack = attackRbList[i].attack;
  162. tempFriendData.avator = attackRbList[i].avator;
  163. tempFriendData.id = attackRbList[i].enemyID;
  164. tempFriendData.itemType = itemType;
  165. tempFriendData.nickName = attackRbList[i].nickName;
  166. tempFriendData.redbagType = 1;
  167. tempFriendData.redbagKey = attackRbList[i].key;
  168. tempFriendData.redbagData = attackRbList[i];
  169. tempFriendData.canOpenTime = attackRbList[i].canOpenTime;
  170. tempListData.push(tempFriendData);
  171. }
  172. for (let i = 0; i < beAttackRbList.length; i++) {
  173. if (beAttackRbList[i].openID <= 0) {
  174. let tempFriendData = new FightItemData();
  175. tempFriendData.attack = beAttackRbList[i].attack;
  176. tempFriendData.avator = beAttackRbList[i].avator;
  177. tempFriendData.id = beAttackRbList[i].enemyID;
  178. tempFriendData.itemType = itemType;
  179. tempFriendData.nickName = beAttackRbList[i].nickName;
  180. tempFriendData.redbagType = 2;
  181. tempFriendData.redbagKey = beAttackRbList[i].key;
  182. tempFriendData.redbagData = beAttackRbList[i];
  183. tempFriendData.canOpenTime = beAttackRbList[i].canOpenTime;
  184. tempListData.push(tempFriendData);
  185. }
  186. }
  187. } else {//复仇
  188. for (let i = 0; i < beAttackRbList.length; i++) {
  189. if (!beAttackRbList[i].revenged) {
  190. if (!this.isExistRevengeEnemy(tempListData, beAttackRbList[i].enemyID)) {
  191. let tempFriendData = new FightItemData();
  192. tempFriendData.attack = beAttackRbList[i].attack;
  193. tempFriendData.avator = beAttackRbList[i].avator;
  194. tempFriendData.id = beAttackRbList[i].enemyID;
  195. tempFriendData.itemType = itemType;
  196. tempFriendData.nickName = beAttackRbList[i].nickName;
  197. tempFriendData.redbagType = -1;
  198. tempFriendData.canOpenTime = null;
  199. tempListData.push(tempFriendData);
  200. }
  201. }
  202. }
  203. }
  204. this.listData = tempListData;
  205. this.list.numItems = this.listData.length;
  206. console.log("Type: " + itemType);
  207. console.log("List: ", this.listData);
  208. } else {
  209. this.listData = [];
  210. this.list.numItems = 0;
  211. }
  212. }
  213. /**复仇列表是否已存在该敌人*/
  214. private isExistRevengeEnemy(listData: any[], enemyId: number) {
  215. for (let i = 0; i < listData.length; i++) {
  216. if (listData[i].id == enemyId) return true;
  217. }
  218. return false;
  219. }
  220. /**当列表渲染时*/
  221. public onListRender(item: Node, index: number) {
  222. if (this.listData && this.listData.length > 0) {
  223. item.getComponent(FightItem).onDataChange(this.listData[index], index);
  224. }
  225. }
  226. /**招募好友*/
  227. private async onClickShare() {
  228. //邀请
  229. let result = await this.http.send("/api/app/share", {});
  230. if (result.code == 0) {
  231. ReportThinking.invite();
  232. platform.wxShare(result.data.url, result.data.title, result.data.descroption);
  233. } else {
  234. WindowSystem.showTips("分享信息获取失败,请稍后再试");
  235. }
  236. }
  237. }