private static async Task Main(string[] args) { /* TODO: Replace this url with config. */ var url = "https://localhost:44390/runnerhub"; var connection = new HubConnectionBuilder() .WithUrl($"{url}") .WithAutomaticReconnect() .Build(); var botService = new BotService(); await connection.StartAsync().ContinueWith(task => { /* Clients should disconnect from the server when the server sends the request to do so. */ connection.On <Guid>("Disconnect", (Id) => { Console.WriteLine("Disconnected:"); connection.StopAsync(); }); /* Get the current WorldState along with the last known state of the current client. */ connection.On <GameState, GameObject>("ReceiveGameState", (gameState, bot) => { Console.WriteLine("Current ConnectionId: " + bot.Id); botService.SetBot(bot); botService.SetGameState(gameState); foreach (var _bot in gameState.GameObjects.Where(o => o.ObjectType == ObjectTypes.Player)) { Console.WriteLine(String.Format("Id - {0}, PositionX - {1}, PositionY - {2}, Size - {3}", _bot.Id, _bot.Position.X, _bot.Position.Y, _bot.Size)); } }); while (true) { /* This sleep is important to sync the two calls below. */ Thread.Sleep(1000); var bot = botService.GetBot(); if (bot == null) { return; } connection.InvokeAsync("RequestGameState"); /* TODO: Add bot logic here between RequestWorldState and SendClientAction, use SetClient to form object to be sent to Runner. */ botService.SetPlayerAction(botService.GetPlayerAction()); connection.InvokeAsync("SendPlayerAction", botService.GetPlayerAction()); } }); }
private static void ConfigureServices() { var context = new DictionaryContext(); context.Database.Migrate(); IUnitOfWork unitOfWork = new UnitOfWork(context); _dictionaryService = new DictionaryService(unitOfWork); _bot = BotService.GetBot(); _manager = new CommandManager(_bot, _dictionaryService); }
private static async Task Main(string[] args) { // Set up configuration sources. var builder = new ConfigurationBuilder().AddJsonFile($"appsettings.json", optional: false); Configuration = builder.Build(); var registrationToken = Environment.GetEnvironmentVariable("Token"); var environmentIp = Environment.GetEnvironmentVariable("RUNNER_IPV4"); var ip = !string.IsNullOrWhiteSpace(environmentIp) ? environmentIp : Configuration.GetSection("RunnerIP").Value; ip = ip.StartsWith("http://") ? ip : "http://" + ip; var port = Configuration.GetSection("RunnerPort"); var url = ip + ":" + port.Value + "/runnerhub"; var connection = new HubConnectionBuilder() .WithUrl($"{url}") .ConfigureLogging(logging => { logging.SetMinimumLevel(LogLevel.Debug); }) .WithAutomaticReconnect() .Build(); var botService = new BotService(); await connection.StartAsync() .ContinueWith( task => { Console.WriteLine("Connected to Runner"); /* Clients should disconnect from the server when the server sends the request to do so. */ connection.On <Guid>( "Disconnect", (id) => { Console.WriteLine("Disconnected:"); connection.StopAsync(); connection.DisposeAsync(); }); connection.On <Guid>( "Registered", (id) => { Console.WriteLine("Registered with the runner"); botService.SetBot( new GameObject { Id = id, GameObjectType = ObjectTypes.Player, Position = new Position { X = 0, Y = 0 }, Size = 10 }); }); /* Get the current WorldState along with the last known state of the current client. */ connection.On <GameStateDto>( "ReceiveGameState", (gameStateDto) => { var gameState = new GameState { World = null, GameObjects = new List <GameObject>(), PlayerGameObjects = new List <GameObject>() }; gameState.World = gameStateDto.World; foreach ((var id, List <int> state) in gameStateDto.GameObjects) { gameState.GameObjects.Add(GameObject.FromStateList(Guid.Parse(id), state)); } foreach ((var id, List <int> state) in gameStateDto.PlayerObjects) { gameState.PlayerGameObjects.Add(GameObject.FromStateList(Guid.Parse(id), state)); } botService.SetGameState(gameState); }); var token = Environment.GetEnvironmentVariable("REGISTRATION_TOKEN"); token = !string.IsNullOrWhiteSpace(token) ? token : Guid.NewGuid().ToString(); Thread.Sleep(1000); Console.WriteLine("Registering with the runner..."); connection.SendAsync("Register", token, "NetNickName"); while (connection.State == HubConnectionState.Connected) { Thread.Sleep(30); Console.WriteLine($"ConState: {connection.State}"); Console.WriteLine($"Bot: {botService.GetBot()?.Id.ToString()}"); var bot = botService.GetBot(); if (bot == null) { continue; } botService.ComputeNextPlayerAction(botService.GetPlayerAction()); connection.InvokeAsync("SendPlayerAction", botService.GetPlayerAction()); } });