|
@@ -0,0 +1,106 @@
|
|
|
+'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 }], is_bye: false });
|
|
|
+ const res = this.computedTeam(matchList, team_id);
|
|
|
+ const info = this.getOtherInfoFromList(matchList, team_id);
|
|
|
+ resList.push({ ...res, ...info });
|
|
|
+ }
|
|
|
+ return _.orderBy(resList, [ 'win', 'score' ], [ 'desc', 'desc' ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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;
|
|
|
+ if (!red_branch && !blue_branch) continue;
|
|
|
+ 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;
|