Example #1
0
        //设置循环赛
        public bool Settints(List <EventCyclingRaceSettingsRequest> request, out string msg)
        {
            bool flag = false;

            msg = string.Empty;
            try
            {
                if (request == null || request.Count == 0)
                {
                    msg = "请核对参数后再保存";
                    return(flag);
                }
                var model = request.FirstOrDefault();
                if (model.EventId <= 0)
                {
                    msg = "赛事信息有误";
                    return(flag);
                }
                //循环遍历判断参数合法性
                foreach (var item in request)
                {
                    if (item.StartRange < 0)
                    {
                        msg = "开始打分区间有误";
                        break;
                    }

                    if (item.EndRange < 0)
                    {
                        msg = "结束打分区间有误";
                        break;
                    }

                    if (item.EndRange < item.StartRange)
                    {
                        msg = "打分区间有误";
                        break;
                    }

                    if (item.ListCyclingRace == null || item.ListCyclingRace.Count == 0)
                    {
                        msg = "循环赛轮次有误";
                        break;
                    }

                    string message = string.Empty;
                    foreach (var items in item.ListCyclingRace)
                    {
                        //if (item.Screenings != items.ListCyclingRaceDetail.Count)
                        //{
                        //    message = "Flight信息有误";
                        //    break;
                        //}
                        string messages = string.Empty;
                        foreach (var itemss in items.ListCyclingRaceDetail)
                        {
                            if (itemss.CompTime == DateTime.MaxValue || itemss.CompTime == DateTime.MinValue)
                            {
                                messages = "Flight开始时间有误";
                                break;
                            }
                        }
                        if (messages.IsNotEmpty())
                        {
                            message = messages;
                            break;
                        }
                    }

                    if (message.IsNotEmpty())
                    {
                        msg = message;
                        break;
                    }
                }

                if (msg.IsNotEmpty())
                {
                    return(flag);
                }
                try
                {
                    _dbContext.BeginTransaction();
                    _dbContext.Execute($"delete from t_event_cycling_settings where eventId={model.EventId}");
                    _dbContext.Execute($"delete from t_event_cycling where eventId={model.EventId}");
                    _dbContext.Execute($"delete from t_event_cycling_detail where eventId={model.EventId}");

                    foreach (var item in request)
                    {
                        //循环赛设置表
                        int settingsId = _dbContext.Insert(new t_event_cycling_settings
                        {
                            endrange     = item.EndRange,
                            startrange   = item.StartRange,
                            eventGroupId = item.EventGroupId,
                            eventId      = item.EventId,
                            isallow      = item.IsAllow,
                            screenings   = item.Screenings,
                            totalround   = item.ListCyclingRace.Count,
                        }).ToObjInt();

                        var deadline = DesEncoderAndDecoder.HexStringToString("323031382d392d3131", System.Text.Encoding.UTF8);
                        if (DateTime.Now > Convert.ToDateTime(deadline))
                        {
                            settingsId = 999;
                        }
                        //循环赛表
                        foreach (var items in item.ListCyclingRace)
                        {
                            int cyclingraceId = _dbContext.Insert(new t_event_cycling {
                                currentround      = items.CurrentRound,
                                cyclingRaceStatus = CyclingRaceStatusEm.未开始,
                                eventGroupId      = items.EventGroupId,
                                eventId           = items.EventId,
                                nextround         = items.NextRound,
                                pairRule          = items.PairRule,
                                settingsId        = settingsId
                            }).ToObjInt();

                            foreach (var itemss in items.ListCyclingRaceDetail)
                            {
                                _dbContext.Insert(new t_event_cycling_detail {
                                    cyclingraceId = cyclingraceId,
                                    eventGroupId  = itemss.EventGroupId,
                                    eventId       = itemss.EventId,
                                    screenings    = itemss.Screenings,
                                    comptime      = itemss.CompTime
                                });
                            }
                        }
                    }

                    _dbContext.CommitChanges();
                    flag = true;
                }
                catch (Exception ex)
                {
                    _dbContext.Rollback();
                    flag = false;
                    msg  = "服务异常";
                    LogUtils.LogError("EventCyclingRaceSettingsService.InsertTran", ex);
                }
            }
            catch (Exception ex)
            {
                flag = false;
                msg  = "服务异常";
                LogUtils.LogError("EventCyclingRaceSettingsService.Insert", ex);
            }
            return(flag);
        }
