/// <summary>
        /// play a word given the GameID and the word object
        /// </summary>
        /// <param name="GameID"></param>
        /// <param name="word"></param>
        /// <returns></returns>
        public WordScore playWord(string GameID, PlayedWord word, out HttpStatusCode httpStatus)
        {
            httpStatus = OK;
            Words     newWord = new Words();
            WordScore pw      = new WordScore();


            GameInfo pg = new GameInfo();
            GameInfo g  = new GameInfo();

            if (String.IsNullOrEmpty(word.Word) || String.IsNullOrEmpty(GameID) || word.Word.Trim().Length > 30)
            {
                httpStatus = Forbidden;
                pw.Score   = -1;
                return(pw);
            }

            try
            {
                pendingGames.TryGetValue(GameID.ToString(), out pg);
                if (pg.GameState.Equals("pending"))
                {
                    httpStatus = Conflict;
                    pw.Score   = -1;
                    return(pw);
                }
            }
            catch (Exception)
            {
            }



            games.TryGetValue(GameID.ToString(), out g);


            if (!g.GameState.Equals("active"))
            {
                httpStatus = Conflict;
                pw.Score   = -1;
                return(pw);
            }

            if (getTimeLeft(g) <= 0)
            {
                httpStatus = Conflict;
                pw.Score   = 0;
                return(pw);
            }

            if (!word.UserToken.Equals(g.Player1.UserToken.ToString()) && !word.UserToken.Equals(g.Player2.UserToken.ToString()))
            {
                httpStatus = Forbidden;
                pw.Score   = 0;
                return(pw);
            }

            if (new BoggleBoard(g.Board).CanBeFormed(word.Word.Trim()))
            {
                if (word.UserToken.Equals(g.Player1.UserToken))
                {
                    foreach (var w in g.Player1.WordsPlayed)
                    {
                        if (w.Word.Equals(word.Word.Trim()))
                        {
                            newWord.Score = 0;
                            newWord.Word  = word.Word.Trim();
                            g.Player1.WordsPlayed.Add(newWord);
                            httpStatus = OK;
                            pw.Score   = newWord.Score;
                            return(pw);
                        }
                    }
                    if (WordValid(word.Word))
                    {
                        newWord.Score = ScoreWord(word.Word);
                        newWord.Word  = word.Word.Trim();
                        g.Player1.WordsPlayed.Add(newWord);
                        g.Player1.Score += newWord.Score;
                        httpStatus       = OK;
                        pw.Score         = newWord.Score;
                        return(pw);
                    }
                }

                else
                {
                    foreach (var w in g.Player2.WordsPlayed)
                    {
                        if (w.Word.Equals(word.Word.Trim()))
                        {
                            newWord.Score = 0;
                            newWord.Word  = word.Word.Trim();
                            g.Player2.WordsPlayed.Add(newWord);
                            httpStatus = OK;
                            pw.Score   = newWord.Score;
                            return(pw);
                        }
                    }
                    if (WordValid(word.Word))
                    {
                        newWord.Score = ScoreWord(word.Word);
                        newWord.Word  = word.Word.Trim();
                        g.Player2.WordsPlayed.Add(newWord);
                        g.Player2.Score += newWord.Score;
                        httpStatus       = OK;
                        pw.Score         = newWord.Score;
                        return(pw);
                    }
                }
            }
            else
            {
                if (word.UserToken.Equals(g.Player1.UserToken))
                {
                    newWord.Score = ScoreWord(word.Word);
                    newWord.Word  = word.Word.Trim();
                    g.Player1.WordsPlayed.Add(newWord);
                    g.Player1.Score += newWord.Score;
                    httpStatus       = OK;
                    pw.Score         = newWord.Score;
                    return(pw);
                }
                else
                {
                    newWord.Score = ScoreWord(word.Word);
                    newWord.Word  = word.Word.Trim();
                    g.Player2.WordsPlayed.Add(newWord);
                    g.Player2.Score += newWord.Score;
                    httpStatus       = OK;
                    pw.Score         = newWord.Score;
                    return(pw);
                }
            }


            return(pw);
        }
