예제 #1
0
파일: User.cs 프로젝트: saroque/server
        public bool LoadDataFromEntityFramework(bool updateInventory = false)
        {
            using (var ctx = new hybrasylEntities(Constants.ConnectionString))
            {
                var playerquery = ctx.players.Where(player => player.name == Name).SingleOrDefault();
                if (playerquery == null)
                {
                    return false;
                }

                foreach (var property in GetType().GetProperties())
                {
                    string value;
                    if (User.EntityFrameworkMapping.TryGetValue(property.Name, out value))
                    {
                        SetValue(property, this, ctx.Entry(playerquery).Property(User.EntityFrameworkMapping[property.Name]).CurrentValue);
                    }
                }

                // Now load our attributes
                Flags = playerquery.flags.ToDictionary(v => v.name, v => true);

                Account = playerquery.account;
                // Set our citizenship
                nation citizenship;
                if (playerquery.nation != null && World.Nations.TryGetValue(playerquery.nation.name, out citizenship))
                    Citizenship = citizenship;
                else
                    Citizenship = World.Nations[Hybrasyl.Constants.DEFAULT_CITIZENSHIP];

                LogoffTime = playerquery.last_logoff ?? DateTime.Now;

                // Legend marks
                LegendMarks = playerquery.legend_marks.ToList();

                // Are we updating inventory & equipment?

                if (updateInventory)
                {
                    var inventory = JArray.Parse((string)playerquery.inventory);
                    var equipment = JArray.Parse((string)playerquery.equipment);

                    foreach (var obj in inventory)
                    {
                        Logger.DebugFormat("Inventory found");
                        PrettyPrinter.PrettyPrint(obj);
                        var itemId = (int)obj["item_id"];
                        int itemSlot = (int)obj["slot"];
                        var variantId = obj.Value<int?>("variant_id") ?? -1;

                        if (variantId > 0)
                            itemId = Game.World.Items[itemId].Variants[variantId].id;

                        var item = Game.World.CreateItem(itemId);
                        item.Count = obj.Value<int?>("count") ?? 1;

                        if (item != null)
                        {
                            Game.World.Insert(item);
                            AddItem(item, (byte)itemSlot);
                        }
                        Logger.DebugFormat("Item is {0}", itemId);
                    }

                    foreach (var obj in equipment)
                    {
                        var itemId = (int)obj["item_id"];
                        var itemSlot = (int)obj["slot"];
                        var variantId = obj.Value<int?>("variant_id") ?? -1;

                        if (variantId > 0)
                            itemId = Game.World.Items[itemId].Variants[variantId].id;

                        var item = Game.World.CreateItem(itemId);

                        if (item != null)
                        {
                            Logger.DebugFormat("Adding equipment: {0} to {1}", item.Name, itemSlot);
                            Game.World.Insert(item);
                            AddEquipment(item, (byte)itemSlot, false);
                        }
                        Logger.DebugFormat("Equipment is {0}", itemId);
                    }
                }
            }
            return true;
        }
예제 #2
0
파일: User.cs 프로젝트: saroque/server
        public bool SaveDataToEntityFramework()
        {
            using (var ctx = new hybrasylEntities(Constants.ConnectionString))
            {
                var playerquery = ctx.players.Where(player => player.name == Name).SingleOrDefault();

                if (playerquery == null)
                {
                    return false;
                }

                var inventory = new JArray();
                var equipment = new JArray();

                for (byte i = 1; i <= Inventory.Size; ++i)
                {
                    if (Inventory[i] == null) continue;
                    var obj = new JObject();
                    obj.Add("slot", (int)i);
                    obj.Add("count", Inventory[i].Count);
                    if (Inventory[i].IsVariant)
                    {
                        obj.Add("item_id", Inventory[i].ParentItem.id);
                        obj.Add("variant_id", Inventory[i].CurrentVariant.id);
                    }
                    else
                    {
                        obj.Add("item_id", Inventory[i].TemplateId);
                    }
                    inventory.Add(obj);
                }

                for (byte i = 1; i < Equipment.Size; ++i)
                {
                    if (Equipment[i] == null) continue;
                    var obj = new JObject();
                    obj.Add("slot", (int)i);
                    if (Equipment[i].IsVariant)
                    {
                        obj.Add("item_id", Equipment[i].ParentItem.id);
                        obj.Add("variant_id", Equipment[i].CurrentVariant.id);
                    }
                    else
                    {
                        obj.Add("item_id", Equipment[i].TemplateId);
                    }
                    equipment.Add(obj);
                }

                foreach (var property in GetType().GetProperties())
                {
                    string value;
                    if (User.EntityFrameworkMapping.TryGetValue(property.Name, out value))
                    {
                        // Nullables on the DB side need special handling to cast correctly.
                        var curType = property.PropertyType;
                        var destType = Nullable.GetUnderlyingType(typeof(player).GetProperty(value).PropertyType) ??
                            typeof(player).GetProperty(value).PropertyType;
                        object safevalue = (value == null) ? null : Convert.ChangeType(property.GetValue(this), destType);

                        // BEWARE - this will break if passed Enums that aren't handled via EF 5's enum support!
                        ctx.Entry(playerquery).Property(value).CurrentValue = safevalue;
                    }
                }

                ctx.Entry(playerquery).Property("inventory").CurrentValue = inventory.ToString();
                ctx.Entry(playerquery).Property("equipment").CurrentValue = equipment.ToString();

                // Save our current location
                ctx.Entry(playerquery).Property("map_id").CurrentValue = (int)Map.Id;
                ctx.Entry(playerquery).Property("map_x").CurrentValue = (int)X; ;
                ctx.Entry(playerquery).Property("map_y").CurrentValue = (int)Y;

                if (Citizenship != null)
                    ctx.Entry(playerquery).Property("nation_id").CurrentValue = Citizenship.id;

                ctx.SaveChanges();

            }

            return true;
        }
예제 #3
0
파일: User.cs 프로젝트: saroque/server
 /// <summary>
 /// Update a player's last logoff time in the database and the live object.
 /// </summary>
 public void UpdateLogoffTime()
 {
     using (var ctx = new hybrasylEntities(Constants.ConnectionString))
     {
         var playerquery = ctx.players.Where(player => player.name == Name).SingleOrDefault();
         if (playerquery == null)
         {
             // This means something very odd is happening; we might want to throw an exception
             // or do something else here
             return;
         }
         else
         {
             var now = DateTime.Now;
             LoginTime = now;
             ctx.Entry(playerquery).Property("last_logoff").CurrentValue = now;
             ctx.SaveChanges();
         }
     }
 }