Exemple #1
0
        public Competition CreateCompetition(AssemblyFile[] files, IEnumerable<TeamInfo> teams, IEnumerable<TeamInfo> networkTeams)
        {
            DynamicAssemblyTypeFinder dynamicAssemblyTypeFinder = new DynamicAssemblyTypeFinder();
              dynamicAssemblyTypeFinder.AddAll(files);

              IGame game = dynamicAssemblyTypeFinder.Create<IGame>().Single();
              Competition competition = new Competition(game);

              IEnumerable<string> teamNames = teams.Select(team => team.Name);
              foreach (IBotFactory botFactory in dynamicAssemblyTypeFinder.Create<IBotFactory>())
              {
            string teamName = Path.GetFileNameWithoutExtension(botFactory.GetType().Assembly.Location);
            if (teamNames.Contains(teamName))
            {
              competition.AddPlayer(new BotPlayer(teamName, botFactory.CreateBot()));
            }
              }

              INetworkBotFactory factory = dynamicAssemblyTypeFinder.Create<INetworkBotFactory>().Single();
              foreach (TeamInfo team in networkTeams)
              {
            competition.AddPlayer(new BotPlayer(team.Name, factory.CreateBot(team.Url)));
              }

              return competition;
        }
        public Competition CreateCompetition(AssemblyFile[] files)
        {
            DynamicAssemblyTypeFinder dynamicAssemblyTypeFinder = new DynamicAssemblyTypeFinder();
            dynamicAssemblyTypeFinder.AddAll(files);

            IGame game = new RockPaperScissorsPro.CompeteGameWrapper();

            Competition competition = new Competition(game);
            IEnumerable<IBotFactory> botFactoryList = dynamicAssemblyTypeFinder.Create<IBotFactory>();

            foreach (IBotFactory botFactory in botFactoryList)
            {
                //if unable to create botFactory, skip adding it to the list
                if (botFactory != null)
                {
                    string teamName = Team.ConvertFileToTeamName(botFactory.GetType().Assembly.Location);
                    competition.AddPlayer(new BotPlayer(teamName, null, botFactory));
                }
            }

            // look for any players that were downloaded but not in competition.  make sure they
            // get a bot that forfeits
            foreach (AssemblyFile f in files)
            {
                string teamName = Team.ConvertFileToTeamName(f.Path);
                if (!competition.ContainsPlayer(teamName))
                {
                    competition.AddPlayer(new BotPlayer(teamName, null, null));
                }
            }

            return competition;
        }
        // run dynamic language implementation in DLR to verify implementation (as much as possible)
        private static IEnumerable<BotValidationError> ValidateViaDlr(String dllPath)
        {
            var errList = new List<BotValidationError>();
            var theScript = String.Empty;
            try
            {
                DynamicAssemblyTypeFinder dynamicAssemblyTypeFinder = new DynamicAssemblyTypeFinder();
                dynamicAssemblyTypeFinder.AddAll(new AssemblyFile[] { new AssemblyFile(dllPath) }.ToList<AssemblyFile>());

                // use reflection to get the script (used later for error messaging)
                theScript = (String) dynamicAssemblyTypeFinder.FindType("RockPaperAzure.MyBot").GetField("theScript").GetValue(null);

                // instantiate the bot
                dynamic theBot = dynamicAssemblyTypeFinder.CreateOne<RockPaperScissorsPro.IRockPaperScissorsBot>();

                // run the MakeMove method (just to see if it's there)
                theBot.MakeMove(new Player("1", null), new Player("2", null), GameRules.Default);

            }
            catch (Exception e)
            {
                // many exceptions will be nested in a TargetInvocationException
                Exception exception = e.InnerException;
                if (exception != null)
                {
                    if (exception.GetType() == typeof(Microsoft.Scripting.SyntaxErrorException))
                    {
                        var se = (Microsoft.Scripting.SyntaxErrorException) exception;

                        String[] lines = theScript.Split(new String[] { System.Environment.NewLine }, StringSplitOptions.None);
                        String codeLine = ((lines.Length >= se.Line) && (se.Line > 0)) ? lines[se.Line - 1] : null;
                        String markerLine = ((codeLine != null) && (se.RawSpan.Start.Column > 0)) ?
                            System.Environment.NewLine + new String(' ', se.RawSpan.Start.Column - 1) + "^" : String.Empty;

                        errList.Add(new BotValidationError(
                            String.Format("Line {0}: {1}", se.Line, se.Message),
                            String.Format("{0}{1}", codeLine, markerLine)));
                    }
                    else
                    {
                        errList.Add(new BotValidationError(exception.Message));
                    }
                }
                else
                {
                    errList.Add(new BotValidationError(e.Message));
                }
            }

            return errList;
        }
        // use reflection in ad hoc AppDomain to verify required interfaces are implemented
        private static IEnumerable<BotValidationError> ValidateDotNetInterfaces(String dllPath)
        {
            var errList = new List<BotValidationError>();
            try
            {
                DynamicAssemblyTypeFinder dynamicAssemblyTypeFinder = new DynamicAssemblyTypeFinder();
                dynamicAssemblyTypeFinder.AddAll(new AssemblyFile[] { new AssemblyFile(dllPath) }.ToList<AssemblyFile>());

                // check that IBotFactory is implemented exactly once
                var botFactoryList = dynamicAssemblyTypeFinder.Create<Compete.Bot.IBotFactory>();
                switch (botFactoryList.Count())
                {
                    case 0: errList.Add(new BotValidationError("IBotFactory interface not implemented"));
                        break;
                    case 1:
                        break;
                    default: errList.Add(new BotValidationError("IBotFactory interface implemented more than once"));
                        break;
                }

                // check that IRockPaperScissorsBot is implemented exactly once
                var botList = dynamicAssemblyTypeFinder.Create<RockPaperScissorsPro.IRockPaperScissorsBot>();
                switch (botList.Count())
                {
                    case 0: errList.Add(new BotValidationError("IRockPaperScissorsBot interface not implemented"));
                        break;
                    case 1:
                        break;
                    default: errList.Add(new BotValidationError("IRockPaperScissorsBot interface implemented more than once"));
                        break;
                }
            }
            catch (Exception e)
            {
                errList.Add(new BotValidationError("ValidateDotNetInterfaces: " + e.Message));
            }

            return errList;
        }