|
@@ -1,8 +1,10 @@
|
|
|
'use strict';
|
|
|
const { CrudService } = require('naf-framework-mongoose-free/lib/service');
|
|
|
+const { isNullOrUndefined, trimData } = require('naf-core').Util;
|
|
|
const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
const _ = require('lodash');
|
|
|
const assert = require('assert');
|
|
|
+const { ObjectId } = require('mongoose').Types;
|
|
|
|
|
|
// 赛程信息
|
|
|
class ScheduleService extends CrudService {
|
|
@@ -10,6 +12,74 @@ class ScheduleService extends CrudService {
|
|
|
super(ctx, 'schedule');
|
|
|
this.model = this.ctx.model.Schedule;
|
|
|
}
|
|
|
+
|
|
|
+ async checkNeedNext(id) {
|
|
|
+ const sch = await this.model.findById(id);
|
|
|
+ const { red_branch, blue_branch, match_position, match_id } = sch;
|
|
|
+ if (!red_branch || !blue_branch) return;
|
|
|
+ if (!match_position) console.error(`Schedule表: id: ${id} 数据缺少流程位置`);
|
|
|
+ // 有比分,有位置, 再看看同一轮其他比赛是不是有轮空的.有轮空的就不安排,没有轮空的就直接安排晋级比赛
|
|
|
+ // 将胜利的队伍的属性取出来,以便下面生成新数据
|
|
|
+ const winProp = [];
|
|
|
+ const propList = [ 'id', 'name', 'logo', 'members' ];
|
|
|
+ let winTeam = 'blue';
|
|
|
+ if (red_branch > blue_branch) winTeam = 'red';
|
|
|
+ for (const p of propList) {
|
|
|
+ winProp.push({ key: p, value: sch[`${winTeam}_${p}`] });
|
|
|
+ }
|
|
|
+ const mpl = match_position.split('-');
|
|
|
+ const head = _.head(mpl);
|
|
|
+ const mpReg = new RegExp(`${head}-`);
|
|
|
+ const condition = { match_id, match_position: mpReg, is_bye: true };
|
|
|
+ const has_bye = await this.model.count(condition);
|
|
|
+ // 说明有轮空,让管理员去创建下一轮的所有比赛赛程
|
|
|
+ if (has_bye > 1) return;
|
|
|
+ /**
|
|
|
+ * 没有轮空,直接创建比赛
|
|
|
+ * 流程位置计算公式:
|
|
|
+ * a:赛程轮数: _.head()
|
|
|
+ * n:该轮场次编号: _.last()
|
|
|
+ * a - n/n+1 => a+1 - (n+1) / 2 ;
|
|
|
+ * 条件: a>=1, n>=1 ,且n为奇数
|
|
|
+ */
|
|
|
+ const turn = parseInt(head) + 1;
|
|
|
+ const last = _.last(mpl);
|
|
|
+ let times;
|
|
|
+ if (last % 2 > 0) {
|
|
|
+ // 奇数,+1再/2 能算出下一轮的场次
|
|
|
+ times = (parseInt(last) + 1) / 2;
|
|
|
+ } else {
|
|
|
+ times = parseInt(last) / 2;
|
|
|
+ }
|
|
|
+ const next_match_position = `${turn}-${times}`;
|
|
|
+ // 先查询有没有 match_name: '晋级赛',
|
|
|
+ let nextMatchCreateData = {
|
|
|
+ match_id,
|
|
|
+ match_position: next_match_position,
|
|
|
+ };
|
|
|
+ let nextMatch = await this.model.findOne(nextMatchCreateData);
|
|
|
+ // 设置队伍数据的函数
|
|
|
+ const setTeamData = (team, source) => {
|
|
|
+ for (const obj of winProp) {
|
|
|
+ const { key, value } = obj;
|
|
|
+ source[`${team}_${key}`] = value;
|
|
|
+ }
|
|
|
+ return source;
|
|
|
+ };
|
|
|
+ if (nextMatch) {
|
|
|
+ // 已经有了下场比赛的数据,做修改,将新数据放蓝队里
|
|
|
+ // 修改,一定是将数据放入蓝队中,所以需要判断下蓝队和红队是不是一个队伍,看下id就行
|
|
|
+ const nmRed_id = _.get(nextMatch, 'red_id');
|
|
|
+ const winTeamId = _.get(winProp.find(f => f.key === 'id'), 'value');
|
|
|
+ if (nmRed_id === winTeamId) return;
|
|
|
+ nextMatch = setTeamData('blue', nextMatch);
|
|
|
+ await nextMatch.save();
|
|
|
+ } else {
|
|
|
+ // 没有,将数据放红队里
|
|
|
+ nextMatchCreateData = setTeamData('red', nextMatchCreateData);
|
|
|
+ await this.model.create(nextMatchCreateData);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
module.exports = ScheduleService;
|