/// <summary> /// Constructs a controller with a window and a game. Draw /// the GUI based on the data gathered and received. Also, /// registers an event handler that performs necessary tasks /// once the user has done viewing results (Close window and /// open Matchmaking form for a new game). /// </summary> public ResultsController(IResultsForm window, BoggleGame game) { this.window = window; this.game = game; window.DoneViewingResults += HandleCloseResults; DrawUI(); }
/// <summary> /// Is called every second while the game is active and /// refreshes the UI. /// </summary> public void HandleRefreshGameEvent() { dynamic response = game.RefreshGame(); game = new BoggleGame(response, homePlayerName); UpdateUI(); }
/// <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); } } }
/// <summary> /// Hides this form, resets its components and opens a new /// Boggle game window. Thread Safe. /// </summary> /// <param name="game">Contains the state of the current game</param> public void DoClose(BoggleGame game) { Invoke((Action)(() => { BoggleForm gui = new BoggleForm(); BoggleController gameController = new BoggleController(gui, game); gui.Present(); Hide(); ResetUI(); })); }
/// <summary> /// Closes this form. /// </summary> public void FinishGame(BoggleGame game) { ResultsForm gui = new ResultsForm(); ResultsController resultsController = new ResultsController(gui, game); Invoke((Action)(() => { Hide(); gui.Present(); })); }
/// <summary> /// Constructs a game with a form and a game /// </summary> /// <param name="window">The interface for the boggle game</param> /// <param name="game">The game which corresponds to the GUI</param> public BoggleController(IBoggleForm window, BoggleGame game) { this.window = window; this.window.PlayWordEvent += HandlePlayWordEvent; this.window.OpenMatchMakerEvent += HandleOpenMatchmakerEvent; this.window.ShowResultsEvent += HandleResults; this.window.RefreshGameEvent += HandleRefreshGameEvent; this.game = game; homePlayerName = game.HomePlayer.Nickname; // Initialize the UI. UpdateUI(); }