Пример #1
0
        static TournamentUsers GetTournamentLosers(Cxt cxt, int tournamentID, int round)
        {
            DataTable       dtLosers = TournamentRounds.GetTournamentLosersByRound(tournamentID, round - 1);
            TournamentUsers tuLosers = new TournamentUsers(cxt, dtLosers);

            return(tuLosers);
        }
Пример #2
0
        public override void Save()
        {
            SqlTransaction trans = null;

            this.DtTournamentUsersMatch = TournamentUsers.GetTournamentUsersByTournamentID(StatusE.Active, this.Tournament.TournamentID);

            try
            {
                for (int i = 0; i < this.Count; i++)
                {
                    trans = SqlHelper.BeginTransaction(Config.ConnectionString);

                    TournamentMatch tournamentMatch = this[i];
                    this.Save(trans);
                    Challenge Challenge = CreateChallenge(tournamentMatch);
                    Challenge.Save(trans);

                    Game Game = null;

                    //if (this.teamLooseStatusE != TeamLooseStatusE.None)
                    //{
                    // Game = CreateGame(this, Challenge);
                    Game.Save(trans);
                    //}

                    if (this.Tournament.TournamentStartDate == new DateTime())
                    {
                        this.Tournament.TournamentStartDate = DateTime.Now;
                        this.Tournament.TournamentStartTime = DateTime.Now;
                        this.Tournament.Save(trans);
                    }
                    SqlHelper.CommitTransaction(trans);
                    trans = SqlHelper.BeginTransaction(Config.ConnectionString);
                    //if (this.teamLooseStatusE != TeamLooseStatusE.None)
                    //{
                    Elo Elo = new Elo(Game);
                    if (Game.IsEloWhiteUpated && Game.IsEloBlackUpated)
                    {
                        Elo.Update(trans);
                    }
                    //}

                    SqlHelper.CommitTransaction(trans);
                }
            }
            catch (Exception ex)
            {
                SqlHelper.RollbackTransaction(trans);
                Log.Write(base.Cxt, ex);
            }
        }
Пример #3
0
        static TournamentUsers GetPreliminaryRound(Cxt cxt, Tournament tournament, DataTable dtTournamentUsers)
        {
            double upperDiff = 0.0, lowerDiff = 0.0;

            TournamentUsers preliminaryRoundUsers = new TournamentUsers();
            bool            isTrue = false;

            //preliminaryRoundUsers = IsPreliminaryRound(dtTournamentUsers, ref isTrue);

            if (!isTrue)
            {
                for (int i = 1; i < 20; i++)
                {
                    double dbl = Math.Pow(2, i);
                    if (dbl >= dtTournamentUsers.Rows.Count)
                    {
                        upperDiff = dbl - Convert.ToDouble(dtTournamentUsers.Rows.Count);
                        lowerDiff = Convert.ToDouble(dtTournamentUsers.Rows.Count) - Math.Pow(2, i - 1);

                        DataView dv = new DataView(dtTournamentUsers);
                        dv.Sort = "EloBefore DESC";
                        DataTable dt = dv.ToTable();

                        /*
                         * if (lowerDiff == 1.0)
                         * {
                         *  dt.Rows[0].Delete();
                         *  preliminaryRoundUsers = new TournamentUsers(cxt, dt);
                         *  break;
                         * }
                         * else */
                        if (upperDiff == 0.0)
                        {
                            preliminaryRoundUsers = new TournamentUsers(cxt, dt);
                            break;
                        }

                        for (int j = 0; j < lowerDiff * 2; j++)
                        {
                            DataRow        dr = dt.Rows[j];
                            TournamentUser tu = new TournamentUser(cxt, dr);
                            preliminaryRoundUsers.Add(tu);
                        }
                        break;
                    }
                }
            }
            return(preliminaryRoundUsers);
        }
Пример #4
0
        static void CreateKnockoutTournamentMatches(Cxt cxt, TournamentUsers tournamentUsers, TournamentMatches tournamentMatches, int round, Tournament Tournament)
        {
            if (tournamentUsers.Count > 0)
            {
                for (int i = 0; i < tournamentUsers.Count / 2; i++)
                {
                    TournamentUser userW = new TournamentUser(cxt, tournamentUsers.DataTable.Rows[i]);
                    TournamentUser userB = new TournamentUser(cxt, tournamentUsers.DataTable.Rows[tournamentUsers.Count - i - 1]);

                    TournamentMatch tournamentMatch = new TournamentMatch(cxt, 0);
                    tournamentMatch.Round                = round;
                    tournamentMatch.WhiteUserID          = userW.UserID;
                    tournamentMatch.BlackUserID          = userB.UserID;
                    tournamentMatch.TournamentID         = Tournament.TournamentID;
                    tournamentMatch.TournamentMatchTypeE = TournamentMatchTypeE.Normal;

                    if (Tournament.TournamentStartDate > DateTime.Now)
                    {
                        tournamentMatch.MatchStartDate = Tournament.TournamentStartDate;
                    }
                    else
                    {
                        tournamentMatch.MatchStartDate = DateTime.Now;
                    }

                    if (Tournament.TournamentStartTime > DateTime.Now)
                    {
                        tournamentMatch.MatchStartTime = Tournament.TournamentStartTime;
                    }
                    else
                    {
                        tournamentMatch.MatchStartTime = DateTime.Now;
                    }

                    tournamentMatch.TimeMin = Tournament.TimeControlMin;
                    tournamentMatch.TimeSec = Tournament.TimeControlSec;

                    tournamentMatch.TournamentMatchStatusE = TournamentMatchStatusE.Scheduled;
                    tournamentMatch.EloWhiteBefore         = userW.EloBefore;
                    tournamentMatch.EloBlackBefore         = userB.EloBefore;
                    tournamentMatch.CreatedBy   = Ap.CurrentUserID;
                    tournamentMatch.DateCreated = DateTime.Now;
                    tournamentMatch.StatusID    = (int)StatusE.Active;
                    tournamentMatches.Add(tournamentMatch);
                }
            }
        }
