Module: BadgeCommands::BadgeCommands_ClassMethods

Defined in:
lib/Badge.rb

Constant Summary collapse

@@libdir =
Util.gem_libdir

Instance Method Summary collapse

Instance Method Details

#sets_count(steamid, use_nonmarketable = true) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/Badge.rb', line 223

def sets_count(steamid, use_nonmarketable = true)




      steamid,token = verify_profileid_or_trade_link_or_steamid(steamid)

      hash = raw_get_inventory(steamid)
      sorted = {}
      classxinstance = {}

      hash["descriptions"].delete_if {|desc|
            conc = desc["classid"] + "_" +  desc["instanceid"]
            classxinstance[conc] = desc
            true
      }



      hash["assets"].each { |asset|
            identity = asset["classid"] + "_" + asset["instanceid"]
            assetdesc = classxinstance[identity]
            if use_nonmarketable == false
                  if assetdesc["marketable"] == 0 || assetdesc["tags"][-1]["localized_tag_name"] != "Trading Card" || assetdesc["tags"][-2]["localized_tag_name"] == "Foil"
                        next
                  end
            else
                  if  assetdesc["tags"][-1]["localized_tag_name"] != "Trading Card" ||assetdesc["tags"][-2]["localized_tag_name"] == "Foil"
                        next
                  end
            end

            name = assetdesc["name"].sub(" (Trading Card)", "")
            appid =assetdesc["market_fee_app"].to_s
            if sorted.has_key?(appid) == true
                  if sorted[appid].has_key?(name) == true
                        sorted[appid][name] = sorted[appid][name] + 1
                  elsif sorted[appid].has_key?(name) == false
                        sorted[appid][name] = 1
                  end
            elsif sorted.has_key?(appid) == false
                  sorted[appid] = {}
                  sorted[appid][name] = 1
            end
      }

      bigdata = JSON.parse(File.read("#{@@libdir}blueprints/byappid.json",:external_encoding => 'utf-8',:internal_encoding => 'utf-8'))

      counted = {}

      sorted.each { |appid,cards|
            begin
                  counted[appid.to_s] = bigdata[appid].merge(cards)
            rescue
                  output "badges blueprint does not include #{appid}"
            end
      }

      counted.each { |appid, cards|
            cards.delete_if { |key,value|
                  key == 'title'
            }
      }
      setsowned = {}
      numberofsets = 0
      total_non_foil = 0

      counted.each { |appid,cards|
            lowest = 9999
            cards.each { |cardname, amount|
                        next if amount.class == String # apptitle
                        if amount < lowest then lowest = amount end
                        total_non_foil =  total_non_foil + amount
            }
            setsowned[appid.to_s] = lowest
            numberofsets = numberofsets + lowest
      }

      persona = ''
      write_badges(counted,setsowned,numberofsets,total_non_foil, use_nonmarketable,persona,steamid)
      if use_nonmarketable == false
            return {'sets' => counted, 'appxsets' => setsowned, 'totalsets' => numberofsets, 'totalcards' => total_non_foil, 'marketable' => false}
      else
             return {'sets' => counted, 'appxsets' => setsowned, 'totalsets' => numberofsets, 'totalcards' => total_non_foil, 'marketable' => true}
      end
end

#update_blueprintObject



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/Badge.rb', line 313

def update_blueprint()
      session = Mechanize.new

      session.pre_connect_hooks << lambda do |agent, request|
         request['Origin'] = 'http://steam.tools'
         request['Referer'] = 'http://steam.tools/cards/'
         request['User-Agent'] ='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 OPR/52.0.2871.99'
       end


      pag = session.get('http://cdn.steam.tools/data/set_data.json')
      data = JSON.parse(pag.content)
      old = JSON.parse(File.read("#{@@libdir}blueprints/byappid.json" ,:external_encoding => 'utf-8',:internal_encoding => 'utf-8'))

      newapps = []
      data["sets"].each { |set|
            newapps << set['appid']
      }
      get = []
      haveapps = old.keys
      newapps.each { |app|
            get << app if haveapps.include?(app) == false
      }

      progress = 0
      error = 0
      (output("your blueprint is up-to-date");return) if get.length.zero?
      get.each {|app|
            begin
                  card = {}
                  steam_nokogiri = Nokogiri::HTML(session.get("http://www.steamcardexchange.net/index.php?inventorygame-appid-#{app}").content)
                  title = steam_nokogiri.css('h2[class=empty]').text.force_encoding(Encoding::UTF_8)
                  steam_nokogiri.css('div[class=name-image-container]').css('span').each do |e|
                        card[e.text.force_encoding(Encoding::UTF_8)] = 0
                  end
                  card['title'] = title
                  full_data = card

                  old[app] = full_data
                  File.open("#{@@libdir}blueprints/byappid.json", 'w:UTF-8') {|f| f.puts old.to_json}

                  progress = progress + 1
                  output "#{progress} / #{get.length} done, error = #{error}"
            rescue Exception => e
                  File.open("#{@@libdir}blueprints/byappid.json", 'w:UTF-8') {|f| f.puts old.to_json}
                  error = error + 1
                  progress = progress + 1
                  output "#{progress} / #{get.length} done, error = #{error}"
                  output "error occured saved data"
                  raise e
            end
      }


end