import { _decorator, Component, Node, Label, instantiate } from 'cc'; import { UserMgr } from '../../module_basic/scripts/UserMgr'; import { SubGameMgr } from '../../module_basic/scripts/SubGameMgr'; import { RoomListItem } from './RoomListItem'; const { ccclass, property } = _decorator; const PAGE_ITEM_NUM = 7; const PAGE_REFRESH_TIME = 3; @ccclass('RoomList') export class RoomList extends Component { @property(Node) contentRoot: Node; @property(Node) tips: Node; @property(Label) lblPage: Label; private _curPage: number = 0; private _pageNum: number = 1; private _itemPrefab: Node; async start() { this.contentRoot.active = false; this.tips.active = true; this._itemPrefab = this.contentRoot.children[0]; this._itemPrefab.removeFromParent(); this.updatePageLabelView(); this.refreshRoomList(this._curPage, PAGE_ITEM_NUM); this.schedule(() => { this.refreshRoomList(this._curPage, PAGE_ITEM_NUM); }, PAGE_REFRESH_TIME); } async refreshRoomList(curPage: number, pageItemNum: number) { if(this._waitingData == true){ return; } this._waitingData = true; let ret = await UserMgr.inst.rpc_GetRoomList(SubGameMgr.current.id, curPage, pageItemNum); this._waitingData = false; if (!ret.isSucc) { return; } if (!this.isValid) { return; } this._curPage = ret.res.curPage; this._pageNum = ret.res.pageNum; this.updatePageLabelView(); let rooms = ret.res.rooms; if (rooms) { this.contentRoot.children.forEach(v => { v.active = false; }); this.tips.active = false; this.contentRoot.active = true; for (let i = 0; i < rooms.length; ++i) { let node = this.contentRoot.children[i]; if (!node) { node = instantiate(this._itemPrefab); this.contentRoot.addChild(node); } node.active = true; let cache = rooms[i]; let item = node.getComponent(RoomListItem); item.lblRoomId.string = cache.displayId; item.lblName.string = cache.name; item.lblPlayerNum.string = cache.playerNum + '/' + cache.maxPlayerNum; let maxSpectatorNum = cache.maxUserNum - cache.maxPlayerNum; let spectatorNum = cache.userNum - cache.playerNum; item.lblSpectatorNum.string = spectatorNum + '/' + maxSpectatorNum; item.needPassword = cache.needPassword; } } } update(deltaTime: number) { } private _waitingData = false; private _lastClickTime = Date.now(); updatePageLabelView(){ if(this._pageNum == 0){ this.lblPage.string = '0/0'; } else{ this.lblPage.string = (this._curPage + 1) + '/' + this._pageNum; } } onBtnPrePage() { if(this._waitingData || Date.now() - this._lastClickTime < 1000){ return; } this._lastClickTime = Date.now(); this._curPage--; if (this._curPage < 0) { this._curPage = 0; } this.refreshRoomList(this._curPage, PAGE_ITEM_NUM); } onBtnNextPage() { if(this._waitingData || Date.now() - this._lastClickTime < 1000){ return; } this._lastClickTime = Date.now(); this._curPage++; if (this._curPage >= this._pageNum) { this._curPage = this._pageNum - 1; } this.refreshRoomList(this._curPage, PAGE_ITEM_NUM); } }