rankPanel.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { GameMode } from "../game/GameMode";
  2. import Main from "../game/Main";
  3. import { SDK as sdk, CMD} from '../game/sdk';
  4. const {ccclass, property} = cc._decorator;
  5. @ccclass
  6. export default class rankPanel extends cc.Component {
  7. @property(cc.Node)
  8. tmpRow: cc.Node = null;
  9. @property(cc.Label)
  10. best: cc.Label = null;
  11. private gap:number = 40;
  12. private items:cc.Node[] = null;
  13. private gameMode:GameMode;
  14. // onLoad () {}
  15. private mId:number;
  16. private data1;
  17. private data2;
  18. private isGeting:boolean = false;
  19. onLoad() {
  20. let me = this;
  21. me.gameMode = GameMode.endless;
  22. this.best.string = "排行榜";
  23. sdk.getUserInfo(function (info) {
  24. me.mId = info.id;
  25. // console.log("我的用户信息:" + info.id + " " + info);
  26. me.ToggleShow();
  27. })
  28. }
  29. public ToggleShow() {
  30. if (this.node.active) {
  31. this.getData();
  32. }
  33. }
  34. private getData(){
  35. let me = this;
  36. if (me.isGeting) { return; }
  37. me.isGeting = true;
  38. me.gameMode = this.gameMode == GameMode.normal ? GameMode.endless : GameMode.normal;
  39. sdk.getRank(Main.Ins.GetSDKGameMode(GameMode.normal), function(args1){ //世界榜
  40. me.data1 = args1;
  41. // console.log("getRank----------1--" + JSON.stringify(args1));
  42. sdk.getRank(Main.Ins.GetSDKGameMode(GameMode.endless), function(args2){//好友榜
  43. me.data2 = args2;
  44. // console.log("getRank-------2-----" + JSON.stringify(args2));
  45. me.best.string = me.gameMode == GameMode.normal ? "世界榜" : "好友榜";
  46. me.best.string +=" 历史最高分:"+ me.getBestScore(args1,args2);
  47. me.showList(me.gameMode == GameMode.normal ? me.data1 : me.data2);
  48. me.isGeting = false;
  49. });
  50. });
  51. }
  52. private checkeValid(args){
  53. if (!args) { return false;}
  54. // console.log("getRank------------" + JSON.stringify(args));
  55. if (!args || !args[1] || !args[1]["list"]) {
  56. // console.error("排行榜数据为空");
  57. return false;
  58. }
  59. return true;
  60. }
  61. private getBestScore(data1,data2):number{
  62. let me =this;
  63. let getMyScore = function (args) {
  64. if (!me.checkeValid(args)) { return 0; }
  65. let datas = args[1]["list"];
  66. for (let i = 0; i < datas.length; i++) {
  67. const element = datas[i];
  68. if (element.user.id == me.mId) {
  69. return element.score;
  70. }
  71. }
  72. return 0;
  73. }
  74. return Math.max(getMyScore(data1),getMyScore(data2));
  75. }
  76. showList(args) {
  77. if (!this.checkeValid(args)) {console.error("排行榜数据为空"); return; }
  78. // console.log("getRank 1111 type: " + args[0] + " == " + args[1] + " == " + args[1]["list"].length);
  79. if (!this.items) {
  80. this.items = [];
  81. this.tmpRow.active = false;
  82. this.items.push(this.tmpRow);
  83. for (let i = 1; i < 10; i++) {
  84. let row = cc.instantiate(this.tmpRow);
  85. row.parent = this.tmpRow.parent;
  86. row.y = this.tmpRow.y - this.gap*i;
  87. this.items.push(row);
  88. }
  89. }
  90. let datas = args[1]["list"];//[{rank:"1",name:"s",score:"44"},{rank:"2",name:"sfr",score:"54"}];//
  91. let count = datas.length;
  92. for (let i = 0; i < this.items.length; i++) {
  93. const element = this.items[i];
  94. element.active = i < count;
  95. if (i < count) {
  96. let data = datas[i];
  97. element.getChildByName("rank").getComponent(cc.Label).string = data.rank;
  98. element.getChildByName("name").getComponent(cc.Label).string = data.user.nickname;
  99. element.getChildByName("score").getComponent(cc.Label).string = data.score;
  100. }
  101. }
  102. }
  103. public Hide(){
  104. this.node.active = false;
  105. }
  106. }