private void FinalizeList(ListHeaderModel model)
        {
            var speedRes = model.ResultsSpeed;

            if (speedRes.Count() > 0)
            {
                FinalizeListSpeed(model, speedRes);
            }
            else
            {
                var leadRes = model.ResultsLead;
                if (leadRes.Count() > 0)
                {
                    FinalizeListLead(model, leadRes);
                }
                else
                {
                    var boulderRes = model.ResultsBoulder;
                    if (boulderRes.Count() > 0)
                    {
                        FinalizeListBoulder(model, boulderRes);
                    }
                }
            }
            var chainList = (model.IidParent == null) ? model : model.Parent;

            foreach (var list in chainList.NextRounds)
            {
                FinalizeList(list);
            }
        }
Example #2
0
        private ActionResult RenderSimpleResultList <T>(ListHeaderModel header, Func <ListHeaderModel, IEnumerable <T> > selector, String viewName)
            where T : ListLineModel
        {
            var model = selector(header).ToList();

            model.Sort(ResultSorter);
            return(PartialView(viewName, model));
        }
        private Dictionary <int, RoundResult> GetResultsByRound(ListHeaderModel model)
        {
            switch (model.ListType)
            {
            case ListTypeEnum.LeadFlash:
                return(GetFlashResult(model));

            case ListTypeEnum.LeadGroups:
            case ListTypeEnum.BoulderGroups:
                return(GetSeveralGroupsResult(model));

            default:
                return(GetSimpleResult(model));
            }
        }
        private static Dictionary <int, RoundResult> GetSimpleResult(ListHeaderModel model)
        {
            Dictionary <int, RoundResult> result = new Dictionary <int, RoundResult>();

            foreach (var res in model.Results)
            {
                var rr = new RoundResult
                {
                    ClimberId = res.Climber.SecretaryId.Value,
                    Pos       = res.Position ?? 0,
                    Pts       = res.Points ?? 0.0,
                    RouteNum  = res.PreQf ? 0 : ((res.Position == null) ? int.MaxValue : 1),
                    ResText   = res.ResText ?? String.Empty
                };
                result.Add(rr.ClimberId, rr);
            }
            return(result);
        }
 private void FinalizeListBoulder(ListHeaderModel model, IEnumerable <BoulderResultLine> results)
 {
     FinalizeList(model, results,
                  r => r.Routes.Count(br => br.Bonus != null) > 0,
                  (a, b) =>
     {
         var aRes = getResult(a);
         var bRes = getResult(b);
         if (aRes != bRes)
         {
             return(bRes.CompareTo(aRes));
         }
         return(a.Climber.Vk.CompareTo(b.Climber.Vk));
     },
                  getResult,
                  //r => (1000 - r.BonusAttempts.Value) + r.Bonus.Value * 1000 + (1000 - r.TopAttempts.Value) * 1000 * 1000 + 1000 * 1000 * 1000 * r.Top.Value,
                  r => r.ResultTypeValue == ResultType.Disqualified,
                  r => r.ResultTypeValue == ResultType.DidNotStarted,
                  false, false, null);
 }
        private Dictionary <int, RoundResult> GetSeveralGroupsResult(ListHeaderModel model)
        {
            Dictionary <int, RoundResult> result = new Dictionary <int, RoundResult>();
            int i = 0;

            foreach (var list in model.Children)
            {
                i++;
                foreach (var res in list.Results)
                {
                    result.Add(res.Climber.SecretaryId.Value,
                               new RoundResult
                    {
                        ClimberId = res.Climber.SecretaryId.Value,
                        Pos       = res.Position ?? int.MaxValue,
                        Pts       = res.Points ?? double.MaxValue,
                        RouteNum  = res.PreQf ? 0 : i
                    });
                }
            }
            return(result);
        }
 private void FinalizeListLead(ListHeaderModel model, IEnumerable <LeadResultLine> results)
 {
     FinalizeList(model, results, (r => r.Result != null && !r.ResText.Equals(String.Empty, StringComparison.Ordinal)),
                  (a, b) => b.Result.Value.CompareTo(a.Result.Value),
                  r => r.Result.Value,
                  r => r.ResText.ToLowerInvariant().Contains("дискв"),
                  r => r.ResText.ToLowerInvariant().Contains("н/я"),
                  model.IidParent != null && model.Parent.ListType == ListTypeEnum.LeadFlash,
                  false,
                  l =>
     {
         if (l.Time == null || l.Time.Value == int.MaxValue || l.Time.Value <= 0)
         {
             return(-1);
         }
         else
         {
             return(l.Time.Value);
         }
     }
                  );
 }
        private ListHeaderModel UpdateListHeader(CompetitionModel comp, ApiListHeader header)
        {
            ListHeaderModel saveList = comp.Lists.FirstOrDefault(l => l.LocalIid == header.Iid);

            if (saveList == null)
            {
                saveList = new ListHeaderModel {
                    LocalIid = header.Iid
                };
                comp.Lists.Add(saveList);
            }
            saveList.BestResultInQf   = header.BestQf;
            saveList.CompetitionRules = header.Rules;
            if (header.GroupId == null)
            {
                saveList.GroupId = null;
            }
            else
            {
                var agr = comp.AgeGroups.FirstOrDefault(g => g.AgeGroupId == header.GroupId.Value);
                if (agr == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                saveList.GroupId = agr.Iid;
            }
            saveList.IidParent       = header.ParentList;
            saveList.LastRefresh     = header.LastRefresh;
            saveList.ListType        = header.ListType;
            saveList.Live            = header.Live;
            saveList.PreviousRoundId = header.PreviousRound;
            saveList.Quota           = header.Quota;
            saveList.Round           = header.Round;
            saveList.RouteQuantity   = header.RouteQuantity;
            saveList.StartTime       = header.StartTime;
            saveList.Style           = header.Style;
            return(saveList);
        }
        public static Dictionary <int, RoundResult> GetFlashResult(ListHeaderModel model)
        {
            List <Dictionary <int, RoundResult> > routes = new List <Dictionary <int, RoundResult> >();

            foreach (var route in model.Children.OrderBy(r => r.Round).ToArray())
            {
                routes.Add(GetSimpleResult(route));
            }
            if (routes.Count < 1)
            {
                return(null);
            }
            int peopleCount = routes.Max(rd => rd.Count);

            if (peopleCount < 1)
            {
                return(null);
            }
            Dictionary <int, RoundResult> results = new Dictionary <int, RoundResult>();

            for (int i = 0; i < routes.Count; i++)
            {
                foreach (var climber in routes[i].Values)
                {
                    FlashRoundResult fRes;
                    if (results.ContainsKey(climber.ClimberId))
                    {
                        fRes = (FlashRoundResult)results[climber.ClimberId];
                    }
                    else
                    {
                        fRes = new FlashRoundResult {
                            ClimberId = climber.ClimberId
                        };
                        results.Add(fRes.ClimberId, fRes);
                    }
                    if (fRes.Routes.Count < i)
                    {
                        for (int k = fRes.Routes.Count; k < i; k++)
                        {
                            fRes.Routes.Add(new RoundResult {
                                ClimberId = fRes.ClimberId, Pos = peopleCount, Pts = peopleCount, RouteNum = 1, ResText = String.Empty
                            });
                        }
                    }
                    fRes.Routes.Add(new RoundResult
                    {
                        ClimberId = fRes.ClimberId,
                        Pos       = (climber.Pos <= 0) ? peopleCount : climber.Pos,
                        Pts       = (climber.Pts <= 0.0) ? (double)peopleCount : climber.Pts,
                        RouteNum  = (climber.RouteNum == 0) ? 0 : 1,
                        ResText   = climber.ResText
                    });
                }
            }
            foreach (var fRes in results.Values.OfType <FlashRoundResult>().Where(r => r.Routes.Count < routes.Count))
            {
                for (int i = fRes.Routes.Count; i < routes.Count; i++)
                {
                    fRes.Routes.Add(new RoundResult {
                        ClimberId = fRes.ClimberId, Pos = peopleCount, Pts = peopleCount, RouteNum = 1
                    });
                }
            }
            var resultList = results.Values.ToList();

            resultList.Sort((a, b) => a.Pts.CompareTo(b.Pts));
            double curPts = -1.0;
            int    curPos = 1;

            for (int i = 0; i < resultList.Count; i++)
            {
                var curClm = resultList[i];
                var rowPts = curClm.Pts;
                if (i == 0 || rowPts > curPts)
                {
                    curPos = (i + 1);
                    curPts = rowPts;
                }
                curClm.Pos = curPos;
            }
            return(results);
        }
        private void FinalizeList <T>(ListHeaderModel model, IEnumerable <T> results,
                                      Func <T, bool> selectorWithResult,
                                      Comparison <T> sortingComparison,
                                      Func <T, long> resultSelector,
                                      Func <T, bool> isDSQ,
                                      Func <T, bool> isDNS,
                                      bool isLeadQualy,
                                      bool forceQf,
                                      Func <T, int> leadTimeSelector
                                      )
            where T : ListLineModel
        {
            var dt = SortingClass.CreateStructure();

            var            posCol = dt.Columns.Add("pos0", typeof(int));
            DataColumn     rNumCol;
            long           curRes             = -1;
            int            curPos             = 1;
            int            i                  = 0;
            HashSet <long> climbersWithResult = new HashSet <long>();
            var            resultList         = results.Where(r => selectorWithResult(r)).ToList();

            resultList.Sort(sortingComparison);
            foreach (var res in resultList)
            {
                i++;
                long rowRes = resultSelector(res);
                if (curRes < 0 || rowRes != curRes)
                {
                    curRes = rowRes;
                    curPos = i;
                }
                int sp;
                if (isDSQ(res))
                {
                    sp = 1;
                }
                else if (isDNS(res))
                {
                    sp = 2;
                }
                else
                {
                    sp = 0;
                }
                dt.Rows.Add(res.Climber.Vk, 0, String.Empty, res.Climber.SecretaryId.Value, String.Empty,
                            0.0, String.Empty, sp, curPos);
                climbersWithResult.Add(res.Climber.SecretaryId.Value);
            }

            if (dt.Rows.Count > 0)
            {
                var currentList = model;
                i = 0;
                while (currentList.PreviousRoundId != null)
                {
                    currentList = currentList.PreviousRound;
                    rNumCol     = dt.Columns.Add(String.Format("rNum{0}", ++i), typeof(int));
                    posCol      = dt.Columns.Add(String.Format("pos{0}", i), typeof(int));
                    var roundRes = GetResultsByRound(currentList);
                    foreach (DataRow row in dt.Rows)
                    {
                        int iid = (int)row["iid"];
                        if (roundRes == null || !roundRes.ContainsKey(iid))
                        {
                            row[rNumCol] = 0;
                            row[posCol]  = 1;
                        }
                        else
                        {
                            var r = roundRes[iid];
                            row[rNumCol] = r.RouteNum;
                            row[posCol]  = r.Pos;
                        }
                    }
                }

                SortingClass.SortResults(dt, model.Quota, i == 0, isLeadQualy, model.CompetitionRules, forceQf);
                if (leadTimeSelector != null)
                {
                    var dtWithTime = SortingClass.CreateStructure();
                    posCol  = dtWithTime.Columns.Add("pos0");
                    rNumCol = dtWithTime.Columns.Add("rNum1");
                    var timeCol = dtWithTime.Columns.Add("pos1");
                    foreach (DataRow dr in dt.Rows)
                    {
                        var clmResult = resultList.FirstOrDefault(r => r.Climber.SecretaryId == (int)dr[3]);
                        int time      = clmResult == null ? -1 : leadTimeSelector(clmResult);
                        dtWithTime.Rows.Add(dr[0], 0, String.Empty, dr[3], String.Empty, 0.0, String.Empty, dr[7],
                                            dr[1], time < 0 ? 0 : 1, time < 0 ? 1 : time);
                    }
                    SortingClass.SortResults(dtWithTime, model.Quota, i == 0, isLeadQualy, model.CompetitionRules, forceQf);
                    dt = dtWithTime;
                }
                foreach (DataRow row in dt.Rows)
                {
                    var climberRes = model.Results.First(r => r.Climber.SecretaryId == (int)row["iid"]);
                    climberRes.Position   = (int)row["pos"];
                    climberRes.PosText    = (string)row["posText"];
                    climberRes.Qf         = (string)row["qf"];
                    climberRes.Points     = (double)row["pts"];
                    climberRes.PointsText = (string)row["ptsText"];
                }
            }
            foreach (var clmRes in model.Results.ToArray().Where(r => !climbersWithResult.Contains(r.Climber.SecretaryId.Value)))
            {
                clmRes.Position   = null;
                clmRes.PosText    = String.Empty;
                clmRes.Qf         = String.Empty;
                clmRes.Points     = null;
                clmRes.PointsText = String.Empty;
            }
        }
        private void FinalizeListSpeed(ListHeaderModel model, IEnumerable <SpeedResultLine> results)
        {
            //throw new NotImplementedException();
            Func <SpeedResultLine, long> resultSelector;
            Func <SpeedResultLine, bool> selector;

            switch (model.ListType)
            {
            case ListTypeEnum.SpeedQualy:
                resultSelector = (r => r.Result.Value);
                selector       = (r => r.Result != null && r.Result.Value > 0);
                break;

            case ListTypeEnum.SpeedQualy2:
                if (model.BestResultInQf)
                {
                    List <ListHeaderModel> qfChain = new List <ListHeaderModel>();
                    qfChain.Add(model);
                    ListHeaderModel curLst = model;
                    while (curLst.PreviousRoundId != null)
                    {
                        curLst = curLst.PreviousRound;
                        qfChain.Add(curLst);
                    }
                    resultSelector = (r =>
                    {
                        long?bestRes = null;
                        foreach (var list in qfChain)
                        {
                            var clmRes = list.ResultsSpeed.FirstOrDefault(qr => qr.Climber.SecretaryId == r.Climber.SecretaryId);
                            if (clmRes == null || clmRes.Result == null || clmRes.Result.Value <= 0)
                            {
                                continue;
                            }
                            if (bestRes == null || clmRes.Result.Value < bestRes.Value)
                            {
                                bestRes = clmRes.Result.Value;
                            }
                        }
                        return(bestRes ?? -1);
                    });
                    selector = (r => resultSelector(r) > 0);
                    break;
                }
                else
                {
                    goto case ListTypeEnum.SpeedQualy;
                }

            default:
                return;
            }
            FinalizeList(model, results, selector,
                         (a, b) => resultSelector(a).CompareTo(resultSelector(b)),
                         resultSelector,
                         r => r.ResText.ToLowerInvariant().Contains("дискв"),
                         r => r.ResText.ToLowerInvariant().Contains("н/я"),
                         false,
                         model.ListType == ListTypeEnum.SpeedQualy && model.BestResultInQf,
                         null);
        }
Example #12
0
        private ActionResult RenderResultListFlash(ListHeaderModel header)
        {
            var results   = ResultsController.GetFlashResult(header);
            var dataTable = SortingClass.CreateStructure();

            dataTable.Columns.Add("pos0", typeof(int));
            //dt.Rows.Add(res.Climber.Vk, 0, String.Empty, res.Climber.SecretaryId.Value, String.Empty,
            //                0.0, String.Empty, sp, curPos);
            //climbersWithResult.Add(res.Climber.SecretaryId.Value);
            foreach (var r in results.Values.OfType <ResultsController.FlashRoundResult>().Where(r => r.RouteNum == 1))
            {
                var climber = header.Competition.Climbers.FirstOrDefault(c => c.SecretaryId == r.ClimberId);
                if (climber == null)
                {
                    continue;
                }
                int  sp;
                bool allDSQ = true, allDNS = true;
                foreach (var route in r.Routes)
                {
                    if (allDNS && !route.ResText.ToLowerInvariant().Contains("н/я"))
                    {
                        allDNS = false;
                    }
                    if (allDSQ && !route.ResText.ToLowerInvariant().Contains("дискв"))
                    {
                        allDSQ = false;
                    }
                    if (!allDNS || allDSQ)
                    {
                        break;
                    }
                }
                if (allDNS && allDSQ)
                {
                    continue;
                }
                if (allDNS)
                {
                    sp = 2;
                }
                else if (allDSQ)
                {
                    sp = 1;
                }
                else
                {
                    sp = 0;
                }
                dataTable.Rows.Add(climber.Vk, 0, String.Empty, r.ClimberId, String.Empty, 0.0, String.Empty, sp, r.Pos);
            }
            if (dataTable.Rows.Count > 0)
            {
                SortingClass.SortResults(dataTable, header.Quota, true, false, header.CompetitionRules, false);
            }
            List <FlashResult> model = new List <FlashResult>();

            foreach (DataRow row in dataTable.Rows)
            {
                FlashResult flshRes = new FlashResult();
                flshRes.Climber    = header.Competition.Climbers.First(r => r.SecretaryId == (int)row[3]);
                flshRes.PointsText = (string)row["ptsText"];
                flshRes.PosText    = (string)row["posText"];
                flshRes.PreQf      = false;
                flshRes.Qf         = (string)row["qf"];
                flshRes.ResText    = results[flshRes.Climber.SecretaryId.Value].Pts.ToString("0.00");
                foreach (var list in header.Children.OrderBy(r => r.LocalIid))
                {
                    var listRes = list.ResultsLead.FirstOrDefault(r => r.ClimberId == flshRes.Climber.Iid);
                    if (listRes == null)
                    {
                        flshRes.Routes.Add(new LeadResultLine
                        {
                            ResText    = String.Empty,
                            PointsText = String.Empty,
                            PosText    = String.Empty
                        });
                    }
                    else
                    {
                        flshRes.Routes.Add(listRes);
                    }
                }
                model.Add(flshRes);
            }

            return(PartialView("RenderResultListFlash", model));
        }
Example #13
0
 private ActionResult RenderResultListGeneral(ListHeaderModel header)
 {
     return(new HttpNotFoundResult());
 }
Example #14
0
 private ActionResult RenderResultListSeveralRoutes(ListHeaderModel header)
 {
     return(new HttpNotFoundResult());
 }