Exemple #1
0
        private static IInventoryItem CloneItem(IInventoryItem item, int amount = -1)
        {
            var type      = item.GetType();
            var newObject = ItemTypeToNewObject(type);

            foreach (var prop in type.GetProperties())
            {
                if (prop.CanWrite)
                {
                    prop.SetValue(newObject, prop.GetValue(item));
                }
            }
            foreach (var field in type.GetFields())
            {
                if (field.IsPublic)
                {
                    try
                    {
                        field.SetValue(newObject, field.GetValue(item));
                    }
                    catch (Exception)
                    {
                        // ignored
                    }
                }
            }
            if (amount != -1)
            {
                newObject.Amount = amount;
            }
            return(newObject);
        }
 /// <summary>
 ///     This will attempt to convert from an IInventoryItem to an InventoryItem object
 /// </summary>
 /// <description>
 ///     In order for this to work the object which implements IInventoryItem must inherit from InventoryItem, otherwise
 ///     an exception is thrown.
 /// </description>
 /// <param name="i">
 ///     The interface to upcast <see cref="IInventoryItem" />
 /// </param>
 /// <returns>
 ///     The object backing the interface implementation <see cref="InventoryItem" />
 /// </returns>
 internal static InventoryItem FromInterface(IInventoryItem i)
 {
     if (typeof(InventoryItem).IsAssignableFrom(i.GetType()))
     {
         return((InventoryItem)i);
     }
     MainConsole.Instance.Error("[MRM] There is no legal conversion from IInventoryItem to InventoryItem");
     return(null);
 }
        private static List <ODataProperty> GetODataProperties(IInventoryItem inventoryItem)
        {
            List <ODataProperty> oDataProperties = new List <ODataProperty>();
            IEnumerable <InventoryPropertyName> supportedSchemaPropertyNames = QueryOptionHelpers.GetSupportedSchemaPropertyNames();
            List <string> inventoryPropertys = new List <string>();

            foreach (var propertyName in supportedSchemaPropertyNames)
            {
                string propertyNameStr = Enum.GetName(typeof(InventoryPropertyName), propertyName);
                inventoryPropertys.Add(propertyNameStr);
            }

            foreach (var property in inventoryItem.GetType().GetProperties())
            {
                if (inventoryPropertys.Contains(property.Name))
                {
                    switch (property.Name)
                    {
                    case "Price":
                        string price = property.GetValue(inventoryItem).ToString();
                        oDataProperties.Add(new ODataProperty
                        {
                            Name  = property.Name,
                            Value = '$' + price
                        });
                        break;

                    case "ArrivedDate":
                        oDataProperties.Add(new ODataProperty
                        {
                            Name  = "Arrived_Date",
                            Value = property.GetValue(inventoryItem)
                        });
                        break;

                    case "StockNo":
                        oDataProperties.Add(new ODataProperty
                        {
                            Name  = property.Name,
                            Value = int.Parse(property.GetValue(inventoryItem).ToString())
                        });
                        break;

                    default:
                        oDataProperties.Add(new ODataProperty
                        {
                            Name  = property.Name,
                            Value = property.GetValue(inventoryItem)
                        });
                        break;
                    }
                }
            }

            return(oDataProperties);
        }
Exemple #4
0
 /// <summary>
 /// This will attempt to convert from an IInventoryItem to an InventoryItem object
 /// </summary>
 /// <description>
 /// In order for this to work the object which implements IInventoryItem must inherit from InventoryItem, otherwise
 /// an exception is thrown.
 /// </description>
 /// <param name="i">
 /// The interface to upcast <see cref="IInventoryItem"/>
 /// </param>
 /// <returns>
 /// The object backing the interface implementation <see cref="InventoryItem"/>
 /// </returns>
 internal static InventoryItem FromInterface(IInventoryItem i)
 {
     if (typeof(InventoryItem).IsAssignableFrom(i.GetType()))
     {
         return((InventoryItem)i);
     }
     else
     {
         throw new ApplicationException("[MRM] There is no legal conversion from IInventoryItem to InventoryItem");
     }
 }
Exemple #5
0
        public void Put(IInventoryItem inventoryItem)
        {
            if (inventoryItem == null)
            {
                Debug.LogWarning("Trying to add to inventory null.");

                return;
            }

            _inventoryItems.Add(inventoryItem);

            Debug.Log(string.Format("Inventory item({0}) was putted in to inventory.", (inventoryItem.GetType().Name)));

            InventoryItemAmountChanged.Invoke(inventoryItem.GetType(), _inventoryItems.Count(x => x.GetType() == inventoryItem.GetType()));
        }
Exemple #6
0
        protected bool TryGetIndex(IInventoryItem item, out int index)
        {
            index = -1;
            for (var i = 0; i < Contents.Count; i++)
            {
                if (Contents[i].GetType() != item.GetType())
                {
                    continue;
                }

                index = i;
                return(true);
            }

            return(false);
        }
        public async Task <IActionResult> Put(int id, string session, [FromBody] UpdateItemRequest request)
        {
            LoadSession(Guid.Parse(session));

            IInventoryItem item = inventory.Find(x => x.Id == id);

            if (item == null)
            {
                SetSession(Guid.Parse(session));
                return(NotFound());
            }

            switch (request.Action)
            {
            case "delete":
                if (item.GetType() == typeof(Item))
                {
                    Item modItem = (Item)item;
                    if (request.Count == null || modItem.Count - request.Count.Value <= 0)
                    {
                        inventory.Remove(item);
                    }
                    else
                    {
                        modItem.Count -= request.Count.Value;
                    }
                }
                else
                {
                    inventory.Remove(item);
                }
                break;

            case "level":
                if (item.GetType() == typeof(Pokemon))
                {
                    Item rarecandy = (Item)inventory.Find(x => x.Name.ToLower() == "rare candy");
                    if (rarecandy == null)
                    {
                        SetSession(Guid.Parse(session));
                        return(new JsonResult(new { Error = $"No rare candies found to evolve {item.Name}" }));
                    }

                    Pokemon pokemon = (Pokemon)item;
                    pokemon.Level++;

                    if (pokemon.Evolution.Count > 0)
                    {
                        if (pokemon.Evolution[0].Trigger == "level-up" && pokemon.Level >= Convert.ToInt32(pokemon.Evolution[0].Level))
                        {
                            await EvolvePokemon(pokemon);
                        }

                        rarecandy.Count -= 1;
                        if (rarecandy.Count == 0)
                        {
                            inventory.Remove(rarecandy);
                        }
                    }
                }
                break;

            default:
                break;
            }

            SetSession(Guid.Parse(session));
            return(new JsonResult(item));
        }
Exemple #8
0
 public ItemTransferModel(IInventoryItem item)
 {
     Item = item;
     Type = Item.GetType();
 }