コード例 #1
0
        /// <summary>
        /// Plays word to game gameID
        ///
        /// If Word is null or empty or longer than 30 characters when trimmed, or if GameID
        /// or UserToken is 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>
        public ScoreResponse PlayWord(PlayWord request, string gameID, out HttpStatusCode status)
        {
            //null and length checks
            if (gameID is null || request is null || request.UserToken is null || (request.UserToken = request.UserToken.Trim()).Length == 0 ||
                request.Word is null || (request.Word = request.Word.Trim()).Length == 0 || request.Word.Length > 30)
            {
                status = Forbidden;
                return(null);
            }

            //values used when doing the query then insert into words
            string   player1, player2, board;
            int      timeLimit;
            DateTime?startTime;

            using (SqlConnection conn = new SqlConnection(BoggleDB))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    using (SqlCommand cmd = new SqlCommand("select GameID, Player1, Player2, Board, TimeLimit, StartTime from Games where GameID = @GameID", conn, trans))
                    {
                        cmd.Parameters.AddWithValue("@GameID", gameID);

                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            //Game doesnt exist
                            if (!reader.HasRows)
                            {
                                status = Forbidden;
                                reader.Close();
                                //trans.Commit();
                                return(null);
                            }

                            //only one row
                            reader.Read();

                            //test if pending
                            if (DBNull.Value.Equals(reader["Player2"]))
                            {
                                status = Conflict;
                                reader.Close();
                                trans.Commit();
                                return(null);
                            }

                            player1 = (string)reader["Player1"];
                            player2 = (string)reader["Player2"];

                            board     = reader["Board"]?.ToString();
                            timeLimit = (int)reader["TimeLimit"];
                            startTime = reader.GetDateTime(5);
                        }
                    }


                    //Checks if the player is in the game
                    if (!(player1.Equals(request.UserToken) || player2.Equals(request.UserToken)))
                    {
                        status = Forbidden;
                        trans.Commit();
                        return(null);
                    }

                    //check if the game is compeleted
                    if ((startTime?.AddSeconds(timeLimit) - DateTime.UtcNow)?.TotalMilliseconds < 0)
                    {
                        status = Conflict;
                        trans.Commit();
                        return(null);
                    }

                    //Standardize words to lower case
                    request.Word = request.Word.ToLower();

                    int score = ScoreWord(board, request.Word);

                    //Check if the word has already been played by this player
                    using (SqlCommand cmd = new SqlCommand("select * from Words where Word = @Word and GameID = @GameID and Player = @Player", conn, trans))
                    {
                        cmd.Parameters.AddWithValue("@Word", request.Word);
                        cmd.Parameters.AddWithValue("@GameID", gameID);
                        cmd.Parameters.AddWithValue("@Player", request.UserToken);

                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                score = 0;
                            }
                        }
                    }

                    //add the new word
                    using (SqlCommand cmd = new SqlCommand("insert into Words (Word, GameID, Player, Score) values(@Word, @GameID, @Player, @Score)", conn, trans))
                    {
                        cmd.Parameters.AddWithValue("@Word", request.Word);
                        cmd.Parameters.AddWithValue("@GameID", gameID);
                        cmd.Parameters.AddWithValue("@Player", request.UserToken);
                        cmd.Parameters.AddWithValue("@Score", score);

                        if (cmd.ExecuteNonQuery() != 1)
                        {
                            throw new Exception("Query failed unexpectedly");
                        }

                        trans.Commit();

                        status = OK;
                        return(new ScoreResponse()
                        {
                            Score = score
                        });
                    }
                }
            }
        }
