private void raiseFinishGameEvent(string filePath) { string fileContent = null; using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { StreamReader sr = new StreamReader(fs); fileContent = sr.ReadToEnd(); } string[] lines = fileContent.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); int fieldHeight = getHeightFromInitLine(lines.First()); List <GameCommand> commands = new List <GameCommand>(); for (int i = fieldHeight + 1; i < lines.Length; i++) { string line = lines[i]; GameCommand command = BotJournalFileHelper.ParseGameCommand(line); if (command.BotId != null) { commands.Add(command); } } // TODO: detect winner basing on last field state GameFinished?.Invoke(this, new GameFinishedEventArgs(null, commands)); }
public static void Main(string[] args) { Console.WriteLine("Bot competition started"); Assembly a = typeof(Program).Assembly; IEnumerable <Type> botTypes = a.GetExportedTypes().Where(t => t.BaseType == typeof(Bot)); List <Bot> bots = new List <Bot>(); string dirPath = args[0]; string fileName = args[1]; string playerName = args[2]; Dictionary <string, Position> botsPositions = parseBotsPosition(args.Skip(3)); foreach (Type botType in botTypes) { Bot b = (Bot)Activator.CreateInstance(botType); Position position = botsPositions[botType.Name]; b.X = position.X; b.Y = position.Y; bots.Add(b); } Field f = null; Console.WriteLine("Before reading field"); using (FileStream fs = new FileStream(Path.Combine(dirPath, fileName), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { f = BotJournalFileHelper.ReadFieldFromStream(fs); } Console.WriteLine("After reading field"); BotJournalFileWatcher watcher = new BotJournalFileWatcher(dirPath, fileName); Console.WriteLine("Adding handlers"); watcher.FieldEdited += (sender, e) => f = e.NewField; watcher.CommandEdited += (sender, e) => { GameCommand command = e.NewCommand; global::System.Console.WriteLine("Game command playerName - " + command.PlayerName); if (!command.PlayerName.Trim().Equals(playerName) || command.BotId != null) { return; } string[] stepArr = makeStep(ref bots, f); using (FileStream fs = new FileStream(Path.Combine(dirPath, fileName), FileMode.Append, FileAccess.Write, FileShare.ReadWrite)) { writeCommandParams(fs, stepArr); } }; Console.WriteLine("Handlers added"); string lastLine = null; using (FileStream fs = new FileStream(Path.Combine(dirPath, fileName), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { StreamReader sr = new StreamReader(fs); string fileContent = sr.ReadToEnd(); lastLine = fileContent.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Last(); } Console.WriteLine("File read to find last line - '" + lastLine + "'"); string[] splittedLine = lastLine.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); Console.WriteLine("Splitted arr - " + stringArrToString(splittedLine) + " Length - " + splittedLine.Length); if (splittedLine.Length == 1) { Console.WriteLine("Matched splitted line"); if (splittedLine[0].Trim().Equals(playerName)) { string[] step = makeStep(ref bots, f); Console.WriteLine("Step params"); using (FileStream fs = new FileStream(Path.Combine(dirPath, fileName), FileMode.Append, FileAccess.Write, FileShare.ReadWrite)) { writeCommandParams(fs, step); } Console.WriteLine(); } } Console.WriteLine("Starting cycle"); while (true) { } }
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); }