resultView.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import EventMgr from "../game/EventMgr";
  2. import { GameEvent } from "../game/GameEvent";
  3. import { GameState } from "../game/GameState";
  4. import Main from "../game/Main";
  5. import { SDKIpt } from "../game/SDKIpt";
  6. import { SDK as sdk, CMD} from '../game/sdk';
  7. import { GameMode } from "../game/GameMode";
  8. import Button from "../game/Button";
  9. const {ccclass, property} = cc._decorator;
  10. @ccclass
  11. export default class resultView extends cc.Component {
  12. @property(cc.Button)
  13. rePlay: cc.Button = null;
  14. @property(cc.Button)
  15. returnMain: cc.Button = null;
  16. @property(cc.Node)
  17. bestStar: cc.Node = null;
  18. @property(cc.Node)
  19. myScore: cc.Node = null;
  20. @property(cc.Node)
  21. list: cc.Node = null;
  22. @property(cc.Sprite)
  23. resultListEmpty: cc.Sprite = null;
  24. @property(cc.Prefab)
  25. finishRankBlock:cc.Prefab = null
  26. @property(cc.Label)
  27. private lbKill: cc.Label = null;
  28. @property(cc.Label)
  29. private lbRound: cc.Label = null;
  30. @property([cc.Node])
  31. private failNodes: cc.Node[] = [];
  32. @property([cc.Node])
  33. private winNodes: cc.Node[] = [];
  34. private get isActive(): boolean { return this.node.active; }
  35. private index:number = 0
  36. private prevRanking:number = 0
  37. private data:Array<any> = []
  38. private user:any = {}
  39. // LIFE-CYCLE CALLBACKS:
  40. // onLoad () {}
  41. onLoad () {
  42. // this.node.active = false;
  43. this.rePlay.node.on("click",this.onRePlay,this);
  44. this.returnMain.node.on("click",this.onReturnMain,this);
  45. EventMgr.Instance.add_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT);
  46. }
  47. ON_SDK_UI_IPT(SDK_UI_IPT: GameEvent, ipt: SDKIpt, ON_SDK_UI_IPT: any) {
  48. if (!this.isActive) { return; }
  49. switch (ipt) {
  50. case SDKIpt.left:
  51. this.index = this.index===0?1:0
  52. this._ButtonResultActive()
  53. break;
  54. case SDKIpt.right:
  55. this.index = this.index===1?0:1
  56. this._ButtonResultActive()
  57. break;
  58. case SDKIpt.rightFoot:
  59. if(this.index === 1)
  60. this.onRePlay();
  61. else
  62. this.onReturnMain();
  63. break;
  64. case SDKIpt.leftFoot:
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. onRePlay() {
  71. this.index = 1
  72. this._ButtonResultActive()
  73. Main.Ins.PlayGame();
  74. this.Hide();
  75. }
  76. onReturnMain() {
  77. this.index = 0
  78. this._ButtonResultActive()
  79. Main.Ins.ShowMainView();
  80. this.Hide();
  81. }
  82. public async Show() {
  83. this.index = 0
  84. this._ButtonResultActive()
  85. // EventMgr.Instance.dispatch_event(GameEvent.sdkActionTip,[new ActionTipData(true,"返回大厅"),new ActionTipData(false,"再来一局")])
  86. EventMgr.Instance.dispatch_event(GameEvent.sdkActionTip,[new ActionTipData(true,"上一项"),new ActionTipData(false,"下一项")])
  87. //杀死数目
  88. this.lbKill.string = Main.Ins.Mode == GameMode.normal? Main.Ins.Score.toString():Main.Ins.EndlessRecordScore.toString();
  89. //最高分展示星星
  90. this.bestStar.active = Main.Ins.IsNewBestKill;
  91. //无尽模式
  92. this.lbRound.node.active = Main.Ins.Mode == GameMode.endless;
  93. if (Main.Ins.Mode == GameMode.endless) {
  94. this.lbRound.string = "第" + Main.Ins.Round + "波";
  95. }
  96. for (let i = 0; i < this.failNodes.length; i++) {
  97. const element = this.failNodes[i];
  98. element.active = Main.Ins.State == GameState.fail;
  99. }
  100. for (let i = 0; i < this.winNodes.length; i++) {
  101. const element = this.winNodes[i];
  102. element.active = Main.Ins.State == GameState.win;
  103. }
  104. let user = await this.getUserInfo()
  105. let data = await this.getRankData(GameMode.normal?1:0)
  106. //上一次的排名
  107. let prevRanking = (<any>data).find(item=>item.user.id===(<any>user).id)
  108. this.prevRanking = prevRanking?prevRanking.rank:0
  109. let currentScore = Main.Ins.Mode == GameMode.normal? Main.Ins.Score:Main.Ins.EndlessRecordScore
  110. sdk.onGameEnd(0,currentScore,0,Main.Ins.GetSDKGameMode(Main.Ins.Mode),0,(data)=>{
  111. this.updateInfo(data)
  112. this.init(0)
  113. this.node.active = true;
  114. });
  115. }
  116. public Hide(){
  117. this.node.active = false;
  118. }
  119. //创建内容项
  120. async createItem(){
  121. this.list.removeAllChildren()
  122. let currentRanking = 0
  123. let idx = this.data.findIndex(item=>item.user.id===this.user.id)
  124. if(this.prevRanking===0){//上次没排名
  125. if(idx>=0){//这次有排名
  126. currentRanking = this.data[idx].rank
  127. this.data = this.data.slice(idx===0?0:idx-1,idx===0?3:idx+2)
  128. }else{//这次没排名
  129. currentRanking = 0
  130. let arr = [{user:{id:this.user.id,nickname:this.user.nickname,gender:this.user.gender,avatar:this.user.avatar},score:Main.Ins.Mode == GameMode.normal? Main.Ins.Score:Main.Ins.EndlessRecordScore,rank:'未上榜'}]
  131. this.data = this.data.slice(-2).concat(arr)
  132. }
  133. }else{//上次有排名
  134. currentRanking = idx>=0?this.data[idx].rank:0
  135. this.data = this.data.slice(idx===0?0:idx-1,idx===0?3:idx+2)
  136. }
  137. for(let index=0; index<this.data.length;index++){
  138. let finishRankBlock = cc.instantiate(this.finishRankBlock)
  139. //上次有排名且比这次排名低 显示提升排名数
  140. if(this.prevRanking>0&&this.prevRanking>currentRanking&&this.user.id===this.data[index].user.id){
  141. finishRankBlock.getChildByName('item').getChildByName('goUpNum').getChildByName('val').getComponent(cc.Label).string = '+'+(this.prevRanking-currentRanking)
  142. finishRankBlock.getChildByName('item').getChildByName('goUpNum').active = true
  143. finishRankBlock.getChildByName('item').getChildByName('goUp').active = false
  144. }
  145. //上次没排名 这次有排名 显示提升icon
  146. if(this.prevRanking===0&&idx>=0&&this.user.id===this.data[index].user.id){
  147. finishRankBlock.getChildByName('item').getChildByName('goUpNum').active = false
  148. finishRankBlock.getChildByName('item').getChildByName('goUp').active = true
  149. }
  150. finishRankBlock.getChildByName('item').getChildByName('num').getComponent(cc.Label).string = this.data[index].rank +''
  151. finishRankBlock.getChildByName('item').getChildByName('name').getComponent(cc.Label).string = this.data[index].user.nickname
  152. finishRankBlock.getChildByName('item').getChildByName('score').getChildByName('value').getComponent(cc.Label).string = this.data[index].score
  153. try{
  154. var spriteFrame = await this.resourcesLoad(this.data[index].user.avatar)
  155. }catch(err){
  156. var spriteFrame:unknown = new cc.SpriteFrame()
  157. }
  158. finishRankBlock.getChildByName('item').getChildByName('avatar').getChildByName('sprite').getComponent(cc.Sprite).spriteFrame = <cc.SpriteFrame>spriteFrame
  159. finishRankBlock.getComponent(cc.Widget).top = 84 * index
  160. this.list.addChild(finishRankBlock)
  161. }
  162. }
  163. updateInfo(data:any){
  164. let currentScore = Main.Ins.Mode == GameMode.normal? Main.Ins.Score:Main.Ins.EndlessRecordScore
  165. let calorie = this.myScore.getChildByName('calorie')
  166. let step = this.myScore.getChildByName('step')
  167. let score = this.myScore.getChildByName('score')
  168. // let mark = this.myScore.getChildByName('mark')
  169. // mark.getComponent(cc.Label).string = GameMode.normal?'击杀一只狼 +1分':'每一分值 +1分'
  170. data.consume||(+data.consume)===0?calorie.getComponent(cc.Label).string = `本局消耗${data.consume}大卡,用时 ${this.timeDifference(Main.Ins.gameStartTime,Date.now())}`:calorie.active = false
  171. data.equivalent||(+data.equivalent)===0?step.getChildByName('num').getComponent(cc.Label).string = data.equivalent:step.active = false
  172. data.unit?step.getChildByName('unit').getComponent(cc.Label).string = data.unit:''
  173. score.getChildByName('value').getComponent(cc.Label).string =`${currentScore} 分`
  174. }
  175. resourcesLoad(url:string){
  176. return new Promise((resolve,reject)=>{
  177. cc.loader.loadRes(url, cc.SpriteFrame, (err, asset:cc.SpriteFrame) => {
  178. if(err) reject('未找到资源')
  179. resolve(asset);
  180. });
  181. })
  182. }
  183. _ButtonResultActive(){
  184. if(this.index == 0){
  185. this.returnMain.node.getComponent(Button).selected();
  186. this.rePlay.node.getComponent(Button).unselected()
  187. }else{
  188. this.returnMain.node.getComponent(Button).unselected();
  189. this.rePlay.node.getComponent(Button).selected()
  190. }
  191. }
  192. getUserInfo(){
  193. return new Promise((resolve)=>{
  194. // resolve({id:1,nickname:'张三',avatar:'12784.png'})
  195. sdk.getUserInfo((data) => {
  196. resolve(data)
  197. });
  198. })
  199. }
  200. //获取事件
  201. getRankData(type:number){
  202. return new Promise((resolve)=>{
  203. // resolve([
  204. // {rank:1,score:10000,user:{id:1,nickname:'张三',avatar:'12784.png'}},
  205. // {rank:2,score:11000,user:{id:2,nickname:'李四',avatar:'12784.png'}},
  206. // {rank:3,score:12000,user:{id:3,nickname:'王五',avatar:'12784.png'}},
  207. // ])
  208. sdk.getRank(type, function(data){
  209. resolve(Array.isArray(data)&&data.length>1&&data[1].list&&data[1].list.length>0?data[1].list:[])
  210. }.bind(this)) //0:世界榜 1:好友榜
  211. })
  212. }
  213. async init(type:number){
  214. let user = await this.getUserInfo()
  215. this.user = <any>user
  216. let data = await this.getRankData(type)
  217. this.data = <any>data
  218. // this.resultListEmpty.node.active = (<any>data).length===0
  219. this.createItem()
  220. }
  221. timeDifference(startTime:any, endTime:any) { //可以传日期时间或时间戳
  222. let start = typeof(startTime) == "number" ? startTime : new Date(startTime).getTime(),
  223. end = typeof(endTime) == "number" ? endTime : new Date(endTime).getTime(),
  224. difference = end - start, //时间差的毫秒数
  225. leave1 = difference % (24 * 3600 * 1000), //计算天数后剩余的毫秒数
  226. hours = ('0' + Math.floor(leave1 / (3600 * 1000))).slice(-2), //计算相差分钟数
  227. leave2 = leave1 % (3600 * 1000), //计算小时数后剩余的毫秒数
  228. minutes = ('0' + Math.floor(leave2 / (60 * 1000))).slice(-2), //计算相差秒数
  229. leave3 = leave2 % (60 * 1000), //计算分钟数后剩余的毫秒数
  230. seconds = ('0' + Math.round(leave3 / 1000)).slice(-2);
  231. return `${hours}:${minutes}:${seconds}`
  232. }
  233. protected onDestroy(): void {
  234. EventMgr.Instance.remove_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT);
  235. }
  236. }