Class: SteamCondenser::Community::GameInventory

Inherits:
Object
  • Object
show all
Includes:
Cacheable
Defined in:
lib/steam-condenser/community/game_inventory.rb,
lib/steam-condenser/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, #cache_id_value, #cache_ids, #cached_instance, #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


112
113
114
115
116
117
118
119
120
121
122
# File 'lib/steam-condenser/community/game_inventory.rb', line 112

def initialize(app_id, steam_id64)
  unless steam_id64.is_a? Fixnum
    steam_id64 = SteamId.resolve_vanity_url steam_id64.to_s
    raise SteamCondenser::Error.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


36
37
38
# File 'lib/steam-condenser/community/game_inventory.rb', line 36

def app_id
  @app_id
end

#itemsArray<GameItem> (readonly)

Returns an array of all items in this players inventory.


41
42
43
# File 'lib/steam-condenser/community/game_inventory.rb', line 41

def items
  @items
end

#preliminary_itemsArray<GameItem> (readonly)

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


46
47
48
# File 'lib/steam-condenser/community/game_inventory.rb', line 46

def preliminary_items
  @preliminary_items
end

#userSteamId (readonly)

Returns the Steam ID of the player owning this inventory


51
52
53
# File 'lib/steam-condenser/community/game_inventory.rb', line 51

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.

Raises:

  • (SteamCondenserException)

    if creating the inventory fails


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
92
93
94
95
# File 'lib/steam-condenser/community/game_inventory.rb', line 67

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 Dota2TestInventory::APP_ID
      cacheable_new = cacheable_new.bind Dota2TestInventory
    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')


100
101
102
# File 'lib/steam-condenser/community/game_inventory.rb', line 100

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


128
129
130
# File 'lib/steam-condenser/community/game_inventory.rb', line 128

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

#fetchObject

Updates the contents of the inventory using the Steam Web API


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/steam-condenser/community/game_inventory.rb', line 133

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


155
156
157
158
# File 'lib/steam-condenser/community/game_inventory.rb', line 155

def inspect
  "#<#{self.class}:#@app_id #@steam_id64 (#{size} items) - " +
  "#{fetch_time || 'not fetched'}>"
end

#item_schemaGameItemSchema

Returns the item schema for this inventory


164
165
166
# File 'lib/steam-condenser/community/game_inventory.rb', line 164

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

#sizeFixnum

Returns the number of items in the user's inventory


171
172
173
# File 'lib/steam-condenser/community/game_inventory.rb', line 171

def size
  @items.size
end