Example #1
0
        public SpeedSystem(int listID, SqlConnection baseCon, string competitionTitle, bool trial)
            :
            base(listID, baseCon, competitionTitle, false)
        {
            InitializeComponent();
            SqlCommand cmd = new SqlCommand();

            cmd.Connection = cn;
            if (cn.State != ConnectionState.Open)
            {
                cn.Open();
            }


            preQfClimb        = SettingsForm.GetPreQfClimb(this.cn, listID);
            cmd.CommandText   = "SELECT round FROM lists(NOLOCK) WHERE iid = " + listID.ToString();
            IsQualyAndNeed1st = (cmd.ExecuteScalar() as string).Equals("������������ 2", StringComparison.InvariantCultureIgnoreCase) && UseBestQfFromTwo;

            gbWorkMode.Visible = gbWorkMode.Enabled = false;
            //if (this.speedAdvancedSystem)
            //{
            //    cbShowView.SelectedIndex = 0;
            //}
            //else
            //    cbShowView.Visible = cbShowView.Enabled = false;

            string select1st, select2nd, sOrderBy;

            if (IsQualyAndNeed1st)
            {
                select1st = " dbo.fn_get1st(l.climber_id, l.list_id) AS [����.1], ";
                select2nd = " dbo.fn_getBest(l.iid) AS [����.����], ";
                sOrderBy  = " dbo.fn_getBestRes(l.iid), ";
            }
            else
            {
                select2nd = select1st = String.Empty;
                sOrderBy  = " l.res, ";
            }

            rbInternational.Visible = rbNational.Visible = false;
            this.Text = StaticClass.GetListName(listID, cn);
            daResW.SelectCommand.CommandText =
                "   SELECT l.posText AS [�����], l.climber_id AS [�], " +
                "          p.surname + ' ' + p.name AS [����������], p.age AS [�.�.], " +
                "          p.qf AS [������], dbo.fn_getTeamName(p.iid, l.list_id) AS [�������], " + select1st +
                "          l.route1_text AS [������ 1], l.route2_text AS [������ 2]," +
                "          l.resText AS [�����], " + select2nd + "l.qf AS [��.], l.start AS [��.�] " +
                "     FROM speedResults l(NOLOCK) " +
                "     JOIN Participants p(NOLOCK) ON l.climber_id = p.iid " +
                "    WHERE l.list_id = " + listID.ToString();
            if (UseBestRoute)
            {
                daResW.SelectCommand.CommandText +=
                    "      AND (ISNULL(route1_text,'') <> ''" +
                    "      AND ISNULL(route2_text,'') <> ''" +
                    "       OR CHARINDEX('�/�',resText) > 0)";
            }
            else
            {
                daResW.SelectCommand.CommandText +=
                    "      AND ISNULL(resText,'') <> '' ";
            }
            daResW.SelectCommand.CommandText +=
                " ORDER BY " + sOrderBy + " l.pos, p.vk, l.route1, [�������], p.surname, p.name";

            daStart.SelectCommand.CommandText =
                "   SELECT l.start AS [��.�], l.climber_id AS [�], (p.surname+' '+p.name) AS [����������]," +
                "          p.age AS [�.�.], p.qf AS [������], dbo.fn_getTeamName(p.iid, l.list_id) AS [�������], " + select1st +
                "          l.route1_text AS [������ 1], l.route2_text AS [������ 2], " +
                "          CASE WHEN l.route1_text IS NULL THEN 1 " +
                "               WHEN l.route1_text = '' THEN 1 " +
                "               ELSE 0 END ps " +
                "     FROM speedResults l(NOLOCK) " +
                "     JOIN Participants p(NOLOCK) ON l.climber_id = p.iid " +
                "    WHERE l.list_id = " + listID.ToString();
            if (UseBestRoute)
            {
                daStart.SelectCommand.CommandText +=
                    "      AND (ISNULL(route1_text,'') = ''" +
                    "       OR ISNULL(route2_text,'') = '')" +
                    "      AND (CHARINDEX('�/�', resText) < 1 " +
                    "       OR restext IS NULL)";
            }
            else
            {
                daStart.SelectCommand.CommandText +=
                    "      AND ISNULL(l.resText,'') = '' ";
            }
            daStart.SelectCommand.CommandText +=
                (preQfClimb ? String.Empty : "      AND l.preQf = 0 ") +
                //"      AND l.route1_text IS NOT NULL "+
                " ORDER BY ps, l.start";
            SetNext();

            cmd.CommandText = "SELECT l.prev_round FROM lists l(NOLOCK) WHERE l.iid=" + this.listID.ToString();

            try
            {
                if (this.cn.State != ConnectionState.Open)
                {
                    cn.Open();
                }
                cmd.Connection = cn;
                baseIID        = Convert.ToInt32(cmd.ExecuteScalar());
            }
            catch { baseIID = 0; }
            try { SetEditMode(false); }
            catch { }
            if (StaticClass.currentAccount == StaticClass.AccountInfo.SECRETARY)
            {
                cmd.CommandText = "SELECT COUNT(*) FROM lists(NOLOCK) WHERE prev_round = " + listID.ToString();
                try { btnNextRound.Enabled = Convert.ToInt32(cmd.ExecuteScalar()) < 1; }
                catch { btnNextRound.Enabled = true; }
            }
            else
            {
                btnNextRound.Enabled = false;
            }
            cbNxtRound.Enabled = btnNextRound.Enabled;
        }
        private ApiListHeader GetListHeader(int iid)
        {
            using (var cmd = this.CreateCommand())
            {
                cmd.CommandText = "select G." + ServiceHelper.REMOTE_ID_COLUMN + " group_id, L.listType, L.style, L.round, isnull(L.quote,0) quote," +
                                  "       L.allowView, isnull(LP." + ServiceHelper.REMOTE_ID_COLUMN + ", '') prevRoundId, isnull(L.routeNumber,0) routeNumber," +
                                  "       isnull(PR." + ServiceHelper.REMOTE_ID_COLUMN + ", '') parentIid," +
                                  "       IsNull(L." + ServiceHelper.REMOTE_ID_COLUMN + ", '') " + ServiceHelper.REMOTE_ID_COLUMN +
                                  "  from Lists L(nolock)" +
                                  " left join Lists LP(nolock) on LP.iid = L.prev_round" +
                                  " left join Lists PR(nolock) on PR.iid = L.iid_parent" +
                                  "      join Groups G(nolock) on G.iid = L.group_id" +
                                  "     where L.iid = @iid";
                cmd.Parameters.Add("@iid", SqlDbType.Int).Value = iid;

                bool iRulesLead = SettingsForm.GetLeaveTrains(this.cn, this.currentTransaction);
                var  sr         = SettingsForm.GetSpeedRules(this.cn, this.currentTransaction);

                using (var rdr = cmd.ExecuteReader())
                {
                    if (!rdr.Read())
                    {
                        return(null);
                    }
                    int routeNumber;
                    var round = ClimbingRoundExtensions.GetByLocalizedValue(rdr["round"].ToString(), out routeNumber, DefaultUICulture);
                    if (routeNumber < 1)
                    {
                        routeNumber = Convert.ToInt32(rdr["routeNumber"]);
                    }

                    ListType listType;
                    try { listType = (ListType)Enum.Parse(typeof(ListType), rdr["listType"].ToString(), true); }
                    catch { listType = ListType.Unknown; }

                    Rules r;
                    var   style = Extensions.StringExtensions.GetEnumByStringValue <ClimbingStyles>(rdr["style"].ToString(), ClimbingStyles.Lead, DefaultUICulture);
                    if (style == ClimbingStyles.Speed)
                    {
                        r = (sr & SpeedRules.InternationalSchema) == SpeedRules.InternationalSchema ? Rules.International : Rules.Russian;
                        if ((sr & SpeedRules.BestResultFromTwoQfRounds) == SpeedRules.BestResultFromTwoQfRounds)
                        {
                            r = r | Rules.BestRouteInQf;
                        }
                    }
                    else
                    {
                        r = iRulesLead ? Rules.International : Rules.Russian;
                    }
                    return(new ApiListHeader
                    {
                        AgeGroupInCompId = rdr["group_id"].ToString(),
                        ClimbingRules = r,
                        IidParent = rdr["parentIid"].ToString(),
                        ListTypeV = listType,
                        Live = Convert.ToInt32(rdr["allowView"]) > 0,
                        PreviousRoundId = rdr["prevRoundId"].ToString(),
                        Quota = Convert.ToInt32(rdr["quote"]),
                        Round = round,
                        RouteNumber = routeNumber,
                        Style = style,
                        Iid = rdr[ServiceHelper.REMOTE_ID_COLUMN].ToString(),
                        SecretaryId = iid
                    });
                }
            }
        }
        ListElem[] GetResults()
        {
            List <ListElem> styles = new List <ListElem>();

            foreach (DataRow drd in ((styleRes)generalResults[0]).dt.Rows)
            {
                ListElem le = new ListElem();
                le.data    = new string[4];
                le.data[0] = drd["�������, ���"].ToString();
                le.data[1] = drd["�.�."].ToString();
                le.data[2] = drd["������"].ToString();
                le.data[3] = drd["�������"].ToString();
                le.pos     = new double[generalResults.Count];
                try { le.pos[0] = (double)drd["posPt"]; }
                catch { continue; }
                for (int k = 1; k < le.pos.Length; k++)
                {
                    le.pos[k] = -1.0;
                }
                le.ps = 0;
                int iid = Convert.ToInt32(drd["�"]);
                le.iid = iid;
                for (int k = 1; k < generalResults.Count; k++)
                {
                    foreach (DataRow drr in ((styleRes)generalResults[k]).dt.Rows)
                    {
                        if ((int)drr["�"] == iid)
                        {
                            le.pos[k] = (double)drr["posPt"];
                            break;
                        }
                    }
                }
                bool b = true;
                for (int k = 0; k < le.pos.Length; k++)
                {
                    if (le.pos[k] < 0.0)
                    {
                        b = false;
                        break;
                    }
                }
                if (b)
                {
                    styles.Add(le);
                }
            }
            bool newRules = SettingsForm.GetCombinedNew(cn);

            if (newRules)
            {
                if (styles.Count > 0)
                {
                    for (int i = 0; i < generalResults.Count; i++)
                    {
                        styles.Sort((a, b) => a.pos[i].CompareTo(b.pos[i]));
                        double   cPts    = styles[0].pos[i];
                        double   curPos  = 1.0;
                        int      cpCount = 1;
                        double[] newPos  = new double[styles.Count];
                        newPos[0] = 1.0;
                        for (int k = 1; k < newPos.Length; k++)
                        {
                            if (styles[k].pos[i].Equals(cPts))
                            {
                                cpCount++;
                            }
                            else
                            {
                                if (cpCount > 1)
                                {
                                    double ptsToSet = (curPos + (double)k) / 2.0;
                                    for (int j = k - cpCount; j < k; j++)
                                    {
                                        newPos[j] = ptsToSet;
                                    }
                                }
                                cpCount = 1;
                                curPos  = (double)(k + 1);
                                cPts    = styles[k].pos[i];
                            }
                            newPos[k] = curPos;
                        }
                        if (cpCount > 1)
                        {
                            int    sc   = styles.Count;
                            double pSet = (curPos + (double)sc) / 2.0;
                            for (int j = sc - cpCount; j < sc; j++)
                            {
                                newPos[j] = pSet;
                            }
                        }

                        for (int k = 0; k < newPos.Length; k++)
                        {
                            styles[k].pos[i] = newPos[k];
                        }
                    }
                }
            }

            styles.Sort(new LEComparer(newRules));

            ListElem[] res = new ListElem[styles.Count];
            for (int i = 0; i < res.Length; i++)
            {
                res[i] = (ListElem)styles[i];
            }

            long curRes = (long)(res[0].minPos * 10.0 + res[0].sum * 10000.0);
            int  curPts = 1;

            res[0].ps = curPts;
            for (int i = 1; i < res.Length; i++)
            {
                long nxtRes = (long)(res[i].minPos * 10.0 + res[i].sum * 10000.0);
                if (curRes != nxtRes)
                {
                    curPts = i + 1;
                    curRes = nxtRes;
                }
                res[i].ps = curPts;
            }

            return(res);
        }