private static long getResult(BoulderResultLine line)
        {
            if (line == null)
            {
                return(long.MaxValue);
            }
            switch (line.ResultTypeValue)
            {
            case ResultType.DidNotStarted:
                return(long.MaxValue - 1);

            case ResultType.Disqualified:
                return(long.MaxValue - 2);

            default:
                long ba     = 1000 - (line.BonusAttempts ?? 1000);
                long b      = line.Bonus ?? 0;
                long ta     = 1000 - (line.TopAttempts ?? 1000);
                long t      = line.Top ?? 0;
                long result = ba + b * 1000;
                result += ta * 1000 * 1000;
                result += t * 1000 * 1000 * 1000;
                return(result);
            }
        }
        private HttpResponseMessage LoadListPackage(APISignedRequest request, bool clearLists)
        {
            CompetitionModel comp;
            var  resultListElem = request.GetRequestParameter <ApiListLineCollection>(db, out comp);
            bool hasLock        = false;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                Monitor.Enter(listLocker, ref hasLock);

                Dictionary <int, ListHeaderModel> lists = new Dictionary <int, ListHeaderModel>();
                if (resultListElem.Data != null)
                {
                    foreach (var n in resultListElem.Data.Select(d => d.ListID).Distinct())
                    {
                        var cList = comp.Lists.FirstOrDefault(l => l.LocalIid == n);
                        if (cList != null)
                        {
                            lists.Add(n, cList);
                        }
                    }

                    if (clearLists)
                    {
                        foreach (var cList in lists.Values)
                        {
                            foreach (var result in cList.Results.ToArray())
                            {
                                db.Results.Remove(result);
                            }
                        }
                    }

                    foreach (var res in resultListElem.Data.OfType <ApiListLineLead>())
                    {
                        if (!lists.ContainsKey(res.ListID))
                        {
                            continue;
                        }
                        var climber = comp.Climbers.FirstOrDefault(c => c.SecretaryId == res.ClimberID);
                        if (climber == null)
                        {
                            continue;
                        }
                        var model = lists[res.ListID].ResultsLead.FirstOrDefault(r => r.ClimberId == climber.Iid);
                        if (model == null)
                        {
                            var otherStyle = lists[res.ListID].Results.FirstOrDefault(r => r.ClimberId == climber.Iid);
                            if (otherStyle != null)
                            {
                                db.Results.Remove(otherStyle);
                            }
                            model = new LeadResultLine {
                                Climber = climber
                            };
                            lists[res.ListID].Results.Add(model);
                        }
                        model.ResText     = String.IsNullOrWhiteSpace(res.ResText) ? String.Empty : res.ResText.Trim();
                        model.Result      = res.Result;
                        model.StartNumber = res.StartNumber;
                        model.Time        = res.Time ?? int.MaxValue;
                        if (model.Time < 1)
                        {
                            model.Time = int.MaxValue;
                        }
                        model.TimeText = String.IsNullOrWhiteSpace(res.TimeText) ? String.Empty : res.TimeText.Trim();
                        model.PreQf    = res.PreQf;
                    }

                    foreach (var res in resultListElem.Data.OfType <ApiListLineSpeed>())
                    {
                        if (!lists.ContainsKey(res.ListID))
                        {
                            continue;
                        }
                        var climber = comp.Climbers.FirstOrDefault(c => c.SecretaryId == res.ClimberID);
                        if (climber == null)
                        {
                            continue;
                        }
                        var model = lists[res.ListID].ResultsSpeed.FirstOrDefault(r => r.ClimberId == climber.Iid);
                        if (model == null)
                        {
                            var otherStyle = lists[res.ListID].Results.FirstOrDefault(r => r.ClimberId == climber.Iid);
                            if (otherStyle != null)
                            {
                                db.Results.Remove(otherStyle);
                            }
                            model = new SpeedResultLine {
                                Climber = climber
                            };
                            lists[res.ListID].Results.Add(model);
                        }
                        model.ResText     = String.IsNullOrWhiteSpace(res.ResText) ? String.Empty : res.ResText.Trim();
                        model.Result      = res.Result;
                        model.Route1Data  = res.Route1Data;
                        model.Route1Text  = String.IsNullOrWhiteSpace(res.Route1Text) ? String.Empty : res.Route1Text.Trim();
                        model.Route2Data  = res.Route2Data;
                        model.Route2Text  = String.IsNullOrWhiteSpace(res.Route2Text) ? String.Empty : res.Route2Text.Trim();
                        model.StartNumber = res.StartNumber;
                        model.Position    = res.Pos;
                        model.PosText     = res.PosText;
                        model.PreQf       = res.PreQf;
                        model.Qf          = res.Qf;
                    }

                    foreach (var res in resultListElem.Data.OfType <ApiListLineBoulder>())
                    {
                        if (!lists.ContainsKey(res.ListID))
                        {
                            continue;
                        }
                        var climber = comp.Climbers.FirstOrDefault(c => c.SecretaryId == res.ClimberID);
                        if (climber == null)
                        {
                            continue;
                        }
                        var model = lists[res.ListID].ResultsBoulder.FirstOrDefault(r => r.ClimberId == climber.Iid);
                        if (model == null)
                        {
                            var otherStyle = lists[res.ListID].Results.FirstOrDefault(r => r.ClimberId == climber.Iid);
                            if (otherStyle != null)
                            {
                                db.Results.Remove(otherStyle);
                            }
                            model = new BoulderResultLine {
                                Climber = climber, Routes = new List <BoulderResultRoute>()
                            };
                            lists[res.ListID].Results.Add(model);
                        }
                        model.PreQf       = res.PreQf;
                        model.StartNumber = res.StartNumber;
                        switch (res.ResultCode)
                        {
                        case ResultLabel.DNS:
                            model.ResText = "н/я";
                            model.Routes.Clear();
                            model.ResultTypeValue = ResultType.DidNotStarted;
                            break;

                        case ResultLabel.DSQ:
                            model.ResText = "дискв.";
                            model.Routes.Clear();
                            model.ResultTypeValue = ResultType.Disqualified;
                            break;

                        default:
                            model.ResText         = String.Empty;
                            model.ResultTypeValue = ResultType.HasResult;
                            var updatedRoutes = res.Routes.Where(r => r.Route > 0 && r.Route <= (lists[res.ListID].RouteQuantity ?? 0)).ToArray();
                            foreach (var routeData in updatedRoutes)
                            {
                                var modelRoute = model.Routes.FirstOrDefault(r => r.Route == routeData.Route);
                                if (modelRoute == null)
                                {
                                    modelRoute = new BoulderResultRoute {
                                        Route = routeData.Route
                                    };
                                    model.Routes.Add(modelRoute);
                                }
                                modelRoute.Top   = routeData.Top;
                                modelRoute.Bonus = routeData.Bonus;
                            }
                            var routesToDelete = model.Routes
                                                 .ToArray()
                                                 .Where(r => updatedRoutes.Count(ur => ur.Route == r.Route) < 1)
                                                 .ToArray();
                            foreach (var r in routesToDelete)
                            {
                                db.BoulderRoutes.Remove(r);
                            }
                            break;
                        }
                    }
                }

                foreach (var lst in lists.Values)
                {
                    FinalizeList(lst);
                }

                db.SaveChanges();
            }
            finally
            {
                if (hasLock)
                {
                    Monitor.Exit(listLocker);
                }
            }
            return(Request.CreateResponse(HttpStatusCode.OK));
        }