lrf 2 years ago
parent
commit
0cda678844
1 changed files with 48 additions and 40 deletions
  1. 48 40
      app/service/eliminate.js

+ 48 - 40
app/service/eliminate.js

@@ -44,7 +44,7 @@ class EliminateService extends CrudService {
     const player_type = _.get(head, 'player_type');
     if (player_type === 'Race.TeamApply') {
       const list = await this.ctx.service.teamApply.query({ _id: players });
-      players = list.map((i) => {
+      players = list.map(i => {
         const { one_member_name, two_member_name, _id } = i;
         const obj = { _id, name: `${one_member_name}-${two_member_name}` };
         return obj;
@@ -103,12 +103,12 @@ class EliminateService extends CrudService {
       // 删除第一个关系
       dup.shift();
       // 查看是否有与第一个关系指向一个目标的地方
-      const r = dup.find((f) => f.target === head.target);
+      const r = dup.find(f => f.target === head.target);
       if (r) {
         // 有,则将这俩关系组为一个比赛,然后在dup中删除
         const sch = { player_one_node: head.source, player_two_node: r.source };
         schList.push(sch);
-        const ri = dup.find((f) => f.target === head.target);
+        const ri = dup.find(f => f.target === head.target);
         dup.splice(ri, 1);
       }
       dupAll = dup;
@@ -156,7 +156,7 @@ class EliminateService extends CrudService {
 
   nextProcess(startLevel, nodes, edges) {
     // 1.正常进行安排:level向前进
-    const lastLevel = nodes.filter((f) => f.level === startLevel - 1);
+    const lastLevel = nodes.filter(f => f.level === startLevel - 1);
     const group = _.chunk(lastLevel, 2);
     const thisLevelNodes = [];
     const thisLevelEdges = [];
@@ -228,7 +228,7 @@ class EliminateService extends CrudService {
   async groupRanking(list) {
     list = JSON.parse(JSON.stringify(list));
     // 先看每场比赛有没有胜者,有胜者就过,没有就设置下
-    list = list.map((i) => {
+    list = list.map(i => {
       const { winner, player_one, player_one_score, player_two, player_two_score } = i;
       if (winner) return i;
       if (player_one_score > player_two_score) i.winner = player_one;
@@ -240,18 +240,18 @@ class EliminateService extends CrudService {
       const groups = list[team_id];
       // 过滤出选手
       let players = [];
-      const p1s = groups.map((i) => i.player_one);
-      const p2s = groups.map((i) => i.player_two);
+      const p1s = groups.map(i => i.player_one);
+      const p2s = groups.map(i => i.player_two);
       players.push(...p1s, ...p2s);
       players = _.uniq(players);
       // 转换成object[], player:选手; win: 胜场; score:净胜球
-      players = players.map((i) => ({ player: i, win: 0, score: 0 }));
+      players = players.map(i => ({ player: i, win: 0, score: 0 }));
       for (const g of groups) {
         const { winner, player_one, player_one_score, player_two, player_two_score } = g;
         const p1RoundScore = this.getScore(player_one_score, player_two_score);
         const p2RoundScore = this.getScore(player_two_score, player_one_score);
-        const p1 = players.find((f) => f.player === player_one);
-        const p2 = players.find((f) => f.player === player_two);
+        const p1 = players.find(f => f.player === player_one);
+        const p2 = players.find(f => f.player === player_two);
         // p1,p2谁没有就不行
         if (!p1 || !p2) continue;
         p1.score = p1.score + p1RoundScore;
@@ -259,7 +259,7 @@ class EliminateService extends CrudService {
         if (winner === player_one) p1.win += 1;
         else p2.win += 1;
       }
-      players = _.orderBy(players, ['win', 'score'], ['desc', 'desc']);
+      players = _.orderBy(players, [ 'win', 'score' ], [ 'desc', 'desc' ]);
       const team = await this.ctx.model.Race.MatchTeamGroup.findById(team_id);
       if (!team) continue;
       const { rise } = team;
@@ -306,33 +306,13 @@ class EliminateService extends CrudService {
     if (sort && _.isString(sort)) {
       sort = { [sort]: desc ? -1 : 1 };
     } else if (sort && _.isArray(sort)) {
-      sort = sort.map((f) => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
+      sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
     }
     // 过滤出ref字段
     const { populate } = this.getRefMods();
     let res = await this.model.findOne(filter, projection).populate(populate).exec();
     res = JSON.parse(JSON.stringify(res));
-    const { referee_id, match_id, group_id, project_id, address_id } = res;
-    if (match_id) {
-      res.match_id_name = _.get(res, 'match_id.name');
-      res.match_id = _.get(res, 'match_id._id');
-    }
-    if (group_id) {
-      res.group_id_name = _.get(res, 'group_id.name');
-      res.group_id = _.get(res, 'group_id._id');
-    }
-    if (project_id) {
-      res.project_id_name = _.get(res, 'project_id.name');
-      res.project_id = _.get(res, 'project_id._id');
-    }
-    if (address_id) {
-      res.address_id_name = _.get(res, 'address_id.name');
-      res.address_id = _.get(res, 'address_id._id');
-    }
-    if (referee_id) {
-      res.referee_id_name = _.get(res, 'referee_id.user_id.name');
-      res.referee_id = _.get(res, 'referee_id._id');
-    }
+    res = await this.getOtherName(res);
     res = await this.getPlayerName(res);
     res = await this.afterFetch(filter, res);
     return res;
@@ -365,6 +345,33 @@ class EliminateService extends CrudService {
     }
     return data;
   }
+  // 获取其他名称
+  async getOtherName(data) {
+    if (!data) return data;
+    data = JSON.parse(JSON.stringify(data));
+    const { referee_id, match_id, group_id, project_id, address_id } = data;
+    if (match_id) {
+      data.match_id_name = _.get(data, 'match_id.name');
+      data.match_id = _.get(data, 'match_id._id');
+    }
+    if (group_id) {
+      data.group_id_name = _.get(data, 'group_id.name');
+      data.group_id = _.get(data, 'group_id._id');
+    }
+    if (project_id) {
+      data.project_id_name = _.get(data, 'project_id.name');
+      data.project_id = _.get(data, 'project_id._id');
+    }
+    if (address_id) {
+      data.address_id_name = _.get(data, 'address_id.name');
+      data.address_id = _.get(data, 'address_id._id');
+    }
+    if (referee_id) {
+      data.referee_id_name = _.get(data, 'referee_id.user_id.name');
+      data.referee_id = _.get(data, 'referee_id._id');
+    }
+    return data;
+  }
   async afterQuery(filter, data) {
     if (data) data = JSON.parse(JSON.stringify(data));
     for (let d of data) {
@@ -377,7 +384,8 @@ class EliminateService extends CrudService {
   async afterUpdate(filter, update, data) {
     const { status } = data;
     if (status === '2') await this.toNextRace(data);
-    data = await this.getPlayerName(data);
+    const { _id } = data;
+    data = await this.fetch({ _id });
     return data;
   }
   // 自动推进至下一场
@@ -399,12 +407,12 @@ class EliminateService extends CrudService {
     // 通过整图关系,找到下一步
     const { edges, nodes } = chartData;
     // 通过边关系找到下一节点
-    const list = edges.filter((f) => f.source === player_one_node || f.source === player_two_node);
-    const getTarget = (nodeEdge) => _.get(nodeEdge, 'target');
+    const list = edges.filter(f => f.source === player_one_node || f.source === player_two_node);
+    const getTarget = nodeEdge => _.get(nodeEdge, 'target');
     if (getTarget(_.head(list)) !== getTarget(_.last(list))) throw new BusinessError(ErrorCode.DATA_INVALID, '两个选手的图位置错误');
     // 确定了胜者晋级的节点id
     const target = getTarget(_.head(list));
-    const node = nodes.find((f) => f.id === target);
+    const node = nodes.find(f => f.id === target);
     const { id } = node;
     const wQeury = {
       $or: [
@@ -418,11 +426,11 @@ class EliminateService extends CrudService {
     await wEData.save();
     // 再得出败者的节点id
     const { mark, level } = node;
-    let midArr = nodes.filter((f) => f.level === level);
+    let midArr = nodes.filter(f => f.level === level);
     // 先过滤掉胜者节点的mark类型
-    midArr = midArr.filter((f) => f.mark !== mark);
+    midArr = midArr.filter(f => f.mark !== mark);
     // 再依次找,哪个节点没有人,没有人就安排上
-    midArr = _.orderBy(midArr, ['pos'], ['asc']);
+    midArr = _.orderBy(midArr, [ 'pos' ], [ 'asc' ]);
     for (const n of midArr) {
       const { id } = n;
       const query = {