Module: GameInventory
- Included in:
- Portal2Inventory, TF2BetaInventory, 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
-
#items ⇒ Array<GameItem>
readonly
Returns an array of all items in this players inventory.
-
#steam_id64 ⇒ String
readonly
Returns the 64bit SteamID of the player owning this inventory.
Class Method Summary collapse
-
.schema_language=(language) ⇒ Object
Sets the language the schema should be fetched in (default is: ‘’en’‘).
Instance Method Summary collapse
-
#[](index) ⇒ GameItem
Returns the item at the given position in the inventory.
-
#app_id ⇒ Fixnum
Returns the application ID of the game this inventory class belongs to.
-
#attribute_schema ⇒ Hash<String, Hash<String, Object>>
Returns the attribute schema.
-
#fetch ⇒ Object
Updates the contents of the inventory using Steam Web API.
-
#initialize(steam_id64, fetch_now = true) ⇒ Object
Creates a new inventory object for the given AppID and SteamID64.
-
#item_schema ⇒ Hash<Fixnum, Hash<String, Object>>
Returns the item schema.
-
#qualities ⇒ Hash<Fixnum, String>
Returns the quality schema.
-
#size ⇒ Fixnum
Returns the number of items in the user’s inventory.
Instance Attribute Details
#items ⇒ Array<GameItem> (readonly)
Returns an array of all items in this players inventory.
18 19 20 |
# File 'lib/steam/community/game_inventory.rb', line 18 def items @items end |
#steam_id64 ⇒ String (readonly)
Returns the 64bit SteamID of the player owning this inventory
23 24 25 |
# File 'lib/steam/community/game_inventory.rb', line 23 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’‘)
36 37 38 |
# File 'lib/steam/community/game_inventory.rb', line 36 def self.schema_language=(language) @@schema_language = language end |
Instance Method Details
#[](index) ⇒ GameItem
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).
57 58 59 |
# File 'lib/steam/community/game_inventory.rb', line 57 def [](index) @items[index - 1] end |
#app_id ⇒ Fixnum
Returns the application ID of the game this inventory class belongs to
64 65 66 |
# File 'lib/steam/community/game_inventory.rb', line 64 def app_id self.class.send :class_variable_get, :@@app_id end |
#attribute_schema ⇒ Hash<String, Hash<String, Object>>
Returns the attribute schema
The schemas are fetched first if not done already
75 76 77 78 79 |
# File 'lib/steam/community/game_inventory.rb', line 75 def attribute_schema update_schema unless @@attribute_schema.key? app_id @@attribute_schema[app_id] end |
#fetch ⇒ Object
Updates the contents of the inventory using Steam Web API
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/steam/community/game_inventory.rb', line 82 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
47 48 49 50 51 |
# File 'lib/steam/community/game_inventory.rb', line 47 def initialize(steam_id64, fetch_now = true) @steam_id64 = steam_id64 super fetch_now end |
#item_schema ⇒ Hash<Fixnum, Hash<String, Object>>
Returns the item schema
The schemas are fetched first if not done already
102 103 104 105 106 |
# File 'lib/steam/community/game_inventory.rb', line 102 def item_schema update_schema unless @@item_schema.key? app_id @@item_schema[app_id] end |
#qualities ⇒ Hash<Fixnum, String>
Returns the quality schema
The schemas are fetched first if not done already
114 115 116 117 118 |
# File 'lib/steam/community/game_inventory.rb', line 114 def qualities update_schema unless @@qualities.key? app_id @@qualities[app_id] end |
#size ⇒ Fixnum
Returns the number of items in the user’s inventory
123 124 125 |
# File 'lib/steam/community/game_inventory.rb', line 123 def size @items.size end |