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); } }
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); } }
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); }
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; } }
//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); } }
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); }