lrf 2 年 前
コミット
4f9142ce6c

+ 10 - 0
app/controller/.statistics.js

@@ -0,0 +1,10 @@
+module.exports = {
+  match: {
+    parameters: {
+      query: {
+        match_id: 'match_id',
+      },
+    },
+  },
+  ranking: {},
+};

+ 13 - 0
app/controller/statistics.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./.statistics.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+// 统计
+class StatisticsController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.statistics;
+  }
+}
+module.exports = CrudController(StatisticsController, meta);

+ 1 - 1
app/model/schedule.js

@@ -26,7 +26,7 @@ const schedule = {
 
   match_time: { type: String }, // 比赛时间
   match_file: { type: Array }, // 比赛图片
-  status: { type: String, default: "0" }, // 状态
+  status: { type: String, default: '0' }, // 状态
   position: { type: String }, // 比赛场次轮数
   format: { type: Array }, // 赛制
   is_bye: { type: Boolean, default: false }, // 是否轮空, 轮空只有1个队伍

+ 15 - 14
app/router.js

@@ -17,21 +17,22 @@ function getIPAdress() {
     }
   }
 }
-module.exports = (app) => {
+module.exports = app => {
   const { router, controller } = app;
   const { routePrefix, cluster } = app.config;
   const ipAddress = getIPAdress();
   console.log(`前缀:http://${ipAddress}:${cluster.listen.port}${routePrefix}`);
-  router.get("/", controller.home.index);
-  router.post("/courtAdmin/api/admin/login", controller.home.login);
-  router.get("/courtAdmin/api/useradmin", controller.admin.index);
-  require("./z_router/menu")(app); // 菜单
-  require("./z_router/user")(app); // 用户
-  require("./z_router/team")(app); // 团队
-  require("./z_router/joinapply")(app); // 加入团队
-  require("./z_router/dismissapply")(app); // 解散团队
-  require("./z_router/match")(app); // 比赛管理
-  require("./z_router/matchteam")(app); // 参赛团队
-  require("./z_router/schedule")(app); // 赛程信息
-  require("./z_router/notice")(app); // 通知通告
-};;
+  router.get('/', controller.home.index);
+  router.post('/courtAdmin/api/admin/login', controller.home.login);
+  router.get('/courtAdmin/api/useradmin', controller.admin.index);
+  require('./z_router/menu')(app); // 菜单
+  require('./z_router/user')(app); // 用户
+  require('./z_router/team')(app); // 团队
+  require('./z_router/joinapply')(app); // 加入团队
+  require('./z_router/dismissapply')(app); // 解散团队
+  require('./z_router/match')(app); // 比赛管理
+  require('./z_router/matchteam')(app); // 参赛团队
+  require('./z_router/schedule')(app); // 赛程信息
+  require('./z_router/notice')(app); // 通知通告
+  require('./z_router/statistics')(app); // 统计
+};

+ 105 - 0
app/service/statistics.js

