Beispiel #1
0
        public void Swap(int slot1, int slot2)
        {
            ItemStack temp = Get(slot1);

            SetRaw(slot1, Get(slot2));
            SetRaw(slot2, temp);

            ValidateSlot(slot1);
            ValidateSlot(slot2);

            //  Invoke an inventory change event on slot1
            InventoryChangeEvent e = new InventoryChangeEvent {
                slot = slot1, itemStack = Get(slot1), inventory = this
            };

            OnInventoryChangeEvent?.Invoke(this, e);
            if (e.cancel == true)
            {
                return;
            }                               //  return if the event has been cancelled by any subscriber

            //  Invoke an inventory change event on slot2
            e = new InventoryChangeEvent {
                slot = slot2, itemStack = Get(slot2), inventory = this
            };
            OnInventoryChangeEvent?.Invoke(this, e);
            if (e.cancel == true)
            {
                return;
            }                               //  return if the event has been cancelled by any subscriber
        }
Beispiel #2
0
 // Called on a inventory change, count represents the amount of items inserted or removed
 public override void InventoryChange(Item item, uint count, InventoryChangeEvent eve)
 {
     if (eve == InventoryChangeEvent.Add)
     {
         Environment.instance.AddItemToPickupUI(item.itemName, count, item.itemSprite);
     }
     UpdateBar();
 }
//---------------------------------------------------------
    //	Events

    protected virtual void OnInventoryChange(InventoryChangeEvent e)
    {
        EventHandler <InventoryChangeEvent> handler = InventoryChange;

        if (handler != null)
        {
            handler(this, e);
        }
    }
Beispiel #4
0
        static void Main(string[] args)
        {
            // initialization
            SiteState state = StateManager.LoadState();

            Mapper.CreateMap <InventoryTruck, InventoryItem>();

            // obtain the consumer
            EventHubReceiver receiver = GetEventHubReceiver(state);


            bool continueProcessing = true;

            while (continueProcessing)
            {
                // retreive a batch of data
                var data = receiver.Receive(10, TimeSpan.FromSeconds(10));

                // iterate through events
                foreach (EventData item in data)
                {
                    // deserialize the InventoryChangeEvent from the message body
                    var bodyStream = item.GetBodyStream();
                    InventoryChangeEvent changeEvent = SerializationHelper.TryExtractInventoryChangedEvent(bodyStream);

                    // verify that deserialization was successful
                    if (changeEvent != null)
                    {
                        // update inventory depending on type of change
                        if (changeEvent.ChangeType == InventoryChangeType.Refresh)
                        {
                            var mergeTruck = Mapper.Map <InventoryItem>(changeEvent.Truck);
                            MergeInventory(mergeTruck);
                            Console.WriteLine("Merged 1 truck into inventory");
                        }
                        else if (changeEvent.ChangeType == InventoryChangeType.Sold)
                        {
                            RemoveInventory(changeEvent.Truck.Id);
                            Console.WriteLine("Removed 1 truck into inventory");
                        }
                    }

                    // update the saved state with the new offset
                    state.Offset = item.Offset;
                }

                // save state on each round
                StateManager.WriteState(state);

                // management of the console host
                Console.WriteLine("Press any key to continue or 'q' to quit...");
                var keyPress = Console.ReadKey(false);
                continueProcessing = keyPress.KeyChar != 'q';
            }
        }
Beispiel #5
0
    private void OnInventoryChange(object sender, InventoryChangeEvent e)
    {
        UpdateViewModels();

        if (inventoryUpdated == false)
        {
            UpdateInventory();
            UpdateHotbar();
            inventoryUpdated = true;
        }
    }
Beispiel #6
0
        private static void SendData(EventHubSender partitionedSender, InventoryTruck item)
        {
            // create the change event
            var changeEvent = new InventoryChangeEvent()
            {
                ChangeType = InventoryChangeType.Refresh, Truck = item
            };

            // create a memory stream with the XML serialized data and use to create event data object
            var       strm = SerializationHelper.SerializeToMemoryStream(changeEvent);
            EventData data = new EventData(strm);

            // send event data to Event Hub
            partitionedSender.Send(data);
        }