コード例 #2
0
        public score Play(PlayWord Word, string GameID)
        {
            if (Word.Word == null || Word.Word.Trim() == "" || Word.Word.Trim().Length > 30)
            {
                SetStatus(Forbidden);
                return(null);
            }

            using (SqlConnection conn = new SqlConnection(BoggleDB))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    //Checking if UserToken exists in Users table.
                    using (SqlCommand command = new SqlCommand("select UserID from Users where UserID = @UserID", conn, trans))
                    {
                        command.Parameters.AddWithValue("@UserID", Word.UserToken);
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (!reader.HasRows)
                            {
                                SetStatus(Forbidden);
                                trans.Commit();
                                return(null);
                            }
                        }
                    }

                    //Checking if GameID exists in Games table and that user is in the game.
                    using (SqlCommand command = new SqlCommand("select GameID from Games where GameID = @GameID and (Player1 = @UserID or Player2 = @UserID2)", conn, trans))
                    {
                        command.Parameters.AddWithValue("@GameID", GameID);
                        command.Parameters.AddWithValue("@UserID", Word.UserToken);
                        command.Parameters.AddWithValue("@UserID2", Word.UserToken);
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (!reader.HasRows)
                            {
                                SetStatus(Forbidden);
                                trans.Commit();
                                return(null);
                            }
                        }
                    }

                    // Check if game is active or not
                    using (SqlCommand command = new SqlCommand("select * from Games where GameID = @GameID and Player2 is not null", conn, trans))
                    {
                        command.Parameters.AddWithValue("@GameID", GameID);
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (!reader.HasRows)
                            {
                                SetStatus(Conflict);
                                trans.Commit();
                                return(null);
                            }
                            while (reader.Read())
                            {
                                DateTime temp     = (DateTime)reader["StartTime"];
                                int      TimeLeft = (int)reader["TimeLimit"] + (int)(temp.Subtract(DateTime.Now).TotalSeconds);
                                if (TimeLeft <= 0)
                                {
                                    SetStatus(Conflict);
                                    trans.Commit();
                                    return(null);
                                }
                            }
                        }
                    }

                    // Checks if word has been played
                    bool   played = false;
                    string word   = Word.Word.ToUpper();
                    using (SqlCommand command = new SqlCommand("select * from Words where GameID = @GameID and Player = @Player and Word = @Word", conn, trans))
                    {
                        command.Parameters.AddWithValue("@GameID", GameID);
                        command.Parameters.AddWithValue("@Player", Word.UserToken);
                        command.Parameters.AddWithValue("@Word", word);
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                played = true;
                            }
                        }
                    }

                    // Calculates score
                    int score = 0;
                    using (SqlCommand command = new SqlCommand("select * from Games where GameID = @GameID", conn, trans))
                    {
                        command.Parameters.AddWithValue("@GameID", GameID);
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                BoggleBoard board = new BoggleBoard((string)reader["Board"]);
                                if (word.Length < 3)
                                {
                                    score = 0;
                                }
                                else if (played)
                                {
                                    score = 0;
                                }
                                else if (board.CanBeFormed(word) && Legal(word))
                                {
                                    if (word.Length < 5)
                                    {
                                        score = 1;
                                    }
                                    else if (word.Length == 5)
                                    {
                                        score = 2;
                                    }
                                    else if (word.Length == 6)
                                    {
                                        score = 3;
                                    }
                                    else if (word.Length == 7)
                                    {
                                        score = 5;
                                    }
                                    else
                                    {
                                        score = 11;
                                    }
                                }
                                else
                                {
                                    score = -1;
                                }
                            }
                        }
                    }

                    score Score = new score
                    {
                        Score = score
                    };
                    using (SqlCommand command = new SqlCommand("insert into Words (Word, GameID, Player, Score) values(@Word, @GameID, @Player, @Score)", conn, trans))
                    {
                        command.Parameters.AddWithValue("@Word", word);
                        command.Parameters.AddWithValue("@GameID", GameID);
                        command.Parameters.AddWithValue("@Player", Word.UserToken);
                        command.Parameters.AddWithValue("@Score", score);
                        command.ExecuteNonQuery();
                        SetStatus(OK);
                        trans.Commit();
                        return(Score);
                    }
                }
            }
        }