steam-condenser 1.3.5 → 1.3.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +228 -0
  3. data/CONTRIBUTING.md +59 -0
  4. data/Gemfile.lock +12 -13
  5. data/README.md +9 -4
  6. data/lib/steam-condenser/version.rb +1 -1
  7. data/lib/steam/community/alien_swarm/alien_swarm_mission.rb +10 -9
  8. data/lib/steam/community/alien_swarm/alien_swarm_stats.rb +74 -72
  9. data/lib/steam/community/css/css_map.rb +2 -2
  10. data/lib/steam/community/css/css_stats.rb +54 -56
  11. data/lib/steam/community/css/css_weapon.rb +4 -4
  12. data/lib/steam/community/game_item.rb +2 -3
  13. data/lib/steam/community/game_leaderboard.rb +22 -15
  14. data/lib/steam/community/l4d/abstract_l4d_stats.rb +64 -61
  15. data/lib/steam/community/l4d/l4d2_stats.rb +26 -21
  16. data/lib/steam/community/steam_id.rb +8 -11
  17. data/lib/steam/community/web_api.rb +19 -14
  18. data/lib/steam/packets/s2a_info2_packet.rb +16 -14
  19. data/lib/steam/packets/s2a_info_detailed_packet.rb +24 -19
  20. data/lib/steam/packets/steam_packet_factory.rb +7 -2
  21. data/lib/steam/servers/game_server.rb +3 -2
  22. data/lib/steam/servers/master_server.rb +1 -1
  23. data/lib/steam/sockets/source_socket.rb +4 -9
  24. data/pkg/steam-condenser-1.3.5.gem +0 -0
  25. data/pkg/steam-condenser-1.3.5/Gemfile +7 -0
  26. data/pkg/steam-condenser-1.3.5/Gemfile.lock +45 -0
  27. data/pkg/steam-condenser-1.3.5/LICENSE +25 -0
  28. data/pkg/steam-condenser-1.3.5/README.md +70 -0
  29. data/pkg/steam-condenser-1.3.5/Rakefile +46 -0
  30. data/pkg/steam-condenser-1.3.5/lib/core_ext/stringio.rb +91 -0
  31. data/pkg/steam-condenser-1.3.5/lib/errors/packet_format_error.rb +13 -0
  32. data/pkg/steam-condenser-1.3.5/lib/errors/rcon_ban_error.rb +22 -0
  33. data/pkg/steam-condenser-1.3.5/lib/errors/rcon_no_auth_error.rb +21 -0
  34. data/pkg/steam-condenser-1.3.5/lib/errors/steam_condenser_error.rb +26 -0
  35. data/pkg/steam-condenser-1.3.5/lib/errors/timeout_error.rb +28 -0
  36. data/pkg/steam-condenser-1.3.5/lib/errors/web_api_error.rb +57 -0
  37. data/pkg/steam-condenser-1.3.5/lib/steam-condenser.rb +18 -0
  38. data/pkg/steam-condenser-1.3.5/lib/steam-condenser/community.rb +24 -0
  39. data/pkg/steam-condenser-1.3.5/lib/steam-condenser/servers.rb +24 -0
  40. data/pkg/steam-condenser-1.3.5/lib/steam-condenser/version.rb +11 -0
  41. data/pkg/steam-condenser-1.3.5/lib/steam/community/alien_swarm/alien_swarm_mission.rb +119 -0
  42. data/pkg/steam-condenser-1.3.5/lib/steam/community/alien_swarm/alien_swarm_stats.rb +186 -0
  43. data/pkg/steam-condenser-1.3.5/lib/steam/community/alien_swarm/alien_swarm_weapon.rb +49 -0
  44. data/pkg/steam-condenser-1.3.5/lib/steam/community/app_news.rb +133 -0
  45. data/pkg/steam-condenser-1.3.5/lib/steam/community/cacheable.rb +199 -0
  46. data/pkg/steam-condenser-1.3.5/lib/steam/community/css/css_map.rb +61 -0
  47. data/pkg/steam-condenser-1.3.5/lib/steam/community/css/css_stats.rb +142 -0
  48. data/pkg/steam-condenser-1.3.5/lib/steam/community/css/css_weapon.rb +69 -0
  49. data/pkg/steam-condenser-1.3.5/lib/steam/community/defense_grid/defense_grid_stats.rb +238 -0
  50. data/pkg/steam-condenser-1.3.5/lib/steam/community/dods/dods_class.rb +90 -0
  51. data/pkg/steam-condenser-1.3.5/lib/steam/community/dods/dods_stats.rb +62 -0
  52. data/pkg/steam-condenser-1.3.5/lib/steam/community/dods/dods_weapon.rb +67 -0
  53. data/pkg/steam-condenser-1.3.5/lib/steam/community/dota2/dota2_beta_inventory.rb +30 -0
  54. data/pkg/steam-condenser-1.3.5/lib/steam/community/dota2/dota2_inventory.rb +30 -0
  55. data/pkg/steam-condenser-1.3.5/lib/steam/community/dota2/dota2_item.rb +33 -0
  56. data/pkg/steam-condenser-1.3.5/lib/steam/community/game_achievement.rb +109 -0
  57. data/pkg/steam-condenser-1.3.5/lib/steam/community/game_class.rb +22 -0
  58. data/pkg/steam-condenser-1.3.5/lib/steam/community/game_inventory.rb +171 -0
  59. data/pkg/steam-condenser-1.3.5/lib/steam/community/game_item.rb +156 -0
  60. data/pkg/steam-condenser-1.3.5/lib/steam/community/game_item_schema.rb +131 -0
  61. data/pkg/steam-condenser-1.3.5/lib/steam/community/game_leaderboard.rb +205 -0
  62. data/pkg/steam-condenser-1.3.5/lib/steam/community/game_leaderboard_entry.rb +43 -0
  63. data/pkg/steam-condenser-1.3.5/lib/steam/community/game_stats.rb +175 -0
  64. data/pkg/steam-condenser-1.3.5/lib/steam/community/game_weapon.rb +41 -0
  65. data/pkg/steam-condenser-1.3.5/lib/steam/community/l4d/abstract_l4d_stats.rb +185 -0
  66. data/pkg/steam-condenser-1.3.5/lib/steam/community/l4d/abstract_l4d_weapon.rb +56 -0
  67. data/pkg/steam-condenser-1.3.5/lib/steam/community/l4d/l4d2_map.rb +90 -0
  68. data/pkg/steam-condenser-1.3.5/lib/steam/community/l4d/l4d2_stats.rb +174 -0
  69. data/pkg/steam-condenser-1.3.5/lib/steam/community/l4d/l4d2_weapon.rb +38 -0
  70. data/pkg/steam-condenser-1.3.5/lib/steam/community/l4d/l4d_explosive.rb +34 -0
  71. data/pkg/steam-condenser-1.3.5/lib/steam/community/l4d/l4d_map.rb +65 -0
  72. data/pkg/steam-condenser-1.3.5/lib/steam/community/l4d/l4d_stats.rb +73 -0
  73. data/pkg/steam-condenser-1.3.5/lib/steam/community/l4d/l4d_weapon.rb +26 -0
  74. data/pkg/steam-condenser-1.3.5/lib/steam/community/portal2/portal2_inventory.rb +30 -0
  75. data/pkg/steam-condenser-1.3.5/lib/steam/community/portal2/portal2_item.rb +55 -0
  76. data/pkg/steam-condenser-1.3.5/lib/steam/community/portal2/portal2_stats.rb +33 -0
  77. data/pkg/steam-condenser-1.3.5/lib/steam/community/steam_game.rb +193 -0
  78. data/pkg/steam-condenser-1.3.5/lib/steam/community/steam_group.rb +129 -0
  79. data/pkg/steam-condenser-1.3.5/lib/steam/community/steam_id.rb +498 -0
  80. data/pkg/steam-condenser-1.3.5/lib/steam/community/tf2/tf2_beta_inventory.rb +28 -0
  81. data/pkg/steam-condenser-1.3.5/lib/steam/community/tf2/tf2_class.rb +92 -0
  82. data/pkg/steam-condenser-1.3.5/lib/steam/community/tf2/tf2_class_factory.rb +40 -0
  83. data/pkg/steam-condenser-1.3.5/lib/steam/community/tf2/tf2_engineer.rb +43 -0
  84. data/pkg/steam-condenser-1.3.5/lib/steam/community/tf2/tf2_golden_wrench.rb +70 -0
  85. data/pkg/steam-condenser-1.3.5/lib/steam/community/tf2/tf2_inventory.rb +30 -0
  86. data/pkg/steam-condenser-1.3.5/lib/steam/community/tf2/tf2_item.rb +55 -0
  87. data/pkg/steam-condenser-1.3.5/lib/steam/community/tf2/tf2_medic.rb +35 -0
  88. data/pkg/steam-condenser-1.3.5/lib/steam/community/tf2/tf2_sniper.rb +28 -0
  89. data/pkg/steam-condenser-1.3.5/lib/steam/community/tf2/tf2_spy.rb +41 -0
  90. data/pkg/steam-condenser-1.3.5/lib/steam/community/tf2/tf2_stats.rb +77 -0
  91. data/pkg/steam-condenser-1.3.5/lib/steam/community/web_api.rb +130 -0
  92. data/pkg/steam-condenser-1.3.5/lib/steam/community/xml_data.rb +27 -0
  93. data/pkg/steam-condenser-1.3.5/lib/steam/packets/a2m_get_servers_batch2_packet.rb +59 -0
  94. data/pkg/steam-condenser-1.3.5/lib/steam/packets/a2s_info_packet.rb +24 -0
  95. data/pkg/steam-condenser-1.3.5/lib/steam/packets/a2s_player_packet.rb +31 -0
  96. data/pkg/steam-condenser-1.3.5/lib/steam/packets/a2s_rules_packet.rb +32 -0
  97. data/pkg/steam-condenser-1.3.5/lib/steam/packets/a2s_serverquery_getchallenge_packet.rb +25 -0
  98. data/pkg/steam-condenser-1.3.5/lib/steam/packets/c2m_checkmd5_packet.rb +31 -0
  99. data/pkg/steam-condenser-1.3.5/lib/steam/packets/m2a_server_batch_packet.rb +52 -0
  100. data/pkg/steam-condenser-1.3.5/lib/steam/packets/m2c_isvalidmd5_packet.rb +34 -0
  101. data/pkg/steam-condenser-1.3.5/lib/steam/packets/m2s_requestrestart_packet.rb +34 -0
  102. data/pkg/steam-condenser-1.3.5/lib/steam/packets/rcon/rcon_auth_request.rb +28 -0
  103. data/pkg/steam-condenser-1.3.5/lib/steam/packets/rcon/rcon_auth_response.rb +30 -0
  104. data/pkg/steam-condenser-1.3.5/lib/steam/packets/rcon/rcon_exec_request.rb +28 -0
  105. data/pkg/steam-condenser-1.3.5/lib/steam/packets/rcon/rcon_exec_response.rb +37 -0
  106. data/pkg/steam-condenser-1.3.5/lib/steam/packets/rcon/rcon_goldsrc_request.rb +35 -0
  107. data/pkg/steam-condenser-1.3.5/lib/steam/packets/rcon/rcon_goldsrc_response.rb +34 -0
  108. data/pkg/steam-condenser-1.3.5/lib/steam/packets/rcon/rcon_packet.rb +57 -0
  109. data/pkg/steam-condenser-1.3.5/lib/steam/packets/rcon/rcon_packet_factory.rb +43 -0
  110. data/pkg/steam-condenser-1.3.5/lib/steam/packets/rcon/rcon_terminator.rb +29 -0
  111. data/pkg/steam-condenser-1.3.5/lib/steam/packets/request_with_challenge.rb +19 -0
  112. data/pkg/steam-condenser-1.3.5/lib/steam/packets/s2a_info2_packet.rb +74 -0
  113. data/pkg/steam-condenser-1.3.5/lib/steam/packets/s2a_info_base_packet.rb +25 -0
  114. data/pkg/steam-condenser-1.3.5/lib/steam/packets/s2a_info_detailed_packet.rb +59 -0
  115. data/pkg/steam-condenser-1.3.5/lib/steam/packets/s2a_logstring_packet.rb +30 -0
  116. data/pkg/steam-condenser-1.3.5/lib/steam/packets/s2a_player_packet.rb +44 -0
  117. data/pkg/steam-condenser-1.3.5/lib/steam/packets/s2a_rules_packet.rb +48 -0
  118. data/pkg/steam-condenser-1.3.5/lib/steam/packets/s2c_challenge_packet.rb +35 -0
  119. data/pkg/steam-condenser-1.3.5/lib/steam/packets/s2m_heartbeat2_packet.rb +72 -0
  120. data/pkg/steam-condenser-1.3.5/lib/steam/packets/steam_packet.rb +52 -0
  121. data/pkg/steam-condenser-1.3.5/lib/steam/packets/steam_packet_factory.rb +108 -0
  122. data/pkg/steam-condenser-1.3.5/lib/steam/servers/game_server.rb +392 -0
  123. data/pkg/steam-condenser-1.3.5/lib/steam/servers/goldsrc_server.rb +74 -0
  124. data/pkg/steam-condenser-1.3.5/lib/steam/servers/master_server.rb +188 -0
  125. data/pkg/steam-condenser-1.3.5/lib/steam/servers/server.rb +115 -0
  126. data/pkg/steam-condenser-1.3.5/lib/steam/servers/source_server.rb +117 -0
  127. data/pkg/steam-condenser-1.3.5/lib/steam/sockets/goldsrc_socket.rb +142 -0
  128. data/pkg/steam-condenser-1.3.5/lib/steam/sockets/master_server_socket.rb +34 -0
  129. data/pkg/steam-condenser-1.3.5/lib/steam/sockets/rcon_socket.rb +103 -0
  130. data/pkg/steam-condenser-1.3.5/lib/steam/sockets/source_socket.rb +78 -0
  131. data/pkg/steam-condenser-1.3.5/lib/steam/sockets/steam_socket.rb +91 -0
  132. data/pkg/steam-condenser-1.3.5/lib/steam/steam_player.rb +143 -0
  133. data/pkg/steam-condenser-1.3.5/steam-condenser.gemspec +26 -0
  134. data/pkg/steam-condenser-1.3.5/test/core_ext/test_stringio.rb +59 -0
  135. data/pkg/steam-condenser-1.3.5/test/fixtures/invalid.xml +2 -0
  136. data/pkg/steam-condenser-1.3.5/test/fixtures/sonofthor.xml +882 -0
  137. data/pkg/steam-condenser-1.3.5/test/fixtures/status_goldsrc +3 -0
  138. data/pkg/steam-condenser-1.3.5/test/fixtures/status_source +3 -0
  139. data/pkg/steam-condenser-1.3.5/test/fixtures/valve-members.xml +231 -0
  140. data/pkg/steam-condenser-1.3.5/test/helper.rb +48 -0
  141. data/pkg/steam-condenser-1.3.5/test/steam/community/test_cacheable.rb +100 -0
  142. data/pkg/steam-condenser-1.3.5/test/steam/community/test_steam_group.rb +83 -0
  143. data/pkg/steam-condenser-1.3.5/test/steam/community/test_steam_id.rb +144 -0
  144. data/pkg/steam-condenser-1.3.5/test/steam/community/test_web_api.rb +98 -0
  145. data/pkg/steam-condenser-1.3.5/test/steam/packets/test_steam_packet.rb +37 -0
  146. data/pkg/steam-condenser-1.3.5/test/steam/servers/test_game_server.rb +296 -0
  147. data/pkg/steam-condenser-1.3.5/test/steam/servers/test_goldsrc_server.rb +59 -0
  148. data/pkg/steam-condenser-1.3.5/test/steam/servers/test_master_server.rb +131 -0
  149. data/pkg/steam-condenser-1.3.5/test/steam/servers/test_server.rb +72 -0
  150. data/pkg/steam-condenser-1.3.5/test/steam/servers/test_source_server.rb +134 -0
  151. data/pkg/steam-condenser-1.3.5/test/steam/sockets/test_goldsrc_socket.rb +127 -0
  152. data/pkg/steam-condenser-1.3.5/test/steam/sockets/test_master_server_socket.rb +42 -0
  153. data/pkg/steam-condenser-1.3.5/test/steam/sockets/test_rcon_socket.rb +118 -0
  154. data/pkg/steam-condenser-1.3.5/test/steam/sockets/test_source_socket.rb +77 -0
  155. data/pkg/steam-condenser-1.3.5/test/steam/sockets/test_steam_socket.rb +97 -0
  156. data/steam-condenser.gemspec +2 -3
  157. data/test/steam/community/test_cacheable.rb +3 -0
  158. data/test/steam/community/test_web_api.rb +15 -2
  159. data/test/steam/packets/test_steam_packet.rb +1 -1
  160. data/test/steam/sockets/test_source_socket.rb +1 -1
  161. metadata +151 -27