Пример #5
0
        static TournamentUsers IsPreliminaryRound(Cxt cxt, DataTable dtTournamentUsers, ref bool isTrue)
        {
            TournamentUsers preliminaryRoundUsers = null;

            if (dtTournamentUsers.Rows.Count % 2 == 1)
            {
                DataView dv = new DataView(dtTournamentUsers);
                dv.Sort = "EloBefore DESC";
                DataTable dt = dv.ToTable();
                dt.Rows[0].Delete();
                preliminaryRoundUsers = new TournamentUsers(cxt, dt);
                isTrue = true;
            }


            return(preliminaryRoundUsers);
        }
Пример #6
0
        public static void CreateKnockoutTournamentMatches(Cxt cxt, Tournament tournament)
        {
            DataTable      dtTournamentUsers = TournamentUsers.GetTournamentUsersByTournamentID(StatusE.Active, tournament.TournamentID);
            SqlTransaction trans             = null;

            try
            {
                trans = SqlHelper.BeginTransaction(Config.ConnectionString);
                CreateKnockoutTournamentMatches(cxt, trans, tournament, dtTournamentUsers);
                SqlHelper.CommitTransaction(trans);
            }
            catch (Exception ex)
            {
                SqlHelper.RollbackTransaction(trans);
                Log.Write(cxt, ex);
            }
        }
Пример #7
0
        static TournamentUsers GetTournamentPreliminaryWinners(Cxt cxt, int tournamentID, int round, DataTable dtTournamentUsers)
        {
            TournamentUsers tuWinners = new TournamentUsers();
            TournamentUsers tuLosers  = GetTournamentLosers(cxt, tournamentID, round);

            if (tuLosers.Count > 0)
            {
                foreach (DataRow item in dtTournamentUsers.Rows)
                {
                    TournamentUser tu1 = new TournamentUser(cxt, item);

                    if (!tuLosers.Contains("UserID = " + tu1.UserID))
                    {
                        tuWinners.Add(tu1);
                    }
                }
            }
            return(tuWinners);
        }
Пример #8
0
        static int GetRoundInProgress(Cxt cxt, Tournament tournament)
        {
            DataTable dt = TournamentMatches.GetTournamentsMatchByTournamentID(tournament.TournamentID);
            DataTable dtTournamentUsers = TournamentUsers.GetTournamentUsersByTournamentID(StatusE.Active, tournament.TournamentID);

            foreach (DataRow item in dt.Rows)
            {
                TournamentMatch tm = new TournamentMatch(cxt, item);
                if (tm.TournamentMatchStatusE == TournamentMatchStatusE.InProgress || tm.TournamentMatchStatusE == TournamentMatchStatusE.Scheduled)
                {
                    return((int)MsgE.ErrorTournamentNextRoundStarted);
                }
            }

            object    objId    = dt.Compute("max(Round)", "");
            int       round    = Convert.ToInt32(objId);
            DataTable dtRounds = TournamentRounds.GetTournamentWinnersByRound(tournament.TournamentID, round);

            if (round == 0 && TournamentMatch.IsPreliminaryRound(round, dtTournamentUsers)) // if it is prelimiry round
            {
                if (dtRounds.Rows.Count == TournamentMatch.CountPreliminaryRoundUsers(dtTournamentUsers))
                {
                    return(0);
                }
                else
                {
                    return((int)MsgE.ErrorTournamentNextRoundStarted);
                }
            }

            DataTable dtUsers = TournamentUsers.GetTournamentUsersByRound(tournament.TournamentID, round);

            if (dtRounds.Rows.Count != dtUsers.Rows.Count / 2)
            {
                return((int)MsgE.ErrorTournamentNextRoundStarted);
            }

            return(0);
        }
