/// <summary> /// Determines how to recieve the incoming request and what type of object needs to be created. /// </summary> /// <param name="ss"></param> /// <param name="contentLength"></param> /// <param name="methodName"></param> private string GetObject(String s) { switch (methodName) { // Used for creating a user. case "CreateUser": UserInfo user = JsonConvert.DeserializeObject <UserInfo>(s); UserTokenObject token = service.CreateUser(user); string result = JsonConvert.SerializeObject(token, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); return(result); // Used for joing a game. case "JoinGame": JoinGameInfo info = JsonConvert.DeserializeObject <JoinGameInfo>(s); GameiD id = service.JoinGame(info); string gID = JsonConvert.SerializeObject(id, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); return(gID); // Used for cancelling a join request. case "CancelJoinRequest": Cancel cancel = JsonConvert.DeserializeObject <Cancel>(s); service.CancelJoinRequest(cancel); return(""); // Used for playing a word case "PlayWord": WordCheck word = JsonConvert.DeserializeObject <WordCheck>(s); WordScore score = service.PlayWord(gameID, word); string wordPlayed = JsonConvert.SerializeObject(score, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); return(wordPlayed); default: return(""); } }
/// <summary> /// Play a word in a game /// /// If Word is null or empty when trimmed, or if GameID or /// UserToken is missing or invalid, or if UserToken is not a /// player in the game identified by GameID, responds with response /// code 403 (Forbidden). /// /// Otherwise, if the game state is anything other than "active", /// responds with response code 409 (Conflict). /// /// Otherwise, records the trimmed Word as being played by UserToken /// in the game identified by GameID. Returns the score for Word in the /// context of the game (e.g. if Word has been played before the score is zero). /// Responds with status 200 (OK). Note: The word is not case sensitive. /// </summary> /// <param name="gameID"></param> /// <param name="UserToken"></param> /// <param name="Word"></param> /// <returns></returns> public WordScore PlayWord(string gameID, WordCheck info) { try { string word = info.Word.Trim(); string userToken = info.UserToken; BoggleBoard PlayBoard; string status1; int score = 0; bool canPlay; DateTime time; int TimeLimit; if (word.Length == 0 || userToken == null || gameID == null) { SetStatus(Forbidden); return(null); } if (!(new Regex(@"\d+").IsMatch(gameID))) { SetStatus(Forbidden); return(null); } //Creates a sql connection using (SqlConnection conn = new SqlConnection(BoggleDB)) { //open connection conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { using (SqlCommand command = new SqlCommand("select GameStatus, StartTime, TimeLimit from Games where GameID = @GameID", conn, trans)) { command.Parameters.AddWithValue("@GameID", gameID); using (SqlDataReader reader = command.ExecuteReader()) { //added check to make sure game was in table if (!reader.HasRows) { SetStatus(Forbidden); return(null); } reader.Read(); status1 = reader.GetString(0); // else we get the time the game started and the calculated time limit from the 2 players. time = reader.GetDateTime(1); TimeLimit = (int)reader.GetValue(2); double elapsedTime = Math.Round((DateTime.Now.TimeOfDay.TotalSeconds - time.TimeOfDay.TotalSeconds), 0); // If the elapsed time is greater than or equal to the time limit, the game is marked as completed so no // Actions can be made to the game. if (elapsedTime >= (double)TimeLimit) { SetStatus(Conflict); status1 = "completed"; } } } } using (SqlTransaction trans = conn.BeginTransaction()) { // Gets thge players and board based on the given GameID. using (SqlCommand command = new SqlCommand("select Player1, Player2, Board, GameStatus from Games where GameID = @GameID", conn, trans)) { string P1; string P2; string status; command.Parameters.AddWithValue("@GameID", gameID); using (SqlDataReader reader = command.ExecuteReader()) { if (!reader.HasRows) { SetStatus(Forbidden); return(null); } reader.Read(); P1 = reader.GetString(0); P2 = reader.GetString(1); status = reader.GetString(3); if (!P1.Equals(userToken) && !P2.Equals(userToken)) { SetStatus(Forbidden); return(null); } if (status1 != "active") { SetStatus(Conflict); return(null); } PlayBoard = new BoggleBoard(reader.GetString(2)); // Checks to make sure P1 and P2 are in the game. if (P1 == null || P2 == null) { SetStatus(Forbidden); return(null); } // If game status is Active, sets a bool that allows up to play a word. If it is anything but Active, // we can't play a word. if (status == "active") { canPlay = true; } else { canPlay = false; SetStatus(Conflict); } } } // If we can play a word, we go in to the if statement. If not, we set the status to 409 - Conflict and return null if (canPlay) { // Checks to make sure the word can be formed on the game board. if (PlayBoard.CanBeFormed(word)) { string scoreString; // Sets the score initially to the word score from the dictionary. Can be changed below if the word has already been played by // the player. if (dictionary.TryGetValue(word.ToUpper(), out scoreString)) { int.TryParse(scoreString, out score); // Gets all the words played by a player in a game. using (SqlCommand command2 = new SqlCommand("select Word from Words where GameID = @GameID and Player = @Player", conn, trans)) { command2.Parameters.AddWithValue("@GameID", gameID); command2.Parameters.AddWithValue("@Player", userToken); using (SqlDataReader reader1 = command2.ExecuteReader()) { // Reads each row/word that a player has played for a game. while (reader1.Read()) { // If the word has been played, score is updated to 0. if (reader1.GetString(0) == word) { score = 0; break; } } } } } } // If the word can't be formed on the board or is an invalid word, score is set to -1. else { score = -1; } // Inserts the word into the Words table. The word is accociated with a GameID, Player, and score. using (SqlCommand command3 = new SqlCommand("Insert into Words (Word, GameID, Player, Score) values (@Word, @GameID, @Player, @Score)", conn, trans)) { command3.Parameters.AddWithValue("@Word", word); command3.Parameters.AddWithValue("@GameID", gameID); command3.Parameters.AddWithValue("@Player", userToken); command3.Parameters.AddWithValue("@Score", score); if (command3.ExecuteNonQuery() != 1) { SetStatus(BadRequest); return(null); } } SetStatus(OK); //comit transaction and return the usertoken trans.Commit(); // Returns a WordScore object that reflect the score of the word a player just played. return(new WordScore { Score = score.ToString() }); } // Only gets done when the game status is anything other than Active. { SetStatus(Conflict); return(null); } } } } catch (Exception) { SetStatus(Conflict); return(null); } }