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