Пример #9
0
        public static void SaveWantinUsers(Cxt cxt, DataTable items, DataTable dtTournamentWantin)
        {
            SqlTransaction sqltrans = null;

            try
            {
                sqltrans = SqlHelper.BeginTransaction(Config.ConnectionString);

                TournamentUsers.CreateRegisterUsers(cxt, sqltrans, items);

                if (dtTournamentWantin != null)
                {
                    foreach (DataRow dr in dtTournamentWantin.Rows)
                    {
                        TournamentWantinUser tournamentWantinUser = new TournamentWantinUser(cxt, dr);
                        //tournamentWantinUser.Save(sqltrans);
                        DateTime DateModified           = DateTime.Now;
                        int      ModifiedBy             = 1;
                        int      TournamentWantinUserID = tournamentWantinUser.TournamentWantinUserID;
                        int      TournamentUserStatusID = tournamentWantinUser.TournamentUserStatusID;

                        BaseCollection.ExecuteSql(sqltrans, InfiChess.TournamentWantinUser,
                                                  "update TournamentWantinUser set TournamentUserStatusID = @p1, ModifiedBy = @p2, DateModified = @p3 WHERE TournamentWantinUserID = @p4",
                                                  TournamentUserStatusID, ModifiedBy, DateModified, TournamentWantinUserID);
                    }
                }


                SqlHelper.CommitTransaction(sqltrans);
            }
            catch (Exception ex)
            {
                SqlHelper.RollbackTransaction(sqltrans);
                Log.Write(cxt, ex);
            }
        }
        private static void CreateSchavengenMatches(Cxt cxt, Tournament tournament, TournamentUsers TournamentUsers)
        {
            TournamentUser userW = null;
            TournamentUser userB = null;

            if (TournamentUsers.Count > 0)
            {
                DataTable dt = null;
                if ((TournamentUsers.Count / 2) % 2 == 1)
                {
                    dt = SchSystemsOdd(TournamentUsers.Count / 2);
                }
                else
                {
                    dt = SchSystems(TournamentUsers.Count / 2);
                }


                foreach (DataRow item in dt.Rows)
                {
                    userW = new TournamentUser(Cxt.Instance, TournamentUsers[Convert.ToInt32(item["Player1"]) - 1]);
                    userB = new TournamentUser(Cxt.Instance, TournamentUsers[Convert.ToInt32(item["Player2"]) - 1]);

                    TournamentMatch tournamentMatch = new TournamentMatch(cxt, 0);
                    tournamentMatch.Round                = Convert.ToInt32(item["Round"]);
                    tournamentMatch.WhiteUserID          = userW.UserID;
                    tournamentMatch.BlackUserID          = userB.UserID;
                    tournamentMatch.TournamentID         = tournament.TournamentID;
                    tournamentMatch.TournamentMatchTypeE = TournamentMatchTypeE.Normal;

                    if (tournament.TournamentStartDate > DateTime.Now)
                    {
                        tournamentMatch.MatchStartDate = tournament.TournamentStartDate;
                    }
                    else
                    {
                        tournamentMatch.MatchStartDate = DateTime.Now;
                    }

                    if (tournament.TournamentStartTime > DateTime.Now)
                    {
                        tournamentMatch.MatchStartTime = tournament.TournamentStartTime;
                    }
                    else
                    {
                        tournamentMatch.MatchStartTime = DateTime.Now;
                    }

                    tournamentMatch.TimeMin = tournament.TimeControlMin;
                    tournamentMatch.TimeSec = tournament.TimeControlSec;

                    tournamentMatch.TournamentMatchStatusE = TournamentMatchStatusE.Scheduled;
                    tournamentMatch.EloWhiteBefore         = userW.EloBefore;
                    tournamentMatch.EloBlackBefore         = userB.EloBefore;
                    tournamentMatch.CreatedBy   = cxt.CurrentUserID;
                    tournamentMatch.DateCreated = DateTime.Now;

                    tournament.TournamentMatches.Add(tournamentMatch);
                }
            }
        }
        private static void CreateSchavengenMatches(Cxt cxt, Tournament tournament, TournamentUsers TournamentUsers, bool isWhite, int counter,
                                                    int round, ref int val, ref int roundValue)
        {
            TournamentUser userW = null;
            TournamentUser userB = null;

            if (TournamentUsers.Count > 0)
            {
                DataTable dt = null;
                if ((TournamentUsers.Count / 2) % 2 == 1)
                {
                    dt = SchSystemsOdd(TournamentUsers.Count / 2);
                }
                else
                {
                    dt = SchSystems(TournamentUsers.Count / 2);
                }

                //DataRow[] dr = dt.Select("1=1", "Round desc");

                //int round = i + counter * Convert.ToInt32(dr[0]["Round"]);

                foreach (DataRow item in dt.Rows)
                {
                    if (val == round)
                    {
                        roundValue++;
                        val = 0;
                    }

                    if (isWhite)
                    {
                        userW = new TournamentUser(Cxt.Instance, TournamentUsers[Convert.ToInt32(item["Player1"]) - 1]);
                        userB = new TournamentUser(Cxt.Instance, TournamentUsers[Convert.ToInt32(item["Player2"]) - 1]);
                    }
                    else
                    {
                        userW = new TournamentUser(Cxt.Instance, TournamentUsers[Convert.ToInt32(item["Player2"]) - 1]);
                        userB = new TournamentUser(Cxt.Instance, TournamentUsers[Convert.ToInt32(item["Player1"]) - 1]);
                    }
                    TournamentMatch tournamentMatch = new TournamentMatch(cxt, 0);
                    tournamentMatch.Round                = roundValue;
                    tournamentMatch.WhiteUserID          = userW.UserID;
                    tournamentMatch.BlackUserID          = userB.UserID;
                    tournamentMatch.TournamentID         = tournament.TournamentID;
                    tournamentMatch.TournamentMatchTypeE = TournamentMatchTypeE.Normal;

                    if (tournament.TournamentStartDate > DateTime.Now)
                    {
                        tournamentMatch.MatchStartDate = tournament.TournamentStartDate;
                    }
                    else
                    {
                        tournamentMatch.MatchStartDate = DateTime.Now;
                    }

                    if (tournament.TournamentStartTime > DateTime.Now)
                    {
                        tournamentMatch.MatchStartTime = tournament.TournamentStartTime;
                    }
                    else
                    {
                        tournamentMatch.MatchStartTime = DateTime.Now;
                    }

                    tournamentMatch.TimeMin = tournament.TimeControlMin;
                    tournamentMatch.TimeSec = tournament.TimeControlSec;

                    tournamentMatch.TournamentMatchStatusE = TournamentMatchStatusE.Scheduled;
                    tournamentMatch.EloWhiteBefore         = userW.EloBefore;
                    tournamentMatch.EloBlackBefore         = userB.EloBefore;
                    tournamentMatch.CreatedBy   = cxt.CurrentUserID;
                    tournamentMatch.DateCreated = DateTime.Now;
                    tournamentMatch.StatusID    = (int)StatusE.Active;
                    tournament.TournamentMatches.Add(tournamentMatch);
                    val++;
                }
            }
        }
        public static void CreateSchavengenSystem(Cxt cxt, SqlTransaction trans, Tournament tournament, DataTable dtTournamentUsers)
        {
            int roundValue = 1;
            int val        = 0;

            TournamentMatches.DeleteTournamentMatchByTournamentID(trans, tournament.TournamentID);

            Teams Teams = new Teams(cxt, Teams.GetTeamsByTournamentID(tournament.TournamentID));

            roundValue = 1;
            int round = dtTournamentUsers.Rows.Count / Teams.Count;

            int doubleRoundNo = tournament.DoubleRoundNo * 2;

            if (doubleRoundNo == 0)
            {
                doubleRoundNo = 1;
            }

            bool isWhite = false;

            for (int k = 0; k <= doubleRoundNo - 1; k++)
            {
                if (isWhite)
                {
                    isWhite = false;
                }
                else
                {
                    isWhite = true;
                }

                //Teams Teams = new Teams(cxt, Teams.GetTeamsByTournamentID(tournament.TournamentID));

                if (Teams.Count > 0)
                {
                    DataTable dtCalculatePair = SchTeamPairing(Teams.Count);
                    Team      teamW           = null;
                    Team      teamB           = null;

                    for (int i = 0; i < dtCalculatePair.Rows.Count; i++)
                    {
                        DataRow drCal = dtCalculatePair.Rows[i];

                        teamW = Teams[Convert.ToInt32(drCal["TeamA"]) - 1];
                        teamB = Teams[Convert.ToInt32(drCal["TeamB"]) - 1];

                        dtTournamentUsers.DefaultView.RowFilter = "TeamID = " + teamW.TeamID.ToString() + " or TeamID = " + teamB.TeamID.ToString();
                        dtTournamentUsers.DefaultView.Sort      = "TeamID";
                        TournamentUsers tu = new TournamentUsers(cxt, dtTournamentUsers.DefaultView.ToTable());



                        if (tu.Count > 0)
                        {
                            CreateSchavengenMatches(cxt, tournament, tu, isWhite, k, round, ref val, ref roundValue);
                            tournament.TournamentMatches.Save(trans);
                        }
                    }
                }
            }
        }
