
const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    @property(cc.Label)
    count: cc.Label = null;
    @property(cc.Label)
    time: cc.Label = null;
    @property(cc.Node)
    selfNode: cc.Node = null;
    @property(cc.Node)
    one: cc.Node = null;
    @property(cc.Node)
    two: cc.Node = null;
    @property(cc.Node)
    three: cc.Node = null;
    @property(cc.Node)
    four: cc.Node = null;
    @property(cc.Node)
    five: cc.Node = null;
    listData: any;
    selfData: any;
    // onLoad () {}
    start () {
        this.count.string = cc.fx.GameConfig.GM_INFO.total + "";
        if(cc.fx.GameConfig.GM_INFO.mean_Time > 10){
            cc.fx.GameConfig.GM_INFO.mean_Time = (parseInt(Math.random()*10+"")+5)/10
        }
        this.time.string = cc.fx.GameConfig.GM_INFO.mean_Time + "s";
        this.init();
    }
    //初始化数据
    init(){
        this.listData = [];
        this.selfData = null;
        this.one.active = false;
        this.two.active = false;
        this.three.active = false;
        this.four.active = false;
        this.five.active = false;
        var urlNow = window.location.href;
        if(this.containsTrain(urlNow)){
            this.node.getChildByName("again").active = false;
            this.node.getChildByName("back").active = false;
            this.node.getChildByName("finishi").active = true;
        }
        else{
            this.node.getChildByName("again").active = true;
            this.node.getChildByName("back").active = true;
            this.node.getChildByName("finishi").active = false;
        }
        this.getRank();
    }
    //打开排行榜
    jumpFinishi(){
        let url = "https://train.sparkus.cn/poster/game/" + cc.fx.GameConfig.GM_INFO.scode + "?suc=1";
        window.location.href = url;
    }
    //打开排行榜
    openRank(){
        cc.director.loadScene("RankScene");
    }
    //重新开始玩
    again(){
        cc.director.loadScene("GameScene");
    }
    //判断来源
    containsTrain(str) {
        return /from=train/i.test(str);
    }
    //获取排行榜
    getRank(){
        //获取排行榜数据  所需数据量
        let dataFile = {
            length:5
        }
         cc.fx.GameTool.getRank(dataFile,data =>this.getRankData(data));
    }
    //设置排行信息
    getRankData(data){
        if(data){
             cc.fx.GameTool.getRankData(data,this,4);
             cc.fx.GameTool.setPic(this.selfNode.getChildByName("pic").getChildByName("icon"),this.selfData.pic);
            for(let i=0;i<=4;i++){
                this.setRank(i,this.listData[i]);
            }
        }
    }
    //根据内容填充排行榜
    setRank(num,data){
        var hitNode = null;
        if(num == 0){
            hitNode = this.one;
        }
        else if(num == 1){
            hitNode = this.two;
        }
        else if(num == 2){
            hitNode = this.three;
        }
        else if(num == 3){
            hitNode = this.four;
        }
        else if(num == 4){
            hitNode = this.five;
        }
        if(hitNode){
            hitNode.active = true;
             cc.fx.GameTool.subName(data.nickName,4);
            hitNode.getChildByName("name").getComponent(cc.Label).string = data.nickName;
            hitNode.getChildByName("total").getComponent(cc.Label).string = data.totalSunCount;
             cc.fx.GameTool.setPic(hitNode.getChildByName("pic").getChildByName("icon"),data.pic);
        }
    }   
}
