| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- import { _decorator, Component, Node, Label } from 'cc';
- import { Http, HttpResponseCode } from '../../core/net/Http';
- import { HttpSystem } from '../../core/net/HttpSystem';
- import { CountDown } from '../../core/ui/CountDown';
- import { OpenWindow } from '../../core/ui/window/OpenWindow';
- import { WindowManager } from '../../core/ui/window/WindowManager';
- import { WindowSystem } from '../../core/ui/window/WindowSystem';
- import { Utils } from '../../core/utils/Utils';
- import { g } from '../../Data/g';
- import List from '../../List/List';
- import { RankItemData } from './RankData';
- import { RankItem } from './RankItem';
- import { RankItemBest } from './RankItemBest';
- const { ccclass, property } = _decorator;
- @ccclass('RankUI')
- export class RankUI extends Component {
- @property({ type: Http, tooltip: "Http组件" }) http: Http = null;
- @property({ type: Label, tooltip: "时间文本" }) txtTime: Label = null;
- @property({ type: [RankItemBest], tooltip: "三甲Item" }) itemBestAry: RankItemBest[] = [];
- @property({ type: RankItem, tooltip: "玩家Item" }) itemSelf: RankItem = null;
- @property({ type: List, displayName: '列表', tooltip: "列表" }) public list: List;
- @property({ type: CountDown, displayName: "倒计时组件", tooltip: "倒计时组件" }) countDown: CountDown = null;
- private listData: RankItemData[] = [];
- private remaindTime: number = 0;
- async start() {
- console.log("RankData Init: ", g.gameData.rankData);
- if (g.gameData.rankData == null) {
- console.log("Local Cur Time: " + Date.now());
- console.log("Server Cur Time: " + HttpSystem.serverTime);
- await this.loadData();
- this.loadOtherData();
- } else {
- let curTime = HttpSystem.serverTime;
- console.log("Local Cur Time: " + Date.now());
- console.log("Server Cur Time: " + HttpSystem.serverTime);
- console.log("DeltaTime: " + (g.gameData.rankData.nextRefreshTime - curTime * 0.001));
- if (curTime * 0.001 > g.gameData.rankData.nextRefreshTime) {
- await this.loadData();
- this.loadOtherData();
- } else {
- this.listData = g.gameData.rankData.rankDatas;
- this.list.numItems = this.listData.length >= 3 ? this.listData.length - 3 : 0;
- this.loadOtherData();
- }
- }
- console.log("RankData Finish: ", g.gameData.rankData);
- if (!localStorage.getItem("FirstRank") || localStorage.getItem("FirstRank") == "0") {
- if (this.node.getComponent(OpenWindow)) {
- this.node.getComponent(OpenWindow).open();
- localStorage.setItem("FirstRank", "1");
- }
- }
- }
- private async loadData() {
- let result = await this.http.send("/api/rank/GetRankData");
- console.log("RankData: ", result);
- // result = null;
- // return;
- if (result && result.code == HttpResponseCode.Success) {
- g.gameData.rankData = result.data;
- g.gameData.rankScore = result.data.myScore;
- g.gameData.isRefreshRankTime = true;
- this.listData = result.data.rankDatas;
- this.list.numItems = this.listData.length >= 3 ? this.listData.length - 3 : 0;
- } else {
- WindowManager.showTips("活动已关闭");
- }
- }
- private loadOtherData() {
- for (let i = 0; i < this.itemBestAry.length; i++) {
- if (this.listData.length >= i + 1) {
- this.itemBestAry[i].onDataChange(this.listData[i], i);
- } else {
- this.itemBestAry[i].onDataChange(null, i);
- }
- }
- let playerRankData: RankItemData;
- let playerIndex = 0;
- for (let i = 0; i < this.listData.length; i++) {
- if (this.listData[i].id == g.userData.id) {
- playerRankData = this.listData[i];
- playerRankData.score = g.gameData.rankScore;
- playerIndex = i;
- break;
- }
- }
- if (playerRankData == null) {
- playerRankData = { id: 0, avator: "", nick: "", score: 0 };
- playerRankData.id = g.userData.id;
- playerRankData.score = g.gameData.rankScore;
- playerRankData.nick = g.userData.nickName;
- playerRankData.avator = g.userData.avator;
- playerIndex = 1001;
- }
- if (g.gameData.rankData != null) {
- let deltaTime = g.gameData.rankData.finishTime - HttpSystem.serverTime * 0.001;
- this.remaindTime = deltaTime > 0 ? deltaTime : 0;
- g.gameData.isCanGetRankReward = this.remaindTime == 0 && !g.gameData.rankData.received;
- if (this.remaindTime > 0) { this.updateTxtTime(deltaTime); } else { this.countDown.stop(); this.finishCooling(); }
- if (!this.countDown.running && this.remaindTime > 0) { this.countDown.value = this.remaindTime; this.countDown.play(); }
- } else {
- this.txtTime.string = `活动已关闭`;
- }
- this.itemSelf.onDataChange(playerRankData, playerIndex, true, true);
- }
- public updateTxtTime(progress) {
- this.txtTime.string = `活动时段:还剩${Utils.formatCountDown(progress * 1000, true, true)}`;
- }
- public finishCooling() {
- this.remaindTime = 0;
- g.gameData.isCanGetRankReward = this.remaindTime == 0 && !g.gameData.rankData.received;
- this.txtTime.string = `活动已结束,领取奖励阶段`;
- }
- /**当列表渲染时*/
- public onListRender(item: Node, index: number) {
- if (this.listData && this.listData.length > 0) {
- //item.getComponent(RankItem).onDataChange(this.listData[index], index + 3, (index + 1) % 2 == 0);
- item.getComponent(RankItem).onDataChange(this.listData[index + 3], index + 3, (index + 1) % 2 == 0);
- }
- }
- }
|