@@ -0,0 +1,105 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+const { ObjectId } = require('mongoose').Types;
+
+// 统计
+class StatisticsService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'statistics');
+  }
+  async match({ match_id }) {
+    const teamList = await this.ctx.model.Matchteam.find({ match_id }, { logo: 1, team_name: 1, team_id: 1 });
+    const resList = [];
+    for (const team of teamList) {
+      const { team_id } = team;
+      const matchList = await this.ctx.model.Schedule.find({ match_id, $or: [{ red_id: team_id }, { blue_id: team_id }] });
+      const res = this.computedTeam(matchList, team_id);
+      const info = this.getOtherInfoFromList(matchList, team_id);
+      resList.push({ ...res, ...info });
+    }
+    return resList;
+  }
+
+  async ranking() {
+    const projecList = [ 'id', 'name', 'branch', 'integral', 'logo' ];
+    const groups = [ 'red', 'blue' ];
+    const projection = {};
+    for (const g of groups) {
+      for (const prop of projecList) {
+        projection[`${g}_${prop}`] = 1;
+      }
+    }
+    // 查出所有的比赛
+    const list = await this.ctx.model.Schedule.find({}, projection);
+    // console.log(list);
+    // 统计出所有队伍(去重)
+    const rts = list.map(f => f.red_id);
+    const bts = list.map(f => f.blue_id);
+    const teamList = _.uniq(_.compact([ ...rts, ...bts ]));
+    const rList = [];
+    for (const team_id of teamList) {
+      // 获得这支队伍在所有比赛中的胜负情况及积分
+      const res = this.computedTeam(list, team_id);
+      const info = this.getOtherInfoFromList(list, team_id);
+      rList.push({ ...info, ...res });
+    }
+    const winList = _.orderBy(rList, 'win', [ 'desc' ]);
+    const scoreList = _.orderBy(rList, 'score', [ 'desc' ]);
+    return { winList, scoreList };
+  }
+
+  /**
+   * 根据队伍id,算出在范围内队伍的 胜负次数 及 积分
+   * @param {Array} list 比赛列表
+   * @param {String} team_id 队伍id
+   */
+  computedTeam(list, team_id) {
+    let winTimes = 0; // 胜利次数
+    let loseTimes = 0; // 失败次数
+    let score = 0; // 积分
+    for (const match of list) {
+      const { red_id, blue_id, red_branch, blue_branch } = match;
+      let teamInMatch = 'blue';
+      if (red_id === team_id) teamInMatch = 'red';
+      else if (blue_id === team_id) teamInMatch = 'blue';
+      else {
+        // 该队伍不是红蓝任意一方,说明不是这的,下一场
+        continue;
+      }
+      // 加积分
+      score += parseInt(match[`${teamInMatch}_integral`]) || 0;
+      // 算胜负: winTeam => true : 红队胜;反之蓝队胜
+      const winTeam = parseInt(red_branch) > parseInt(blue_branch);
+      if (winTeam && teamInMatch === 'red') winTimes++;
+      else if (!winTeam && teamInMatch !== 'red') winTimes++;
+      else loseTimes++;
+    }
+    return { win: winTimes, lose: loseTimes, score };
+  }
+  /**
+   * 根据比赛列表获取队伍信息
+   * @param {Array} list 比赛列表
+   * @param {String} team_id 队伍id
+   */
+  getOtherInfoFromList(list, team_id) {
+    const match = list.find(f => f.red_id === team_id || f.blue_id === team_id);
+    let logo = {};
+    let name = '';
+    if (match) {
+      const { red_id } = match;
+      if (red_id === team_id) {
+        logo = match.red_logo;
+        name = match.red_name;
+      } else {
+        logo = match.blue_logo;
+        name = match.blue_name;
+      }
+    }
+    return { logo, name };
+  }
+}
+
+module.exports = StatisticsService;

+ 25 - 0
app/z_router/statistics.js

@@ -0,0 +1,25 @@
+'use strict';
+// 路由配置
+const rkey = 'statistics';
+const ckey = 'statistics';
+const keyZh = '统计';
+const routes = [
+  { method: 'get', path: `${rkey}/match`, controller: `${ckey}.match`, name: `${ckey}Match`, zh: `${keyZh}-比赛排名` },
+  { method: 'get', path: `${rkey}/ranking`, controller: `${ckey}.ranking`, name: `${ckey}Query`, zh: `${keyZh}-排行榜` },
+];
+
+module.exports = app => {
+  const { router, config } = app;
+  const mwares = app.middleware;
+  for (const route of routes) {
+    const { method, path, controller: ctl, zh } = route;
+    let { middleware = [] } = route;
+    if (!method || !path || !ctl) continue;
+    // 拼全路径
+    const allPath = `${config.routePrefix}/${path}`;
+    // 处理中间件
+    if (middleware.length > 0) middleware = middleware.map(i => mwares[i]({ enable: true }));
+    // 注册路由
+    router[method](zh, allPath, ...middleware, ctl);
+  }
+};