Module: GameInventory

Included in:
Portal2Inventory, TF2Inventory
Defined in:
lib/steam/community/game_inventory.rb

Overview

Provides basic functionality to represent an inventory of player in a game

Constant Summary collapse

@@attribute_schema =
{}
@@item_schema =
{}
@@qualities =
{}
@@schema_language =
'en'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



13
14
15
# File 'lib/steam/community/game_inventory.rb', line 13

def items
  @items
end

#steam_id64Object (readonly)

Returns the value of attribute steam_id64.



13
14
15
# File 'lib/steam/community/game_inventory.rb', line 13

def steam_id64
  @steam_id64
end

Class Method Details

.schema_language=(language) ⇒ Object

Sets the language the schema should be fetched in (default is: ‘en’)



24
25
26
# File 'lib/steam/community/game_inventory.rb', line 24

def self.schema_language=(language)
  @@schema_language = language
end

Instance Method Details

#[](index) ⇒ Object

Returns the item at the given position in the inventory. The positions range from 1 to 100 instead of the usual array indices (0 to 99).



39
40
41
# File 'lib/steam/community/game_inventory.rb', line 39

def [](index)
  @items[index - 1]
end

#app_idObject

Returns the AppID of the game this inventory class belongs to



44
45
46
# File 'lib/steam/community/game_inventory.rb', line 44

def app_id
  self.class.send :class_variable_get, :@@app_id
end

#attribute_schemaObject

Returns the attribute schema

The schemas are fetched first if not done already



51
52
53
54
55
# File 'lib/steam/community/game_inventory.rb', line 51

def attribute_schema
  update_schema unless @@attribute_schema.key? app_id

  @@attribute_schema[app_id]
end

#fetchObject

Updates the contents of the inventory using Steam Web API



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/steam/community/game_inventory.rb', line 58

def fetch
  result = WebApi.json!("IEconItems_#{app_id}", 'GetPlayerItems', 1, { :SteamID => @steam_id64 })
  item_class = self.class.send :class_variable_get, :@@item_class

  @items = []
  result[:items].each do |item_data|
    unless item_data.nil?
      item = item_class.new(self, item_data)
      @items[item.backpack_position - 1] = item
    end
  end
end

#initialize(steam_id64, fetch_now = true) ⇒ Object

Creates a new inventory object for the given AppID and SteamID64. This calls update to fetch the data and create the item instances contained in this players backpack



31
32
33
34
35
# File 'lib/steam/community/game_inventory.rb', line 31

def initialize(steam_id64, fetch_now = true)
  @steam_id64 = steam_id64

  super fetch_now
end

#item_schemaObject

Returns the item schema

The schemas are fetched first if not done already



74
75
76
77
78
# File 'lib/steam/community/game_inventory.rb', line 74

def item_schema
  update_schema unless @@item_schema.key? app_id

  @@item_schema[app_id]
end

#qualitiesObject

Returns the quality schema

The schemas are fetched first if not done already



83
84
85
86
87
# File 'lib/steam/community/game_inventory.rb', line 83

def qualities
  update_schema unless @@qualities.key? app_id

  @@qualities[app_id]
end

#sizeObject

Returns the number of items in the user’s inventory



90
91
92
# File 'lib/steam/community/game_inventory.rb', line 90

def size
  @items.size
end