private async void BtnRollDice_Click(object sender, EventArgs e) { int[] rolls = await DiceController.RollDice(this, client); DiceController.PopulateRolls(rolls, this); GameController.UpdateGameState(this); }
internal static async Task <int[]> RollDice(FormMainPage form, HttpClient client) { DiceController dice = await GetDiceAsync("https://rolz.org/api/?2d6.json", form, client); if (dice != null) { int[] diceValues = new int[2]; diceValues[0] = Convert.ToInt32(Regex.Match(dice.details, @"(?<=\()\d").ToString()); diceValues[1] = Convert.ToInt32(Regex.Match(dice.details, @"(?<=\+)\d").ToString()); return(diceValues); } else { MessageBox.Show("Connection Failed. Generating Rolls Locally"); Random rnd = new Random(); return(new int[] { rnd.Next(1, 7), rnd.Next(1, 7) }); } }
private static async Task <DiceController> GetDiceAsync(string path, FormMainPage form, HttpClient httpClient) { try { DiceController dice = null; HttpResponseMessage response = await httpClient.GetAsync(path); if (response.IsSuccessStatusCode) { dice = await response.Content.ReadAsAsync <DiceController>(); } return(dice); } catch (Exception e) { MessageBox.Show(e.Message); return(null); } }