Beispiel #7
0
        }                                                                       //  USE CAREFULLY! VALIDATE SLOTS!

        private void Set(int slot, ItemStack stack)
        {
            //  Invoke an inventory change event
            InventoryChangeEvent e = new InventoryChangeEvent {
                reason = InventoryChangeReason.SET, slot = slot, itemStack = stack, inventory = this
            };

            OnInventoryChangeEvent?.Invoke(this, e);
            if (e.cancel == true)
            {
                return;
            }                               //  return if the event has been cancelled by any subscriber

            contents[e.slot] = e.itemStack;
            ValidateSlot(slot);
        }
Beispiel #8
0
        //  Attempt to add a stack to a specific slot with no overflow control
        public ItemStack Combine(int fromSlot, int toSlot)
        {
            int amount = Get(fromSlot).GetAmount();

            if (Get(toSlot) != null && Get(toSlot).GetItem() == Get(fromSlot).GetItem())
            {
                //  Get overflow/remainder that would be left after adding
                int overflow = Util.GetOverflow(Get(toSlot).GetAmount() + amount, 0, Get(toSlot).GetStackSize());

                //  Attempt adding the amount to the current matching stack
                Get(toSlot).ModifyAmount(amount);
                ValidateSlot(toSlot);

                //  Update the current stack to the remainder
                Get(fromSlot).SetAmountRaw(overflow);
                ValidateSlot(fromSlot);
            }
            else if (Get(toSlot) == null)
            {
                SetAt(toSlot, Get(fromSlot));
            }

            //  Invoke an inventory change event on the from slot
            InventoryChangeEvent e = new InventoryChangeEvent {
                reason = InventoryChangeReason.REMOVE, slot = fromSlot, itemStack = Get(toSlot), inventory = this
            };

            OnInventoryChangeEvent?.Invoke(this, e);
            if (e.cancel == true)
            {
                return(e.itemStack);
            }                                           //  return if the event has been cancelled by any subscriber

            //  Invoke an inventory change event on the to slot
            e = new InventoryChangeEvent {
                reason = InventoryChangeReason.ADD, slot = toSlot, itemStack = Get(fromSlot), inventory = this
            };
            OnInventoryChangeEvent?.Invoke(this, e);
            if (e.cancel == true)
            {
                return(e.itemStack);
            }                                           //  return if the event has been cancelled by any subscriber

            return(Get(fromSlot));
        }
Beispiel #9
0
        //  Attempt to remove an amount of item from the inventory
        public void Remove(Item item, int amount = 1)
        {
            List <int> matchingIndices = new List <int>();

            //  Find all matching stacks
            for (int i = 0; i < contents.Length; i++)
            {
                if (Get(i) != null && Get(i).GetItem() == item)
                {
                    matchingIndices.Add(i);
                }
            }

            //  Remove from matching stacks until there is none left
            foreach (int index in matchingIndices)
            {
                //  Get the overflow/remainder that would be left after removal
                int overflow = Util.GetOverflow(Get(index).GetAmount() - amount, 0, Get(index).GetStackSize());

                //  Attempt removing the amount from the current matching stack
                Get(index).ModifyAmount(-amount);

                //  Update the current amount to the remainder
                amount = Mathf.Abs(overflow);

                //  Invoke an inventory change event
                InventoryChangeEvent e = new InventoryChangeEvent {
                    reason = InventoryChangeReason.REMOVE, slot = index, itemStack = Get(index), inventory = this
                };
                OnInventoryChangeEvent?.Invoke(this, e);
                if (e.cancel == true)
                {
                    return;
                }                               //  return if the event has been cancelled by any subscriber

                if (amount <= 0)
                {
                    return;          //  Finished, early exit
                }
            }
        }
Beispiel #10
0
        //  Attempt to remove an amount from the specified slot
        public void RemoveAt(int slot, int amount = 1)
        {
            List <int> matchingIndices = new List <int>();

            if (Get(slot) != null)
            {
                Get(slot).ModifyAmount(-amount);
                ValidateSlot(slot);
            }

            //  Invoke an inventory change event
            InventoryChangeEvent e = new InventoryChangeEvent {
                reason = InventoryChangeReason.REMOVE, slot = slot, itemStack = Get(slot), inventory = this
            };

            OnInventoryChangeEvent?.Invoke(this, e);
            if (e.cancel == true)
            {
                return;
            }                               //  return if the event has been cancelled by any subscriber
        }
