Class: GameItem

Inherits:
Object
  • Object
show all
Defined in:
lib/steam/community/game_item.rb

Overview

A module implementing basic functionality for classes representing an item in a game

Author:

  • Sebastian Staudt

Direct Known Subclasses

Dota2Item, Portal2Item, TF2Item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inventory, item_data) ⇒ GameItem

Creates a new instance of a GameItem with the given data

Parameters:

  • inventory (GameInventory)

    The inventory this item is contained in

  • item_data (Hash<Symbol, Object>)

    The data representing this item



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/steam/community/game_item.rb', line 88

def initialize(inventory, item_data)
  @inventory = inventory

  @defindex          = item_data[:defindex]
  @backpack_position = item_data[:inventory] & 0xffff
  @count             = item_data[:quantity]
  @craftable         = !!item_data[:flag_cannot_craft]
  @id                = item_data[:id]
  @item_class        = schema_data[:item_class]
  @item_set          = inventory.item_schema.item_sets[schema_data[:item_set]]
  @level             = item_data[:level]
  @name              = schema_data[:item_name]
  @original_id       = item_data[:original_id]
  @preliminary       = item_data[:inventory] & 0x40000000 != 0
  @quality           = inventory.item_schema.qualities[item_data[:quality]]
  @tradeable         = !!item_data[:flag_cannot_trade]
  @type              = schema_data[:item_type_name]

  if item_data.key? :origin
    @origin = inventory.item_schema.origins[item_data[:origin]]
  end

  attributes_data = schema_data[:attributes] || []
  attributes_data += item_data[:attributes] if item_data.key? :attributes

  @attributes = []
  attributes_data.each do |attribute_data|
    attribute_key = attribute_data[:defindex] || attribute_data[:name]

    unless attribute_key.nil?
      schema_attribute_data = inventory.item_schema.attributes[attribute_key]
      @attributes << attribute_data.merge(schema_attribute_data)
    end
  end
end

Instance Attribute Details

#attributesHash<Symbol, Object>? (readonly)

Return the attributes of this item

Returns:

  • (Hash<Symbol, Object>, nil)

    The attributes of this item



17
18
19
# File 'lib/steam/community/game_item.rb', line 17

def attributes
  @attributes
end

#backpack_positionFixnum (readonly)

Returns the position of this item in the player’s inventory

Returns:

  • (Fixnum)

    The position of this item in the player’s inventory



22
23
24
# File 'lib/steam/community/game_item.rb', line 22

def backpack_position
  @backpack_position
end

#countFixnum (readonly)

Returns the number of items the player owns of this item

Returns:

  • (Fixnum)

    The quantity of this item



27
28
29
# File 'lib/steam/community/game_item.rb', line 27

def count
  @count
end

#defindexFixnum (readonly)

Returns the index where the item is defined in the schema

Returns:

  • (Fixnum)

    The schema index of this item



32
33
34
# File 'lib/steam/community/game_item.rb', line 32

def defindex
  @defindex
end

#idFixnum (readonly)

Returns the ID of this item

Returns:

  • (Fixnum)

    The ID of this item



37
38
39
# File 'lib/steam/community/game_item.rb', line 37

def id
  @id
end

#inventoryGameInventory (readonly)

Returns the inventory this items belongs to

Returns:



42
43
44
# File 'lib/steam/community/game_item.rb', line 42

def inventory
  @inventory
end

#item_classString (readonly)

Returns the class of this item

Returns:

  • (String)

    The class of this item



47
48
49
# File 'lib/steam/community/game_item.rb', line 47

def item_class
  @item_class
end

#item_setHash<Symbol, Object>? (readonly)

Returns the item set this item belongs to

Returns:

  • (Hash<Symbol, Object>, nil)

    The set this item belongs to



52
53
54
# File 'lib/steam/community/game_item.rb', line 52

def item_set
  @item_set
end

#levelFixnum (readonly)

Returns the level of this item

Returns:

  • (Fixnum)

    The level of this item



57
58
59
# File 'lib/steam/community/game_item.rb', line 57

def level
  @level
end

#nameString (readonly)

Returns the level of this item

Returns:

  • (String)

    The level of this item



62
63
64
# File 'lib/steam/community/game_item.rb', line 62

def name
  @name
end

#originString (readonly)

Returns the origin of this item

Returns:

  • (String)

    The origin of this item



67
68
69
# File 'lib/steam/community/game_item.rb', line 67

def origin
  @origin
end

#original_idFixnum (readonly)

Returns the original ID of this item

Returns:

  • (Fixnum)

    The original ID of this item



72
73
74
# File 'lib/steam/community/game_item.rb', line 72

def original_id
  @original_id
end

#qualityString (readonly)

Returns the quality of this item

Returns:

  • (String)

    The quality of this item



77
78
79
# File 'lib/steam/community/game_item.rb', line 77

def quality
  @quality
end

#typeString (readonly)

Returns the type of this item

Returns:

  • (String)

    The type of this item



82
83
84
# File 'lib/steam/community/game_item.rb', line 82

def type
  @type
end

Instance Method Details

#craftable?Boolean

Returns whether this item is craftable

Returns:

  • (Boolean)

    ‘true` if this item is craftable



127
128
129
# File 'lib/steam/community/game_item.rb', line 127

def craftable?
  @craftable
end

#preliminary?Boolean

Returns whether this item is preliminary

Preliminary means that this item was just found or traded and has not yet been added to the inventory

Returns:

  • (Boolean)

    ‘true` if this item is preliminary



137
138
139
# File 'lib/steam/community/game_item.rb', line 137

def preliminary?
  @preliminary
end

#schema_dataHash<Symbol, Object>

Returns the data for this item that’s defined in the item schema

Returns:

  • (Hash<Symbol, Object>)

    The schema data for this item



144
145
146
# File 'lib/steam/community/game_item.rb', line 144

def schema_data
  inventory.item_schema.items[@defindex]
end

#tradeable?Boolean

Returns whether this item is tradeable

Returns:

  • (Boolean)

    ‘true` if this item is tradeable



151
152
153
# File 'lib/steam/community/game_item.rb', line 151

def tradeable?
  @tradeable
end