Class: RuneRb::Game::Item::Container

Inherits:
Object
  • Object
show all
Includes:
System::Log
Defined in:
app/game/item/container.rb

Overview

A container of ItemStacks.

Since:

  • 0.9.3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from System::Log

#class_name, #err, #err!, #log, #log!, #symbolize_file

Constructor Details

#initialize(capacity, stackable: false) ⇒ Container

Constructs a new Container.

Parameters:

  • capacity (Integer)

    the capacity of the Container

  • stackable (Boolean) (defaults to: false)

    should all ItemStacks in the Container be stackable?

Since:

  • 0.9.3


15
16
17
18
19
# File 'app/game/item/container.rb', line 15

def initialize(capacity, stackable: false)
  @stackable = stackable
  @limit = capacity
  empty!
end

Instance Attribute Details

#dataObject (readonly)

Internal container object for ItemStacks

Since:

  • 0.9.3


7
8
9
# File 'app/game/item/container.rb', line 7

def data
  @data
end

#limitObject (readonly)

Maximum number of slots that can be filled in this container.

Since:

  • 0.9.3


10
11
12
# File 'app/game/item/container.rb', line 10

def limit
  @limit
end

Instance Method Details

#add(item_stack) ⇒ Object

Attempts to add an ItemStack to the container

Parameters:

Since:

  • 0.9.3


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/game/item/container.rb', line 29

def add(item_stack)
  if item_stack.definition[:stackable] || @stackable
    if has?(item_stack.definition.id) && (item_stack.size + @data[slot_for(item_stack.definition.id)].size) < RuneRb::Game::Item::MAX_SIZE
      slot = slot_for(item_stack.definition.id)
      @data[slot].size += item_stack.size
    else
      slot = next_slot&.first
      return false if slot.nil? # Inventory full

      @data[slot] = item_stack
    end
  else
    slot = next_slot&.first
    return false if slot.nil? # Inventory full

    @data[slot] = item_stack
  end
  true
end

#at(slot) ⇒ Object

Returns the ItemStack at the given slot

Parameters:

  • slot (Integer)

    the slot to check

Since:

  • 0.9.3


110
111
112
# File 'app/game/item/container.rb', line 110

def at(slot)
  @data[slot]
end

#count(id) ⇒ Object

Returns a sum of the size of each ItemStack where the ID is equal to the specified id.

Parameters:

  • id (Integer)

Since:

  • 0.9.3


101
102
103
104
105
106
# File 'app/game/item/container.rb', line 101

def count(id)
  @data.inject(0) do |itr, slot_stack|
    itr += slot_stack[1]&.size if slot_stack[1]&.definition&.id == id
    itr
  end
end

#empty!Object

Clears all slots.

Since:

  • 0.9.3


115
116
117
# File 'app/game/item/container.rb', line 115

def empty!
  @data = {}.tap { |hash| (1..@limit).each { |itr| hash[itr] = nil } }
end

#from(data) ⇒ Object

Since:

  • 0.9.3


78
79
80
# File 'app/game/item/container.rb', line 78

def from(data)
  @data = data if data.is_a? Hash
end

#has?(id, at = nil, amt = 1) ⇒ Boolean

Checks if the container has an entry with the specified ID and optional amount.

Parameters:

  • id (Integer)

    the ID of the item

  • at (Integer) (defaults to: nil)

    an optional slot specifier

  • amt (Integer) (defaults to: 1)

    the amount of the item

Returns:

  • (Boolean)

    true if the container has an entry where the specified ID matches and the ItemStack size is greater than or equal to the specified amount.

Since:

  • 0.9.3


87
88
89
90
91
92
93
94
95
96
97
# File 'app/game/item/container.rb', line 87

def has?(id, at = nil, amt = 1)
  if at
    !@data[at].nil? && @data[at].id == id && @data[at].size >= amt
  else
    @data.any? do |_slot, stack|
      next if stack.nil?

      ((stack.definition.id == id) && (stack.size >= amt))
    end
  end
end

#inspectObject

Since:

  • 0.9.3


119
120
121
122
123
124
125
126
127
128
129
# File 'app/game/item/container.rb', line 119

def inspect
  itr = 0
  string = "\n"
  string << @data.inject('') do |str, values|
    itr += 1
    str << "\tS#{values[0]}:#{values[1]&.definition&.name} x #{values[1]&.size}\t|"
    str << "\n" if (itr % 4).zero?
    str
  end
  string
end

#remove(id, amt = 1) ⇒ Object

Attempts to remove a specified amount of items from the container

Parameters:

  • id (Integer)

    the item ID of the item to remove

  • amt (Integer) (defaults to: 1)

    the amount to remove.

Since:

  • 0.9.3


64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/game/item/container.rb', line 64

def remove(id, amt = 1)
  slot = slot_for(id)
  if RuneRb::System::Database::Item[id][:stackable] || @stackable
    (@data[slot].size - amt) < 1 ? @data[slot] = nil : @data[slot].size -= amt
    true
  else
    until amt.zero?
      @data[slot_for(id)] = nil
      amt -= 1
    end
    true
  end
end

#remove_at(slot) ⇒ Object

Removes the item at the specified slot.

Parameters:

  • slot (Integer)

    the slot to remove at.

Since:

  • 0.9.3


23
24
25
# File 'app/game/item/container.rb', line 23

def remove_at(slot)
  @data[slot] = nil
end

#swap(from, to) ⇒ Object

Attempts to swap item stacks from one slot to another

Parameters:

  • from (Integer)

    the slot number to swap from

  • to (Integer)

    the slot number to swap to

Since:

  • 0.9.3


52
53
54
55
56
57
58
59
# File 'app/game/item/container.rb', line 52

def swap(from, to)
  from += 1
  to += 1
  old = @data[from] if @data[from]
  new = @data[to] if @data[to]
  @data[from] = new
  @data[to] = old
end