/// <summary> /// Using the previously obtained GameID, waits for that game to start up. Once the game is /// no longer pending, sets up the view accordingly. /// </summary> /// <returns>Returns a Task with no return type so this method can be awaited</returns> private async Task StartGame() { try { using (HttpClient client = CreateClient(DesiredServer)) { view.SetUpControlsWhileWaitingForGame(); // Setup all the stuff necessary to query the server. tokenSource = new CancellationTokenSource(); dynamic body = new ExpandoObject(); body.GameID = GameID; StringContent content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); string gamesURI = DesiredServer + "/BoggleService/games/" + GameID + "/false"; // Query the server HttpResponseMessage response = await client.GetAsync(gamesURI, tokenSource.Token); // Parse the server's response. string responseAsString = await response.Content.ReadAsStringAsync(); GetGameStatus responseBody = JsonConvert.DeserializeObject <GetGameStatus>(responseAsString); if (responseBody.GameState.Equals("pending")) { await CheckGameStatus(); } // Requery the server for the new information. response = await client.GetAsync(gamesURI, tokenSource.Token); responseAsString = await response.Content.ReadAsStringAsync(); responseBody = JsonConvert.DeserializeObject <GetGameStatus>(responseAsString); // Parse all the returned things. Board = responseBody.Board; view.SetTimeLimit(responseBody.TimeLimit); view.SetUpBoard(Board.ToString()); if (ArePlayerOne) { view.SetOpponentNickname(responseBody.Player2.Nickname); } else { view.SetOpponentNickname(responseBody.Player1.Nickname); } view.EnableTimer(true); InAGame = true; view.SetUpControlsInGame(); } } catch (Exception e) { if (e is TaskCanceledException) { view.SetUpControlsAfterRegister(); return; } } }
/// <summary> /// Method called by the timer every second that updates the view to contain the most current /// information about the current state of the game. /// </summary> private async void UpdateBoard() { try { using (HttpClient client = CreateClient(DesiredServer)) { // Setup the stuff necessary to query the server. tokenSource = new CancellationTokenSource(); dynamic body = new ExpandoObject(); body.GameID = GameID; StringContent content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); string gamesURI = DesiredServer + "/BoggleService/games/" + GameID + "/false"; // Check the server every second until the game is completed. // Query the server HttpResponseMessage response = await client.GetAsync(gamesURI, tokenSource.Token); string responseAsString = await response.Content.ReadAsStringAsync(); GetGameStatus responseBody = JsonConvert.DeserializeObject <GetGameStatus>(responseAsString); // If the game is completed, finish up the game if (responseBody.GameState.Equals("completed")) { FinishGame(responseBody); return; } // Otherwise, update the view's elements to match the newly received information. view.SetRemainingTime(responseBody.TimeLeft); // Depending on if you are or aren't player one, set's the scores accordingly. if (ArePlayerOne) { view.SetPlayerScore(responseBody.Player1.Score.ToString()); view.SetOpponentScore(responseBody.Player2.Score.ToString()); } else { view.SetPlayerScore(responseBody.Player2.Score.ToString()); view.SetOpponentScore(responseBody.Player1.Score.ToString()); } } } catch (Exception e) { if (!(e is TaskCanceledException)) { view.ShowMessage("A server side error has occurred. Please try again."); } } }
/// <summary> /// Finishes the game by disabling the timer and displaying each player's list of fina words /// played as well as each word's associated score. /// </summary> /// <param name="responseBody"></param> private void FinishGame(GetGameStatus responseBody) { Player player1 = responseBody.Player1; Player player2 = responseBody.Player2; view.Reset(); view.SetUpControlsAfterRegister(); InAGame = false; if (player1.WordsPlayed == null) { player1.WordsPlayed = new WordAndScore[0]; } if (player2.WordsPlayed == null) { player2.WordsPlayed = new WordAndScore[0]; } view.EnableTimer(false); view.DisplayFinalScore(player1.Nickname, player2.Nickname, player1.Score.ToString(), player2.Score.ToString(), player1.WordsPlayed, player2.WordsPlayed); }
/// <summary> /// Repeatedly checks the game's status until the user either cancel's their request or the /// game is no longer pending. /// </summary> /// <returns></returns> private async Task CheckGameStatus() { try { using (HttpClient client = CreateClient(DesiredServer)) { tokenSource = new CancellationTokenSource(); bool isPending = true; //view.EnableControls(false); dynamic body = new ExpandoObject(); body.GameID = GameID; StringContent content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); string gamesURI = DesiredServer + "/BoggleService/games/" + GameID + "/true"; while (isPending) { HttpResponseMessage response = await client.GetAsync(gamesURI, tokenSource.Token); string responseAsString = await response.Content.ReadAsStringAsync(); GetGameStatus responseAsObject = JsonConvert.DeserializeObject <GetGameStatus>(responseAsString); if (responseAsObject.GameState.Equals("active")) { isPending = false; } } } } catch (Exception e) { if (e is TaskCanceledException) { view.ShowMessage("Game canceled."); } } }