Пример #1
0
        public void RunCode(string[] firstPlayerCode, string[] secondPlayerCode, string pathToField, GameFinishType finishType, IEnumerable <BLL.Models.Bot> bots1, IEnumerable <BLL.Models.Bot> bots2, int finalX = 0, int finalY = 0)
        {
            CompilationResult compResult1 = compiler.CompileCode(TaskParameters.Build(firstPlayerCode.Length), firstPlayerCode);
            CompilationResult compResult2 = compiler.CompileCode(TaskParameters.Build(secondPlayerCode.Length), secondPlayerCode);

            if (!compResult1.IsCodeCorrect || !compResult2.IsCodeCorrect)
            {
                throw new ArgumentException("Unable to compile code");
            }

            FieldBuilder fieldBuilder = new FieldBuilder(pathToField);

            Runner.CodeRunners.Models.Field field = fieldBuilder.GetFieldForRunner();
            field = fieldBuilder.PlaceBots(field, bots1.Select(b => new Models.Bot(b.X, b.Y, b.Name)), 1);
            field = fieldBuilder.PlaceBots(field, bots2.Select(b => new Models.Bot(b.X, b.Y, b.Name)), 2);

            FinishGameCondition finishCondition = null;

            if (finishType == GameFinishType.CommandsNumber)
            {
                finishCondition = new CommandNumberCondition();
            }
            else
            {
                finishCondition = new BotOnPointCondition(finalX, finalY);
            }

            IEnumerable <Runner.CodeRunners.Models.Bot> runnerBots1 = bots1.Select(mapper.Map <Runner.CodeRunners.Models.Bot>);
            IEnumerable <Runner.CodeRunners.Models.Bot> runnerBots2 = bots2.Select(mapper.Map <Runner.CodeRunners.Models.Bot>);

            runner.RunCodeGame(compResult1.InformationForCodeRunner, compResult2.InformationForCodeRunner, field, runnerBots1, runnerBots2, finishCondition);

            runner.GameFinished += Runner_GameFinished;
        }
Пример #2
0
        public string RunCodeGame(RunnerInformation player1Info, RunnerInformation player2Info, Field field, IEnumerable <Bot> bots1, IEnumerable <Bot> bots2, FinishGameCondition finishCondition)
        {
            CSharpRunnerInformation csRunInfo1 = player1Info as CSharpRunnerInformation;
            CSharpRunnerInformation csRunInfo2 = player2Info as CSharpRunnerInformation;

            this.verifyRunParameters(csRunInfo1, csRunInfo2);

            string dirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CompilerTempFiles");

            string fileName = generateUniqueFileName(dirPath);

            using (FileStream fs = new FileStream(Path.Combine(dirPath, fileName), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                BotJournalFileHelper.WriteFieldToFile(fs, field);

                StreamWriter sw = new StreamWriter(fs);
                sw.Write($" { csRunInfo1.PlayerName } ;");
                sw.Flush();

                BotJournalFileWatcher botFileWatcher = new BotJournalFileWatcher(dirPath, fileName);

                Process player1Process = null, player2Process = null;

                botFileWatcher.CommandEdited += (sender, e) =>
                {
                    GameCommand command = e.NewCommand;

                    Console.WriteLine("File changed");

                    if (command.BotId == null)
                    {
                        return;
                    }

                    Console.WriteLine("user wrote full command");

                    try
                    {
                        IActionHandler actionHandler = ActionHandlersProvider.GetActionHandler(command.ActionType);
                        Field          newField      = actionHandler.ApplyStep(command.StepParams, field);

                        if (!newField.Equals(field))
                        {
                            field       = newField;
                            fs.Position = 0;
                            BotJournalFileHelper.WriteFieldToFile(fs, field);
                        }
                    }
                    catch (ArgumentException)
                    { }

                    using (FileStream s = new FileStream(Path.Combine(dirPath, fileName), FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                    {
                        sw = new StreamWriter(s);

                        if (command.PlayerName.Trim().Equals(player1Info.PlayerName))
                        {
                            sw.Write($" { player2Info.PlayerName } ;");
                        }
                        else
                        {
                            sw.Write($" { player1Info.PlayerName } ;");
                        }
                        sw.Flush();
                    }

                    if (finishCondition.IsFinished(field, botFileWatcher.CommandCount))
                    {
                        player1Process.Kill();
                        player2Process.Kill();

                        raiseFinishGameEvent(Path.Combine(dirPath, fileName));

                        botFileWatcher.Dispose();
                    }

                    Console.WriteLine("user command processed");
                };

                player1Process = Process.Start(csRunInfo1.PathToExecutable, $" { dirPath } { fileName } { csRunInfo1.PlayerName } { generateStringParameterForBots(bots1) }");

                player2Process = Process.Start(csRunInfo2.PathToExecutable, $" { dirPath } { fileName } { csRunInfo2.PlayerName } { generateStringParameterForBots(bots2) } ");
            }

            return(string.Empty);
        }