Пример #13
0
        public static void CreateChildMatchIfRequired(Cxt cxt, TournamentMatch m, DataTable dtTournamentUsers)
        {
            #region Entry Conditions
            if (m == null)
            {
                return;
            }
            Tournament t = new Tournament(cxt, m.TournamentID);

            if (t.TournamentTypeE != TournamentTypeE.Knockout)
            {
                return;
            }

            if (dtTournamentUsers == null)
            {
                dtTournamentUsers = TournamentUsers.GetTournamentUsersByTournamentID(StatusE.Active, t.TournamentID);
            }

            //if (m.Round == 0 && IsPreliminaryRound(m.Round, dtTournamentUsers)) // if it is prelimiry round
            //{
            //    return;
            //}

            #endregion

            #region DataMembers
            DataTable dtMatches   = App.Model.Db.TournamentMatches.GetTournamentMatchByRound(t.ID, m.Round);
            DataRow[] drMatches   = null;
            int       whiteUserId = m.WhiteUserID;
            int       blackUserId = m.BlackUserID;
            decimal   whitePoints = -1;
            decimal   blackPoints = -1;
            string    filter      = "";

            filter += "(WhiteUserId = " + whiteUserId + " or BlackUserId = " + whiteUserId;
            filter += " or WhiteUserId = " + blackUserId + " or BlackUserId = " + blackUserId + ")";
            #endregion

            switch (m.TournamentMatchTypeE)
            {
            case TournamentMatchTypeE.Normal:
                #region Normal Round Matches
                drMatches = dtMatches.Select(filter + " and TournamentMatchTypeID = " + (int)TournamentMatchTypeE.Normal);

                whitePoints = CalculatePlayerPoints(cxt, whiteUserId, drMatches);
                blackPoints = CalculatePlayerPoints(cxt, blackUserId, drMatches);

                if (drMatches.Length >= t.NoOfGamesPerRound)     // NoOfGamesPerRound completed.
                {
                    #region If NoOfGamesPerRound completed
                    if (whitePoints > blackPoints)
                    {
                        // white win
                        SaveTournamentRound(cxt, dtMatches, t.ID, m.Round, whiteUserId, blackUserId, whitePoints, blackPoints, true);
                    }
                    else if (whitePoints < blackPoints)
                    {
                        // white lose
                        SaveTournamentRound(cxt, dtMatches, t.ID, m.Round, whiteUserId, blackUserId, whitePoints, blackPoints, false);
                    }
                    else
                    {
                        // draw/tie, now start tie-break matches if required.
                        if (t.IsTieBreak && t.NoOfTieBreaks > 0)
                        {
                            CreateChildMatch(t, m, TournamentMatchTypeE.TieBreak);
                        }
                    }
                    #endregion
                }
                else     // NoOfGamesPerRound not completed, so continue with round's matches if no player win yet.
                {
                    #region if NoOfGamesPerRound not completed
                    decimal winningPoints = (decimal)(t.NoOfGamesPerRound / 2 + 0.5);
                    if (whitePoints >= winningPoints)
                    {
                        // white win
                        SaveTournamentRound(cxt, dtMatches, t.ID, m.Round, whiteUserId, blackUserId, whitePoints, blackPoints, true);
                    }
                    else if (blackPoints >= winningPoints)
                    {
                        // white lose
                        SaveTournamentRound(cxt, dtMatches, t.ID, m.Round, whiteUserId, blackUserId, whitePoints, blackPoints, false);
                    }
                    else
                    {
                        CreateChildMatch(t, m, TournamentMatchTypeE.Normal);
                    }
                    #endregion
                }
                #endregion
                break;

            case TournamentMatchTypeE.TieBreak:
                #region Tie-Break Matches
                drMatches = dtMatches.Select(filter + " and TournamentMatchTypeID = " + (int)TournamentMatchTypeE.TieBreak);

                whitePoints = CalculatePlayerPoints(cxt, whiteUserId, drMatches);
                blackPoints = CalculatePlayerPoints(cxt, blackUserId, drMatches);

                if (drMatches.Length >= t.NoOfTieBreaks)     // NoOfTieBreaks completed.
                {
                    #region if NoOfTieBreaks completed

                    if (whitePoints > blackPoints)
                    {
                        // white win
                        SaveTournamentRound(cxt, dtMatches, t.ID, m.Round, whiteUserId, blackUserId, whitePoints, blackPoints, true);
                    }
                    else if (whitePoints < blackPoints)
                    {
                        // white lose
                        SaveTournamentRound(cxt, dtMatches, t.ID, m.Round, whiteUserId, blackUserId, whitePoints, blackPoints, false);
                    }
                    else
                    {
                        // draw/tie, now required SuddenDeath match
                        CreateChildMatch(t, m, TournamentMatchTypeE.SuddenDeath);
                    }
                    #endregion
                }
                else     // NoOfTieBreaks not completed, so continue with tie-break's matches
                {
                    decimal winningPoints = (decimal)(t.NoOfTieBreaks / 2 + 0.5);
                    if (whitePoints >= winningPoints)
                    {
                        // white win
                        SaveTournamentRound(cxt, dtMatches, t.ID, m.Round, whiteUserId, blackUserId, whitePoints, blackPoints, true);
                    }
                    else if (blackPoints >= winningPoints)
                    {
                        // white lose
                        SaveTournamentRound(cxt, dtMatches, t.ID, m.Round, whiteUserId, blackUserId, whitePoints, blackPoints, false);
                    }
                    else
                    {
                        CreateChildMatch(t, m, TournamentMatchTypeE.TieBreak);
                    }
                }
                #endregion
                break;

            case TournamentMatchTypeE.SuddenDeath:
                #region SuddenDeath Match
                bool isWhiteWin = false;

                #region Check Result
                switch (m.GameResultIDE)
                {
                case GameResultE.None:
                    #region Check TournamentMatchStatusE
                    switch (m.TournamentMatchStatusE)
                    {
                    case TournamentMatchStatusE.ForcedWhiteWin:
                    case TournamentMatchStatusE.BlackBye:
                        isWhiteWin = true;
                        break;

                    default:
                        break;
                    }
                    #endregion
                    break;

                case GameResultE.WhiteWin:
                case GameResultE.BlackBye:
                case GameResultE.ForcedWhiteWin:
                    isWhiteWin = true;
                    break;

                default:
                    break;
                }
                #endregion

                if (isWhiteWin)
                {
                    // white win
                    whitePoints = 1;
                    blackPoints = 0;
                    SaveTournamentRound(cxt, dtMatches, t.ID, m.Round, whiteUserId, blackUserId, whitePoints, blackPoints, true);
                }
                else
                {
                    // white lose
                    whitePoints = 0;
                    blackPoints = 1;
                    SaveTournamentRound(cxt, dtMatches, t.ID, m.Round, whiteUserId, blackUserId, whitePoints, blackPoints, false);
                }
                #endregion
                break;

            default:
                break;
            }
        }