Beispiel #11
0
        public ItemStack AddAt(int slot, ItemStack stack)
        {
            int amount = stack.GetAmount();

            if (Get(slot) != null && Get(slot).GetItem() == stack.GetItem())
            {
                //  Get overflow/remainder that would be left after adding
                int overflow = Util.GetOverflow(Get(slot).GetAmount() + amount, 0, Get(slot).GetStackSize());

                //  Attempt adding the amount to the current matching stack
                Get(slot).ModifyAmount(amount);
                ValidateSlot(slot);

                //  Update the current stack to the remainder
                stack.SetAmountRaw(overflow);
            }
            else if (Get(slot) == null)
            {
                SetAt(slot, stack);
            }

            //  Invoke an inventory change event
            InventoryChangeEvent e = new InventoryChangeEvent {
                reason = InventoryChangeReason.ADD, slot = slot, itemStack = stack, inventory = this
            };

            OnInventoryChangeEvent?.Invoke(this, e);
            if (e.cancel == true)
            {
                return(e.itemStack);
            }                                           //  return if the event has been cancelled by any subscriber

            //  Update the stack with any changes by the event
            stack?.SetAmountRaw(e.itemStack.GetAmount());
            stack?.SetItem(e.itemStack.GetItem());

            return(stack);
        }
Beispiel #12
0
 // Called on a inventory change, count represents the amount of items inserted or removed
 public abstract void InventoryChange(Item item, uint count, InventoryChangeEvent eve);
Beispiel #13
0
 // Called on a inventory change, count represents the amount of items inserted or removed
 public override void InventoryChange(Item item, uint count, InventoryChangeEvent eve)
 {
 }
Beispiel #14
0
        public ItemStack Add(ItemStack stack)
        {
            int        amount          = stack.GetAmount();
            List <int> matchingIndices = new List <int>();

            //  Find all matching stacks
            for (int i = 0; i < contents.Length; i++)
            {
                if (Get(i) != null && Get(i).GetItem() == stack.GetItem())
                {
                    matchingIndices.Add(i);
                }
            }

            //  Combine matching stacks until there is none left
            foreach (int index in matchingIndices)
            {
                //  Get overflow/remainder that would be left after adding
                int overflow = Util.GetOverflow(Get(index).GetAmount() + amount, 0, Get(index).GetStackSize());

                //  Attempt adding the amount to the current matching stack
                Get(index).ModifyAmount(amount);

                //  Update the current stack to the remainder
                stack.SetAmountRaw(overflow);

                //  Invoke an inventory change event
                InventoryChangeEvent e = new InventoryChangeEvent {
                    reason = InventoryChangeReason.ADD, slot = index, itemStack = stack, inventory = this
                };
                OnInventoryChangeEvent?.Invoke(this, e);
                if (e.cancel == true)
                {
                    return(e.itemStack);
                }                                           //  return if the event has been cancelled by any subscriber

                //  Update the stack with any changes by the event
                stack.SetAmountRaw(e.itemStack.GetAmount());
                stack.SetItem(e.itemStack.GetItem());

                if (stack.IsValid() == false)
                {
                    return(stack);                         //  Finished, early exit
                }
            }

            //  Attempt to fill empty slots with any remainder until there is none left
            for (int i = 0; i < contents.Length; i++)
            {
                if (IsSlotValid(i) == false) //  Find empty slots
                {
                    //  Add a stack to the empty slot
                    Set(i, new ItemStack(stack.GetItem()));
                    Get(i).SetAmount(stack.GetAmount());

                    //  Cover case scenarios where the stack is larger than its size limits by breaking it into multiple stacks
                    int remainder = stack.GetAmount() - Get(i).GetAmount();

                    //  Update the current stack to the remainder that overflows the stack size
                    stack.SetAmountRaw(remainder);

                    //  Invoke an inventory change event
                    InventoryChangeEvent e = new InventoryChangeEvent {
                        reason = InventoryChangeReason.ADD, slot = i, itemStack = stack, inventory = this
                    };
                    OnInventoryChangeEvent?.Invoke(this, e);
                    if (e.cancel == true)
                    {
                        return(e.itemStack);
                    }                                           //  return if the event has been cancelled by any subscriber

                    if (stack.IsValid() == false)
                    {
                        return(stack);                         //  Finished, early exit
                    }
                }
            }

            return(stack);
        }