public void IsBracketComplete_TestInvalid_WhenCalled(string input, string invalidReason)
        {
            // Arrange

            // Act
            var result = BracketHelper.IsBracketComplete(input);

            // Assert
            Assert.IsFalse(result, invalidReason);
        }
        public void IsBracketComplete_TestValid_WhenCalled(string input)
        {
            // Arrange

            // Act
            var result = BracketHelper.IsBracketComplete(input);

            // Assert
            Assert.IsTrue(result);
        }
Esempio n. 3
0
        public void ForceRedWin()
        {
            WinnerId   = RedTeamId;
            Finished   = true;
            Status     = Status.Finished;
            FinishDate = DateTime.UtcNow;

            Mongo.Matches.Save(this);

            BracketHelper.NewMatch(this);
        }
        public ActionResult BuildTournamentStructure()
        {
            if (User.Identity.Name != "Steven")
            {
                return(View("AuthenticationError"));
            }

            var confirmation = HttpContext.Request.QueryString["confirmation"] != null;

            if (confirmation)
            {
                BracketHelper.CreatePoolStructure();
                BracketHelper.CreateFinaleStructure();

                return(RedirectToAction("Index", "Admin"));
            }

            return(View());
        }
Esempio n. 5
0
        public ActionResult Callback(CallbackResult obj)
        {
            if (obj == null)
            {
                Log("obj == null");
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            Log("obj != null");
            // Get the match from the database by looking up the tournament code
            var match = Mongo.Matches.FindOne(Query <Match> .Where(x => x.TournamentCode == obj.TournamentCode || x.TournamentCodeBlind == obj.TournamentCode));

            Log("tournament code = " + obj.TournamentCode);

            if (match == null)
            {
                Log("match == null");
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Tournament code not found"));
            }

            Log("match != null");

            // Check which side won
            var winningTeam = obj.WinningTeam.Select(y => y.SummonerId);
            var losingTeam  = obj.LosingTeam.Select(y => y.SummonerId);

            Log("defined winningTeam, losingTeam");

            var blueSideWon = match.BlueTeam.Participants.All(x => winningTeam.Contains(x.Summoner.Id));
            var redSideWon  = match.RedTeam.Participants.All(x => winningTeam.Contains(x.Summoner.Id));

            Log("defined blueSideWon, redSideWon");

            var blueSideLost = match.BlueTeam.Participants.All(x => losingTeam.Contains(x.Summoner.Id));
            var redSideLost  = match.RedTeam.Participants.All(x => losingTeam.Contains(x.Summoner.Id));

            Log("defined blueSideLost, redSideLost");

            // Check if the results are sane
            if (blueSideWon && redSideLost || redSideWon && blueSideLost)
            {
                Log("result sane");

                // Set winner ID to blue if blue side won.
                match.WinnerId    = blueSideWon ? match.BlueTeamId : match.RedTeamId;
                match.StartTime   = obj.StartTime;
                match.FinishDate  = DateTime.UtcNow;
                match.RiotMatchId = obj.GameId;
                match.Finished    = true;
                match.Status      = Status.Finished;

                // Preliminary duration, includes champ select
                match.Duration = match.FinishDate - match.StartTime;

                // Save to database
                Mongo.Matches.Save(match);

                Log("saved preliminary stats");

                MatchDetail matchDetails;

                // Get more match details
                try
                {
                    matchDetails = _tournamentApi.GetTournamentMatch(Region.euw, obj.GameId, match.TournamentCode, false);
                }
                catch (Exception)
                {
                    Log("match not found, deferring to scraper");

                    match.Invalid       = true;
                    match.InvalidReason = "MATCH_NOT_FOUND";

                    // Save to database
                    Mongo.Matches.Save(match);
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Match ID not found"));
                }

                // Get the players that were supposed to be on blue side
                var supposedToBeBluePlayers = matchDetails.Participants.Where(
                    matchDetailParticipant =>
                    match.BlueTeam.Participants.Select(
                        localMatchParticipant => localMatchParticipant.Summoner.Name.ToLower())
                    .Contains(
                        matchDetails.ParticipantIdentities.Single(
                            identity => identity.ParticipantId == matchDetailParticipant.ParticipantId)
                        .Player.SummonerName.ToLower())).ToList();

                if (supposedToBeBluePlayers.Count == 0)
                {
                    Log("supposedToBeBluePlayers.Count == 0");

                    // Teams did not have correct summoners playing the match. Match invalid.
                    match.Invalid       = true;
                    match.InvalidReason = "INCORRECT_SUMMONER_COUNT_BLUE_TEAM";

                    // Save to database
                    Mongo.Matches.Save(match);
                    return(new HttpStatusCodeResult(HttpStatusCode.OK));
                }

                // Set the team id to the side they actually played
                var blueTeamId = supposedToBeBluePlayers.First().TeamId;

                // Get the players that were supposed to be on red side
                var supposedToBeRedPlayers = matchDetails.Participants.Where(
                    matchDetailParticipant =>
                    match.RedTeam.Participants.Select(
                        localMatchParticipant => localMatchParticipant.Summoner.Name.ToLower())
                    .Contains(
                        matchDetails.ParticipantIdentities.Single(
                            identity => identity.ParticipantId == matchDetailParticipant.ParticipantId)
                        .Player.SummonerName.ToLower())).ToList();

                if (supposedToBeRedPlayers.Count == 0)
                {
                    Log("supposedToBeRedPlayers.Count == 0");

                    // Teams did not have correct summoners playing the match. Match invalid.
                    match.Invalid       = true;
                    match.InvalidReason = "INCORRECT_SUMMONER_COUNT_RED_TEAM";

                    // Save to database
                    Mongo.Matches.Save(match);
                    return(new HttpStatusCodeResult(HttpStatusCode.OK));
                }

                // Set the team id to the side they actually played
                var redTeamId = supposedToBeRedPlayers.First().TeamId;

                // Set statistics
                match.Duration     = matchDetails.MatchDuration;
                match.CreationTime = matchDetails.MatchCreation;
                match.RiotMatchId  = matchDetails.MatchId;

                match.ChampionIds = matchDetails.Participants.Select(x => x.ChampionId).ToArray();

                // Exclude null bans for blind pick and for teams that forgot all their bans
                match.BanIds = matchDetails.Teams.Where(x => x.Bans != null).SelectMany(x => x.Bans).Select(x => x.ChampionId).ToArray();

                match.AssistsBlueTeam = matchDetails.Participants.Where(x => x.TeamId == blueTeamId).Sum(x => x.Stats.Assists);
                match.KillsBlueTeam   = matchDetails.Participants.Where(x => x.TeamId == blueTeamId).Sum(x => x.Stats.Kills);
                match.DeathsBlueTeam  = matchDetails.Participants.Where(x => x.TeamId == blueTeamId).Sum(x => x.Stats.Deaths);

                match.AssistsRedTeam = matchDetails.Participants.Where(x => x.TeamId == redTeamId).Sum(x => x.Stats.Assists);
                match.KillsRedTeam   = matchDetails.Participants.Where(x => x.TeamId == redTeamId).Sum(x => x.Stats.Kills);
                match.DeathsRedTeam  = matchDetails.Participants.Where(x => x.TeamId == redTeamId).Sum(x => x.Stats.Deaths);

                match.Invalid = false;

                Log("set all stats");

                if (blueTeamId == 200)
                {
                    match.PlayedWrongSide = true;
                }

                // Save to database
                Mongo.Matches.Save(match);

                Log("executing NewMatch hook");
                // Call the new match hook
                BracketHelper.NewMatch(match);
                Log("========================done========================");
            }
            else
            {
                Log("incorrect summoners");

                // Teams did not have correct summoners playing the match. Match invalid.
                match.Invalid       = true;
                match.InvalidReason = "INCORRECT_SUMMONERS";

                // Save to database
                Mongo.Matches.Save(match);
            }

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
Esempio n. 6
0
        private const int NumPools = 8; // number of initial rounds in knockout phase

        public ScheduleViewModel()
        {
            Teams = Mongo.Teams.Find(Query <Team> .Where(x => !x.Cancelled && !x.Name.Contains("TESTCOGNAC")))
                    .OrderBy(x => x.Pool)
                    .ThenBy(x => x.Name)
                    .ToList();

            if (Teams.Count != 32 || Mongo.Matches.Count() == 0)
            {
                Teams = new List <Team>();
                return;
            }

            KnockOutTeams = new string[NumPools][];
            int[] pools = { 0, 1, 4, 5, 7, 6, 3, 2 };
            for (int i = 0; i < NumPools; i += 2)
            {
                // Couple pool 1,2 | 3,4 etc.
                var pool      = pools[i];
                var otherPool = pools[i + 1];
                // Teams from the same pool should be on the other side of the bracket. 1,5 | 2,6 etc.
                var matchIndex   = i / 2;
                var otherBracket = matchIndex + NumPools / 2;
                KnockOutTeams[matchIndex]   = new string[2];
                KnockOutTeams[otherBracket] = new string[2];
                if (BracketHelper.PoolFinished(pool))
                {
                    var ranks = BracketHelper.GetPoolRanking(pool);
                    KnockOutTeams[matchIndex][0]   = ranks[0].Name;
                    KnockOutTeams[otherBracket][1] = ranks[1].Name;
                }
                else
                {
                    KnockOutTeams[matchIndex][0]   = "#1 pool " + (pool + 1);
                    KnockOutTeams[otherBracket][1] = "#2 pool " + (pool + 1);
                }

                if (BracketHelper.PoolFinished(otherPool))
                {
                    var ranks = BracketHelper.GetPoolRanking(otherPool);
                    KnockOutTeams[matchIndex][1]   = ranks[1].Name;
                    KnockOutTeams[otherBracket][0] = ranks[0].Name;
                }
                else
                {
                    KnockOutTeams[matchIndex][1]   = "#2 pool " + (otherPool + 1);
                    KnockOutTeams[otherBracket][0] = "#1 pool " + (otherPool + 1);
                }
            }

            int numRounds = (int)Math.Round(Math.Log(NumPools * 2, 2));

            KnockOutResults = new int?[numRounds][][];
            var teamsLeft = new ObjectId[NumPools * 2];

            for (int i = 0; i < NumPools; i++)
            {
                var team1 = Teams.FirstOrDefault(t => t.Name == KnockOutTeams[i][0]);
                var team2 = Teams.FirstOrDefault(t => t.Name == KnockOutTeams[i][1]);
                teamsLeft[i * 2]     = team1?.Id ?? ObjectId.Empty;
                teamsLeft[i * 2 + 1] = team2?.Id ?? ObjectId.Empty;
            }
            for (int i = 0; i < numRounds; i++)
            {
                int numGames       = (int)Math.Round(NumPools / Math.Pow(2, i));
                var advancingTeams = new ObjectId[numGames];
                KnockOutResults[i] = new int?[numGames == 1 ? 2 : numGames][]; // Also count bronze final
                for (int j = 0; j < teamsLeft.Length; j += 2)
                {
                    var team1 = teamsLeft[j];
                    var team2 = teamsLeft[j + 1];
                    if (team1 == ObjectId.Empty || team2 == ObjectId.Empty)
                    {
                        KnockOutResults[i][j / 2] = new int?[] { null, null };
                        continue;
                    }
                    // Find the match in the winner's bracket where the two teams played eachother
                    var match = Mongo.Matches.FindOne(Query <Match> .Where(m =>
                                                                           m.Phase == Phase.WinnerBracket &&
                                                                           (
                                                                               (m.BlueTeamId == team1 && m.RedTeamId == team2) ||
                                                                               (m.RedTeamId == team1 && m.BlueTeamId == team2)
                                                                           )));
                    if (match != null && match.Finished)
                    {
                        KnockOutResults[i][j / 2] = match.WinnerId == team1 ? new int?[] { 1, 0 } : new int?[] { 0, 1 };

                        advancingTeams[j / 2] = match.WinnerId;
                    }
                    else
                    {
                        KnockOutResults[i][j / 2] = new int?[] { null, null };
                    }
                }
                if (numGames == 1)
                {
                    // Add the bronze final
                    KnockOutResults[i][1] = new int?[] { null, null };
                    var bronzeMatch = Mongo.Matches.Find(Query <Match> .Where(x => x.Phase == Phase.BronzeFinale)).First();
                    if (bronzeMatch != null && bronzeMatch.Finished)
                    {
                        var teamNames = KnockOutTeams.SelectMany(t => t).ToArray();
                        KnockOutResults[i][1] = Array.IndexOf(teamNames, bronzeMatch.Winner.Name) < NumPools ? new int?[] { 1, 0 } : new int?[] { 0, 1 };
                    }
                }
                teamsLeft = advancingTeams;
            }
        }