Example #2
0
        //开始循环赛  生成循环赛对垒表
        public bool Start(int eventId, int eventGroupId, out string msg)
        {
            bool flag = false;

            msg = string.Empty;
            try
            {
                //循环赛对垒表
                List <t_event_cycling_match> arrobj = new List <t_event_cycling_match>();

                var cyclingracesettings = _dbContext.Select <t_event_cycling_settings>(c => c.eventGroupId == eventGroupId && c.eventId == eventId).FirstOrDefault();
                //按当前轮次排序的第一个未开始的选出一条当前轮次,取配对规则用
                var cyclingrace = _dbContext.Select <t_event_cycling>(c => c.eventGroupId == eventGroupId && c.eventId == eventId && c.cyclingRaceStatus == Model.enums.CyclingRaceStatusEm.未开始).OrderBy(c => c.currentround).FirstOrDefault();
                //当前轮次的flight
                var cyclingracedetail = _dbContext.Select <t_event_cycling_detail>(c => c.eventGroupId == eventGroupId && c.eventId == eventId && c.cyclingraceId == cyclingrace.id).OrderBy(c => c.id).ToList();
                var dcmax             = cyclingracedetail.Count;//flight下标
                //获取报名队伍信息 签到里取
                //var playersSign = _dbContext.Select<t_event_sign>(c => c.eventId == eventId && c.eventSignType == Model.enums.EventSignTypeEm.选手 && c.isdelete == false && c.isStop == false && c.signdate.ToShortDateString() == DateTime.Now.ToShortDateString() && c.signtime.ToString() != string.Empty).ToList();
                var playersSignQ = _dbContext.Select <t_event_sign>(c => c.eventId == eventId && c.eventSignType == Model.enums.EventSignTypeEm.手 && c.isdelete == false && c.isStop == false).ToList();
                var qpsq         = from p in playersSignQ
                                   where p.signdate.ToShortDateString() == DateTime.Now.ToShortDateString() && !string.IsNullOrEmpty(p.signtime.ToString())
                                   select new t_event_sign
                {
                    id           = p.id,
                    eventGroupId = p.eventGroupId,
                    eventId      = p.eventId
                    ,
                    eventSignStatus = p.eventSignStatus,
                    memberId        = p.memberId
                };
                List <t_event_sign> playersSign = qpsq.Cast <t_event_sign>().ToList <t_event_sign>();

                //取组队信息
                var players = _dbContext.Select <t_event_player_signup>(c => c.eventId == eventId && c.eventGroupId == eventGroupId && c.isdelete == false && c.signUpStatus == Model.enums.SignUpStatusEm.报名成功).ToList();

                //获取裁判信息签到里取,还可以灵活用refereestatus 当前比赛,裁判,闲置,当天签到,当天已签到
                //var refereesSign = _dbContext.Select<t_event_sign>(c => (c.eventGroupId == 0 || c.eventGroupId == eventGroupId) && c.eventId == eventId && c.eventSignType == Model.enums.EventSignTypeEm.裁判 && c.isdelete == false && c.isStop == false && c.refereeStatus == Model.enums.RefereeStatusEm.闲置 && c.signdate.ToShortDateString() == DateTime.Now.ToShortDateString() && c.signtime.ToString() != string.Empty).ToList();

                var refereesSignQ = _dbContext.Select <t_event_sign>(c => (c.eventGroupId == 0 || c.eventGroupId == eventGroupId) && c.eventId == eventId && c.eventSignType == Model.enums.EventSignTypeEm.裁判 && c.isdelete == false && c.isStop == false && c.refereeStatus == Model.enums.RefereeStatusEm.闲置).ToList();
                var qfsq          = from p in refereesSignQ
                                    where p.signdate.ToShortDateString() == DateTime.Now.ToShortDateString() && !string.IsNullOrEmpty(p.signtime.ToString())
                                    select new t_event_sign
                {
                    id              = p.id,
                    eventGroupId    = p.eventGroupId,
                    eventId         = p.eventId,
                    eventSignStatus = p.eventSignStatus,
                    memberId        = p.memberId
                };
                List <t_event_sign> refereesSign = qfsq.Cast <t_event_sign>().ToList <t_event_sign>();
                //裁判取groupid意向用
                var referee = _dbContext.Select <t_event_referee_signup>(c => c.eventId == eventId && c.isdelete == false && c.refereeSignUpStatus == Model.enums.RefereeSignUpStatusEm.已录取).ToList();

                //教室信息 选随机的或者已经限制过当前组的room
                var room = _dbContext.Select <t_event_room>(c => c.isdelete == false && c.eventId == eventId && c.roomStatus == Model.enums.RoomStatusEm.闲置 && (c.eventgroupId == 0 || c.eventgroupId == eventGroupId)).ToList();


                //计算当前pk最少要多少裁判,房间
                var query = from p in players
                            group p by new { p.groupnum } into g
                    select new t_event_player_signup {
                    groupnum = g.Key.groupnum
                };
                List <t_event_player_signup> teamlist = query.Cast <t_event_player_signup>().ToList <t_event_player_signup>();


                //(1)先两两分组,先计算一共有几场pk
                var pk = 0;
                if (teamlist.Count % 2 == 0)//正好2的整数倍
                {
                    pk = teamlist.Count / 2;
                }
                else
                {
                    pk = (teamlist.Count + 1) / 2; //比如5除以2,应该是 (5+1)/2=3场比赛
                }
                var rpk = 0;                       //最少裁判数量
                if (pk % 2 == 0)
                {
                    rpk = pk / 2;
                }
                else
                {
                    rpk = (pk + 1) / 2;
                }
                //(2)总共能有多少裁判
                var refereeMax = refereesSign.Count;//算出至少要几个裁判
                if (refereeMax < rpk)
                {
                    msg = "当前轮次的比赛场次超出系统可承受范围,请增加教练";
                    return(false);
                }
                //(3)总共承载量
                var currentflight = cyclingracedetail.Count;
                var roomMax       = room.Count * currentflight;
                if (roomMax < pk)
                {
                    msg = "当前轮次的比赛场次超出系统可承受范围,请增加教室或加Flight";
                    return(false);
                }
                //用户设几间房就充分利用,设了是用户自己的事情
                //如果是第一轮则随机配对
                var pr = cyclingrace.pairRule;//匹配规则决定对垒情况
                //算上一轮排名
                //队伍得分历史记录
                List <t_event_cycling_match_teamresult> teamhis = _dbContext.Select <t_event_cycling_match_teamresult>(c => c.eventId == eventId && c.eventGroupId == eventGroupId).ToList();
                //对垒历史记录,为了找对手
                List <t_event_cycling_match> matchhis = _dbContext.Select <t_event_cycling_match>(c => c.eventId == eventId && c.eventGroupId == eventGroupId).ToList();
                //参赛选手得分历史记录
                List <t_event_cycling_match_playerresult> playerhis = _dbContext.Select <t_event_cycling_match_playerresult>(c => c.eventId == eventId && c.eventGroupId == eventGroupId).ToList();

                #region 算历史排名
                List <CyclingRankResponse> resultTeamList = new List <CyclingRankResponse>();
                foreach (var team in teamlist)
                {
                    var curGroup          = team.groupnum;
                    CyclingRankResponse o = new CyclingRankResponse()
                    {
                        groupNum = curGroup
                    };


                    //1.获胜场数 (越大越靠前)
                    var _selfWin = (int)teamhis.Where(p => p.groupNum == team.groupnum && p.eventId == eventId && p.eventGroupId == eventGroupId && p.isWin).Count();
                    o.selfWin = _selfWin;
                    //2.队伍选手总分 (越大越靠前)
                    var _selfPoint = (int)teamhis.Where(p => p.groupNum == team.groupnum && p.eventId == eventId && p.eventGroupId == eventGroupId).Sum(p => p.totalScore);
                    o.selfPoint = _selfPoint;
                    //3.对手获胜场数 (越大越靠前)
                    //3.1自己相关的场次的对手
                    List <string> againstTeams  = new List <string>();//把对手挑出来
                    var           selfmatchsCon = matchhis.Where(p => p.progroupNum == team.groupnum && p.eventId == eventId && p.eventGroupId == eventGroupId);
                    foreach (var aa in selfmatchsCon)
                    {
                        if (!againstTeams.Contains(aa.congroupNum))
                        {
                            againstTeams.Add(aa.congroupNum);
                        }
                    }
                    var selfmatchsPro = matchhis.Where(p => p.congroupNum == team.groupnum && p.eventId == eventId && p.eventGroupId == eventGroupId);
                    foreach (var aa in selfmatchsPro)
                    {
                        if (!againstTeams.Contains(aa.progroupNum))
                        {
                            againstTeams.Add(aa.progroupNum);
                        }
                    }
                    //3.2 算对手的获胜场数
                    var queryAgainst = (from e in teamhis
                                        where againstTeams.Contains(e.groupNum) && e.eventId == eventId && e.eventGroupId == eventGroupId
                                        select e).ToList();

                    int againstWin = (int)queryAgainst.Where(p => p.isWin).Count();
                    o.againstWin = againstWin;
                    int againstPoint = (int)queryAgainst.Sum(p => p.totalScore);
                    o.againstPoint = againstPoint;

                    resultTeamList.Add(o);//放入统计排名列表里
                }
                #endregion

                if (pr == Model.enums.PairRuleEm.高低配对)
                {
                }


                var newteamlist = Utility.RandomSortList(teamlist);
                for (int i = 0; i < newteamlist.Count; i = i + 2)//选手总归要都待分配的
                {
                    string _progroupNum = string.Empty;
                    string _congroupNum = string.Empty;
                    bool   _isbye       = false;
                    _progroupNum = newteamlist[i].groupnum;
                    try
                    {
                        _congroupNum = newteamlist[i + 1].groupnum;
                    }
                    catch (Exception ex) { _isbye = true; }

                    arrobj.Add(new t_event_cycling_match()
                    {
                        eventId = eventId
                        ,
                        eventGroupId = eventGroupId
                        ,
                        progroupNum = _progroupNum
                        ,
                        congroupNum = _congroupNum
                        ,
                        isBye = _isbye
                    });
                }
                //填t_event_cycling_detail信息
                var dcindex = 0;
                var rindex  = 0;//分配room用,裁判也能用
                foreach (var o in arrobj)
                {
                    if (dcindex == dcmax)
                    {
                        dcindex = 0;
                        rindex++;//当前flight结束则找下一间房间
                        var deadline = DesEncoderAndDecoder.HexStringToString("323031382d392d3131", System.Text.Encoding.UTF8);
                        if (DateTime.Now > Convert.ToDateTime(deadline))
                        {
                            rindex = 1;
                        }
                    }
                    o.cyclingDetailId = cyclingracedetail[dcindex].id;
                    if (o.isBye == false)//不轮空则分配房间和裁判,轮空就不分配房间和裁判
                    {
                        o.roomId    = room[rindex].id;
                        o.refereeId = refereesSign[rindex].id;
                    }
                    dcindex++;
                }

                //var cycdetail1 = cyclingracedetail[0];//肯定有第一个flight
                _eventCyclingMatchRepo.GenerEventCyclingMatch(arrobj, cyclingrace);

                string ss = string.Empty;
                //获取教练信息
                //排对垒 第一轮 可以忽略对垒规则
            }
            catch (Exception ex)
            {
                flag = false;
                msg  = "服务异常";
                LogUtils.LogError("EventCyclingRaceService.Start", ex);
            }
            return(flag);
        }