@@ -0,0 +1,171 @@
1
+ # This code is free software; you can redistribute it and/or modify it under
2
+ # the terms of the new BSD License.
3
+ #
4
+ # Copyright (c) 2011-2013, Sebastian Staudt
5
+
6
+ require 'steam/community/cacheable'
7
+ require 'steam/community/game_item'
8
+ require 'steam/community/game_item_schema'
9
+ require 'steam/community/steam_id'
10
+ require 'steam/community/web_api'
11
+
12
+ class GameInventory
13
+ end
14
+
15
+ require 'steam/community/dota2/dota2_beta_inventory'
16
+ require 'steam/community/dota2/dota2_inventory'
17
+ require 'steam/community/portal2/portal2_inventory'
18
+ require 'steam/community/tf2/tf2_beta_inventory'
19
+ require 'steam/community/tf2/tf2_inventory'
20
+
21
+ # Provides basic functionality to represent an inventory of player in a game
22
+ #
23
+ # @author Sebastian Staudt
24
+ class GameInventory
25
+
26
+ include Cacheable
27
+ cacheable_with_ids [:app_id, :steam_id64]
28
+
29
+ # Returns the application ID of the game this inventory class belongs to
30
+ #
31
+ # @return [Fixnum] The application ID of the game
32
+ attr_reader :app_id
33
+
34
+ # Returns an array of all items in this players inventory.
35
+ #
36
+ # @return [Array<GameItem>] All items in the backpack
37
+ attr_reader :items
38
+
39
+ # Returns an array of all items that this player just found or traded
40
+ #
41
+ # @return [Array<GameItem>] All preliminary items of the inventory
42
+ attr_reader :preliminary_items
43
+
44
+ # Returns the Steam ID of the player owning this inventory
45
+ #
46
+ # @return [SteamId] The Steam ID of the owner of this inventory
47
+ attr_reader :user
48
+
49
+ @@item_class = GameItem
50
+
51
+ @@schema_language = 'en'
52
+
53
+ # This is a wrapper around all subclasses of `GameInventory` so that an
54
+ # instance of correct subclass is returned for a given application ID. If
55
+ # there's no specific subclass for an application ID exists, a generic
56
+ # instance of `GameInventory` is created.
57
+ #
58
+ # @param [Fixnum] app_id The application ID of the game
59
+ # @param [Fixnum] steam_id The 64bit Steam ID or vanity URL of the user
60
+ # @return [GameInventory] The inventory for the given user and game
61
+ # @raise [SteamCondenserException] if creating the inventory fails
62
+ # @macro cacheable
63
+ def self.new(app_id, steam_id = nil, *args)
64
+ args = args.unshift steam_id unless steam_id.nil?
65
+ if self == GameInventory
66
+ raise ArgumentError, 'wrong number of arguments (1 for 2)' if args.empty?
67
+ else
68
+ args = args.unshift app_id
69
+ app_id = self::APP_ID
70
+ end
71
+
72
+ cacheable_new = Cacheable::ClassMethods.instance_method :new
73
+
74
+ case app_id
75
+ when Dota2BetaInventory::APP_ID
76
+ cacheable_new = cacheable_new.bind Dota2BetaInventory
77
+ when Dota2Inventory::APP_ID
78
+ cacheable_new = cacheable_new.bind Dota2Inventory
79
+ when Portal2Inventory::APP_ID
80
+ cacheable_new = cacheable_new.bind Portal2Inventory
81
+ when TF2BetaInventory::APP_ID
82
+ cacheable_new = cacheable_new.bind TF2BetaInventory
83
+ when TF2Inventory::APP_ID
84
+ cacheable_new = cacheable_new.bind TF2Inventory
85
+ else
86
+ cacheable_new = cacheable_new.bind GameInventory
87
+ return cacheable_new.call app_id, *args
88
+ end
89
+
90
+ cacheable_new.call *args
91
+ end
92
+
93
+ # Sets the language the schema should be fetched in (default is: `'en'`)
94
+ #
95
+ # @param [String] language The ISO 639-1 code of the schema language
96
+ def self.schema_language=(language)
97
+ @@schema_language = language
98
+ end
99
+
100
+ # Creates a new inventory object for the given AppID and SteamID64. This
101
+ # calls update to fetch the data and create the item instances contained in
102
+ # this players backpack
103
+ #
104
+ # @param [Fixnum] app_id The application ID of the game
105
+ # @param [Fixnum] steam_id64 The 64bit SteamID of the player to get the
106
+ # inventory for
107
+ # @macro cacheable
108
+ def initialize(app_id, steam_id64)
109
+ unless steam_id64.is_a? Fixnum
110
+ steam_id64 = SteamId.resolve_vanity_url steam_id64.to_s
111
+ raise SteamCondenserError.new 'User not found' if steam_id64.nil?
112
+ end
113
+
114
+ @app_id = app_id
115
+ @items = []
116
+ @steam_id64 = steam_id64
117
+ @user = SteamId.new steam_id64, false
118
+ end
119
+
120
+ # Returns the item at the given position in the inventory. The positions
121
+ # range from 1 to 100 instead of the usual array indices (0 to 99).
122
+ #
123
+ # @return [GameItem] The item at the given position in the inventory
124
+ def [](index)
125
+ @items[index - 1]
126
+ end
127
+
128
+ # Updates the contents of the inventory using the Steam Web API
129
+ def fetch
130
+ params = { :SteamID => @user.steam_id64 }
131
+ result = WebApi.json! "IEconItems_#@app_id", 'GetPlayerItems', 1, params
132
+ item_class = self.class.send :class_variable_get, :@@item_class
133
+
134
+ @items = []
135
+ @preliminary_items = []
136
+ result[:items].each do |item_data|
137
+ unless item_data.nil?
138
+ item = item_class.new(self, item_data)
139
+ if item.preliminary?
140
+ @preliminary_items << item
141
+ else
142
+ @items[item.backpack_position - 1] = item
143
+ end
144
+ end
145
+ end
146
+ end
147
+
148
+ # Returns a short, human-readable string representation of this inventory
149
+ #
150
+ # @return [String] A string representation of this inventory
151
+ def inspect
152
+ "#<#{self.class}:#@app_id #@steam_id64 (#{size} items) - " +
153
+ "#{fetch_time || 'not fetched'}>"
154
+ end
155
+
156
+ # Returns the item schema for this inventory
157
+ #
158
+ # @return [GameItemSchema] The item schema for the game this inventory
159
+ # belongs to
160
+ def item_schema
161
+ @item_schema ||= GameItemSchema.new app_id, @@schema_language
162
+ end
163
+
164
+ # Returns the number of items in the user's inventory
165
+ #
166
+ # @return [Fixnum] The number of items in the inventory
167
+ def size
168
+ @items.size
169
+ end
170
+
171
+ end
@@ -0,0 +1,156 @@
1
+ # This code is free software; you can redistribute it and/or modify it under
2
+ # the terms of the new BSD License.
3
+ #
4
+ # Copyright (c) 2011-2013, Sebastian Staudt
5
+
6
+ require 'steam/community/web_api'
7
+
8
+ # A module implementing basic functionality for classes representing an item in
9
+ # a game
10
+ #
11
+ # @author Sebastian Staudt
12
+ module GameItem
13
+
14
+ # Return the attributes of this item
15
+ #
16
+ # @return [Hash<Symbol, Object>, nil] The attributes of this item
17
+ attr_reader :attributes
18
+
19
+ # Returns the position of this item in the player's inventory
20
+ #
21
+ # @return [Fixnum] The position of this item in the player's inventory
22
+ attr_reader :backpack_position
23
+
24
+ # Returns the number of items the player owns of this item
25
+ #
26
+ # @return [Fixnum] The quanitity of this item
27
+ attr_reader :count
28
+
29
+ # Returns the index where the item is defined in the schema
30
+ #
31
+ # @return [Fixnum] The schema index of this item
32
+ attr_reader :defindex
33
+
34
+ # Returns the ID of this item
35
+ #
36
+ # @return [Fixnum] The ID of this item
37
+ attr_reader :id
38
+
39
+ # Returns the inventory this items belongs to
40
+ #
41
+ # @return [GameInventory] The inventory this item belongs to
42
+ attr_reader :inventory
43
+
44
+ # Returns the class of this item
45
+ #
46
+ # @return [String] The class of this item
47
+ attr_reader :item_class
48
+
49
+ # Returns the item set this item belongs to
50
+ #
51
+ # @return [Hash<Symbol, Object>, nil] The set this item belongs to
52
+ attr_reader :item_set
53
+
54
+ # Returns the level of this item
55
+ #
56
+ # @return [Fixnum] The level of this item
57
+ attr_reader :level
58
+
59
+ # Returns the level of this item
60
+ #
61
+ # @return [String] The level of this item
62
+ attr_reader :name
63
+
64
+ # Returns the origin of this item
65
+ #
66
+ # @return [String] The origin of this item
67
+ attr_reader :origin
68
+
69
+ # Returns the original ID of this item
70
+ #
71
+ # @return [Fixnum] The original ID of this item
72
+ attr_reader :original_id
73
+
74
+ # Returns the quality of this item
75
+ #
76
+ # @return [String] The quality of this item
77
+ attr_reader :quality
78
+
79
+ # Returns the type of this item
80
+ #
81
+ # @return [String] The type of this item
82
+ attr_reader :type
83
+
84
+ # Creates a new instance of a GameItem with the given data
85
+ #
86
+ # @param [GameInventory] inventory The inventory this item is contained in
87
+ # @param [Hash<Symbol, Object>] item_data The data representing this item
88
+ def initialize(inventory, item_data)
89
+ @inventory = inventory
90
+
91
+ @defindex = item_data[:defindex]
92
+ @backpack_position = item_data[:inventory] & 0xffff
93
+ @count = item_data[:quantity]
94
+ @craftable = !!item_data[:flag_cannot_craft]
95
+ @id = item_data[:id]
96
+ @item_class = schema_data[:item_class]
97
+ @item_set = inventory.item_schema.item_sets[schema_data[:item_set]]
98
+ @level = item_data[:level]
99
+ @name = schema_data[:item_name]
100
+ @original_id = item_data[:original_id]
101
+ @preliminary = item_data[:inventory] & 0x40000000 != 0
102
+ @quality = inventory.item_schema.qualities[item_data[:quality]]
103
+ @tradeable = !!item_data[:flag_cannot_trade]
104
+ @type = schema_data[:item_type_name]
105
+
106
+ if item_data.key? :origin
107
+ @origin = inventory.item_schema.origins[item_data[:origin]]
108
+ end
109
+
110
+ attributes_data = schema_data[:attributes] || []
111
+ attributes_data += item_data[:attributes] if item_data.key? :attributes
112
+
113
+ @attributes = []
114
+ attributes_data.each do |attribute_data|
115
+ attribute_key = attribute_data[:defindex]
116
+ attribute_key = attribute_data[:name] if attribute_key.nil?
117
+
118
+ unless attribute_key.nil?
119
+ schema_attribute_data = inventory.item_schema.attributes[attribute_key]
120
+ @attributes << attribute_data.merge(schema_attribute_data)
121
+ end
122
+ end
123
+ end
124
+
125
+ # Returns whether this item is craftable
126
+ #
127
+ # @return [Boolean] `true` if this item is craftable
128
+ def craftable?
129
+ @craftable
130
+ end
131
+
132
+ # Returns whether this item is preliminary
133
+ #
134
+ # Preliminary means that this item was just found or traded and has not yet
135
+ # been added to the inventory
136
+ #
137
+ # @return [Boolean] `true` if this item is preliminary
138
+ def preliminary?
139
+ @preliminary
140
+ end
141
+
142
+ # Returns the data for this item that's defined in the item schema
143
+ #
144
+ # @return [Hash<Symbol, Object>] The schema data for this item
145
+ def schema_data
146
+ inventory.item_schema.items[@defindex]
147
+ end
148
+
149
+ # Returns whether this item is tradeable
150
+ #
151
+ # @return [Boolean] `true` if this item is tradeable
152
+ def tradeable?
153
+ @tradeable
154
+ end
155
+
156
+ end
@@ -0,0 +1,131 @@
1
+ # This code is free software; you can redistribute it and/or modify it under
2
+ # the terms of the new BSD License.
3
+ #
4
+ # Copyright (c) 2012, Sebastian Staudt
5
+
6
+ require 'steam/community/cacheable'
7
+
8
+ # Provides item definitions and related data that specify the items of a game
9
+ #
10
+ # @author Sebastian Staudt
11
+ class GameItemSchema
12
+
13
+ include Cacheable
14
+ cacheable_with_ids [ :app_id, :language ]
15
+
16
+ # Returns the application ID of the game this item schema belongs to
17
+ #
18
+ # @return [Fixnum] The application ID of the game
19
+ attr_reader :app_id
20
+
21
+ # The attributes defined for this game's items
22
+ #
23
+ # @return [Hash<Symbol, Object>] This item schema's attributes
24
+ attr_reader :attributes
25
+
26
+ # The effects defined for this game's items
27
+ #
28
+ # @return [Hash<Symbol, Object>] This item schema's effects
29
+ attr_reader :effects
30
+
31
+ # The levels defined for this game's items
32
+ #
33
+ # @return [Hash<Symbol, Object>] This item schema's item levels
34
+ attr_reader :item_levels
35
+
36
+ # A mapping from the item name to the item's defindex
37
+ #
38
+ # @return [Hash<Symbol, Object>] The item name mapping
39
+ attr_reader :item_names
40
+
41
+ # The item sets defined for this game's items
42
+ #
43
+ # @return [Hash<Symbol, Object>] This item schema's item sets
44
+ attr_reader :item_sets
45
+
46
+ # The items defined for this game
47
+ #
48
+ # @return [Hash<Symbol, Object>] The items in this schema
49
+ attr_reader :items
50
+
51
+ # The language of this item schema
52
+ #
53
+ # @return [Symbol] The language of this item schema
54
+ attr_reader :language
55
+
56
+ # The item origins defined for this game's items
57
+ #
58
+ # @return [Array<String>] This item schema's origins
59
+ attr_reader :origins
60
+
61
+ # The item qualities defined for this game's items
62
+ #
63
+ # @return [Array<String>] This item schema's qualities
64
+ attr_reader :qualities
65
+
66
+ # Creates a new item schema for the game with the given application ID and
67
+ # with descriptions in the given language
68
+ #
69
+ # @param [Fixnum] app_id The application ID of the game
70
+ # @param [Symbol] language The language of description strings
71
+ # @macro cacheable
72
+ def initialize(app_id, language = nil)
73
+ @app_id = app_id
74
+ @language = language
75
+ end
76
+
77
+ # Updates the item definitions of this schema using the Steam Web API
78
+ def fetch
79
+ params = { :language => language }
80
+ data = WebApi.json!("IEconItems_#{app_id}", 'GetSchema', 1, params)
81
+
82
+ @attributes = {}
83
+ data[:attributes].each do |attribute|
84
+ @attributes[attribute[:defindex]] = attribute
85
+ @attributes[attribute[:name]] = attribute
86
+ end
87
+
88
+ @effects = {}
89
+ data[:attribute_controlled_attached_particles].each do |effect|
90
+ @effects[effect[:id]] = effect[:name]
91
+ end
92
+
93
+ @items = {}
94
+ @item_names = {}
95
+ data[:items].each do |item|
96
+ @items[item[:defindex]] = item
97
+ @item_names[item[:name]] = item[:defindex]
98
+ end
99
+
100
+ @item_levels = {}
101
+ data[:item_levels].each do |item_level_type|
102
+ @item_levels[item_level_type[:name]] = {}
103
+ item_level_type[:levels].each do |level|
104
+ @item_levels[item_level_type[:name]][level[:level]] = level[:name]
105
+ end
106
+ end if data.key? :item_levels
107
+
108
+ @item_sets = {}
109
+ data[:item_sets].each do |item_set|
110
+ @item_sets[item_set[:item_set]] = item_set
111
+ end
112
+
113
+ @origins = []
114
+ data[:originNames].each do |origin|
115
+ @origins[origin[:origin]] = origin[:name]
116
+ end
117
+
118
+ @qualities = []
119
+ data[:qualities].keys.each_with_index do |key, index|
120
+ @qualities[index] = data[:qualityNames][key] || key.to_s.capitalize
121
+ end
122
+ end
123
+
124
+ # Returns a short, human-readable string representation of this item schema
125
+ #
126
+ # @return [String] A string representation of this item schema
127
+ def inspect
128
+ "#<#{self.class}:#@app_id (#@language) - #{fetch_time || 'not fetched'}>"
129
+ end
130
+
131
+ end