Ejemplo n.º 1
0
        /// <summary>
        /// Helper method returns a game object from the T-SQL database from the Games table
        /// </summary>
        private GamesDB QueryGame(int GameID)
        {
            using (SqlConnection conn = new SqlConnection(BoggleDB))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    using (SqlCommand command = new SqlCommand("select * from Games where GameID=@GameID", conn, trans))
                    {
                        command.Parameters.AddWithValue("@GameID", GameID);

                        //initialize the reader to read the row selected
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (!reader.HasRows)
                            {
                                return(null);
                            }
                            reader.Read();
                            GamesDB game = new GamesDB();
                            game.GameID  = GameID;
                            game.Player1 = (string)reader["Player1"];
                            //if game is pending don't set other values
                            if (!(reader["Player2"] is System.DBNull))
                            {
                                game.Player2   = (string)reader["Player2"];
                                game.Board     = (string)reader["Board"];
                                game.TimeLimit = (int)reader["TimeLimit"];
                                game.StartTime = (DateTime)reader["StartTime"];
                            }
                            return(game);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Plays a word.
        /// </summary>
        public UserInfo PlayWord(string GameID, UserInfo user)
        {
            int GameInt;

            int.TryParse(GameID, out GameInt);
            //trim word
            if (user.Word != null)
            {
                user.Word = user.Word.Trim();
            }
            GamesDB game = QueryGame(GameInt);

            // forbidden when:
            // usertoken is null
            // word is null or ""
            // game doesn't exist
            // usertoken not associated with the game
            if (game == null || user.Word == null || user.Word.Length == 0 || !(game.Player1 == user.UserToken || game.Player2 == user.UserToken))
            {
                UserInfo ret = new UserInfo();
                ret.HttpStatus = "403 Forbidden";
                return(ret);
            }

            // Conflict
            // game state isn't active
            if ((game.Player2 == null) || game.StartTime.AddSeconds(game.TimeLimit) < DateTime.Now)
            {
                UserInfo ret = new UserInfo();
                ret.HttpStatus = "409 Conflict";
                return(ret);
            }

            //gets all words played so far by this user
            //returns 0 if word has already been played
            LinkedList <WordsDB> played = QueryWord(GameInt, user.UserToken);

            foreach (WordsDB word in played)
            {
                if (word.Word == user.Word)
                {
                    UserInfo ret = new UserInfo();
                    ret.HttpStatus = "200 OK";
                    ret.Score      = "0";
                    return(ret);
                }
            }
            //Calculates the words score
            int score = CheckScore(user.Word, new BoggleBoard(game.Board));

            using (SqlConnection conn = new SqlConnection(BoggleDB))
            {
                // Connections must be opened
                conn.Open();
                UserInfo ret = new UserInfo();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    using (SqlCommand command =
                               new SqlCommand("insert into Words (GameID, Player, Score, Word) values(@GameID, @Player, @Score, @Word)",
                                              conn,
                                              trans))
                    {
                        command.Parameters.AddWithValue("@GameID", GameInt);
                        command.Parameters.AddWithValue("@Player", user.UserToken);
                        command.Parameters.AddWithValue("@Score", score);
                        command.Parameters.AddWithValue("@Word", user.Word);

                        //attempt query twice
                        if (command.ExecuteNonQuery() == 1)
                        {
                        }
                        else
                        {
                            ret            = new UserInfo();
                            ret.HttpStatus = "409 Conflict";
                            return(ret);
                        }
                    }
                    trans.Commit();
                }

                ret.HttpStatus = "200 OK";
                ret.Score      = score + "";
                return(ret);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns the status of a game.
        /// </summary>
        public GameStatus GameStatus(string GameID, string Brief)
        {
            bool brief = false;

            if (Brief == "yes")
            {
                brief = true;
            }
            int GameId;

            int.TryParse(GameID, out GameId);
            GamesDB    game = QueryGame(GameId);
            GameStatus ret  = new GameStatus();

            if (game == null)
            {
                ret.HttpStatus = "403 Forbidden";
                return(ret);
            }
            if (game.Player2 == null)
            {
                ret.HttpStatus = "200 OK";
                ret.GameState  = "pending";
                return(ret);
            }
            else if (game.StartTime.AddSeconds(game.TimeLimit) < DateTime.Now)
            {
                ret.GameState = "completed";
                ret.TimeLeft  = 0;
            }
            else
            {
                ret.GameState = "active";
                ret.TimeLeft  = (game.StartTime.AddSeconds(game.TimeLimit) - DateTime.Now).Seconds + (game.StartTime.AddSeconds(game.TimeLimit) - DateTime.Now).Minutes * 60;
            }

            if (!brief)
            {
                ret.Board     = game.Board;
                ret.TimeLimit = game.TimeLimit;
            }

            //Player info
            PlayerModel P1 = new PlayerModel();
            PlayerModel P2 = new PlayerModel();

            ret.Player1 = P1;
            ret.Player2 = P2;
            P1.Score    = 0;
            P2.Score    = 0;
            if (!brief)
            {
                P1.Nickname = QueryUser(game.Player1);
                P2.Nickname = QueryUser(game.Player2);
            }

            LinkedList <WordsDB> words1 = QueryWord(GameId, game.Player1);
            LinkedList <WordsDB> words2 = QueryWord(GameId, game.Player2);

            //Player1 calculate score and words played
            List <WordPlayed> WordsPlayed = new List <WordPlayed>();

            foreach (WordsDB word in words1)
            {
                WordPlayed curr = new WordPlayed();
                curr.Word  = word.Word;
                curr.Score = word.Score;
                P1.Score  += curr.Score;
                WordsPlayed.Add(curr);
            }
            if (ret.GameState == "completed" && !brief)
            {
                P1.WordsPlayed = WordsPlayed;
            }
            //Player2 calculate score and words played
            WordsPlayed = new List <WordPlayed>();
            foreach (WordsDB word in words2)
            {
                WordPlayed curr = new WordPlayed();
                curr.Word  = word.Word;
                curr.Score = word.Score;
                P2.Score  += curr.Score;
                WordsPlayed.Add(curr);
            }
            if (ret.GameState == "completed" && !brief)
            {
                P2.WordsPlayed = WordsPlayed;
            }
            ret.HttpStatus = "200 OK";
            return(ret);
        }