Class: GameInventory

Inherits:
Object
  • Object
show all
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

Author:

  • Sebastian Staudt

Constant Summary collapse

@@schema_language =
'en'

Instance Attribute Summary collapse

Attributes included from Cacheable

#fetch_time

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cacheable

#cache, #fetched?, included

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

Parameters:

  • app_id (Fixnum)

    The application ID of the game

  • steam_id64 (Fixnum)

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

  • fetch (Boolean)

    if ‘true` the object’s data is fetched after creation

  • bypass_cache (Boolean)

    if ‘true` the object’s data is fetched again even if it has been cached already



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_idFixnum (readonly)

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

Returns:

  • (Fixnum)

    The application ID of the game



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

def app_id
  @app_id
end

#itemsArray<GameItem> (readonly)

Returns an array of all items in this players inventory.

Returns:

  • (Array<GameItem>)

    All items in the backpack



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

def items
  @items
end

#preliminary_itemsArray<GameItem> (readonly)

Returns an array of all items that this player just found or traded

Returns:

  • (Array<GameItem>)

    All preliminary items of the inventory



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

def preliminary_items
  @preliminary_items
end

#userSteamId (readonly)

Returns the Steam ID of the player owning this inventory

Returns:

  • (SteamId)

    The Steam ID of the owner of 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.

Parameters:

  • app_id (Fixnum)

    The application ID of the game

  • steam_id (Fixnum) (defaults to: nil)

    The 64bit Steam ID or vanity URL of the user

  • fetch (Boolean)

    if ‘true` the object’s data is fetched after creation

  • bypass_cache (Boolean)

    if ‘true` the object’s data is fetched again even if it has been cached already

Returns:

Raises:

  • (SteamCondenserException)

    if creating the inventory fails



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

Parameters:

  • language (String)

    The ISO 639-1 code of the schema language



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

Returns:

  • (GameItem)

    The item at the given position in the inventory



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

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

#fetchObject

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

#inspectString

Returns a short, human-readable string representation of this inventory

Returns:

  • (String)

    A 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_schemaGameItemSchema

Returns the item schema for this inventory

Returns:

  • (GameItemSchema)

    The item schema for the game this inventory belongs to



160
161
162
# File 'lib/steam/community/game_inventory.rb', line 160

def item_schema
  @item_schema ||= GameItemSchema.new app_id, @@schema_language
end

#sizeFixnum

Returns the number of items in the user’s inventory

Returns:

  • (Fixnum)

     The number of items in the inventory



167
168
169
# File 'lib/steam/community/game_inventory.rb', line 167

def size
  @items.size
end