Пример #14
0
        static TournamentUsers TournamentUserResult(Cxt cxt, DataTable dtTUser, TournamentMatch tournamentMatch, TournamentMatches matches, DataTable dtGames)
        {
            #region Find UserID 2

            TournamentUsers tournamentUsers = new TournamentUsers();

            int wUserID = tournamentMatch.WhiteUserID;
            int bUserID = tournamentMatch.BlackUserID;

            DataRow[] drWhite = dtTUser.Select("UserID = " + wUserID);
            DataRow[] drBlack = dtTUser.Select("UserID = " + bUserID);

            if (drWhite.Length == 0)
            {
                drWhite = dtTUser.Select("UserID2 = " + wUserID);
            }

            if (drBlack.Length == 0)
            {
                drBlack = dtTUser.Select("UserID2 = " + bUserID);
            }

            TournamentUser tournamentWUser = new TournamentUser(cxt, drWhite[0]);
            TournamentUser tournamentBUser = new TournamentUser(cxt, drBlack[0]);


            TournamentMatch tm = matches.GetByID(tournamentMatch.TournamentMatchID);

            #endregion

            if (tm.TournamentMatchStatusE == TournamentMatchStatusE.Scheduled ||
                tm.TournamentMatchStatusE == TournamentMatchStatusE.InProgress)
            {
                #region Points Calculation
                decimal wPoints = 0;
                decimal bPoints = 0;

                switch (tournamentMatch.TournamentMatchStatusE)
                {
                case TournamentMatchStatusE.Draw:
                    wPoints = 0.5m;
                    bPoints = 0.5m;
                    break;

                case TournamentMatchStatusE.WhiteBye:
                    wPoints = 0;
                    bPoints = 1;
                    break;

                case TournamentMatchStatusE.BlackBye:
                    wPoints = 1;
                    bPoints = 0;
                    break;

                case TournamentMatchStatusE.ForcedWhiteWin:
                    wPoints = 1;
                    bPoints = 0;
                    break;

                case TournamentMatchStatusE.ForcedWhiteLose:
                    wPoints = 0;
                    bPoints = 1;
                    break;

                case TournamentMatchStatusE.ForcedDraw:
                    wPoints = 0.5m;
                    bPoints = 0.5m;
                    break;

                default:
                    break;
                }

                tournamentBUser.TournamentPoints = tournamentBUser.TournamentPoints + bPoints;
                tournamentWUser.TournamentPoints = tournamentWUser.TournamentPoints + wPoints;
                #endregion
            }
            else
            {
                #region Points Calculation
                switch (tm.GameResultIDE)
                {
                case GameResultE.WhiteWin:
                    WhiteWin(tournamentMatch, tournamentWUser, tournamentBUser);
                    break;

                case GameResultE.WhiteLose:
                    WhiteLose(tournamentMatch, tournamentWUser, tournamentBUser);
                    break;

                case GameResultE.ForcedWhiteWin:
                    ForcedWhiteWin(tournamentMatch, tournamentWUser, tournamentBUser);
                    break;

                case GameResultE.ForcedWhiteLose:
                    ForcedWhiteLose(tournamentMatch, tournamentWUser, tournamentBUser);
                    break;

                case GameResultE.WhiteBye:
                    WhiteBye(tournamentMatch, tournamentWUser, tournamentBUser);
                    break;

                case GameResultE.BlackBye:
                    BlackBye(tournamentMatch, tournamentWUser, tournamentBUser);
                    break;

                case GameResultE.ForcedDraw:
                    ForcedDraw(tournamentMatch, tournamentWUser, tournamentBUser);
                    break;

                case GameResultE.Absent:
                    Absent(tournamentMatch, tournamentWUser, tournamentBUser);
                    break;

                case GameResultE.Draw:
                    ForcedDraw(tournamentMatch, tournamentWUser, tournamentBUser);
                    break;
                }
                #endregion
            }
            tournamentUsers.Add(tournamentWUser);
            tournamentUsers.Add(tournamentBUser);
            return(tournamentUsers);
            //tournamentUsers.Save(trans);
        }
