seishun / node-steam-trade

Node.js wrapper around Steam trading
162 stars 36 forks source link

Cannot load inventory (example scrapbank) #24

Closed royalgarter closed 11 years ago

royalgarter commented 11 years ago

Hi seishun,

I tried your lib with dota2 store with some modify with new version of node-steam, but It's look like the loadInventory was broken with cookie.When I debug to your lib the inventory is 'undefined', did I do something wrong?

var username = 'myusername';
var password = 'mypass';
var keyfile = username+'.key';
var steamGuard = require('fs').existsSync(keyfile) ? require('fs').readFileSync(keyfile) : ''; // code received by email
var admin = ''; // put your steamid here to say 'give' to the bot and receive all non-scrap items

var Steam = require('steam');
var SteamTrade = require('./'); // change to 'steam-trade' if not running from the same directory

var steam = new Steam.SteamClient();
var steamTrade = new SteamTrade();

steam.logOn({
accountName: username,
password: password
    ,shaSentryfile: steamGuard
    //,authCode: 'NYGQQ'
});

/*
- comment both shaSentryfile, authCode, log in failed, check mail, get authCode
- comment both shaSentryfile, enable authCode, log in ok, save sentry
- comment both authCode, enable shaSentryfile, log in ok with saved sentry
*/

steam.on('sentry', function(sentryHash) {
    /**/console.log('Saving sentry file hash');
    require('fs').writeFile(keyfile,sentryHash,function(err) {
        if(err){
            /**/console.log(err);
        } else {
            /**/console.log('Saved');
        }
    });
});

steam.on('debug', console.log);

steam.on('loggedOn', function(result) {
    /**/console.log('Logged in!');
    steam.setPersonaState(Steam.EPersonaState.Online);
});

steam.on('webSessionID', function(sessionID) {
    /**/console.log('got a new session ID:', sessionID);
    steamTrade.sessionID = sessionID;
    steam.webLogOn(function(cookies) {
        /**/console.log('got a new cookie:', cookies);
        cookies.toString().split(';').forEach(function(cookie) {
            steamTrade.setCookie(cookie);
        });
    });
});

var inventory;
var scrap;
var weapons;
var addedScrap;
var client;

steam.on('tradeProposed', function(tradeID, otherClient) {
    /**/console.log('tradeProposed');
    steam.respondToTrade(tradeID, true);
});

steam.on('sessionStart', function(otherClient) {
    inventory = [];
    scrap = [];
    weapons = 0;
    addedScrap = [];
    client = otherClient;

    console.log('trading ' + steam.users[client].playerName);
    steamTrade.open(otherClient);
    steamTrade.loadInventory(570, 2, function(inv) {
        inventory = inv;    
        jsonString = JSON.stringify(inventory);

        require('fs').writeFile('inventory.json', jsonString, function(err) {
            if(err) {
                console.log(err);
            } else {
                console.log('inventory saved');
            }
        }); 

        scrap = inv.filter(function(item) { return item.name == 'Scrap Metal';});
        /**/console.log(scrap);
    });
});
seishun commented 11 years ago

The cookies in webLogOn are already an array, just add each one of them.

https://github.com/seishun/node-steam/releases/tag/v0.6.0

webLogOn now calls back with an array of cookies, rather than a single string joined with a semicolon - no need for split(';') anymore when adding to a request jar or working with node-steam-trade;

royalgarter commented 11 years ago

Thank you, sorry for didn't notice that