Module: InventoryCommands::Inventory_ClassMethods

Defined in:
lib/Inventory.rb

Constant Summary collapse

@@libdir =
Util.gem_libdir
@@session =
Mechanize.new

Instance Method Summary collapse

Instance Method Details

#get_inventory_chunk_normal_way(appid, context, steamid, last_id) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/Inventory.rb', line 347

def get_inventory_chunk_normal_way(appid,context,steamid,last_id)
            html = ''
            tries = 1

            until html != ''
                  begin
                        html = @@session.get("https://steamcommunity.com/inventory/#{steamid}/#{appid}/#{context}?start_assetid=#{last_id}&count=5000").content
                  rescue
                        raise "Cannot get inventory, tried 3 times" if tries == 3
                        tries = tries + 1
                        sleep(0.5)
                  end
            end


            get = JSON.parse(html)
            raise "something totally unexpected happened while getting inventory with appid #{appid} of steamid #{steamid} with contextid #{context}" if get.key?("error") == true
            if get["total_inventory_count"] == 0
                  output "EMPTY :: inventory with appid #{appid} of steamid #{steamid} with contextid #{context}"
                  return {'assets' => [], 'new_last_id' =>false}
            end
            if get.keys[3].to_s == "last_assetid"

                    new_last_id = get.values[3].to_s

            else
                    new_last_id = false

            end

            assets = get["assets"]
            descriptions = get["descriptions"]


            descriptions_classids = {} ###sorting descriptions by key value || key is classid of the item's description
            descriptions.each {|description|
                 classidxinstance = description["classid"] + '_' + description["instanceid"] # some items has the same classid but different instane id
                 descriptions_classids[classidxinstance] = description
            }

            assets.each { |asset| ## merging assets with names
                 classidxinstance = asset["classid"] + '_' + asset["instanceid"]
                 asset.replace(asset.merge(descriptions_classids[classidxinstance]))
            }


          return {'assets' => assets, 'new_last_id' =>new_last_id}

end

#get_inventory_chunk_raw_way(appid, context, steamid, last_id, trim) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/Inventory.rb', line 447

def get_inventory_chunk_raw_way(appid,context,steamid,last_id,trim)


        html = ''
        tries = 1

        until html != ''
              begin
                    html = @@session.get("https://steamcommunity.com/inventory/#{steamid}/#{appid}/#{context}?start_assetid=#{last_id}&count=5000").content
              rescue
                    raise "Cannot get inventory, tried 3 times" if tries == 3
                    tries = tries + 1
                    sleep(0.5)
              end
        end

        get = JSON.parse(html)
        raise "something totally unexpected happened while getting inventory with appid #{appid} of steamid #{steamid} with contextid #{context}" if get.key?("error") == true
        if get["total_inventory_count"] == 0
              output "EMPTY :: inventory with appid #{appid} of steamid #{steamid} with contextid #{context}"
              return {'assets' => [], "descriptions" => [], 'new_last_id' =>false}
        end
        if get.keys[3].to_s == "last_assetid"

                new_last_id = get.values[3].to_s

        else
                new_last_id = false

        end

        assets = get["assets"]
        descriptions = get["descriptions"]
        if trim == true
              descriptions.each { |desc|
                    desc.delete_if {|key, value| key != "appid" && key != "classid" && key != "instanceid" && key != "tags" && key != "type" && key != "market_fee_app" && key != "marketable" &&key != "name" }
                    desc["tags"].delete_at(0)
                    desc["tags"].delete_at(0)
              }
        end

       return {'assets' => get["assets"], "descriptions" => get["descriptions"], 'new_last_id' =>new_last_id}

end

#normal_get_inventory(steamid, appid = 753) ⇒ Object



310
311
312
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
# File 'lib/Inventory.rb', line 310

def normal_get_inventory(steamid ,appid = 753)
      appid = appid.to_s
      context = 6
e
      if appid.to_s != "753"
            context = 2
      end
      #end verify given another game
      # end verify given appid only
      #verify trade link
      steamid,token = verify_profileid_or_trade_link_or_steamid(steamid)
      raise "invalid steamid : #{steamid}, length of received :: #{steamid.to_s.length}, normal is 17" if steamid.to_s.length != 17
      ## verify appid
      if ["753","730",'570','440'].include?(appid.to_s) == false
            allgames = JSON.parse(File.read("#{@@libdir}blueprints/game_inv_list.json"))
            raise "invalid appid: #{appid}" if allgames.include?(appid.to_s) == false
      end
      ## end verify appid

      items = []
      last_id = 0
      until last_id == false
            received = get_inventory_chunk_normal_way(appid,context,steamid,last_id)
            last_id = received['new_last_id']
            items = items + received['assets']
            output "loaded #{items.length}"
      end

      output "total loaded #{items.length} asset"


      return items
end

#raw_get_inventory(steamid, *params) ⇒ Object

steamid = @steamid ,appid = 753, trim = true



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/Inventory.rb', line 399

def raw_get_inventory(steamid, *params)#steamid = @steamid ,appid = 753, trim = true
      raise "expected 2 paramters, given #{params.length}"if params.length > 2
      appid = 753
      trim = true
      context = 6
      v = params.length
      if params.length == 2
            (appid = params[0]; v= v - 1;) if (3..6).to_a.include?(params[0].to_i.to_s.length)
           (trim = params[1]; v= v - 1;) if [TrueClass,FalseClass].include?(params[1].class)
      elsif params.length == 1
            (appid = params[0]; v= v - 1;) if (3..6).to_a.include?(params[0].to_i.to_s.length)
            (trim = params[0]; v= v - 1;) if [TrueClass,FalseClass].include?(params[0].class)
      end
      raise "invalid params given" if v != 0

      steamid,token = verify_profileid_or_trade_link_or_steamid(steamid)
      raise "invalid steamid : #{steamid}, length of received :: #{steamid.to_s.length}, normal is 17" if steamid.to_s.length != 17
      ## verify appid
      if ["753","730",'570','440'].include?(appid.to_s) == false
            allgames = JSON.parse(File.read("#{@libdir}blueprints/game_inv_list.json"))
            raise "invalid appid: #{appid}" if allgames.include?(appid.to_s) == false
      end
      ## end verify appid

      if appid.to_s != "753"
            context = 2
      end


      last_id = 0
      hash = {"assets" => [], "descriptions" => []}
      until last_id == false
            received = get_inventory_chunk_raw_way(appid,context,steamid,last_id,trim)
            last_id = received['new_last_id']
            hash["assets"] = hash["assets"] + received['assets']
            hash["descriptions"] = hash["descriptions"] + received["descriptions"]
            output "loaded #{hash["assets"].length}"
      end

      output "total loaded #{hash["assets"].length} asset"


      return hash
end