GameRankingList.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. itemTemplate:cc.Node,
  5. scrollViewContent: cc.Node,
  6. prefabRankItem: cc.Prefab,
  7. prefabGameOverRank: cc.Prefab,
  8. gameOverRankLayout: cc.Node,
  9. current:0,
  10. users:[],
  11. spacing:0,
  12. loadingLabel:cc.Label,
  13. },
  14. onLoad(){
  15. // cc.view._convertPointWithScale=function(point) {
  16. // var viewport = this._viewPortRect;
  17. // point.x = (point.x - viewport.x) / (this._scaleX / 2);
  18. // point.y = (point.y - viewport.y) / (this._scaleY / 2);
  19. // };
  20. // cc.view._convertTouchesWithScale=function(touches) {
  21. // var viewport = this.viewPortRect, scaleX = this.scaleX / 2, scaleY = this._scaleY / 2, selTouch, selPoint, selPrePoint;
  22. // for (var i = 0; i < touches.length; i++) {
  23. // selTouch = touches[i];
  24. // selPoint = selTouch._point;
  25. // selPrePoint = selTouch._prevPoint;
  26. // selPoint.x = (selPoint.x - viewport.x) / scaleX;
  27. // selPoint.y = (selPoint.y - viewport.y) / scaleY;
  28. // selPrePoint.x = (selPrePoint.x - viewport.x) / scaleX;
  29. // selPrePoint.y = (selPrePoint.y - viewport.y) / scaleY;
  30. // }
  31. // };
  32. },
  33. start() {
  34. this.removeChild();
  35. if (window.wx != undefined) {
  36. // window.wx.postMessage({
  37. // messageType: 1,
  38. // MAIN_MENU_NUM: "score",
  39. // });
  40. window.wx.onMessage(data => {
  41. console.log("接收主域发来消息:", data)
  42. if (data.messageType == 0) {//移除排行榜
  43. this.removeChild();
  44. } else if (data.messageType == 1) {//获取好友排行榜
  45. this.fetchFriendData(data.MAIN_MENU_NUM);
  46. } else if (data.messageType == 3) {//提交得分
  47. this.submitScore(data.MAIN_MENU_NUM, data.score);
  48. } else if (data.messageType == 4) {//获取好友排行榜横向排列展示模式
  49. this.gameOverRank(data.MAIN_MENU_NUM);
  50. } else if (data.messageType == 5) {//获取群排行榜
  51. this.fetchGroupFriendData(data.MAIN_MENU_NUM, data.shareTicket);
  52. } else if (data.messageType == 6) {
  53. this.previousPage();
  54. } else if (data.messageType == 7) {
  55. this.nextPage();
  56. }
  57. });
  58. } else {
  59. this.fetchFriendData(1000);
  60. // this.gameOverRank(1000);
  61. }
  62. },
  63. submitScore(MAIN_MENU_NUM, score) { //提交得分
  64. if (window.wx != undefined) {
  65. window.wx.getUserCloudStorage({
  66. // 以key/value形式存储
  67. keyList: [MAIN_MENU_NUM],
  68. success: function (getres) {
  69. console.log('getUserCloudStorage', 'success', getres)
  70. if (getres.KVDataList.length != 0) {
  71. if (getres.KVDataList[0].value > score) {
  72. return;
  73. }
  74. }
  75. // 对用户托管数据进行写数据操作
  76. window.wx.setUserCloudStorage({
  77. KVDataList: [{key: MAIN_MENU_NUM, value: "" + score}],
  78. success: function (res) {
  79. console.log('setUserCloudStorage', 'success', res)
  80. },
  81. fail: function (res) {
  82. console.log('setUserCloudStorage', 'fail')
  83. },
  84. complete: function (res) {
  85. console.log('setUserCloudStorage', 'ok')
  86. }
  87. });
  88. },
  89. fail: function (res) {
  90. console.log('getUserCloudStorage', 'fail')
  91. },
  92. complete: function (res) {
  93. console.log('getUserCloudStorage', 'ok')
  94. }
  95. });
  96. } else {
  97. cc.log("提交得分:" + MAIN_MENU_NUM + " : " + score)
  98. }
  99. },
  100. removeChild() {
  101. this.node.removeChildByTag(1000);
  102. this.scrollViewContent.removeAllChildren();
  103. this.gameOverRankLayout.active = false;
  104. this.gameOverRankLayout.removeAllChildren();
  105. },
  106. fetchFriendData(MAIN_MENU_NUM) {
  107. this.removeChild();
  108. if (window.wx != undefined) {
  109. wx.getUserInfo({
  110. openIdList: ['selfOpenId'],
  111. success: (userRes) => {
  112. this.loadingLabel.active = false;
  113. console.log('success', userRes.data)
  114. let userData = userRes.data[0];
  115. //取出所有好友数据
  116. wx.getFriendCloudStorage({
  117. keyList: [MAIN_MENU_NUM],
  118. success: res => {
  119. console.log("wx.getFriendCloudStorage success", res);
  120. let data = res.data;
  121. data.sort((a, b) => {
  122. if (a.KVDataList.length == 0 && b.KVDataList.length == 0) {
  123. return 0;
  124. }
  125. if (a.KVDataList.length == 0) {
  126. return 1;
  127. }
  128. if (b.KVDataList.length == 0) {
  129. return -1;
  130. }
  131. return b.KVDataList[0].value - a.KVDataList[0].value;
  132. });
  133. this.users = data;
  134. this.notifyData();
  135. for (let i = 0; i < data.length; i++) {
  136. if (data[i].avatarUrl == userData.avatarUrl) {
  137. console.log('width: '+this.itemTemplate.width)
  138. this.itemTemplate.getComponent('RankItem').init(i, data[i]);
  139. }
  140. }
  141. },
  142. fail: res => {
  143. console.log("wx.getFriendCloudStorage fail", res);
  144. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  145. },
  146. });
  147. },
  148. fail: (res) => {
  149. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  150. }
  151. });
  152. }
  153. },
  154. notifyData(){
  155. let start = this.current;
  156. let end = this.users.length-start>5?start+5:this.users.length;
  157. let users = this.users.slice(start,end);
  158. let offset = this.scrollViewContent.height/2-20;
  159. console.log("offset: "+offset);
  160. this.scrollViewContent.removeAllChildren();
  161. for (let i = 0; i < users.length; ++i) {
  162. let item = cc.instantiate(this.itemTemplate);
  163. this.scrollViewContent.addChild(item);
  164. let y = offset-item.height * (0.5 + i) - this.spacing * (i + 1);
  165. item.setPosition(0, y);
  166. let user = this.users[this.current+i];
  167. item.getComponent('RankItem').init(start+i,user);
  168. }
  169. },
  170. previousPage(){
  171. if(this.current>0){
  172. let start = this.current - 5;
  173. this.current = start;
  174. this.notifyData();
  175. }else{
  176. // wx.showToast({
  177. // title:'已经是第一页了~'
  178. // })
  179. }
  180. },
  181. nextPage(){
  182. let start = this.current + 5;
  183. if(start >= this.users.length){
  184. if(this.hasMore){
  185. this.getTotalRank();
  186. }else{
  187. // wx.showToast({
  188. // title:'没有下一页了~'
  189. // })
  190. }
  191. }else{
  192. this.current = start;
  193. this.notifyData();
  194. }
  195. },
  196. fetchGroupFriendData(MAIN_MENU_NUM, shareTicket) {
  197. this.removeChild();
  198. if (window.wx != undefined) {
  199. wx.getUserInfo({
  200. openIdList: ['selfOpenId'],
  201. success: (userRes) => {
  202. console.log('success', userRes.data)
  203. let userData = userRes.data[0];
  204. //取出所有好友数据
  205. wx.getGroupCloudStorage({
  206. shareTicket: shareTicket,
  207. keyList: [MAIN_MENU_NUM],
  208. success: res => {
  209. console.log("wx.getGroupCloudStorage success", res);
  210. this.loadingLabel.active = false;
  211. let data = res.data;
  212. data.sort((a, b) => {
  213. if (a.KVDataList.length == 0 && b.KVDataList.length == 0) {
  214. return 0;
  215. }
  216. if (a.KVDataList.length == 0) {
  217. return 1;
  218. }
  219. if (b.KVDataList.length == 0) {
  220. return -1;
  221. }
  222. return b.KVDataList[0].value - a.KVDataList[0].value;
  223. });
  224. for (let i = 0; i < data.length; i++) {
  225. var playerInfo = data[i];
  226. var item = cc.instantiate(this.prefabRankItem);
  227. item.getComponent('RankItem').init(i, playerInfo);
  228. item.getComponent('RankItem').isSelf = false;
  229. this.scrollViewContent.addChild(item);
  230. if (data[i].avatarUrl == userData.avatarUrl) {
  231. let userItem = cc.instantiate(this.prefabRankItem);
  232. userItem.getComponent('RankItem').isSelf = true;
  233. userItem.getComponent('RankItem').init(i, playerInfo);
  234. userItem.y = -354;
  235. this.node.addChild(userItem, 1, 1000);
  236. }
  237. }
  238. },
  239. fail: res => {
  240. console.log("wx.getFriendCloudStorage fail", res);
  241. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  242. },
  243. });
  244. },
  245. fail: (res) => {
  246. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  247. }
  248. });
  249. }
  250. },
  251. gameOverRank(MAIN_MENU_NUM) {
  252. this.removeChild();
  253. this.gameOverRankLayout.active = true;
  254. if (window.wx != undefined) {
  255. wx.getUserInfo({
  256. openIdList: ['selfOpenId'],
  257. success: (userRes) => {
  258. cc.log('success', userRes.data)
  259. let userData = userRes.data[0];
  260. //取出所有好友数据
  261. wx.getFriendCloudStorage({
  262. keyList: [MAIN_MENU_NUM],
  263. success: res => {
  264. cc.log("wx.getFriendCloudStorage success", res);
  265. this.loadingLabel.active = false;
  266. let data = res.data;
  267. data.sort((a, b) => {
  268. if (a.KVDataList.length == 0 && b.KVDataList.length == 0) {
  269. return 0;
  270. }
  271. if (a.KVDataList.length == 0) {
  272. return 1;
  273. }
  274. if (b.KVDataList.length == 0) {
  275. return -1;
  276. }
  277. return b.KVDataList[0].value - a.KVDataList[0].value;
  278. });
  279. for (let i = 0; i < data.length; i++) {
  280. if (data[i].avatarUrl == userData.avatarUrl) {
  281. if ((i - 1) >= 0) {
  282. if ((i + 1) >= data.length && (i - 2) >= 0) {
  283. let userItem = cc.instantiate(this.prefabGameOverRank);
  284. userItem.getComponent('GameOverRank').init(i - 2, data[i - 2]);
  285. this.gameOverRankLayout.addChild(userItem);
  286. }
  287. let userItem = cc.instantiate(this.prefabGameOverRank);
  288. userItem.getComponent('GameOverRank').init(i - 1, data[i - 1]);
  289. this.gameOverRankLayout.addChild(userItem);
  290. } else {
  291. if ((i + 2) >= data.length) {
  292. let node = new cc.Node();
  293. node.width = 200;
  294. this.gameOverRankLayout.addChild(node);
  295. }
  296. }
  297. let userItem = cc.instantiate(this.prefabGameOverRank);
  298. userItem.getComponent('GameOverRank').init(i, data[i], true);
  299. this.gameOverRankLayout.addChild(userItem);
  300. if ((i + 1) < data.length) {
  301. let userItem = cc.instantiate(this.prefabGameOverRank);
  302. userItem.getComponent('GameOverRank').init(i + 1, data[i + 1]);
  303. this.gameOverRankLayout.addChild(userItem);
  304. if ((i - 1) < 0 && (i + 2) < data.length) {
  305. let userItem = cc.instantiate(this.prefabGameOverRank);
  306. userItem.getComponent('GameOverRank').init(i + 2, data[i + 2]);
  307. this.gameOverRankLayout.addChild(userItem);
  308. }
  309. }
  310. }
  311. }
  312. },
  313. fail: res => {
  314. console.log("wx.getFriendCloudStorage fail", res);
  315. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  316. },
  317. });
  318. },
  319. fail: (res) => {
  320. this.loadingLabel.string = "数据加载失败,请检测网络,谢谢。";
  321. }
  322. });
  323. }
  324. },
  325. });