public Game GetGameStatus(string gameID, bool brief)
        {
            string gameStatus = "";
            Game   response   = new Game()
            {
                Player1 = new User(),
                Player2 = new User()
            };

            string playerOne = "";
            string playerTwo = "";

            try
            {
                Convert.ToInt32(gameID);
            } catch (Exception e)
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden);
            }

            using (SqlConnection conn = new SqlConnection(DBConnection))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    // Determine if they are asking for a valid GameID.
                    using (SqlCommand checkValidGameID =
                               new SqlCommand("SELECT * FROM Games WHERE GameID = @GameID", conn, trans))
                    {
                        checkValidGameID.Parameters.AddWithValue("@GameID", gameID);
                        using (SqlDataReader reader = checkValidGameID.ExecuteReader())
                        {
                            reader.Read();
                            // If the reader doesn't have any rows, then the game ID is invalid.
                            if (!reader.HasRows)
                            {
                                throw new HttpResponseException(HttpStatusCode.Forbidden);
                            }
                        }
                    }

                    // Determine if the game they are asking for is just a pending game.
                    using (SqlCommand checkPending =
                               new SqlCommand("SELECT * FROM Games WHERE GameID = @GameID AND Player2 IS NULL",
                                              conn,
                                              trans))
                    {
                        checkPending.Parameters.AddWithValue("@GameID", gameID);
                        using (SqlDataReader readPending = checkPending.ExecuteReader())
                        {
                            // If the reader has rows, that means the game didn't have a second
                            // player, so the game is pending.
                            readPending.Read();
                            if (readPending.HasRows)
                            {
                                response.GameState = "pending";
                                readPending.Close();
                                trans.Commit();
                                return(response);
                            }
                        }
                    }

                    // Compute the status of the game based off of time left.
                    int timeLeft;
                    using (SqlCommand getGameStatus =
                               new SqlCommand("SELECT StartTime, TimeLimit FROM Games WHERE GameID = @GameID",
                                              conn,
                                              trans))
                    {
                        getGameStatus.Parameters.AddWithValue("@GameID", gameID);
                        using (SqlDataReader readTiming = getGameStatus.ExecuteReader())
                        {
                            readTiming.Read();
                            DateTime timeStarted = readTiming.GetDateTime(0);
                            int      timeLimit   = readTiming.GetInt32(1);
                            timeLeft = computeTimeLeft(timeStarted, timeLimit);
                        }
                    }

                    // Set the game status according to how much time is left.
                    gameStatus = timeLeft > 0 ? "active" : "completed";



                    if (gameStatus.Equals("active") && brief)
                    {
                        response.GameState = "active";
                        // Obtain the information needed for a response for a brief response
                        // and an active game.
                        using (SqlCommand briefActiveResponse =
                                   new SqlCommand("SELECT StartTime, TimeLimit, Player1, Player2 FROM Games WHERE GameID = @GameID",
                                                  conn,
                                                  trans))
                        {
                            briefActiveResponse.Parameters.AddWithValue("@GameID", gameID);
                            using (SqlDataReader reader = briefActiveResponse.ExecuteReader())
                            {
                                reader.Read();
                                response.TimeLeft = computeTimeLeft(reader.GetDateTime(0), reader.GetInt32(1));
                                playerOne         = reader.GetString(2);
                                playerTwo         = reader.GetString(3);
                            }
                        }

                        calculatePlayersScore(response, gameID, playerOne, playerTwo, conn, trans);
                    }
                    // Obtain the info necessary to construct a brief response for a completed game.
                    else if (gameStatus.Equals("completed") && brief)
                    {
                        // Set the game state to completed.
                        response.GameState = "completed";

                        // Obtain the necessary information to complete a brief response for a
                        // completed game.
                        using (SqlCommand completedBriefResponse =
                                   new SqlCommand("SELECT Player1, Player2 FROM Games WHERE GameID = @GameID",
                                                  conn,
                                                  trans))
                        {
                            completedBriefResponse.Parameters.AddWithValue("@GameID", gameID);
                            using (SqlDataReader readCompletedBrief = completedBriefResponse.ExecuteReader())
                            {
                                readCompletedBrief.Read();
                                playerOne = readCompletedBrief.GetString(0);
                                playerTwo = readCompletedBrief.GetString(1);
                            }
                        }
                        calculatePlayersScore(response, gameID, playerOne, playerTwo, conn, trans);
                    }
                    else if (gameStatus.Equals("active"))
                    {
                        // Set the game state to active.
                        response.GameState = "active";

                        // Obtain the info needed for a non-brief game response for an active game.
                        using (SqlCommand activeGameInfo =
                                   new SqlCommand("SELECT Board, TimeLimit, StartTime, Player1, Player2 FROM Games WHERE GameID = @GameID",
                                                  conn,
                                                  trans))
                        {
                            activeGameInfo.Parameters.AddWithValue("@GameID", gameID);
                            using (SqlDataReader readGameInfo = activeGameInfo.ExecuteReader())
                            {
                                readGameInfo.Read();
                                response.Board     = readGameInfo.GetString(0);
                                response.TimeLimit = readGameInfo.GetInt32(1);
                                response.TimeLeft  = computeTimeLeft(readGameInfo.GetDateTime(2), response.TimeLimit);

                                // Save the tokens for player one and two so we can access them later.
                                playerOne = readGameInfo.GetString(3);
                                playerTwo = readGameInfo.GetString(4);
                            }
                        }
                        obtainUsername(response, playerOne, playerTwo, conn, trans);
                        calculatePlayersScore(response, gameID, playerOne, playerTwo, conn, trans);
                    }
                    else
                    {
                        // Set the game state to completed.
                        response.GameState = "completed";
                        using (SqlCommand completedGameInfo =
                                   new SqlCommand("SELECT Board, TimeLimit, Player1, Player2 FROM Games WHERE GameID = @GameID",
                                                  conn,
                                                  trans))
                        {
                            completedGameInfo.Parameters.AddWithValue("@GameID", gameID);
                            using (SqlDataReader readCompletedGame = completedGameInfo.ExecuteReader())
                            {
                                // Read in the info from the SQL Query.
                                readCompletedGame.Read();
                                response.Board     = readCompletedGame.GetString(0);
                                response.TimeLimit = readCompletedGame.GetInt32(1);
                                playerOne          = readCompletedGame.GetString(2);
                                playerTwo          = readCompletedGame.GetString(3);
                            }
                        }

                        // Obtain the usernames and scores for the players.
                        obtainUsername(response, playerOne, playerTwo, conn, trans);
                        calculatePlayersScore(response, gameID, playerOne, playerTwo, conn, trans);

                        // Create a new list that will contain all of the words that the players played.
                        response.Player1.WordsPlayed = new List <WordAndScore>();
                        response.Player2.WordsPlayed = new List <WordAndScore>();


                        // Obtain all of the words that each player played.
                        using (SqlCommand getPlayer1Words =
                                   new SqlCommand("SELECT Word, Score FROM Words WHERE Player = @Player1 AND GameID = @GameID",
                                                  conn,
                                                  trans))
                        {
                            getPlayer1Words.Parameters.AddWithValue("@Player1", playerOne);
                            getPlayer1Words.Parameters.AddWithValue("GameID", gameID);
                            using (SqlDataReader readPlayer1Words = getPlayer1Words.ExecuteReader())
                            {
                                // Read in every word that Player1 played then add it to the list.
                                while (readPlayer1Words.Read())
                                {
                                    WordAndScore word = new WordAndScore
                                    {
                                        Word  = readPlayer1Words.GetString(0),
                                        Score = readPlayer1Words.GetInt32(1)
                                    };
                                    response.Player1.WordsPlayed.Add(word);
                                }
                            }
                        }


                        using (SqlCommand getPlayer2Words =
                                   new SqlCommand("SELECT Word, Score FROM Words WHERE Player = @Player2 AND GameID = @GameID",
                                                  conn,
                                                  trans))
                        {
                            getPlayer2Words.Parameters.AddWithValue("@Player2", playerTwo);
                            getPlayer2Words.Parameters.AddWithValue("GameID", gameID);
                            using (SqlDataReader readPlayer2Words = getPlayer2Words.ExecuteReader())
                            {
                                // Read in every word that Player2 played then add it to the list.
                                while (readPlayer2Words.Read())
                                {
                                    WordAndScore word = new WordAndScore
                                    {
                                        Word  = readPlayer2Words.GetString(0),
                                        Score = readPlayer2Words.GetInt32(1)
                                    };
                                    response.Player2.WordsPlayed.Add(word);
                                }
                            }
                        }
                    }
                    // Commit the transaction, regardless of what action was actually taken.
                    trans.Commit();
                }
            }
            return(response);
        }
        public int PutPlayWord([FromUri] string gameID, PutWordRequest request)
        {
            // Check for all of the possible errors that could occur according to the API
            string      UserToken = request.UserToken;
            string      Word      = request.Word.ToUpper();
            BoggleBoard currentBoard;

            // Can't have a null word.
            if (Word == null)
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden);
            }
            // Can't have no word or a word that's too long.
            else if (Word.Trim().Length == 0 || Word.Trim().Length > 30)
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden);
            }

            // Start the process for querying the SQL database.
            using (SqlConnection conn = new SqlConnection(DBConnection))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    //Checks if the User is even registered in Users
                    using (SqlCommand checkValidRegistration = new SqlCommand("SELECT Nickname from Users where UserID = @UserID", conn, trans))
                    {
                        checkValidRegistration.Parameters.AddWithValue("@UserID", UserToken);
                        using (SqlDataReader readResult = checkValidRegistration.ExecuteReader())
                        {
                            readResult.Read();
                            if (!readResult.HasRows)
                            {
                                throw new HttpResponseException(HttpStatusCode.Forbidden);
                            }
                        }
                    }

                    //Checks if the UserID is Player1 or Player2 in a game that matches the GameID and if the game is pending.
                    using (SqlCommand checkUserInGame =
                               new SqlCommand("SELECT Player1, Player2 from Games WHERE GameID = @GameID",
                                              conn,
                                              trans))
                    {
                        checkUserInGame.Parameters.AddWithValue("@GameID", gameID);
                        using (SqlDataReader readResult = checkUserInGame.ExecuteReader())
                        {
                            while (readResult.Read())
                            {
                                // If the reader doesn't have any rows, then the game ID isn't valid.
                                if (!readResult.HasRows)
                                {
                                    throw new HttpResponseException(HttpStatusCode.Forbidden);
                                }
                                // If the user token isn't either player, they can't play a word.
                                else if (!(readResult.GetString(0).Equals(UserToken) || readResult.GetString(1).Equals(UserToken)))
                                {
                                    throw new HttpResponseException(HttpStatusCode.Forbidden);
                                }
                            }
                        }
                    }

                    // Check to see if the game is actually active, completed, or pending.
                    using (SqlCommand getGameStatus =
                               new SqlCommand("SELECT StartTime, TimeLimit FROM Games WHERE GameID = @GameID AND Player2 IS NOT NULL",
                                              conn,
                                              trans))
                    {
                        getGameStatus.Parameters.AddWithValue("@GameID", gameID);
                        using (SqlDataReader readGameStatus = getGameStatus.ExecuteReader())
                        {
                            readGameStatus.Read();
                            // If the reader doesn't have rows, the game is pending, as there isn't a
                            // second player.
                            if (!readGameStatus.HasRows)
                            {
                                throw new HttpResponseException(HttpStatusCode.Conflict);
                            }
                            // If the time left is less than zero, the game is completed and no new
                            // words can be played.
                            int timeLeft = computeTimeLeft(readGameStatus.GetDateTime(0), readGameStatus.GetInt32(1));
                            if (timeLeft < 0)
                            {
                                throw new HttpResponseException(HttpStatusCode.Conflict);
                            }
                        }
                    }

                    //Gets the game's board
                    using (SqlCommand getGameBoard = new SqlCommand("SELECT Board from Games where GameID = @GameID", conn, trans))
                    {
                        getGameBoard.Parameters.AddWithValue("@GameID", gameID);
                        using (SqlDataReader readResult = getGameBoard.ExecuteReader())
                        {
                            readResult.Read();
                            if (!readResult.HasRows)
                            {
                                throw new DatabaseException("It should never hit this since the last SQLCommand checked if the User was in a game. You messed up you twig.");
                            }
                            else
                            {
                                currentBoard = new BoggleBoard(readResult.GetString(0));
                            }
                        }
                    }

                    WordAndScore wordBeingPlayed = new WordAndScore()
                    {
                        Word = Word
                    };

                    // If the word isn't in the dictionary, or can't be played on the board, it's score
                    // should be negative one.
                    // TODO: POSSIBLE SCORING BUG. KEEP AN EYE ON IT
                    if (Word.Length > 2 && (!dictionary.Contains(Word) || !currentBoard.CanBeFormed(Word)))
                    {
                        wordBeingPlayed.Score = -1;
                    }
                    else
                    {
                        // Otherwise, assign a point value to the word based on its length.
                        switch (Word.Length)
                        {
                        case 1:
                        case 2:
                            wordBeingPlayed.Score = 0;
                            break;

                        case 3:
                        case 4:
                            wordBeingPlayed.Score = 1;
                            break;

                        case 5:
                            wordBeingPlayed.Score = 2;
                            break;

                        case 6:
                            wordBeingPlayed.Score = 3;
                            break;

                        case 7:
                            wordBeingPlayed.Score = 5;
                            break;

                        default:
                            wordBeingPlayed.Score = 11;
                            break;
                        }
                    }

                    // Tests to see if the user has already played this word in the current game.
                    using (SqlCommand testIfPlayedWord =
                               new SqlCommand("SELECT Score FROM Words WHERE Player = @Player AND Word = @Word AND GameID = @GameID",
                                              conn,
                                              trans))
                    {
                        testIfPlayedWord.Parameters.AddWithValue("@Player", UserToken);
                        testIfPlayedWord.Parameters.AddWithValue("@Word", Word);
                        testIfPlayedWord.Parameters.AddWithValue("@GameID", gameID);
                        if (testIfPlayedWord.ExecuteScalar() != null)
                        {
                            wordBeingPlayed.Score = 0;
                        }
                    }

                    using (SqlCommand playWord =
                               new SqlCommand("INSERT INTO Words (Word, GameID, Player, Score) VALUES (@Word, @GameID, @Player, @Score)",
                                              conn,
                                              trans))
                    {
                        playWord.Parameters.AddWithValue("@Word", wordBeingPlayed.Word);
                        playWord.Parameters.AddWithValue("@GameID", gameID);
                        playWord.Parameters.AddWithValue("@Player", UserToken);
                        playWord.Parameters.AddWithValue("@Score", wordBeingPlayed.Score);
                        if (playWord.ExecuteNonQuery() != 1)
                        {
                            throw new DatabaseException("Failed to add the played word to the database.");
                        }
                    }
                    trans.Commit();
                    return(wordBeingPlayed.Score);
                }
            }
        }