private void FillWinnersAndLoosersMatches() { // Fill main structure foreach (KeyValuePair <int, tournamentMatch> item in _matches) { if (((tournamentMatchBracket)item.Value).Opponent1PreviousMatch != null) { if (((tournamentMatchBracket)item.Value).Opponent1LoosePreviousMatch) { ((tournamentMatchBracket)item.Value).Opponent1PreviousMatch.LooserNextMatch = (tournamentMatchBracket)item.Value; } else { ((tournamentMatchBracket)item.Value).Opponent1PreviousMatch.WinnerNextMatch = (tournamentMatchBracket)item.Value; } } if (((tournamentMatchBracket)item.Value).Opponent2PreviousMatch != null) { if (((tournamentMatchBracket)item.Value).Opponent2LoosePreviousMatch) { ((tournamentMatchBracket)item.Value).Opponent2PreviousMatch.LooserNextMatch = (tournamentMatchBracket)item.Value; } else { ((tournamentMatchBracket)item.Value).Opponent2PreviousMatch.WinnerNextMatch = (tournamentMatchBracket)item.Value; } } } foreach (KeyValuePair <int, tournamentMatch> item in _matches) { if (((tournamentMatchBracket)item.Value).WinnerNextMatch != null) { ((tournamentMatchBracket)item.Value).WinnerNextMatch = _matches[((tournamentMatchBracket)item.Value).WinnerNextMatch.MatchId] as tournamentMatchBracket; } if (((tournamentMatchBracket)item.Value).LooserNextMatch != null) { ((tournamentMatchBracket)item.Value).LooserNextMatch = _matches[((tournamentMatchBracket)item.Value).LooserNextMatch.MatchId] as tournamentMatchBracket; } } // Update winner's bracket structure foreach (int item in WinnersBracketMatchesIds) { ((tournamentMatchBracket)_matches[item]).MatchRoundAlias = String.Format("Winner's bracket, round {0}", _tournamentWinnersDepth - ((tournamentMatchBracket)_matches[item]).Depth); } // Update looser's bracket structure // First remove final matches (grand final and bracket reset) from looser's bracket LoosersBracketMatchesIds.Remove(FinalMatch.MatchId); foreach (int item in LoosersBracketMatchesIds) { if (((tournamentMatchBracket)_matches[item]).WinnerNextMatch.MatchId == FinalMatch.Opponent1PreviousMatch.MatchId) { _loosersFinalMatch = ((tournamentMatchBracket)_matches[item]); _loosersFinalMatch.Depth = 0; _tournamentLoosersDepth = 1; SetNextDepthInLoosers(_loosersFinalMatch, true); } } if (_loosersFinalMatch == null) { if (!FinalMatch.Opponent1PreviousMatch.InWinnersBracket && FinalMatch.Opponent2PreviousMatch.InWinnersBracket) { _loosersFinalMatch = (FinalMatch.Opponent1PreviousMatch); _loosersFinalMatch.Depth = 0; _tournamentLoosersDepth = 1; SetNextDepthInLoosers(_loosersFinalMatch, true); } if (FinalMatch.Opponent1PreviousMatch.InWinnersBracket && !FinalMatch.Opponent2PreviousMatch.InWinnersBracket) { _loosersFinalMatch = (FinalMatch.Opponent2PreviousMatch); _loosersFinalMatch.Depth = 0; _tournamentLoosersDepth = 1; SetNextDepthInLoosers(_loosersFinalMatch, true); } } foreach (int item in LoosersBracketMatchesIds) { ((tournamentMatchBracket)_matches[item]).MatchRoundAlias = String.Format("Looser's bracket, round {0}", _tournamentLoosersDepth - ((tournamentMatchBracket)_matches[item]).Depth); } }
private tournamentMatchBracket AddMatchToTournament(Dictionary <int, wr_Match> matchPool, int baseMatchId, int matchDepth, bool isInLoosersBracket) { int opponent1Id; int opponent2Id; int.TryParse(matchPool[baseMatchId].Player1Id, out opponent1Id); int.TryParse(matchPool[baseMatchId].Player2Id, out opponent2Id); tournamentMatchBracket currentMatch = new tournamentMatchBracket(baseMatchId, matchPool[baseMatchId].Identifier, GetParticipantFromId(opponent1Id), GetParticipantFromId(opponent2Id), new matchStation(), matchPool[baseMatchId].Player1Points, matchPool[baseMatchId].Player2Points, matchPool[baseMatchId].Player1IsPrereqMatchLooserField == "true", matchPool[baseMatchId].Player2IsPrereqMatchLooserField == "true", tournamentMatch.MatchStatus.NotRunYet); currentMatch.Depth = matchDepth; bool player1PrevMatchLoose = false; bool player2PrevMatchLoose = false; int player1PreviousMatchId = -1; int player2PreviousMatchId = -1; // In looser's bracket, consider previous match only if winning if (matchPool[baseMatchId].Player1PrereqMatchIdField != null) { player1PreviousMatchId = int.Parse(matchPool[baseMatchId].Player1PrereqMatchIdField); if (matchPool[baseMatchId].Player1IsPrereqMatchLooserField == "true") { player1PrevMatchLoose = true; } // if (matchPool[baseMatchId].Player1PrereqMatchIdField == matchPool[baseMatchId].Player2PrereqMatchIdField) //{ // tournamentMatchBracket player1PrereqMatch = AddMatchToTournament(matchPool, matchId, matchDepth + 1, false); // currentMatch.Opponent1PreviousMatch = player1PrereqMatch; // ((tournamentMatchBracket)_matches[matchId]).WinnerNextMatch = currentMatch; //} //else if (matchPool[baseMatchId].Player1IsPrereqMatchLooserField == "false") //{ // tournamentMatchBracket player1PrereqMatch = AddMatchToTournament(matchPool, matchId, matchDepth + 1, isInLoosersBracket); // currentMatch.Opponent1PreviousMatch = player1PrereqMatch; // ((tournamentMatchBracket)_matches[matchId]).LooserNextMatch = currentMatch; //} //else if(matchPool[baseMatchId].Player1IsPrereqMatchLooserField == "true") //{ // tournamentMatchBracket player1PrereqMatch = AddMatchToTournament(matchPool, matchId, matchDepth + 1, false); // currentMatch.Opponent1PreviousMatch = player1PrereqMatch; // ((tournamentMatchBracket)_matches[matchId]).LooserNextMatch = currentMatch; // player1PrevMatchLoose = true; //} } if (matchPool[baseMatchId].Player2PrereqMatchIdField != null) { player2PreviousMatchId = int.Parse(matchPool[baseMatchId].Player2PrereqMatchIdField); if (matchPool[baseMatchId].Player2IsPrereqMatchLooserField == "true") { player2PrevMatchLoose = true; } //if ((matchPool[baseMatchId].Player2PrereqMatchIdField == matchPool[baseMatchId].Player2PrereqMatchIdField) || // (matchPool[baseMatchId].Player2IsPrereqMatchLooserField == "false")) //{ // tournamentMatchBracket player2PrereqMatch = AddMatchToTournament(matchPool, matchId, matchDepth + 1, false); // currentMatch.Opponent1PreviousMatch = player2PrereqMatch; // ((tournamentMatchBracket)_matches[matchId]).WinnerNextMatch = currentMatch; //} //else if (matchPool[baseMatchId].Player2IsPrereqMatchLooserField == "true") //{ // if (!isInLoosersBracket) // { // tournamentMatchBracket player2PrereqMatch = AddMatchToTournament(matchPool, matchId, matchDepth + 1, true); // currentMatch.Opponent1PreviousMatch = player2PrereqMatch; // ((tournamentMatchBracket)_matches[matchId]).LooserNextMatch = currentMatch; // } // player2PrevMatchLoose = true; //} } isInLoosersBracket = isInLoosersBracket || (player1PrevMatchLoose && player2PrevMatchLoose); if (isInLoosersBracket) { if (player1PreviousMatchId != -1) { if (!player1PrevMatchLoose) { tournamentMatchBracket player1PrereqMatch = AddMatchToTournament(matchPool, player1PreviousMatchId, matchDepth + 1, true); currentMatch.Opponent1PreviousMatch = player1PrereqMatch; } else { //tournamentMatchBracket player1PrereqMatch = AddMatchToTournament(matchPool, player1PreviousMatchId, matchDepth + 1, false); currentMatch.Opponent1PreviousMatch = (tournamentMatchBracket)_matches[player1PreviousMatchId]; } ((tournamentMatchBracket)_matches[player1PreviousMatchId]).WinnerNextMatch = currentMatch; } if (player2PreviousMatchId != -1) { if (!player2PrevMatchLoose) { tournamentMatchBracket player2PrereqMatch = AddMatchToTournament(matchPool, player2PreviousMatchId, matchDepth + 1, true); currentMatch.Opponent2PreviousMatch = player2PrereqMatch; } else { //tournamentMatchBracket player2PrereqMatch = AddMatchToTournament(matchPool, player2PreviousMatchId, matchDepth + 1, false); currentMatch.Opponent2PreviousMatch = (tournamentMatchBracket)_matches[player2PreviousMatchId]; } ((tournamentMatchBracket)_matches[player2PreviousMatchId]).WinnerNextMatch = currentMatch; } } else { if (player1PreviousMatchId != -1) { bool previousMatchInLoosers = false; if (!player1PrevMatchLoose) { // if 2 matches before, player1 loosed, its previous match is in looser's if (matchPool[player1PreviousMatchId].Player1IsPrereqMatchLooserField == "true") { previousMatchInLoosers = true; } } tournamentMatchBracket player1PrereqMatch = AddMatchToTournament(matchPool, player1PreviousMatchId, matchDepth + 1, previousMatchInLoosers); currentMatch.Opponent1PreviousMatch = player1PrereqMatch; ((tournamentMatchBracket)_matches[player1PreviousMatchId]).WinnerNextMatch = currentMatch; } if (player2PreviousMatchId != -1) { bool previousMatchInLoosers = false; if (!player2PrevMatchLoose) { // if 2 matches before, player1 loosed, its previous match is in looser's if (matchPool[player2PreviousMatchId].Player1IsPrereqMatchLooserField == "true") { previousMatchInLoosers = true; } } tournamentMatchBracket player2PrereqMatch = AddMatchToTournament(matchPool, player2PreviousMatchId, matchDepth + 1, previousMatchInLoosers); currentMatch.Opponent2PreviousMatch = player2PrereqMatch; ((tournamentMatchBracket)_matches[player2PreviousMatchId]).WinnerNextMatch = currentMatch; } } AddMatch(currentMatch); if (isInLoosersBracket) { if (!LoosersBracketMatchesIds.Contains(currentMatch.MatchId)) { LoosersBracketMatchesIds.Add(currentMatch.MatchId); ((tournamentMatchBracket)_matches[currentMatch.MatchId]).InWinnersBracket = false; _tournamentLoosersDepth = Math.Max(matchDepth, _tournamentLoosersDepth); } } else { if (!WinnersBracketMatchesIds.Contains(currentMatch.MatchId)) { WinnersBracketMatchesIds.Add(currentMatch.MatchId); ((tournamentMatchBracket)_matches[currentMatch.MatchId]).InWinnersBracket = true; _tournamentWinnersDepth = Math.Max(matchDepth, _tournamentWinnersDepth); } } _finalMatch = currentMatch; return(currentMatch); }