Ejemplo n.º 1
0
        private void verifyRunParameters(CSharpRunnerInformation csRunInfo1, CSharpRunnerInformation csRunInfo2)
        {
            if (csRunInfo1 == null || csRunInfo2 == null)
            {
                throw new ArgumentNullException("Runner Information cannot be null.");
            }

            if (csRunInfo1.PlayerName == csRunInfo2.PlayerName)
            {
                throw new ArgumentException("Player names must be different");
            }
        }
Ejemplo n.º 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);
        }