/// <summary> /// It handles the request by the user to create a new game. If the game was /// created, waits for the other player to join. Once joined, terminates the /// Matchmaking process and begins the Boggle game. /// </summary> /// /// <param name="serverName">Name of the server</param> /// <param name="userName">The player name</param> /// <param name="timeLimit">Requested time limit for the game</param> private void HandleGameRequest(string serverName, string userName, int timeLimit) { HandleStatusUpdate("Starting matchmaking..."); // Create a new Matchmaker and allow it to send back updates along the way api = new BoggleApi(serverName, userName, timeLimit); api.StatusUpdate += HandleStatusUpdate; // Initiate the game request int statusCode = api.CreateRequest(); // If the game was "Created", wait for the other player to join. Once the // game is "Accepted", terminate the currently active Matchmaking window and // begin the boggle game. if ((api.HasCreatedGame && statusCode == 201 && !api.cancel) || (api.HasCreatedGame && statusCode == 202 && !api.cancel)) { dynamic game = api.GetGameRequest(); if (statusCode == 202 || game.GameState == "pending") { // Keep refreshing the game waiting for another player to join while (!api.cancel && game.GameState != "active") { game = api.GetGameRequest(); } } // Don't open the game if we are to cancel. if (!api.cancel) { BoggleGame newGame = new BoggleGame(game, api.UserName); window.DoClose(newGame); } } }