index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. const app = getApp()
  2. Page({
  3. query: {
  4. "grouping_id": "62e601c72dc7343e137fff6f",
  5. "match_id": "62e388d4fd61d871861b80af",
  6. "project_id": "62e5fff82dc7343e137ffef5"
  7. },
  8. data: {
  9. view: 'graph',
  10. frameStyle: { useTop: true, name: '注册', leftArrow: false, useBar: false },
  11. canvasWidth: 375,
  12. canvasHeight: 600,
  13. pixelRatio: 1,
  14. forceMini: false,
  15. // 数据
  16. data: undefined,
  17. riseList: [],
  18. arrangeList: [],
  19. raceList: [],
  20. groundList: [],
  21. refereeList: [],
  22. arrangeData: {},
  23. // picker
  24. show: false,
  25. personList: [],
  26. selectNode: undefined
  27. },
  28. // 计算晋级图
  29. computedTreeData() {
  30. const getWinnerNumList = this.data.riseList; // 将每个小组取多少人收到1个纯数字的数组中
  31. const getWinnerNum = getWinnerNumList.reduce((p, n) => p + n, 0); // 小组赛选取人数: 应该是该比赛项目的 各组选取人数加和
  32. let level = 1;
  33. let loop = true;
  34. while (loop) {
  35. const needUserNum = 2 ** (level - 1);
  36. // 相等,说明已经计算出结果
  37. if (needUserNum === getWinnerNum) loop = false;
  38. // 小于,说明还可以继续往后计算
  39. else if (needUserNum < getWinnerNum) level++;
  40. else {
  41. // 大于,说明不符合淘汰赛安排,需要手动定位
  42. loop = false;
  43. level = NaN
  44. }
  45. }
  46. if (!level) {
  47. console.log('不继续,返回了,编不了数据')
  48. return;
  49. }
  50. const levelList = [];
  51. // 1:16;2:8;3:4;4:2
  52. for (let i = 0; i < level - 1; i++) {
  53. const t = level - i - 1;
  54. // 计算:该层的位置数量
  55. const number = 2 ** t;
  56. const arr = [];
  57. for (let j = 1; j <= number; j++) {
  58. const obj = { id: `${i}-${j}`, num: `${i}-${j}`, pos: [i, j] }
  59. arr.push(obj);
  60. }
  61. levelList.push(arr)
  62. }
  63. // 进行数据组合,从最少的入手,依次取回,然后将取出来的数据删除掉
  64. let getLevelIndex = levelList.length - 1;
  65. const data = this.resetData(levelList, getLevelIndex)
  66. let obj = { id: `${levelList.length}`, num: `${levelList.length}`, children: data }
  67. return obj;
  68. },
  69. resetData(dataList, index = 0) {
  70. const thisLevelList = dataList[index]
  71. // 取出2条数据
  72. const p1 = thisLevelList[0]
  73. const p2 = thisLevelList[1]
  74. // 删除取出的2条数据
  75. dataList[index].shift()
  76. dataList[index].shift()
  77. if (index - 1 < 0) return [p1, p2]
  78. const p1c = this.resetData(dataList, index - 1)
  79. const p2c = this.resetData(dataList, index - 1)
  80. p1.children = p1c
  81. p2.children = p2c;
  82. return [p1, p2]
  83. },
  84. // 点击节点事件
  85. nodeTap(e) {
  86. const node = e.detail;
  87. if (!node) return;
  88. this.setData({ selectNode: node })
  89. this.toOpen();
  90. },
  91. // 获取最基础的那一层节点数据
  92. getFirstLevelNodes(node) {
  93. if (!node.children) return node;
  94. const res = [];
  95. const nodes = node.children;
  96. for (const node of nodes) {
  97. if (node?.children) {
  98. let list = this.getFirstLevelNodes(node);
  99. res.push(list);
  100. } else {
  101. res.push(node)
  102. }
  103. }
  104. return res.flat();
  105. },
  106. // 选择人员的处理
  107. selectChange(changeNode, selectNode) {
  108. const obj = { player_name: changeNode.label, player_id: changeNode.value, node_id: selectNode.id, label: 'player_name', pos: selectNode.pos }
  109. const arrangeList = this.data.arrangeList;
  110. const i = arrangeList.findIndex(f => f.player_id === obj.player_id && f.pos && obj.pos && f.pos[0] === obj.pos[0])
  111. if (i >= 0) arrangeList.splice(i, 1)
  112. arrangeList.push(obj)
  113. this.setData({ arrangeList })
  114. // 并非使用数据重组的方式,将选择的数据放入创建晋级图的数据中心,而是使用一维数组,以节点id对应的方式进行数据更新,更为灵活
  115. // 接下来需要生成比赛信息的数据
  116. this.getRaceList();
  117. },
  118. // 设置比赛数据
  119. getRaceList() {
  120. const arrangeList = this.data.arrangeList;
  121. arrangeList.sort((a, b) => {
  122. if (!a.pos) return b - a
  123. else if (!b.pos) return a - b
  124. else {
  125. // 根据 晋级节点位置,小组内节点位置进行排序
  126. const a1 = a.pos[0]
  127. const b1 = b.pos[0]
  128. if (a1 - b1 !== 0) return a1 - b1
  129. else {
  130. const a2 = a.pos[1]
  131. const b2 = b.pos[1]
  132. return a2 - b2
  133. }
  134. }
  135. })
  136. const raceList = [];
  137. for (let i = 0; i < arrangeList.length; i += 2) {
  138. const d1 = arrangeList[i]
  139. const d2 = arrangeList[i + 1]
  140. // 前后缺项,没法比赛
  141. if (!(d1 && d2)) continue;
  142. const { pos: d1pos } = d1
  143. const { pos: d2pos } = d2;
  144. // 前后两项的层级不一样,不能形成比赛
  145. if (d1pos[0] !== d2pos[0]) continue
  146. // 前后两项层级一样,但是不是连续的数字,例如:1-1与1-3,不能进行比赛,必须是 前+1=后
  147. if (d1pos[1] + 1 !== d2pos[1]) continue
  148. const { player_id: player_one, player_name: player_name_one, node_id: node_id_one } = d1
  149. const { player_id: player_two, player_name: player_name_two, node_id: node_id_two } = d2
  150. raceList.push({ player_one, player_name_one, node_id_one, player_two, player_name_two, node_id_two })
  151. }
  152. // 因为是数组,所以需要将model特殊处理下,按照一定规则处理可以正常赋值
  153. for (let i = 0; i < raceList.length; i++) {
  154. const d = raceList[i];
  155. d.groundModelName = `ground_id-${i}`;
  156. d.refereeModelName = `referee_id-${i}`;
  157. d.timeModelName = `match_time-${i}`;
  158. }
  159. const oRaceList = this.data.raceList;
  160. for (const race of raceList) {
  161. const { node_id_one, node_id_two } = race;
  162. const r = oRaceList.find(f => f.node_id_one === node_id_one && f.node_id_two === node_id_two)
  163. if (r) {
  164. const ri = oRaceList.findIndex(f => f.node_id_one === node_id_one && f.node_id_two === node_id_two)
  165. const { groundModelName, refereeModelName, timeModelName } = race
  166. r = { ...r, groundModelName, refereeModelName, timeModelName }
  167. oRaceList.splice(ri, 1, r)
  168. } else {
  169. oRaceList.push(race)
  170. }
  171. }
  172. this.setData({ raceList: oRaceList })
  173. },
  174. async onLoad() {
  175. await this.getTeams()
  176. // 设置初始的晋级图
  177. const data = this.computedTreeData();
  178. this.setData({ data })
  179. // 请求 小组赛的 胜者列表
  180. await this.searchWinnerList();
  181. await this.search();
  182. await this.searchOthers()
  183. },
  184. // 查询已安排的数据
  185. async search() {
  186. let res = await app.$get(`/newCourt/api/eliminatArrange/getOne`, this.query)
  187. if (app.$checkRes(res)) {
  188. const { arrange } = res.data;
  189. this.setData({
  190. arrangeData: res.data,
  191. arrangeList: arrange
  192. })
  193. }
  194. res = await app.$get(`/newCourt/api/eliminateRace`, this.query)
  195. if (app.$checkRes(res)) {
  196. this.setData({
  197. raceList: res.data
  198. })
  199. }
  200. // 数据本地化处理下
  201. this.getRaceList();
  202. },
  203. // 获取该项目的小组,形成晋级图
  204. async getTeams() {
  205. const res = await app.$get(`/newCourt/api/raceTeam`, this.query)
  206. if (app.$checkRes(res)) {
  207. const { data } = res
  208. const riseList = data.map(i => i.rise)
  209. this.setData({ riseList })
  210. }
  211. },
  212. // 获取小组赛胜者名单
  213. async searchWinnerList() {
  214. const res = await app.$post(`/newCourt/api/race/getWinnerList`, this.query)
  215. if (app.$checkRes(res)) {
  216. const { data } = res;
  217. const arr = [];
  218. for (const i of data) {
  219. const { personList, name } = i;
  220. for (const person of personList) {
  221. // person.player_name += `(${name})`;
  222. const obj = { label: person.player_name, value: person.player_id }
  223. arr.push(obj)
  224. }
  225. }
  226. this.setData({
  227. personList: arr
  228. })
  229. }
  230. },
  231. /**自动排列最底层,挨个放 */
  232. toAutoInitData() {
  233. const personList = this.data.personList;
  234. const data = this.data.data
  235. const getBaseList = data => {
  236. if (!data.children) return data;
  237. const arr = [];
  238. for (const d of data.children) {
  239. const r = getBaseList(d)
  240. arr.push(r);
  241. }
  242. return arr.flat();
  243. }
  244. const firstList = getBaseList(data);
  245. for (let i = 0; i < firstList.length; i++) {
  246. const f = firstList[i];
  247. const p = personList[i];
  248. this.selectChange(p, f);
  249. }
  250. },
  251. /**查询选项的数据 */
  252. async searchOthers() {
  253. let res;
  254. res = await app.$get(`/newCourt/api/ground`)
  255. if (app.$checkRes(res)) {
  256. this.setData({ groundList: res.data })
  257. }
  258. res = await app.$get(`/newCourt/api/user`, { type: '1' })
  259. if (app.$checkRes(res)) {
  260. this.setData({ refereeList: res.data })
  261. }
  262. },
  263. turnView(e) {
  264. const view = e?.target?.dataset?.view || 'graph';
  265. this.setData({ view })
  266. },
  267. //picker
  268. onChange(event) {
  269. const obj = event?.detail?.value;
  270. this.selectChange(obj, this.data.selectNode);
  271. this.toClose();
  272. },
  273. toOpen() {
  274. this.setData({ show: true })
  275. },
  276. toClose() {
  277. this.setData({ show: false, selectNode: undefined })
  278. },
  279. toSelected(event) {
  280. const data = event?.detail;
  281. const { value, model } = data;
  282. if (!model) return;
  283. const arr = model.split('-')
  284. const modelName = arr[0]
  285. const index = arr[1]
  286. const raceList = this.data.raceList;
  287. raceList[index][modelName] = value;
  288. this.setData({ raceList })
  289. },
  290. async toSubmit() {
  291. // 生成的比赛信息
  292. let raceList = this.data.raceList;
  293. // 安排信息
  294. let arrangeList = this.data.arrangeList;
  295. // arrangeList:需要将match_id,grouping_id,project_id 也放入,确定是这个项目的淘汰赛
  296. let arrange = this.data.arrangeData;
  297. if (!arrange._id) {
  298. arrange = { arrange: arrangeList, ...this.query }
  299. }
  300. // raceList:淘汰赛的比赛赛程
  301. raceList = raceList.map(i => {
  302. const { groundModelName, refereeModelName, timeModelName, ...others } = i
  303. return { ...others, ...this.query }
  304. })
  305. // 创建安排
  306. let res
  307. if (arrange._id) {
  308. res = await app.$post(`/newCourt/api/eliminatArrange/${arrange._id}`, arrange)
  309. if (app.$checkRes(res)) {
  310. console.log('淘汰赛安排创建成功')
  311. }
  312. } else {
  313. res = await app.$post(`/newCourt/api/eliminatArrange`, arrange)
  314. if (app.$checkRes(res)) {
  315. console.log('淘汰赛安排创建成功')
  316. }
  317. }
  318. res = await app.$post('/newCourt/api/eliminateRace/saveAll', raceList)
  319. wx.showToast({
  320. title: '保存成功',
  321. })
  322. }
  323. });