public void EmptyString()
 {
     Assert.IsTrue(
         TeamNameParser.TryGetTeamNamesFromParts("", out IList <string> names, out string errorMessage),
         "No teams should be parsable.");
     Assert.AreEqual(0, names.Count, "Names should be empty.");
 }
Пример #2
0
        public static bool TryParseTeams(string message, out IEnumerable <Team> teams, out string errorMessage)
        {
            Verify.IsNotNull(message, nameof(message));

            string[]    teamsList = message.Split("\n");
            List <Team> allTeams  = new List <Team>();

            for (int i = 0; i < teamsList.Length; i++)
            {
                string teamList = teamsList[i];
                if (!TeamNameParser.TryGetTeamNamesFromParts(
                        teamList, out IList <string> newTeamNames, out errorMessage))
                {
                    teams = null;
                    return(false);
                }

                allTeams.AddRange(newTeamNames.Select(teamName =>
                                                      new Team()
                {
                    Name    = teamName,
                    Bracket = i
                }));
            }

            errorMessage = null;
            teams        = allTeams;
            return(true);
        }
        public void TrailingCommaProducesError()
        {
            const string teamNames = "Team1,";

            Assert.IsFalse(
                TeamNameParser.TryGetTeamNamesFromParts(teamNames, out IList <string> names, out string errorMessage),
                "No teams should be parsable.");
            Assert.IsNotNull(errorMessage, "Error message should have a value.");
        }
        public void OneTeam()
        {
            const string teamName = "Team";

            Assert.IsTrue(
                TeamNameParser.TryGetTeamNamesFromParts(teamName, out IList <string> names, out string errorMessage),
                "Should be parsable.");
            Assert.AreEqual(1, names.Count, "There should be one name");
            Assert.IsTrue(names.Contains(teamName), $"Team name was not found. Names: {string.Join(",", names)}");
        }
        public void TwoTeams()
        {
            const string teamNames = "Team1,Team2";

            Assert.IsTrue(
                TeamNameParser.TryGetTeamNamesFromParts(teamNames, out IList <string> names, out string errorMessage),
                "Should be parsable.");
            Assert.AreEqual(2, names.Count, "There should be one name");
            Assert.IsTrue(names.Contains("Team1"), $"Team 'Team1' name was not found. Names: {string.Join(",", names)}");
            Assert.IsTrue(names.Contains("Team2"), $"Team 'Team2' name was not found. Names: {string.Join(",", names)}");
        }
        public void GetTeamName_ComplexIndexString_ReturnTeam()
        {
            //ARRANGE
            TeamNameParser sut = new TeamNameParser();

            string args     = "pat_scpublish-event-processing_access_index-2017-07-08_1";
            string expected = "pat";

            //ACT
            var result = sut.GetTeamName(args);

            //ASSERT
            Assert.Equal(expected, result);
        }
        public void EscapedCommaThenSeparatorComma()
        {
            const string escapedTeamNames = "One,,,Team";
            const string firstTeamName    = "One,";
            const string secondTeam       = "Team";

            Assert.IsTrue(
                TeamNameParser.TryGetTeamNamesFromParts(
                    escapedTeamNames, out IList <string> names, out string errorMessage),
                "Should be parsable.");
            Assert.AreEqual(2, names.Count, "There should be one name");
            Assert.IsTrue(
                names.Contains(firstTeamName),
                $"Team with commas in name was not found. Names: {string.Join(",", names)}");
            Assert.IsTrue(
                names.Contains(secondTeam), $"Second team was not found. Names: {string.Join(",", names)}");
        }
Пример #8
0
        public async Task SetupFinalsAsync(IGuildUser readerUser, string rawTeamNameParts)
        {
            ITextChannel channel = null;

            await this.DoReadWriteActionOnCurrentTournamentAsync(
                async currentTournament =>
            {
                if (currentTournament?.Stage != TournamentStage.RunningTournament)
                {
                    this.Logger.Debug("Could not start finals in stage {stage}", currentTournament?.Stage);
                    await this.SendChannelMessage(BotStrings.ErrorFinalsOnlySetDuringPrelimsOrPlayoffs);
                    return;
                }

                if (!currentTournament.TryGetReader(readerUser.Id, out Reader reader))
                {
                    this.Logger.Debug("Could not start finals because {1} is not a reader", readerUser.Id);
                    await this.SendChannelMessage(BotStrings.ErrorGivenUserIsntAReader);
                    return;
                }

                if (rawTeamNameParts == null)
                {
                    this.Logger.Debug(
                        "Could not start finals because no teams were specified");
                    await this.SendChannelMessage(BotStrings.ErrorNoTeamsSpecified);
                    return;
                }

                string combinedTeamNames = string.Join(" ", rawTeamNameParts).Trim();
                if (!TeamNameParser.TryGetTeamNamesFromParts(
                        combinedTeamNames, out IList <string> teamNames, out string errorMessage))
                {
                    this.Logger.Debug(
                        "Could not start finals because of this error: {errorMessage}", errorMessage);
                    await this.SendChannelMessage(BotStrings.ErrorGenericMessage(errorMessage));
                    return;
                }

                if (teamNames.Count != 2)
                {
                    this.Logger.Debug(
                        "Could not start finals because {count} teams were specified", teamNames.Count);
                    await this.SendChannelMessage(BotStrings.ErrorTwoTeamsMustBeSpecifiedFinals(teamNames.Count));
                    return;
                }

                Team[] teams = teamNames.Select(name => new Team()
                {
                    Name = name
                })
                               .ToArray();
                if (currentTournament.Teams.Intersect(teams).Count() != teams.Length)
                {
                    this.Logger.Debug(
                        "Could not start finals because some teams were not in the tournament", teamNames.Count);
                    await this.SendChannelMessage(
                        BotStrings.ErrorAtLeastOneTeamNotInTournament(string.Join(", ", teamNames)));
                    return;
                }

                // Create a finals channel and give access to the teams and readers
                Game finalsGame = new Game()
                {
                    Reader = reader,
                    Teams  = teams
                };

                int finalsRoundNumber   = currentTournament.Schedule.Rounds.Count + 1;
                IList <Game> finalGames =
                    currentTournament.Schedule.Rounds[currentTournament.Schedule.Rounds.Count - 1].Games;
                int roomIndex = 0;
                foreach (Game game in finalGames)
                {
                    if (game.Reader.Equals(reader))
                    {
                        break;
                    }

                    roomIndex++;
                }

                if (roomIndex >= finalGames.Count)
                {
                    // Need to have a fall-back somehow. For now default to the first room.
                    roomIndex = 0;
                }

                // TODO: Look into creating the channels after the update stage so we can release the lock
                // sooner. However, this does mean that a failure to create channels will leave us in a bad
                // state.
                channel = await this.ChannelManager.CreateChannelsForFinals(
                    this.Context.Client.CurrentUser, currentTournament, finalsGame, finalsRoundNumber, roomIndex);

                currentTournament.UpdateStage(
                    TournamentStage.Finals, out string nextStageTitle, out string nextStageInstructions);
            });

            if (channel != null)
            {
                this.Logger.Debug("Finals started successfully");
                await this.Context.Channel.SendMessageAsync(
                    BotStrings.FinalsParticipantsPleaseJoin(channel.Mention),
                    options : RequestOptionsSettings.Default);
            }
        }