RoomList.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. cc.Class({
  2. extends: cc.BaseClass,
  3. properties: {
  4. //分页控制按钮
  5. pagePrevBtn: {
  6. default: null,
  7. type: cc.Button,
  8. displayName: '上一页按钮'
  9. },
  10. PageScrollView: {
  11. default: null,
  12. type: cc.ScrollView,
  13. displayName: '分页视图'
  14. },
  15. content: {
  16. default: null,
  17. type: cc.Node,
  18. displayName: '分页视图内容'
  19. },
  20. pageNextBtn: {
  21. default: null,
  22. type: cc.Button,
  23. displayName: '下一页按钮'
  24. },
  25. // 页码按钮容器
  26. pageButtonsContainer: {
  27. default: null,
  28. type: cc.Node,
  29. displayName: '页码按钮容器'
  30. },
  31. // 页码按钮预制体
  32. pageButtonPrefab: {
  33. default: null,
  34. type: cc.Prefab,
  35. displayName: '页码按钮预制体'
  36. },
  37. // 分页数据
  38. m_currentPage: 1,//当前页码
  39. m_pageSize: 20,//每页显示的房间数量
  40. m_totalPages: 1,//总页数
  41. m_pagedRoomList: [],//分页的房间列表
  42. // 完整的房间列表数据
  43. m_RoomList: [], // 完整的房间列表数据
  44. //创建空数组,用于接收服务器返回的大厅列表数据
  45. m_LobbyList: [],
  46. },
  47. cotor: function () {
  48. //创建空数组,用于接收服务器返回的大厅列表数据
  49. this.m_LobbyList = [],
  50. this.m_Hook = null;
  51. // 分页相关
  52. this.m_currentPage = 1;//当前页码
  53. this.m_pageSize = 20;//每页显示的房间数量
  54. this.m_totalPages = 0; // 总页数
  55. this.m_pagedRoomList = [];//分页的房间列表
  56. this.m_RoomList = [] // 完整的房间列表数据
  57. },
  58. start() {
  59. this.getLobbyList();
  60. },
  61. onLoad() {
  62. // 生成模拟数据用于测试分页功能
  63. // this.generateMockData();
  64. setInterval(() => {
  65. this.getLobbyList();
  66. }, 4000);
  67. this.InitView();
  68. this.updatePageData();
  69. },
  70. // 初始化列表控件
  71. initListCtrl: function () {
  72. if (!this.m_ListCtrl) {
  73. this.m_ListCtrl = this.$('@CustomListCtrl');
  74. if (this.m_ListCtrl) {
  75. this.m_ListCtrl.InitList(0, 'roomListItem', this);
  76. }
  77. }
  78. },
  79. // 初始化页码按钮
  80. initPageButtons: function () {
  81. if (!this.pageButtonsContainer || !this.pageButtonPrefab) {
  82. console.warn('页码按钮容器或预制体未设置');
  83. return;
  84. }
  85. // 清空现有页码按钮
  86. this.pageButtonsContainer.removeAllChildren();
  87. console.log('初始化页码按钮容器');
  88. },
  89. InitView: function () {
  90. this.initListCtrl();
  91. this.initPageButtons();
  92. },
  93. OnShowView: function () {
  94. this.InitView();
  95. ShowO2I(this.node);
  96. },
  97. // 生成模拟数据用于测试分页功能
  98. // generateMockData: function () {
  99. // // 生成50条模拟数据
  100. // for (let i = 1; i <= 280; i++) {
  101. // const roomData = {
  102. // dwRoomID: 10000 + i, // 房间ID
  103. // dwClubID: 0, // 俱乐部ID(部分房间有俱乐部)
  104. // byPartID: 0, // 分区ID
  105. // wKindID: 21201, // 游戏类型
  106. // dwRules: new Array(67158024, 30, 10, 0, 0), // 房间规则数组(5个元素)
  107. // dwServerRules: 589824, // 服务器规则
  108. // wProgress: 0, // 当前局数
  109. // dwCreateTime: Math.floor(Date.now() / 1000) - Math.floor(Math.random() * 3600), // 创建时间(秒级时间戳)
  110. // dwCreaterID: 101, // 创建者ID
  111. // byPlayerCnt: 1, // 玩家人数(1-4人)
  112. // dwUserID: new Array(97, 0, 0, 0) // 玩家ID数组(4个元素)
  113. // };
  114. // this.m_RoomList.push(roomData);
  115. // }
  116. // },
  117. //获取大厅整个列表
  118. getLobbyList: function () {
  119. //获取大厅整个列表
  120. let obj = new CMD_C_GetLobbyList();
  121. obj.dwUserID = g_GlobalUserInfo.GetGlobalUserData().dwUserID;
  122. obj.wType = 0;
  123. let LoginMission = new CGPLoginMission(this, MDM_GP_GET_SERVER, SUB_GP_GET_LOBBY_LIST, obj);
  124. //列表返回
  125. // onSocketGetLobbyList
  126. //更新用户状态 //用户状态
  127. //onSocketUserGameStatus
  128. },
  129. //接受服务器返回的大厅列表数据
  130. onSocketGetLobbyList: function (data, size) { //CMD_S_GetLobbyList
  131. //服务器返回的大厅列表数据不为空
  132. if (data.wListCnt > 0) {
  133. this.m_RoomList = data.ListInfo; //更新房间列表数据
  134. console.log("房间列表数据更新成功!", m_RoomList);
  135. // 立即更新分页数据
  136. this.m_currentPage = 1;
  137. this.updatePageData();
  138. this.updatePageControls();
  139. }
  140. //存储获取到的大厅列表数据
  141. this.m_LobbyList = data;
  142. },
  143. //初始化分页组件
  144. initPagination: function () {
  145. if (this.pagePrevBtn) {
  146. // 上一页按钮点击事件
  147. this.pagePrevBtn.node.on('click', this.onPrevPage, this);
  148. }
  149. if (this.pageNextBtn) {
  150. // 下一页按钮点击事件
  151. this.pageNextBtn.node.on('click', this.onNextPage, this);
  152. }
  153. },
  154. // 更新分页控制状态
  155. updatePageControls: function () {
  156. //当当前页为第一页时,禁用上一页按钮
  157. if (this.m_currentPage == 1) {
  158. this.pagePrevBtn.interactable = false;
  159. }
  160. if (this.m_currentPage > 1) {
  161. this.pagePrevBtn.interactable = true;
  162. //当当前页不是第一页时,隐藏Background,显示mark
  163. var Background = this.pagePrevBtn.node.children[0];
  164. Background.active = false;
  165. var mark = this.pagePrevBtn.node.children[1];
  166. mark.active = true;
  167. } else {
  168. // 当前页是第一页时,禁用上一页按钮
  169. this.pagePrevBtn.interactable = false;
  170. // 隐藏Background,显示Background
  171. var Background = this.pagePrevBtn.node.children[0];
  172. Background.active = true;
  173. var mark = this.pagePrevBtn.node.children[1];
  174. mark.active = false;
  175. }
  176. //当当前页为最后一页时,禁用下一页按钮
  177. if (this.m_currentPage == this.m_totalPages) {
  178. this.pageNextBtn.interactable = false;
  179. //当当前页不是最后一页时,隐藏mark,显示Background
  180. var Background = this.pageNextBtn.node.children[0];
  181. Background.active = true;
  182. var mark = this.pageNextBtn.node.children[1];
  183. mark.active = false;
  184. } else {
  185. // 当前页不是最后一页时,启用下一页按钮
  186. this.pageNextBtn.interactable = true;
  187. // 隐藏Background,显示mark
  188. var Background = this.pageNextBtn.node.children[0];
  189. Background.active = false;
  190. var mark = this.pageNextBtn.node.children[1];
  191. mark.active = true;
  192. }
  193. },
  194. // 上一页
  195. onPrevPage: function () {
  196. cc.gSoundRes.PlaySound('Button');
  197. this.m_totalPages = Math.ceil(this.m_RoomList.length / this.m_pageSize);
  198. console.log('🎯 上一页按钮被点击了!');
  199. console.log('当前页:', this.m_currentPage, '总页数:', this.m_totalPages);
  200. if (this.m_currentPage > 1) {
  201. this.m_currentPage--;
  202. this.updatePageData();
  203. this.updatePageControls();
  204. }
  205. },
  206. // 下一页
  207. onNextPage: function () {
  208. cc.gSoundRes.PlaySound('Button');
  209. this.m_totalPages = Math.ceil(this.m_RoomList.length / this.m_pageSize);
  210. console.log('🎯 下一页按钮被点击了!');
  211. console.log('当前页:', this.m_currentPage, '总页数:', this.m_totalPages);
  212. if (this.m_currentPage < this.m_totalPages) {
  213. this.m_currentPage++;
  214. this.updatePageData();
  215. this.updatePageControls();
  216. } else {
  217. console.log('已经是最后一页,无法继续下一页');
  218. }
  219. },
  220. // 更新分页数据
  221. updatePageData: function () {
  222. // if (!this.m_RoomList || this.m_RoomList.length === 0) {
  223. // this.m_pagedRoomList = [];
  224. // return;
  225. // }
  226. // 计算分页数据
  227. const startIndex = (this.m_currentPage - 1) * this.m_pageSize;
  228. const endIndex = Math.min(startIndex + this.m_pageSize, this.m_RoomList.length);
  229. console.log('分页数据范围:', startIndex, endIndex);
  230. this.m_pagedRoomList = this.m_RoomList.slice(startIndex, endIndex);
  231. // 更新总页数
  232. this.m_totalPages = Math.ceil(this.m_RoomList.length / this.m_pageSize);
  233. if (this.m_totalPages === 0) {
  234. this.m_totalPages = 1;
  235. }
  236. // 更新页码按钮显示
  237. this.updatePageButtons();
  238. // 渲染分页数据到UI
  239. this.renderPagedRoomList();
  240. },
  241. // 渲染分页后的房间列表
  242. renderPagedRoomList: function () {
  243. // 清空当前列表
  244. this.m_ListCtrl.RemoveListPre(0);
  245. // console.log('当前页的房间数据:', this.m_pagedRoomList);
  246. // 渲染当前页的房间数据
  247. for (let i = 0; i < this.m_pagedRoomList.length; i++) {
  248. this.m_ListCtrl.InsertListInfo(0, this.m_pagedRoomList[i]);
  249. }
  250. // 更新无房间提示
  251. // this.$('NoRoom').active = this.m_pagedRoomList.length === 0;
  252. },
  253. // 更新页码按钮显示
  254. updatePageButtons: function () {
  255. if (!this.pageButtonsContainer || !this.pageButtonPrefab) {
  256. return;
  257. }
  258. // 清空现有页码按钮
  259. this.pageButtonsContainer.removeAllChildren();
  260. if (this.m_totalPages <= 1) {
  261. this.createPageButton(1);
  262. return;
  263. }
  264. // 最多显示7个页码按钮(包括省略号)
  265. const maxVisiblePages = 6;
  266. let startPage = 1;
  267. let endPage = this.m_totalPages;
  268. // 计算显示的页码范围
  269. if (this.m_totalPages > maxVisiblePages) {
  270. if (this.m_currentPage <= 4) {
  271. // 当前页在前4页,显示1-5...最后一页
  272. startPage = 1;
  273. endPage = 5;
  274. } else if (this.m_currentPage >= this.m_totalPages - 3) {
  275. // 当前页在后4页,显示第一页...最后5页
  276. startPage = this.m_totalPages - 4;
  277. endPage = this.m_totalPages;
  278. } else {
  279. // 当前页在中间,显示当前页前后各2页
  280. startPage = this.m_currentPage - 2;
  281. endPage = this.m_currentPage + 2;
  282. }
  283. }
  284. // console.log('生成页码按钮:', startPage, '到', endPage, '总页数:', this.m_totalPages);
  285. // 添加第一页按钮(如果需要省略号)
  286. if (startPage > 1) {
  287. this.createPageButton(1);
  288. if (startPage > 2) {
  289. this.createEllipsisButton();
  290. }
  291. }
  292. // 添加页码按钮
  293. for (let i = startPage; i <= endPage; i++) {
  294. this.createPageButton(i);
  295. }
  296. // 添加最后一页按钮(如果需要省略号)
  297. if (endPage < this.m_totalPages) {
  298. if (endPage < this.m_totalPages - 1) {
  299. this.createEllipsisButton();
  300. }
  301. this.createPageButton(this.m_totalPages);
  302. }
  303. },
  304. // 创建页码按钮
  305. createPageButton: function (pageNum) {
  306. const pageButton = cc.instantiate(this.pageButtonPrefab);
  307. const buttonComponent = pageButton.getComponent(cc.Button);
  308. const pageLabel = pageButton.getChildByName('Label')
  309. const labelComponent = pageButton.getChildByName('Label').getComponent(cc.Label);
  310. // 设置按钮文本(当前页为#A80101,其他的页码都是#FFFFFF)
  311. labelComponent.string = pageNum.toString();
  312. labelComponent.fontSize = 30;
  313. if (pageNum === this.m_currentPage) {
  314. pageLabel.color = new cc.Color(168, 1, 1)
  315. }
  316. // 设置按钮点击事件
  317. buttonComponent.node.on('click', () => {
  318. this.gotoPage(pageNum);
  319. });
  320. // 设置当前页按钮样式
  321. if (pageNum === this.m_currentPage) {
  322. pageButton.getComponent(cc.Button).interactable = false;
  323. }
  324. this.pageButtonsContainer.addChild(pageButton);
  325. },
  326. // 创建省略号按钮
  327. createEllipsisButton: function () {
  328. const ellipsisNode = new cc.Node();
  329. const label = ellipsisNode.addComponent(cc.Label);
  330. label.string = '...';
  331. label.fontSize = 30;
  332. label.node.color = new cc.Color(0, 0, 0);
  333. this.pageButtonsContainer.addChild(ellipsisNode);
  334. },
  335. // // 跳转到指定页
  336. gotoPage: function (pageNum) {
  337. // console.log('🎯 页码按钮被点击了!目标页码:', pageNum, '总页数:', this.m_totalPages);
  338. if (pageNum >= 1 && pageNum <= this.m_totalPages) {
  339. this.m_currentPage = pageNum;
  340. this.updatePageData();
  341. this.updatePageControls();
  342. }
  343. },
  344. DeleteRoom: function (dwRoomID) {
  345. if (!this.node.active || !this.m_ListCtrl) return;
  346. this.m_ListCtrl.ForEachCtrl(0, function (item) {
  347. if (dwRoomID == item.m_dwRoomID) item.node.active = false;
  348. }.bind(this));
  349. },
  350. });