RankUI.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { _decorator, Component, Node, Label } from 'cc';
  2. import { Http, HttpResponseCode } from '../../core/net/Http';
  3. import { HttpSystem } from '../../core/net/HttpSystem';
  4. import { CountDown } from '../../core/ui/CountDown';
  5. import { OpenWindow } from '../../core/ui/window/OpenWindow';
  6. import { WindowManager } from '../../core/ui/window/WindowManager';
  7. import { WindowSystem } from '../../core/ui/window/WindowSystem';
  8. import { Utils } from '../../core/utils/Utils';
  9. import { g } from '../../Data/g';
  10. import List from '../../List/List';
  11. import { RankItemData } from './RankData';
  12. import { RankItem } from './RankItem';
  13. import { RankItemBest } from './RankItemBest';
  14. const { ccclass, property } = _decorator;
  15. @ccclass('RankUI')
  16. export class RankUI extends Component {
  17. @property({ type: Http, tooltip: "Http组件" }) http: Http = null;
  18. @property({ type: Label, tooltip: "时间文本" }) txtTime: Label = null;
  19. @property({ type: [RankItemBest], tooltip: "三甲Item" }) itemBestAry: RankItemBest[] = [];
  20. @property({ type: RankItem, tooltip: "玩家Item" }) itemSelf: RankItem = null;
  21. @property({ type: List, displayName: '列表', tooltip: "列表" }) public list: List;
  22. @property({ type: CountDown, displayName: "倒计时组件", tooltip: "倒计时组件" }) countDown: CountDown = null;
  23. private listData: RankItemData[] = [];
  24. private remaindTime: number = 0;
  25. async start() {
  26. console.log("RankData Init: ", g.gameData.rankData);
  27. if (g.gameData.rankData == null) {
  28. console.log("Local Cur Time: " + Date.now());
  29. console.log("Server Cur Time: " + HttpSystem.serverTime);
  30. await this.loadData();
  31. this.loadOtherData();
  32. } else {
  33. let curTime = HttpSystem.serverTime;
  34. console.log("Local Cur Time: " + Date.now());
  35. console.log("Server Cur Time: " + HttpSystem.serverTime);
  36. console.log("DeltaTime: " + (g.gameData.rankData.nextRefreshTime - curTime * 0.001));
  37. if (curTime * 0.001 > g.gameData.rankData.nextRefreshTime) {
  38. await this.loadData();
  39. this.loadOtherData();
  40. } else {
  41. this.listData = g.gameData.rankData.rankDatas;
  42. this.list.numItems = this.listData.length >= 3 ? this.listData.length - 3 : 0;
  43. this.loadOtherData();
  44. }
  45. }
  46. console.log("RankData Finish: ", g.gameData.rankData);
  47. if (!localStorage.getItem("FirstRank") || localStorage.getItem("FirstRank") == "0") {
  48. if (this.node.getComponent(OpenWindow)) {
  49. this.node.getComponent(OpenWindow).open();
  50. localStorage.setItem("FirstRank", "1");
  51. }
  52. }
  53. }
  54. private async loadData() {
  55. let result = await this.http.send("/api/rank/GetRankData");
  56. console.log("RankData: ", result);
  57. // result = null;
  58. // return;
  59. if (result && result.code == HttpResponseCode.Success) {
  60. g.gameData.rankData = result.data;
  61. g.gameData.rankScore = result.data.myScore;
  62. g.gameData.isRefreshRankTime = true;
  63. this.listData = result.data.rankDatas;
  64. this.list.numItems = this.listData.length >= 3 ? this.listData.length - 3 : 0;
  65. } else {
  66. WindowManager.showTips("活动已关闭");
  67. }
  68. }
  69. private loadOtherData() {
  70. for (let i = 0; i < this.itemBestAry.length; i++) {
  71. if (this.listData.length >= i + 1) {
  72. this.itemBestAry[i].onDataChange(this.listData[i], i);
  73. } else {
  74. this.itemBestAry[i].onDataChange(null, i);
  75. }
  76. }
  77. let playerRankData: RankItemData;
  78. let playerIndex = 0;
  79. for (let i = 0; i < this.listData.length; i++) {
  80. if (this.listData[i].id == g.userData.id) {
  81. playerRankData = this.listData[i];
  82. playerRankData.score = g.gameData.rankScore;
  83. playerIndex = i;
  84. break;
  85. }
  86. }
  87. if (playerRankData == null) {
  88. playerRankData = { id: 0, avator: "", nick: "", score: 0 };
  89. playerRankData.id = g.userData.id;
  90. playerRankData.score = g.gameData.rankScore;
  91. playerRankData.nick = g.userData.nickName;
  92. playerRankData.avator = g.userData.avator;
  93. playerIndex = 1001;
  94. }
  95. if (g.gameData.rankData != null) {
  96. let deltaTime = g.gameData.rankData.finishTime - HttpSystem.serverTime * 0.001;
  97. this.remaindTime = deltaTime > 0 ? deltaTime : 0;
  98. g.gameData.isCanGetRankReward = this.remaindTime == 0 && !g.gameData.rankData.received;
  99. if (this.remaindTime > 0) { this.updateTxtTime(deltaTime); } else { this.countDown.stop(); this.finishCooling(); }
  100. if (!this.countDown.running && this.remaindTime > 0) { this.countDown.value = this.remaindTime; this.countDown.play(); }
  101. } else {
  102. this.txtTime.string = `活动已关闭`;
  103. }
  104. this.itemSelf.onDataChange(playerRankData, playerIndex, true, true);
  105. }
  106. public updateTxtTime(progress) {
  107. this.txtTime.string = `活动时段:还剩${Utils.formatCountDown(progress * 1000, true, true)}`;
  108. }
  109. public finishCooling() {
  110. this.remaindTime = 0;
  111. g.gameData.isCanGetRankReward = this.remaindTime == 0 && !g.gameData.rankData.received;
  112. this.txtTime.string = `活动已结束,领取奖励阶段`;
  113. }
  114. /**当列表渲染时*/
  115. public onListRender(item: Node, index: number) {
  116. if (this.listData && this.listData.length > 0) {
  117. //item.getComponent(RankItem).onDataChange(this.listData[index], index + 3, (index + 1) % 2 == 0);
  118. item.getComponent(RankItem).onDataChange(this.listData[index + 3], index + 3, (index + 1) % 2 == 0);
  119. }
  120. }
  121. }