Esempio n. 1
0
        private static async Task OpenGameConnection()
        {
            var httpType = _settings.UseHttps ? "https" : "http";

            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Add("AdminPassword", _settings.ServerAdminPassword);

            chaosApiClient = new ChaosApiClient($"{httpType}://{_settings.HostingApiUrl}", httpClient);

            var hostingHubUrl = $"{httpType}://{_settings.HostingApiUrl}/hostinghub";

            _hubConnection = new HubConnectionBuilder()
                             .WithUrl(hostingHubUrl)
                             .WithAutomaticReconnect()
                             .Build();

            int attempts = 0;

            while (attempts < _apiAttempts)
            {
                try
                {
                    await _hubConnection.StartAsync();

                    break;
                }
                catch (Exception)
                {
                    attempts++;

                    if (attempts == _apiAttempts)
                    {
                        throw;
                    }

                    Console.WriteLine($"An error occurred while connecting to the hosting servers message hub, attempt number: {attempts} of {_apiAttempts}, retrying in 5s.");
                    await Task.Delay(5000);
                }
            }

            _hubConnection.On("newVoteResult", (string chaosId) =>
            {
                Console.WriteLine("New vote result received");

                lock (_lock)
                {
                    File.WriteAllText(Path.Combine(_settings.VoteFilesDirectory, "vote.txt"), chaosId);
                }
            });

            await _hubConnection.SendAsync("RegisterAsHost", _roomCode, _settings.RoomPassword, _settings.ServerAdminPassword);
        }
Esempio n. 2
0
        private static async Task SetupGame()
        {
            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Add("AdminPassword", _settings.ServerAdminPassword);

            var httpType = _settings.UseHttps ? "https" : "http";

            var chaosClient = new ChaosApiClient($"{httpType}://{_settings.HostingApiUrl}", httpClient);

            _roomCode = CreateRoomCode();

            int attempts = 0;

            while (attempts < _apiAttempts)
            {
                try
                {
                    await chaosClient.CreateGameAsync(_roomCode, _settings.RoomPassword);

                    break;
                }
                catch (Exception)
                {
                    attempts++;

                    if (attempts == _apiAttempts)
                    {
                        throw;
                    }

                    Console.WriteLine($"An error occurred while creating the game on the API, attempt number: {attempts} of {_apiAttempts}, retrying in 5s.");
                    await Task.Delay(5000);
                }
            }


            await File.WriteAllTextAsync(@"C:\chaos\roomCode.txt", _roomCode);
        }