Forum C# Missing Reflections...

Missing Reflections...

Postby Pawel Wozniak » October 26th, 2015, 7:59 pm

I have a Player class with some properties like:
public int cash;
public String nick;
public Double kdr;
public Boolean isArmed;
etc.

I need a "savePlayerPropertiesInHisDatabaseObject" method however Reflections are restricted in PlayerIO so i cant enumerate all properties to put them in databaseObject or load them from database object to Player class instance.

Is there any walkaround for this?
Pawel Wozniak
Paid Member
 
Posts: 96
Joined: October 14th, 2012, 10:47 pm

Re: Missing Reflections...

Postby Guillaume » October 26th, 2015, 10:55 pm

You can do it this way, i think the same way / logic that BigDB is using, i coded something for you! I don't have tested the code, but this compile and should work, just do some test with you project.

With this code, you can Load Players Data from BigDB automatically in Player object, Save Players Data directly in BigDB.
Just take a look at how i have defined your player properties. If you need additionnal types to manage, just add their implementation with the same logic.

Here the code:

Code: Select all
public class Player : BasePlayer
{
   private Dictionary<string, object> properties;
   
   public Player()
   {
      properties = new Dictionary<string, object>();
   }

    private void CreateOrUpdateData(string varName, object value)
    {
        if (properties.ContainsKey(varName) == false)
        {
            properties.Add(varName, value);
        }
        properties[varName] = value;
    }

   private bool GetValue(string varName, out object result)
   {
        if (properties.ContainsKey(varName))
        {
            result = properties[varName];
            return true;
        }
        result = null;
        return false;
   }

    #region GET VALUE BY TYPE SIGNATURE

    private int GetValueOrDefault(string varName, int defaultValue)
   {
        object result;

        if (GetValue(varName, out result))
            return (int)result;

        //If not there, we will compute a default value then continue
        CreateOrUpdateData(varName, defaultValue);
        return defaultValue;
   }

    private string GetValueOrDefault(string varName, string defaultValue)
    {
        object result;

        if (GetValue(varName, out result))
            return (string)result;

        //If not there, we will compute a default value then continue
        CreateOrUpdateData(varName, defaultValue);
        return defaultValue;
    }

    private double GetValueOrDefault(string varName, double defaultValue)
    {
        object result;

        if (GetValue(varName, out result))
            return (double)result;

        //If not there, we will compute a default value then continue
        CreateOrUpdateData(varName, defaultValue);
        return defaultValue;
    }

    private bool GetValueOrDefault(string varName, bool defaultValue)
    {
        object result;

        if (GetValue(varName, out result))
            return (bool)result;

        //If not there, we will compute a default value then continue
        CreateOrUpdateData(varName, defaultValue);
        return defaultValue;
    }

    #endregion

    #region PLAYER PROPERTIES

    public int cash
   {
      get {
         return GetValueOrDefault("cash", 0);
      }
        set {
            CreateOrUpdateData("cash", value);
        }
   }

    public string nick
    {
        get
        {
            return GetValueOrDefault("nick", string.Empty);
        }
        set
        {
            CreateOrUpdateData("nick", value);
        }
    }

    public double kdr
    {
        get
        {
            return GetValueOrDefault("kdr", (double)0);
        }
        set
        {
            CreateOrUpdateData("kdr", value);
        }
    }

    public bool isArmed
    {
        get
        {
            return GetValueOrDefault("isArmed", false);
        }
        set
        {
            CreateOrUpdateData("isArmed", value);
        }
    }

    #endregion

    #region SAVING IN BIGDB

    public void SavePlayerPropertiesInHisDatabaseObject()
    {
        SavePlayerPropertiesInHisDatabaseObject(null);
    }

    public void SavePlayerPropertiesInHisDatabaseObject(Callback onSuccess)
    {
        SavePlayerPropertiesInHisDatabaseObject(null, null);
    }

    public void SavePlayerPropertiesInHisDatabaseObject(Callback onSuccess, Callback<PlayerIOError> onError)
    {
        this.GetPlayerObject(delegate(DatabaseObject db)
        {
            foreach (KeyValuePair<string, object> kvp in properties)
            {
                SetBigDBDataWithCorrectType(db, kvp.Key, kvp.Value);
            }

            //Data updated. Now saving...
            db.Save(false, false, delegate()
            {
                if (onSuccess != null)
                    onSuccess();
            }, delegate(PlayerIOError err)
            {
                if (onError != null)
                    onError(err);
            });
        });
    }

    private void SetBigDBDataWithCorrectType(DatabaseObject db, string propertyName, object data)
    {
        if (data is int)
            db.Set(propertyName, (int)data);
        else if (data is double)
            db.Set(propertyName, (double)data);
        else if (data is string)
            db.Set(propertyName, (string)data);
        else if (data is bool)
            db.Set(propertyName, (bool)data);
        //Else we do nothing...
    }

    #endregion

    #region LOADING FROM BIGDB

    /// <summary>
    /// Wait for onFinish callback to ensure that all data has been retrieved
    /// </summary>
    /// <param name="onFinish"></param>
    public void LoadPlayerPropertiesFromHistDatabaseObject(Callback onFinish)
    {
        //We don't care if the field exist on Player Object, our dictionnary will reference any value
        this.GetPlayerObject(delegate(DatabaseObject db)
        {
            foreach (string property in db.Properties)
            {
                if (properties.ContainsKey(property) == false)
                    properties.Add(property, db[property]);
                else
                    properties[property] = db[property];
            }

            if (onFinish != null)
                onFinish();
        });
    }

    #endregion
}
Guillaume
 
Posts: 277
Joined: March 28th, 2014, 1:59 am
Location: France

Re: Missing Reflections...

Postby Pawel Wozniak » October 26th, 2015, 11:13 pm

Thanks a lot. As I`m not too familiar with c# I need to read and test it few times to get better understanding of your sollution.
Pawel Wozniak
Paid Member
 
Posts: 96
Joined: October 14th, 2012, 10:47 pm

Re: Missing Reflections...

Postby Henrik » October 30th, 2015, 7:51 am

Why not just automatically load the PlayerObject for each player, and then user the properties on that all the time, instead of having properties on the user? That way you can easily call .Save() when something changes?
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Missing Reflections...

Postby Guillaume » October 30th, 2015, 1:36 pm

+1 with Henrik, as it would give you a little more performance (Less server time) with directly managing the properties index on the BigDB Object.

The only advantage of my code is that you have a sort of refactoring if you change something somewhere, but btw, using const string properties name somewhere is also a good way to have a consistency in the variable utilisation...And this is what i am using on my current project.
Guillaume
 
Posts: 277
Joined: March 28th, 2014, 1:59 am
Location: France


Return to C#



cron