static async System.Threading.Tasks.Task Main(string[] args) { BattleMaster battleMaster = null; IGuiDisplay gui = new ConsoleGui(); string outputFile = null; bool displayBattle = false; Parser.Default.ParseArguments <Options>(args) .WithParsed(o => { var com = new ServerCommunication(); if (o.ListBots) { var enemies = com.GetEnemiesFromServer().Result; gui.ListEnemyBots(enemies); return; } var startProcess = new FileInfo(o.StartProcess); if (startProcess.Exists) { outputFile = o.OutputFile; gui.Initialize(o.EnemyBot, o.MapName); var botProcess = new BotProcess(startProcess, o.Arguments); gui.GetTokenFromServer(); var gameToken = com.StartBattleOnServer(o.EnemyBot, o.MapName).Result; gui.GotToken(gameToken); battleMaster = new BattleMaster(botProcess, com, gameToken, gui); displayBattle = o.showresults; } else { throw new ArgumentException($"Not possible to locate file: {startProcess.FullName}"); } }); if (battleMaster != null) { await battleMaster.Start(); if (outputFile != null) { File.WriteAllLines(outputFile, battleMaster.GetHistory()); gui.SaveBattle(outputFile); } gui.BattleDone(); } if (displayBattle) { } }
private async Task GetNewGameState(ServerCommunication com, TokenDto gameToken) { gui.NewRound(round++); var gameState = await com.GetGameStateFromServer(gameToken); gui.GotNewGameState(gameState); if (gameState == null) { var winner = await com.GetWinnerFromServer(gameToken); gui.WinnerFound(winner); Terminate(); return; } history.Add(gameState); gui.SendGameStateToUserBot(); botProcess.SendGameState(gameState); }
public BattleMaster(BotProcess botProcess, ServerCommunication com, TokenDto gameToken, IGuiDisplay guiDisplayer) { this.botProcess = botProcess; gui = guiDisplayer; this.botProcess.BotReady += () => { gui.UserBotReady(); GetNewGameState(com, gameToken).Wait(); }; this.botProcess.GotMessage += (message) => { if (string.IsNullOrEmpty(message) == false) { gui.GotMessageFromUserBot(message); gui.SendFleet(message); com.SendFleetToServer(gameToken, message).Wait(); GetNewGameState(com, gameToken).Wait(); } }; }