|
@@ -4,8 +4,76 @@ import { ReturnModelType } from '@typegoose/typegoose';
|
|
|
import { BaseService } from 'free-midway-component';
|
|
|
import { Course } from '../entity/course.entity';
|
|
|
type modelType = ReturnModelType<typeof Course>;
|
|
|
+import _ = require('lodash');
|
|
|
@Provide()
|
|
|
export class CourseService extends BaseService<modelType> {
|
|
|
@InjectEntityModel(Course)
|
|
|
model: modelType;
|
|
|
+
|
|
|
+ // 排名
|
|
|
+ async rankQuery(filter) {
|
|
|
+ const { match_id } = filter;
|
|
|
+ const projecList = ['team_id', 'team_name', 'score'];
|
|
|
+ const groups = ['red', 'blue'];
|
|
|
+ const projection = {};
|
|
|
+ for (const g of groups) {
|
|
|
+ for (const prop of projecList) {
|
|
|
+ projection[`${g}_${prop}`] = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 查出所有的比赛
|
|
|
+ const list: any = await this.model.find({ match_id }, projection);
|
|
|
+ // 统计出所有队伍(去重)
|
|
|
+ const rts = list.map(f => f.red_team_id);
|
|
|
+ const bts = list.map(f => f.blue_team_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 scoreList = _.orderBy(rList, 'score', ['desc']);
|
|
|
+ return { scoreList };
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 根据队伍id,算出在范围内队伍的分数
|
|
|
+ * @param {Array} list 比赛列表
|
|
|
+ * @param {String} team_id 队伍id
|
|
|
+ */
|
|
|
+ computedTeam(list, team_id) {
|
|
|
+ let score = 0; // 分数
|
|
|
+ for (const match of list) {
|
|
|
+ const { red_team_id, blue_team_id, red_score, blue_score } = match;
|
|
|
+ if (!red_score && !blue_score) continue;
|
|
|
+ let teamInMatch = 'blue';
|
|
|
+ if (red_team_id === team_id) teamInMatch = 'red';
|
|
|
+ else if (blue_team_id === team_id) teamInMatch = 'blue';
|
|
|
+ else {
|
|
|
+ // 该队伍不是红蓝任意一方,说明不是这的,下一场
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 加分数
|
|
|
+ score += parseInt(match[`${teamInMatch}_score`]) || 0;
|
|
|
+ }
|
|
|
+ return { score };
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 根据比赛列表获取队伍信息
|
|
|
+ * @param {Array} list 比赛列表
|
|
|
+ * @param {String} team_id 队伍id
|
|
|
+ */
|
|
|
+ getOtherInfoFromList(list, team_id) {
|
|
|
+ const match = list.find(
|
|
|
+ f => f.red_team_id === team_id || f.blue_team_id === team_id
|
|
|
+ );
|
|
|
+ let name = '';
|
|
|
+ if (match) {
|
|
|
+ const { red_team_id } = match;
|
|
|
+ if (red_team_id === team_id) name = match.red_team_name;
|
|
|
+ else name = match.blue_team_name;
|
|
|
+ }
|
|
|
+ return { name };
|
|
|
+ }
|
|
|
}
|