Class: GameInventory
- Inherits:
-
Object
- Object
- GameInventory
- Includes:
- Cacheable
- Defined in:
- lib/steam/community/game_inventory.rb,
lib/steam/community/game_inventory.rb
Overview
Provides basic functionality to represent an inventory of player in a game
Direct Known Subclasses
Dota2BetaInventory, Dota2Inventory, Portal2Inventory, TF2BetaInventory, TF2Inventory
Constant Summary collapse
- @@schema_language =
'en'
Instance Attribute Summary collapse
-
#app_id ⇒ Fixnum
readonly
Returns the application ID of the game this inventory class belongs to.
-
#items ⇒ Array<GameItem>
readonly
Returns an array of all items in this players inventory.
-
#preliminary_items ⇒ Array<GameItem>
readonly
Returns an array of all items that this player just found or traded.
-
#user ⇒ SteamId
readonly
Returns the Steam ID of the player owning this inventory.
Attributes included from Cacheable
Class Method Summary collapse
-
.new(app_id, steam_id, args, fetch = true, bypass_cache = false) ⇒ GameInventory
This is a wrapper around all subclasses of ‘GameInventory` so that an instance of correct subclass is returned for a given application ID.
-
.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.
-
#fetch ⇒ Object
Updates the contents of the inventory using the Steam Web API.
-
#initialize(app_id, steam_id64, fetch = true, bypass_cache = false) ⇒ GameInventory
constructor
Creates a new inventory object for the given AppID and SteamID64.
-
#inspect ⇒ String
Returns a short, human-readable string representation of this inventory.
-
#item_schema ⇒ GameItemSchema
Returns the item schema for this inventory.
-
#size ⇒ Fixnum
Returns the number of items in the user’s inventory.
Methods included from Cacheable
Constructor Details
#initialize(app_id, steam_id64, fetch = true, bypass_cache = false) ⇒ GameInventory
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
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/steam/community/game_inventory.rb', line 108 def initialize(app_id, steam_id64) unless steam_id64.is_a? Fixnum steam_id64 = SteamId.resolve_vanity_url steam_id64.to_s raise SteamCondenserError.new 'User not found' if steam_id64.nil? end @app_id = app_id @items = [] @steam_id64 = steam_id64 @user = SteamId.new steam_id64, false end |
Instance Attribute Details
#app_id ⇒ Fixnum (readonly)
Returns the application ID of the game this inventory class belongs to
32 33 34 |
# File 'lib/steam/community/game_inventory.rb', line 32 def app_id @app_id end |
#items ⇒ Array<GameItem> (readonly)
Returns an array of all items in this players inventory.
37 38 39 |
# File 'lib/steam/community/game_inventory.rb', line 37 def items @items end |
#preliminary_items ⇒ Array<GameItem> (readonly)
Returns an array of all items that this player just found or traded
42 43 44 |
# File 'lib/steam/community/game_inventory.rb', line 42 def preliminary_items @preliminary_items end |
#user ⇒ SteamId (readonly)
Returns the Steam ID of the player owning this inventory
47 48 49 |
# File 'lib/steam/community/game_inventory.rb', line 47 def user @user end |
Class Method Details
.new(app_id, steam_id, args, fetch = true, bypass_cache = false) ⇒ GameInventory
This is a wrapper around all subclasses of ‘GameInventory` so that an instance of correct subclass is returned for a given application ID. If there’s no specific subclass for an application ID exists, a generic instance of ‘GameInventory` is created.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/steam/community/game_inventory.rb', line 63 def self.new(app_id, steam_id = nil, *args) args = args.unshift steam_id unless steam_id.nil? if self == GameInventory raise ArgumentError, 'wrong number of arguments (1 for 2)' if args.empty? else args = args.unshift app_id app_id = self::APP_ID end cacheable_new = Cacheable::ClassMethods.instance_method :new case app_id when Dota2BetaInventory::APP_ID cacheable_new = cacheable_new.bind Dota2BetaInventory when Dota2Inventory::APP_ID cacheable_new = cacheable_new.bind Dota2Inventory when Portal2Inventory::APP_ID cacheable_new = cacheable_new.bind Portal2Inventory when TF2BetaInventory::APP_ID cacheable_new = cacheable_new.bind TF2BetaInventory when TF2Inventory::APP_ID cacheable_new = cacheable_new.bind TF2Inventory else cacheable_new = cacheable_new.bind GameInventory return cacheable_new.call app_id, *args end cacheable_new.call *args end |
.schema_language=(language) ⇒ Object
Sets the language the schema should be fetched in (default is: ‘’en’‘)
96 97 98 |
# File 'lib/steam/community/game_inventory.rb', line 96 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).
124 125 126 |
# File 'lib/steam/community/game_inventory.rb', line 124 def [](index) @items[index - 1] end |
#fetch ⇒ Object
Updates the contents of the inventory using the Steam Web API
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/steam/community/game_inventory.rb', line 129 def fetch params = { :SteamID => @user.steam_id64 } result = WebApi.json! "IEconItems_#@app_id", 'GetPlayerItems', 1, params item_class = self.class.instance_variable_get :@item_class @items = [] @preliminary_items = [] result[:items].each do |item_data| unless item_data.nil? item = item_class.new(self, item_data) if item.preliminary? @preliminary_items << item else @items[item.backpack_position - 1] = item end end end end |
#inspect ⇒ String
Returns a short, human-readable string representation of this inventory
151 152 153 154 |
# File 'lib/steam/community/game_inventory.rb', line 151 def inspect "#<#{self.class}:#@app_id #@steam_id64 (#{size} items) - " + "#{fetch_time || 'not fetched'}>" end |
#item_schema ⇒ GameItemSchema
Returns the item schema for this inventory
160 161 162 |
# File 'lib/steam/community/game_inventory.rb', line 160 def item_schema @item_schema ||= GameItemSchema.new app_id, @@schema_language end |
#size ⇒ Fixnum
Returns the number of items in the user’s inventory
167 168 169 |
# File 'lib/steam/community/game_inventory.rb', line 167 def size @items.size end |