Пример #2
0
        /// <summary>
        /// Helper method that generates a Game object specific to the status of a game, and if brief was supplied in the URL or not.
        /// </summary>
        public Game GenerateGameStatus(SqlConnection conn, SqlTransaction trans, string status, string brief, string GameID, double elapsedTime)
        {
            // This method is a little messy and there is a lot going on but everything appears to be working correctly.


            UserInfo player1 = new UserInfo();
            UserInfo player2 = new UserInfo();
            // These are the ID's for the players of a game
            string p1ID;
            string p2ID;
            // The temporary game we will be returning.
            Game   temp = new Game();
            string board;
            int    timeLimit;
            double timeRemaing;
            string P1Nickname;
            string P2Nickname;

            // Initializing the words played for ewach player
            player1.WordsPlayed = new List <Words>();
            player2.WordsPlayed = new List <Words>();


            // To start, we pull the players, borad, and time limit of a game. we set the variables above to these values.
            using (SqlCommand command = new SqlCommand("select Player1, Player2, Board, TimeLimit from Games where GameID = @GameID",
                                                       conn, trans))
            {
                command.Parameters.AddWithValue("@GameID", GameID);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    reader.Read();

                    p1ID        = reader.GetString(0);
                    p2ID        = reader.GetString(1);
                    board       = reader.GetString(2);
                    timeLimit   = (int)reader.GetValue(3);
                    timeRemaing = timeLimit - elapsedTime;
                }
            }

            // Not very good at writing queries so had to make multipul for similar operations. This one and the one
            // below it just get the nicknames for each player
            using (SqlCommand command = new SqlCommand("select Nickname from Users where UserID = @P1",
                                                       conn, trans))
            {
                command.Parameters.AddWithValue("@P1", p1ID);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    reader.Read();

                    P1Nickname = reader.GetString(0);
                }
            }

            using (SqlCommand command = new SqlCommand("select Nickname from Users where UserID = @P2",
                                                       conn, trans))
            {
                command.Parameters.AddWithValue("@P2", p2ID);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    reader.Read();

                    P2Nickname = reader.GetString(0);
                }
            }


            // The following 2 queries generate the list of words and their scores that the player has played.
            // This first one is for player 1, and the next one is for player 2.
            using (SqlCommand command = new SqlCommand("select Word, Score from Words where GameID = @GameID and Player = @Player",
                                                       conn, trans))
            {
                command.Parameters.AddWithValue("@GameID", GameID);
                command.Parameters.AddWithValue("@Player", p1ID);

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    int score = 0;
                    while (reader.Read())
                    {
                        // create a new Word object
                        Words word = new Words();
                        //Set the word and score
                        word.Word  = reader.GetString(0);
                        word.Score = (int)reader.GetValue(1);
                        //add the word to player 1's list of words.
                        player1.WordsPlayed.Add(word);
                        // Update the players overall score.
                        score += word.Score;
                    }
                    player1.Score = score;
                }
            }

            // This is for player 2.
            using (SqlCommand command = new SqlCommand("select Word, Score from Words where GameID = @GameID and Player = @Player",
                                                       conn, trans))
            {
                command.Parameters.AddWithValue("@GameID", GameID);
                command.Parameters.AddWithValue("@Player", p2ID);

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    int score = 0;
                    while (reader.Read())
                    {
                        Words word = new Words();
                        word.Word  = reader.GetString(0);
                        word.Score = (int)reader.GetValue(1);
                        player2.WordsPlayed.Add(word);
                        score += word.Score;
                    }
                    player2.Score = score;
                }
            }

            // Once we have calculated and gathered everything, we check the game status and if brief was yes or not.
            if (status == "active")
            {
                if (brief.ToLower() == "yes")
                {
                    // Since brief was "yes", we don't return the words so we set the words played by the players to null.
                    player1.WordsPlayed = null;
                    player2.WordsPlayed = null;
                    temp.GameState      = status;
                    temp.TimeLeft       = timeRemaing;
                    temp.Player1        = player1;
                    temp.Player2        = player2;
                    // This is where we return the appropriate Game object for the game status.
                    return(temp);
                }

                // If brief was null or anything other than "yes", we return a more detailed Game object.
                else
                {
                    // Words are still null for an Active game.
                    player1.WordsPlayed = null;
                    player2.WordsPlayed = null;
                    player1.Nickname    = P1Nickname;
                    player2.Nickname    = P2Nickname;

                    temp.Board     = board;
                    temp.TimeLimit = timeLimit;
                    temp.GameState = status;
                    temp.TimeLeft  = timeRemaing;
                    temp.Player1   = player1;
                    temp.Player2   = player2;
                    return(temp);
                }
            }

            // Almost the exact same as for Active, only we DO return words when brief is null of anything other than "yes".
            if (status == "completed")
            {
                if (brief.ToLower() == "yes")
                {
                    player1.WordsPlayed = null;
                    player2.WordsPlayed = null;
                    temp.GameState      = status;
                    temp.TimeLeft       = 0;
                    temp.Player1        = player1;
                    temp.Player2        = player2;
                    return(temp);
                }

                else
                {
                    player1.Nickname = P1Nickname;
                    player2.Nickname = P2Nickname;

                    temp.Board     = board;
                    temp.TimeLimit = timeLimit;
                    temp.GameState = status;
                    temp.TimeLeft  = 0;
                    temp.Player1   = player1;
                    temp.Player2   = player2;
                    return(temp);
                }
            }
            return(null);
        }