예제 #1
0
 /// <summary>
 /// Remove item from registry
 /// </summary>
 /// <param name="guid"></param>
 public static void UnRegister(System.Guid guid)
 {
     if (_dict.Remove(guid))
     {
         OnRemovedItem?.Invoke(guid);
     }
 }
예제 #2
0
        public virtual Result <CollectionRemoveResult> Remove(TElementType item, int amount, CollectionContext context)
        {
            var canRemove = CanRemove(item, amount, context);

            if (canRemove.result == false)
            {
                return(new Result <CollectionRemoveResult>(null, canRemove.error));
            }

            var indices        = IndexOfAll(o => ReferenceEquals(o, item));
            var affectedSlots  = new List <SlotAmount>();
            var amountToRemove = amount;

            amountToRemove = RemoveFromIndices(indices, amountToRemove, affectedSlots, context);

            if (amountToRemove > 0)
            {
                indices        = IndexOfAll(item);
                amountToRemove = RemoveFromIndices(indices, amountToRemove, affectedSlots, context);
            }

            var response = new Result <CollectionRemoveResult>(new CollectionRemoveResult(affectedSlots.ToArray()));

            if (context.HasFlag(CollectionContext.Events.Remove))
            {
                OnRemovedItem?.Invoke(this, response.result);
            }

            if (context.HasFlag(CollectionContext.Events.SlotChanged))
            {
                OnSlotsChanged?.Invoke(this, new CollectionSlotsChangedResult(response.result.affectedSlots.Select(o => o.slot).ToArray()));
            }

            return(response);
        }
예제 #3
0
            /// <summary>
            /// Clear registry
            /// </summary>
            public void Clear()
            {
                foreach (var kvp in _dict)
                {
                    OnRemovedItem?.Invoke(kvp.Key, kvp.Value);
                }

                _dict.Clear();
            }
예제 #4
0
            /// <summary>
            /// Remove existing value from registry
            /// </summary>
            /// <param name="identifier"></param>
            public void UnRegister(TKey identifier)
            {
                TValue currentValue;

                if (TryGet(identifier, out currentValue))
                {
                    _dict.Remove(identifier);
                    OnRemovedItem?.Invoke(identifier, currentValue);
                }
            }
예제 #5
0
        public virtual Result <CollectionRemoveResult> Remove(TElementType item, int amount, CollectionContext context)
        {
            var canRemove = CanRemove(item, amount, context);

            if (canRemove.result == false)
            {
                return(new Result <CollectionRemoveResult>(null, canRemove.error));
            }

            var indices        = IndexOfAll(item);
            var affectedSlots  = new List <SlotAmount>();
            var amountToRemove = amount;

            foreach (int index in indices)
            {
                if (amountToRemove - slots[index].amount < 0)
                {
                    // Just remove moveAmount, there's enough in this slot to complete
                    SetInternal(index, slots[index].item, slots[index].amount - amountToRemove, context);
                    affectedSlots.Add(new SlotAmount(index, amountToRemove));
                    amountToRemove = 0;
                    break;
                }

                affectedSlots.Add(new SlotAmount(index, slots[index].amount));
                amountToRemove -= slots[index].amount;
                SetInternal(index, default(TElementType), 0, context);
            }

            var response = new Result <CollectionRemoveResult>(new CollectionRemoveResult(affectedSlots.ToArray()));

            if (context.HasFlag(CollectionContext.Events.Remove))
            {
                OnRemovedItem?.Invoke(this, response.result);
            }

            if (context.HasFlag(CollectionContext.Events.SlotChanged))
            {
                OnSlotsChanged?.Invoke(this, new CollectionSlotsChangedResult(response.result.affectedSlots.Select(o => o.slot).ToArray()));
            }

            return(response);
        }
예제 #6
0
        public void Clear(CollectionContext context)
        {
            if (isReadOnly)
            {
                return;
            }

            var l            = new List <SlotAmount>();
            int removedCount = 0;

            for (var i = 0; i < slots.Length; i++)
            {
                var slot = slots[i];
                if (slot.isOccupied)
                {
                    l.Add(new SlotAmount(i, GetAmount(i)));
                    removedCount += slot.amount;
                    slot.Clear();
                }
            }

            if (l.Count > 0)
            {
                var affectedSlots = l.ToArray();

                if (context.HasFlag(CollectionContext.Events.Remove))
                {
                    OnRemovedItem?.Invoke(this, new CollectionRemoveResult(affectedSlots));
                }

                if (context.HasFlag(CollectionContext.Events.SlotChanged))
                {
                    OnSlotsChanged?.Invoke(this, new CollectionSlotsChangedResult(affectedSlots.Select(o => o.slot).ToArray()));
                }
            }
        }
예제 #7
0
        public virtual Result <bool> Set(int index, TElementType item, int amount, CollectionContext context)
        {
            var canSet = CanSet(index, item, amount, context);

            if (canSet.result == false)
            {
                return(canSet);
            }

            // Slot is empty and item is empty, ignore call
            if (slots[index].isOccupied == false && IsNull(item))
            {
                return(true);
            }

            if (AreEqual(slots[index].item, item) && GetAmount(index) == amount)
            {
                // Still set for *SpecificInstance
                SetInternal(index, item, amount, context);
                return(true);
            }

            var currentAmount = GetAmount(index);

            if (AreEqual(slots[index].item, item))
            {
                SetInternal(index, item, amount, context);

                var diff = Math.Abs(amount - currentAmount);
                if (amount < currentAmount)
                {
                    if (context.HasFlag(CollectionContext.Events.Remove))
                    {
                        OnRemovedItem?.Invoke(this, new CollectionRemoveResult(new SlotAmount[] { new SlotAmount(index, diff) }));
                    }
                }
                else if (amount > currentAmount)
                {
                    if (context.HasFlag(CollectionContext.Events.Add))
                    {
                        OnAddedItem?.Invoke(this, new CollectionAddResult(new SlotAmount[] { new SlotAmount(index, diff) }));
                    }
                }
            }
            else
            {
                if (IsNull(item))
                {
                    SetInternal(index, item, 0, context);
                    if (context.HasFlag(CollectionContext.Events.Remove))
                    {
                        OnRemovedItem?.Invoke(this, new CollectionRemoveResult(new [] { new SlotAmount(index, currentAmount) }));
                    }
                }
                else
                {
                    if (IsNull(slots[index].item) == false)
                    {
                        SetInternal(index, default(TElementType), 0, context);
                        if (context.HasFlag(CollectionContext.Events.Remove))
                        {
                            OnRemovedItem?.Invoke(this, new CollectionRemoveResult(new [] { new SlotAmount(index, currentAmount) }));
                        }
                    }

                    // Add the item
                    SetInternal(index, item, amount, context);
                    if (context.HasFlag(CollectionContext.Events.Add))
                    {
                        OnAddedItem?.Invoke(this, new CollectionAddResult(new [] { new SlotAmount(index, amount) }));
                    }
                }
            }

            if (context.HasFlag(CollectionContext.Events.SlotChanged))
            {
                OnSlotsChanged?.Invoke(this, new CollectionSlotsChangedResult(new int[] { index }));
            }

            return(true);
        }
예제 #8
0
 protected void InvokeOnRemovedItem(CollectionRemoveResult <TElementType> removeResult)
 {
     OnRemovedItem?.Invoke(this, removeResult);
 }