1
\$\begingroup\$

Here is a code example from my name, where I use a MySQL db and store players data.

In this code I'm using a DataReader when loading and saving the data in and out. Although there's only 10 fields, I have reduced the number for this example. I plan on having 50+.

The main focus of this question is to find a more efficient way of managing players' data, a way that doesn't require so much code and hard work to load / save it.

Adding new features is a pain as I have to code a whole new set of fields to load and save, not forgetting the features.

My only idea was to make a dict and load it as [columnName] = columnValue;, cache it, grab if loaded previously and save all items on save. The problem with that was I didn't like the fact I had to explicitly call by the columns name. What if I changed it?

using MySql.Data.MySqlClient;

namespace Pitaya.Game.Players.Data
{
    internal class PlayerData
    {
        public int Id;
        public string Username;
        public int Rank;
        public string Motto;
        public PlayerOutfit Outfit;
        public PlayerGender Gender;
        public long Credits;
        public long Pixels;
        public long Diamonds;
        public bool CanChange;



        public void AssignData(MySqlDataReader reader)
        {
            while (reader.Read())
            {
                Id = reader.GetInt32("id");
                Username = reader.GetString("username");
                Rank = reader.GetInt32("rank");
                Motto = reader.GetString("motto");
                Outfit = new PlayerOutfit(reader.GetString("look"));
                Gender = reader.GetString("gender").ToUpper() == "M" ? PlayerGender.Male : PlayerGender.Female;
                Credits = reader.GetInt64("credits");
                Pixels = reader.GetInt64("activity_points");
                Diamonds = reader.GetInt64("vip_points");
                CanChange = reader.GetInt32("can_change") == 1;
            }
        }

        public void SaveData()
        {
            using (var dbConnection = Program.Server.Database.Connection)
            {
                dbConnection.SetQuery("UPDATE `users` SET `username` = @username, `rank` = @rank, `motto = @motto, `look` = @look, `gender` = @gender, `credits` = @credits, `pixels` = @pixels, `diamonds` = @diamonds, `can_change` = @canChange WHERE `id` = @id");
                dbConnection.AppendParameter("id", Id);
                dbConnection.AppendParameter("username", Username);
                dbConnection.AppendParameter("rank", Rank);
                dbConnection.AppendParameter("motto", Motto);
                dbConnection.AppendParameter("look", Outfit.FigureCode);
                dbConnection.AppendParameter("gender", Gender == PlayerGender.Male ? "M" : "F");
                dbConnection.AppendParameter("credits", Credits);
                dbConnection.AppendParameter("pixels", Pixels);
                dbConnection.AppendParameter("diamonds", Diamonds);
                dbConnection.AppendParameter("canChange", CanChange ? 1 : 0);
                dbConnection.ExecuteNonQuery();
            }
        }
    }
}
\$\endgroup\$
1
\$\begingroup\$

Since you have a lot of fields, I would suggest using a DataTable where each column is a field. This not only keeps your data organized and easily accessible, but you can very efficiently write the data to a database, and display the data in a DataGridView, which also gives you ready access to methods for adding and editing the data.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.