Sun_moves/assets/Script/GameOver.ts
2024-05-28 11:38:16 +08:00

168 lines
4.8 KiB
TypeScript

// Learn TypeScript:
// - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
import GameData from "./GameData";
import List from "./ListView/List";
import HttpUtil from "./crypto/serverAPI";
import Rq from "./crypto/serverAPI";
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;
// LIFE-CYCLE CALLBACKS:
// onLoad () {}
start () {
this.count.string = GameData._instance.GM_INFO.total;
this.time.string = GameData._instance.GM_INFO.mean_Time + "s";
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;
// this.setData();
this.getRank();
}
success(data){
}
back(){
cc.director.loadScene("RankScene");
}
again(){
cc.director.loadScene("GameScene");
}
getRank(){
let postData = {
"page":1,
"pageSize":100
};
HttpUtil.rankData(2,this.getRankData.bind(this),postData);
}
getRankData(data){
if(data){
this.listData = data.data.list;
this.selfData = data.data.info;
let rankData = [];
let self = false;
if(this.selfData.nickName.length > 4){
this.selfData.nickName= this.selfData.nickName.substring(0,4) + "..."
}
this.selfNode.getChildByName("name").getComponent(cc.Label).string = this.selfData.nickName;
this.selfNode.getChildByName("total").getComponent(cc.Label).string = this.selfData.totalSunCount;
this.setPic(this.selfNode.getChildByName("pic").getChildByName("icon"),this.selfData.pic);
for(let i=0;i<=this.listData.length-1;i++){
rankData.push({rank:(i+1), name:this.listData[i].nickName, total:this.listData[i].totalSunCount, pic:this.listData[i].pic});
if(GameData._instance.GM_INFO.userId == this.listData[i].userId){
self = true;
this.selfNode.getChildByName("rank").getComponent(cc.Label).string =(i+1) + "";
}
if(i == (this.listData.length-1) && self == false){
this.selfNode.getChildByName("rank").getComponent(cc.Label).string = "99+";
}
if(i<5) 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;
if(data.nickName.length > 4){
data.nickName= data.nickName.substring(0,4) + "..."
}
hitNode.getChildByName("name").getComponent(cc.Label).string = data.nickName;
hitNode.getChildByName("total").getComponent(cc.Label).string = data.totalSunCount;
this.setPic(hitNode.getChildByName("pic").getChildByName("icon"),data.pic);
}
}
public setPic(node,pic){
node.active = false;
let url = pic;
setTimeout(() => {
fetch(url)
.then(response => {
return response.headers.get('Content-Length');
})
.then(errNo => {
if(errNo == "5093"){
node.active = true;
}
})
.catch(error => {
console.error('Error fetching X-Info:', error);
});
}, 100);
cc.assetManager.loadRemote(url, {ext:'.jpg'},(err, texture:cc.Texture2D) => {
if(texture){
node.active = true;
node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
}
else{
console.log(err,texture)
}
})
}
// update (dt) {}
}