Пример #15
0
        //public static void UpdateTournamentMatchStatus(Cxt cxt, int tournamentID, int tournamentMatchStatusID, string matchIDs)
        //{

        //    string[] matches = matchIDs.Split(',');

        //    foreach (string item in matches)
        //    {
        //        UpdateTournamentMatchStatus(cxt, tournamentMatchStatusID, Convert.ToInt32(item));
        //    }
        //}

        #region Update Match Status

        public static void UpdateTournamentMatchStatus(Cxt cxt, TournamentMatchStatusE tournamentMatchStatusID, TournamentMatch m)
        {
            SqlTransaction t = null;
            Game           g = null;
            Challenge      c = null;

            DataTable         dtMatches = TournamentMatches.GetTournamentsMatchByTournamentID(m.TournamentID);
            TournamentMatches matches   = new TournamentMatches(cxt, dtMatches);
            DataTable         dtGame    = Games.GetAllGamesByTournamentID(cxt, m.TournamentID);

            DataTable dtTUser = TournamentUsers.GetTournamentUsersByTournamentID(StatusE.Active, m.TournamentID);

            try
            {
                if (!TournamentMatchRules.Instance.CanChangeStatus(m.TournamentMatchStatusE, tournamentMatchStatusID))
                {
                    return;
                }

                m.EloBlackBefore         = (m.EloBlackBefore == 0) ? 1500 : m.EloBlackBefore;
                m.EloWhiteBefore         = (m.EloWhiteBefore == 0) ? 1500 : m.EloWhiteBefore;
                m.TournamentMatchStatusE = tournamentMatchStatusID;

                if (GetGameResultID((TournamentMatchStatusE)tournamentMatchStatusID) != GameResultE.None)
                {
                    m.GameResultIDE = GetGameResultID((TournamentMatchStatusE)tournamentMatchStatusID);
                }

                if (m.TournamentMatchStatusE == TournamentMatchStatusE.InProgress || m.IsBye || m.IsForced)
                {
                    c = CreateChallenge(m);
                }

                t = SqlHelper.BeginTransaction(Config.ConnectionString);

                m.Save(t);

                if (c != null)
                {
                    if (c.IsNew)
                    {
                        c.Save(t);
                    }
                }

                if (m.IsBye || m.IsForced)
                {
                    if (c != null)
                    {
                        c.ChallengeStatusIDE = ChallengeStatusE.Decline;
                        c.StatusIDE          = StatusE.Inactive;
                        c.Save(t);
                    }

                    TournamentUserResult(cxt, dtTUser, m, matches, dtGame).Save(t);

                    g = GetGame(cxt, m);

                    if (!g.IsNew)
                    {
                        g.GameResultIDE = m.GameResultIDE;
                        g.GameFlags     = "";
                        g.Save(t);
                    }
                }

                SqlHelper.CommitTransaction(t);

                switch (m.TournamentMatchStatusE)
                {
                case TournamentMatchStatusE.Draw:
                case TournamentMatchStatusE.WhiteBye:
                case TournamentMatchStatusE.BlackBye:
                case TournamentMatchStatusE.ForcedWhiteWin:
                case TournamentMatchStatusE.ForcedWhiteLose:
                case TournamentMatchStatusE.ForcedDraw:
                    TournamentMatch.CreateChildMatchIfRequired(cxt, m, dtTUser);
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                SqlHelper.RollbackTransaction(t);
                Log.Write(cxt, ex);
            }
        }
Пример #16
0
        public static void CreateKnockoutTournamentMatches(Cxt cxt, SqlTransaction trans, Tournament tournament, DataTable dtTournamentUsers)
        {
            TournamentMatches tournamentMatches = new TournamentMatches();
            TournamentUsers   tournamentUsers   = null;

            if (tournament.TournamentCurrentRound > 0)
            {
                TournamentUsers ratedTournamentUsers = null;
                if (TournamentMatch.IsPreliminaryRound(tournament.TournamentCurrentRound - 1, dtTournamentUsers))
                {
                    ratedTournamentUsers = GetTournamentPreliminaryWinners(cxt, tournament.TournamentID, tournament.TournamentCurrentRound, dtTournamentUsers);
                }
                else
                {
                    ratedTournamentUsers = GetTournamentWinners(cxt, tournament.TournamentID, tournament.TournamentCurrentRound);
                }

                if (ratedTournamentUsers.Count == 0)
                {
                    return;
                }

                if (tournament.TournamentCurrentRound > 2) // make sure,"tournament.TournamentCurrentRound - 1" is not preliminary round
                {
                    TournamentUsers previousRoundUsers = GetTournamentWinners(cxt, tournament.TournamentID, tournament.TournamentCurrentRound - 1);
                    if (previousRoundUsers.Count == 2 && ratedTournamentUsers.Count == 2)
                    {
                        return;
                    }
                }

                CreateKnockoutTournamentMatches(cxt, ratedTournamentUsers, tournamentMatches, tournament.TournamentCurrentRound, tournament);

                if (tournament.MaxWinners >= 3 && ratedTournamentUsers.Count == 2) // for multiple winners
                {
                    ratedTournamentUsers = GetTournamentLosers(cxt, tournament.TournamentID, tournament.TournamentCurrentRound);
                    CreateKnockoutTournamentMatches(cxt, ratedTournamentUsers, tournamentMatches, tournament.TournamentCurrentRound, tournament);
                }
            }
            else
            {
                if (dtTournamentUsers.Rows.Count == 0)
                {
                    return;
                }

                if (tournament.TournamentCurrentRound == 0)
                {
                    if (!TournamentMatch.IsPreliminaryRound(tournament.TournamentCurrentRound, dtTournamentUsers))
                    {
                        tournament.TournamentCurrentRound = 1;
                        tournamentUsers = new TournamentUsers(cxt, dtTournamentUsers);
                        //CreateKnockoutTournamentMatches(cxt, tournamentUsers, tournamentMatches, tournament.TournamentCurrentRound, tournament);
                        //break;
                    }
                    else
                    {
                        tournamentUsers = GetPreliminaryRound(cxt, tournament, dtTournamentUsers);
                    }
                }

                CreateKnockoutTournamentMatches(cxt, tournamentUsers, tournamentMatches, tournament.TournamentCurrentRound, tournament);
            }

            if (tournamentMatches.Count > 0)
            {
                tournamentMatches.Save(trans);
            }
        }
Пример #17
0
        public static int GetKnockoutTournamentMatchesCount(Cxt cxt, Tournament tournament)
        {
            DataTable         dtTournamentUsers = TournamentUsers.GetTournamentUsersByTournamentID(StatusE.Active, tournament.TournamentID);
            TournamentMatches tournamentMatches = new TournamentMatches();
            TournamentUsers   tournamentUsers   = null;

            if (tournament.TournamentCurrentRound > 0)
            {
                TournamentUsers ratedTournamentUsers = null;
                if (TournamentMatch.IsPreliminaryRound(tournament.TournamentCurrentRound - 1, dtTournamentUsers))
                {
                    ratedTournamentUsers = GetTournamentPreliminaryWinners(cxt, tournament.TournamentID, tournament.TournamentCurrentRound, dtTournamentUsers);
                }
                else
                {
                    ratedTournamentUsers = GetTournamentWinners(cxt, tournament.TournamentID, tournament.TournamentCurrentRound);
                }

                if (ratedTournamentUsers.Count == 0)
                {
                    return(0);
                }

                if (tournament.TournamentCurrentRound > 2) // make sure,"tournament.TournamentCurrentRound - 1" is not preliminary round
                {
                    TournamentUsers previousRoundUsers = GetTournamentWinners(cxt, tournament.TournamentID, tournament.TournamentCurrentRound - 1);
                    if (previousRoundUsers.Count == 2 && ratedTournamentUsers.Count == 2)
                    {
                        return(0);
                    }
                }

                CreateKnockoutTournamentMatches(cxt, ratedTournamentUsers, tournamentMatches, tournament.TournamentCurrentRound, tournament);

                if (tournament.MaxWinners >= 3 && ratedTournamentUsers.Count == 2) // for multiple winners
                {
                    ratedTournamentUsers = GetTournamentLosers(cxt, tournament.TournamentID, tournament.TournamentCurrentRound);
                    CreateKnockoutTournamentMatches(cxt, ratedTournamentUsers, tournamentMatches, tournament.TournamentCurrentRound, tournament);
                }
            }
            else
            {
                if (dtTournamentUsers.Rows.Count == 0)
                {
                    return(0);
                }

                if (tournament.TournamentCurrentRound == 0)
                {
                    if (!TournamentMatch.IsPreliminaryRound(tournament.TournamentCurrentRound, dtTournamentUsers))
                    {
                        tournament.TournamentCurrentRound = 1;
                        tournamentUsers = new TournamentUsers(cxt, dtTournamentUsers);
                        //CreateKnockoutTournamentMatches(cxt, tournamentUsers, tournamentMatches, tournament.TournamentCurrentRound, tournament);
                        //break;
                    }
                    else
                    {
                        tournamentUsers = GetPreliminaryRound(cxt, tournament, dtTournamentUsers);
                    }
                }

                CreateKnockoutTournamentMatches(cxt, tournamentUsers, tournamentMatches, tournament.TournamentCurrentRound, tournament);
            }

            return(tournamentMatches.Count);
        }
Пример #18
0
        static TournamentUsers GetTournamentPreliminaryWinners1(Cxt cxt, int tournamentID, int round, DataTable dtTournamentUsers)
        {
            TournamentUsers   tuWinners         = new TournamentUsers();
            TournamentMatches tournamentMatches = new TournamentMatches(cxt, GetTournamntPlayers(tournamentID));
            TournamentMatches tmW = new TournamentMatches();
            TournamentUsers   tuW = new TournamentUsers();
            TournamentUsers   tuL = new TournamentUsers();

            int wCounter = 0, bCounter = 0, dCounter = 0;

            foreach (DataRow item in tournamentMatches.DataTable.Rows)
            {
                TournamentMatch tournamentMatch = new TournamentMatch(cxt, item);

                TournamentUser tournamentUserB = null;
                TournamentUser tournamentUserW = null;


                switch (tournamentMatch.GameResultIDE)
                {
                case GameResultE.None:
                    break;

                case GameResultE.InProgress:
                    break;

                case GameResultE.WhiteWin:
                    bCounter       += 1;
                    tournamentUserW = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.WhiteUserID);
                    tournamentUserB = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.BlackUserID);
                    tuW.Add(tournamentUserW);
                    tuL.Add(tournamentUserB);
                    break;

                case GameResultE.WhiteLose:
                {
                    wCounter       += 1;
                    tournamentUserB = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.BlackUserID);
                    tournamentUserW = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.WhiteUserID);
                    tuW.Add(tournamentUserB);
                    tuL.Add(tournamentUserW);
                }
                break;

                case GameResultE.Draw:
                    dCounter += 1;
                    tmW.Add(tournamentMatch);
                    break;

                case GameResultE.Absent:
                    break;

                case GameResultE.NoResult:
                    break;

                case GameResultE.WhiteBye:
                    wCounter       += 1;
                    tournamentUserB = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.BlackUserID);
                    tournamentUserW = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.WhiteUserID);
                    tuW.Add(tournamentUserB);
                    tuL.Add(tournamentUserW);
                    break;

                case GameResultE.BlackBye:
                    bCounter       += 1;
                    tournamentUserW = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.WhiteUserID);
                    tournamentUserB = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.BlackUserID);
                    tuW.Add(tournamentUserW);
                    tuL.Add(tournamentUserB);
                    break;

                case GameResultE.ForcedWhiteWin:
                    bCounter       += 1;
                    tournamentUserW = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.WhiteUserID);
                    tournamentUserB = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.BlackUserID);
                    tuW.Add(tournamentUserW);
                    tuL.Add(tournamentUserB);
                    break;

                case GameResultE.ForcedWhiteLose:
                    wCounter       += 1;
                    tournamentUserB = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.BlackUserID);
                    tournamentUserW = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.WhiteUserID);
                    tuW.Add(tournamentUserB);
                    tuL.Add(tournamentUserW);
                    break;

                case GameResultE.ForcedDraw:
                    dCounter += 1;
                    tmW.Add(tournamentMatch);
                    break;

                default:
                    break;
                }
            }

            if (dCounter > 0)
            {
                foreach (DataRow item in tmW.DataTable.Rows)
                {
                    TournamentMatch tournamentMatch = new TournamentMatch(cxt, item);
                    if (tournamentMatch.EloWhiteAfter > tournamentMatch.EloBlackAfter)
                    {
                        TournamentUser tournamentUserW = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.WhiteUserID);
                        TournamentUser tournamentUserB = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.BlackUserID);
                        tuW.Add(tournamentUserW);
                        tuL.Add(tournamentUserB);
                    }
                    else if (tournamentMatch.EloBlackAfter > tournamentMatch.EloWhiteAfter)
                    {
                        TournamentUser tournamentUserB = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.BlackUserID);
                        TournamentUser tournamentUserW = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.WhiteUserID);
                        tuW.Add(tournamentUserB);
                        tuL.Add(tournamentUserW);
                    }
                    else
                    {
                        TournamentUser tournamentUserB = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.BlackUserID);
                        TournamentUser tournamentUserW = TournamentUser.GetTournamentUserById(cxt, tournamentID, tournamentMatch.WhiteUserID);
                        tuW.Add(tournamentUserB);
                        tuW.Add(tournamentUserW);
                    }
                }
            }
            if (tuL.Count > 0)
            {
                foreach (DataRow item in dtTournamentUsers.Rows)
                {
                    TournamentUser tu1 = new TournamentUser(cxt, item);

                    if (!tuL.Contains("UserID = " + tu1.UserID))
                    {
                        tuWinners.Add(tu1);
                    }
                }
            }
            return(tuWinners);
        }