Example #1
0
        public void DestroyItem(ItemEntity item)
        {
            if (this.IsItemLoaded(item.ID) == false)
            {
                throw new ArgumentException("Cannot destroy an item that was not loaded by this item manager");
            }

            // remove the item from the list
            this.mItemList.Remove(item.ID);

            // check if there are inventories loaded for this item
            if (this.IsItemLoaded(item.LocationID) == true)
            {
                ItemInventory inventory = this.GetItem(item.LocationID) as ItemInventory;

                // remove the item from the inventory
                inventory.RemoveItem(item);

                // try to remove from meta inventories too
                try
                {
                    ItemInventory metaInventory =
                        this.MetaInventoryManager.GetOwnerInventoriesAtLocation(item.LocationID, item.OwnerID);

                    metaInventory.RemoveItem(item);
                }
                catch (ArgumentOutOfRangeException)
                {
                }
            }

            // set the item to the recycler location
            item.LocationID = this.LocationRecycler.ID;

            // finally remove the item off the database
            item.Destroy();
        }
Example #2
0
        private void HandleOnItemUpdate(Notifications.Nodes.Inventory.OnItemChange change)
        {
            foreach ((PyInteger itemID, PyDictionary _changes) in change.Updates)
            {
                PyDictionary <PyString, PyTuple> changes = _changes.GetEnumerable <PyString, PyTuple>();

                ItemEntity item = this.ItemFactory.LoadItem(itemID, out bool loadRequired);

                // if the item was just loaded there's extra things to take into account
                // as the item might not even need a notification to the character it belongs to
                if (loadRequired == true)
                {
                    // trust that the notification got to the correct node
                    // load the item and check the owner, if it's logged in and the locationID is loaded by us
                    // that means the item should be kept here
                    if (this.ItemFactory.TryGetItem(item.LocationID, out ItemEntity location) == false || this.CharacterManager.IsCharacterConnected(item.OwnerID) == false)
                    {
                        // this item should not be loaded, so unload and return
                        this.ItemFactory.UnloadItem(item);
                        return;
                    }

                    bool locationBelongsToUs = true;

                    switch (location)
                    {
                    case Station _:
                        locationBelongsToUs = this.SystemManager.StationBelongsToUs(location.ID);
                        break;

                    case SolarSystem _:
                        locationBelongsToUs = this.SystemManager.SolarSystemBelongsToUs(location.ID);
                        break;
                    }

                    if (locationBelongsToUs == false)
                    {
                        this.ItemFactory.UnloadItem(item);
                        return;
                    }
                }

                OnItemChange itemChange = new OnItemChange(item);

                // update item and build change notification
                if (changes.TryGetValue("locationID", out PyTuple locationChange) == true)
                {
                    PyInteger oldValue = locationChange[0] as PyInteger;
                    PyInteger newValue = locationChange[1] as PyInteger;

                    itemChange.AddChange(ItemChange.LocationID, oldValue);
                    item.LocationID = newValue;
                }

                if (changes.TryGetValue("quantity", out PyTuple quantityChange) == true)
                {
                    PyInteger oldValue = quantityChange[0] as PyInteger;
                    PyInteger newValue = quantityChange[1] as PyInteger;

                    itemChange.AddChange(ItemChange.Quantity, oldValue);
                    item.Quantity = newValue;
                }

                if (changes.TryGetValue("ownerID", out PyTuple ownerChange) == true)
                {
                    PyInteger oldValue = ownerChange[0] as PyInteger;
                    PyInteger newValue = ownerChange[1] as PyInteger;

                    itemChange.AddChange(ItemChange.OwnerID, oldValue);
                    item.OwnerID = newValue;
                }

                if (changes.TryGetValue("singleton", out PyTuple singletonChange) == true)
                {
                    PyBool oldValue = singletonChange[0] as PyBool;
                    PyBool newValue = singletonChange[1] as PyBool;

                    itemChange.AddChange(ItemChange.Singleton, oldValue);
                    item.Singleton = newValue;
                }

                // TODO: IDEALLY THIS WOULD BE ENQUEUED SO ALL OF THEM ARE SENT AT THE SAME TIME
                // TODO: BUT FOR NOW THIS SHOULD SUFFICE
                // send the notification
                this.NotificationManager.NotifyCharacter(item.OwnerID, "OnMultiEvent",
                                                         new PyTuple(1)
                {
                    [0] = new PyList(1)
                    {
                        [0] = itemChange
                    }
                });

                if (item.LocationID == this.ItemFactory.LocationRecycler.ID)
                {
                    // the item is removed off the database if the new location is the recycler
                    item.Destroy();
                }
                else if (item.LocationID == this.ItemFactory.LocationMarket.ID)
                {
                    // items that are moved to the market can be unloaded
                    this.ItemFactory.UnloadItem(item);
                }
                else
                {
                    // save the item if the new location is not removal
                    item.Persist();
                }
            }
        }