private async void CheckIfSomeoneJoinedToGameTimer_Tick(object sender, EventArgs e) { if (_isConnectedToServer) { try { using (HttpClient httpClient = new HttpClient()) { var httpResponse = await httpClient.GetAsync(_baseAddress + "api/values"); var lastContentOfWaitingRoom = httpResponse.Content.ReadAsStringAsync().Result; var index = lastContentOfWaitingRoom.IndexOf('2'); if (index > 0) { var nameOfOpponent = lastContentOfWaitingRoom.Substring(index + 1); Player player = new Player() { Name = nameOfOpponent, JoinToExistingGame = false }; StopAllTimers(); DeletePlayersFromWaitingRoom(); battleForm battleForm = new battleForm(player, playerNameTextBox.Text); battleForm.Show(); this.Hide(); } } } catch (Exception ex) { MessageBox.Show("Błąd połączenia z serwerem", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private async void JoinToGameIfExists(string playerName) { var player = new Player() { Name = playerName, JoinToExistingGame = true }; var stringPlayer = await Task.Run(() => JsonConvert.SerializeObject(player)); var httpContent = new StringContent(stringPlayer, Encoding.UTF8, "application/json"); try { using (HttpClient httpClient = new HttpClient()) { var httpResponse = await httpClient.PostAsync(_baseAddress + "api/values", httpContent); var nameOfOpponent = httpResponse.Content.ReadAsStringAsync().Result; if (nameOfOpponent.Length != 0 && nameOfOpponent.Contains('1')) { var opponentPlayer = new Player() { Name = nameOfOpponent.Replace("1", ""), JoinToExistingGame = true }; StopAllTimers(); battleForm form = new battleForm(opponentPlayer, playerNameTextBox.Text); form.Show(); this.Hide(); } else { MessageBox.Show("Nie znaleziono żadnej gry"); } } } catch (Exception ex) { MessageBox.Show("Błąd połączenia z serwerem", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error); } }