Exemple #1
0
        /// <summary>
        /// helper function to get the word that should be guessed in the game
        /// </summary>
        /// <param name="category"></param>
        /// <returns> Icategory interface that represents a category model whose name will
        /// be used as a word to guess
        /// </returns>
        public ICategory getCategoryInstance(string category)
        {
            // the player instace
            PlayerModel player = PlayerModel.Instance;

            // the query to send to the database
            string sqlQuery = $"SELECT * FROM musicword.{category} ORDER BY RAND() LIMIT 1;";

            MySqlDataReader reader = null;

            try
            {
                // set the query
                MySqlCommand queryCmd = new MySqlCommand(sqlQuery, _connention);
                // get the object that reads the query output
                reader = queryCmd.ExecuteReader();
                if (reader == null)
                {
                    return(getCategoryInstance(category));
                }
            }
            catch (MySqlException)
            {
                MessageBox.Show("MySqlCommand Error");
            }

            string    answer      = null;
            ICategory resCategory = null;

            // if the query has results
            if (reader.HasRows)
            {
                // read the first entry
                reader.Read();
                // define the return object according to the current player category
                switch (player.Category)
                {
                case "albums":
                    // in this case the answer - album name is in the third column
                    try
                    {
                        answer = reader.GetString(2);
                        // create a new album model
                        resCategory = new AlbumModel(reader.GetInt64(0),
                                                     answer, reader.GetInt16(1), reader.GetInt64(3));
                    }
                    catch (SqlNullValueException e)
                    {
                        answer = null;
                    }

                    break;

                case "songs":
                    try
                    {
                        // in this case the answer - song name is in the second column
                        answer = reader.GetString(1);
                        // create a new album model
                        resCategory = new SongModel(reader.GetInt64(0),
                                                    answer, reader.GetInt64(2), reader.GetInt64(3));
                    }
                    catch (SqlNullValueException e)
                    {
                        answer = null;
                    }

                    break;

                case "artists":
                    try
                    {
                        // in this case the answer - artist name is in the second column
                        answer = reader.GetString(1);
                        // create a new artist model
                        resCategory = new ArtistModel(reader.GetInt64(0), answer, reader.GetString(4));
                    }
                    catch (SqlNullValueException e)
                    {
                        answer = null;
                    }
                    break;

                default:
                    break;
                }
            }
            else
            // if there are no entries to read
            {
                Console.WriteLine("No rows found.");
            }
            // if the word was already asked, call this function again
            if (!player.isInSet(answer))
            {
                // _connention.Close();
                reader.Close();
                return(getCategoryInstance(sqlQuery));
            }
            // close reader and connection
            reader.Close();
            // return the category model
            return(resCategory);
        }