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

Author:

  • Sebastian Staudt

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

#itemsArray<GameItem> (readonly)

Returns an array of all items in this players inventory.

Returns:

  • (Array<GameItem>)

    All items in the backpack



18
19
20
# File 'lib/steam/community/game_inventory.rb', line 18

def items
  @items
end

#steam_id64String (readonly)

Returns the 64bit SteamID of the player owning this inventory

Returns:

  • (String)

    The 64bit SteamID



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’‘)

Parameters:

  •  The (String)

    ISO 639-1 code of the schema language



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).

Returns:

  • (GameItem)

    The item at the given position in the inventory



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

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

#app_idFixnum

Returns the application ID of the game this inventory class belongs to

Returns:

  • (Fixnum)

    The application ID of the game



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_schemaHash<String, Hash<String, Object>>

Returns the attribute schema

The schemas are fetched first if not done already

Returns:

  • (Hash<String, Hash<String, Object>>)

    The attribute schema for the game this inventory belongs to

See Also:

  • #update_schema


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

#fetchObject

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

Parameters:

  •  steam_id64 (Fixnum)

    The 64bit SteamID of the player to get the inventory for

  •  fetch_now (Boolean)

    if ‘true` the data will be fetched immediately



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_schemaHash<Fixnum, Hash<String, Object>>

Returns the item schema

The schemas are fetched first if not done already

Returns:

  • (Hash<Fixnum, Hash<String, Object>>)

    The item schema for the game this inventory belongs to

See Also:

  • #upde_schema


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

#qualitiesHash<Fixnum, String>

Returns the quality schema

The schemas are fetched first if not done already

Returns:

  • (Hash<Fixnum, String>)

    The quality schema for this game

See Also:

  • #update_schema


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

#sizeFixnum

Returns the number of items in the user’s inventory

Returns:

  • (Fixnum)

     The number of items in the inventory



123
124
125
# File 'lib/steam/community/game_inventory.rb', line 123

def size
  @items.size
end