import { GameMode } from "../game/GameMode"; import Main from "../game/Main"; import { SDK as sdk, CMD} from '../game/sdk'; const {ccclass, property} = cc._decorator; @ccclass export default class rankPanel extends cc.Component { @property(cc.Node) tmpRow: cc.Node = null; @property(cc.Label) best: cc.Label = null; private gap:number = 40; private items:cc.Node[] = null; private gameMode:GameMode; // onLoad () {} private mId:number; private data1; private data2; private isGeting:boolean = false; onLoad() { let me = this; me.gameMode = GameMode.endless; this.best.string = "排行榜"; sdk.getUserInfo(function (info) { me.mId = info.id; // console.log("我的用户信息:" + info.id + " " + info); me.ToggleShow(); }) } public ToggleShow() { if (this.node.active) { this.getData(); } } private getData(){ let me = this; if (me.isGeting) { return; } me.isGeting = true; me.gameMode = this.gameMode == GameMode.normal ? GameMode.endless : GameMode.normal; sdk.getRank(Main.Ins.GetSDKGameMode(GameMode.normal), function(args1){ //世界榜 me.data1 = args1; // console.log("getRank----------1--" + JSON.stringify(args1)); sdk.getRank(Main.Ins.GetSDKGameMode(GameMode.endless), function(args2){//好友榜 me.data2 = args2; // console.log("getRank-------2-----" + JSON.stringify(args2)); me.best.string = me.gameMode == GameMode.normal ? "世界榜" : "好友榜"; me.best.string +=" 历史最高分:"+ me.getBestScore(args1,args2); me.showList(me.gameMode == GameMode.normal ? me.data1 : me.data2); me.isGeting = false; }); }); } private checkeValid(args){ if (!args) { return false;} // console.log("getRank------------" + JSON.stringify(args)); if (!args || !args[1] || !args[1]["list"]) { // console.error("排行榜数据为空"); return false; } return true; } private getBestScore(data1,data2):number{ let me =this; let getMyScore = function (args) { if (!me.checkeValid(args)) { return 0; } let datas = args[1]["list"]; for (let i = 0; i < datas.length; i++) { const element = datas[i]; if (element.user.id == me.mId) { return element.score; } } return 0; } return Math.max(getMyScore(data1),getMyScore(data2)); } showList(args) { if (!this.checkeValid(args)) {console.error("排行榜数据为空"); return; } // console.log("getRank 1111 type: " + args[0] + " == " + args[1] + " == " + args[1]["list"].length); if (!this.items) { this.items = []; this.tmpRow.active = false; this.items.push(this.tmpRow); for (let i = 1; i < 10; i++) { let row = cc.instantiate(this.tmpRow); row.parent = this.tmpRow.parent; row.y = this.tmpRow.y - this.gap*i; this.items.push(row); } } let datas = args[1]["list"];//[{rank:"1",name:"s",score:"44"},{rank:"2",name:"sfr",score:"54"}];// let count = datas.length; for (let i = 0; i < this.items.length; i++) { const element = this.items[i]; element.active = i < count; if (i < count) { let data = datas[i]; element.getChildByName("rank").getComponent(cc.Label).string = data.rank; element.getChildByName("name").getComponent(cc.Label).string = data.user.nickname; element.getChildByName("score").getComponent(cc.Label).string = data.score; } } } public Hide(){ this.node.active = false; } }