DoctorMcKay / node-tf2

Simple module to interact with the TF2 game coordinator
https://www.npmjs.com/package/tf2
MIT License
54 stars 26 forks source link

events `accountLoaded` and `backpackLoaded` not emitted #27

Open udisun opened 2 years ago

udisun commented 2 years ago

not getting events accountLoaded and backpackLoaded to emit. after several restarts it works once and then stops working again.

node: v12.13.0 tf2: v3.0.2

My bot has both tf2 and csgo connecting to GC , they connect successfully but seems to stop at the UpdateItemSchema event.

here is the code that I use to connect and subscribe to messages:

const client = new SteamUser()
const tf2Client = new TeamFortress2(client)
const csgoClient = new GlobalOffensive(client)

const logOnOptions: LogOnOptions = {
    accountName: Constants.STEAM_USERNAME,
    password: Constants.STEAM_PASSWORD,
    twoFactorCode: SteamTotp.getAuthCode(Constants.SHARED_SECRET),
    rememberPassword: true,
    autoRelogin: true,
    // @ts-ignore
    loginKey: null
}

client.logOn(logOnOptions)

client.on('loggedOn', () => {
    util.log(prefix + "Logged into Steam")
    client.gamesPlayed([440, 730])
})

csgoClient.on('connectedToGC', () => {
  console.log('connected to CSGO GC')
  if (!csgoClient.inventory) {
      return;
  }
})

tf2Client.on('connectedToGC', () => {
    console.log('connected to TF2 GC')
})

tf2Client.on('debug', (msg: any) => {
    console.log(`TF2 Debug: ${msg}`)
})

tf2Client.on('backpackLoaded', () => {
    console.log('TF2 backpack contents', JSON.stringify(tf2Client.backpack))
})

tf2Client.on('accountLoaded', () => {
    console.log('TF2 account loaded', {
        premium: tf2Client.premium,
        backpackSlots: tf2Client.backpackSlots,
        usedSlots: tf2Client.backpack ? tf2Client.backpack.length : 'unknown yet',
    })
})

here are the messages I get from listening to the debug event:

2022-03-23T10:41:04.844230321Z [Wed, 23 Mar 2022 10:41:04 GMT]: [Bot 198]: Logged into Steam
2022-03-23T10:41:04.847689246Z TF2 Debug: Sending GC message ClientHello
2022-03-23T10:41:05.346424602Z TF2 Debug: Got unhandled GC message 6518
2022-03-23T10:41:05.348713155Z TF2 Debug: Got handled GC message ClientWelcome
2022-03-23T10:41:05.349659025Z connected to TF2 GC
2022-03-23T10:41:05.350579532Z TF2 Debug: Got handled GC message SO_CacheSubscriptionCheck
2022-03-23T10:41:05.350821561Z TF2 Debug: Requesting SO cache subscription refresh
2022-03-23T10:41:05.351052822Z TF2 Debug: Sending GC message SO_CacheSubscriptionRefresh
2022-03-23T10:41:05.353659083Z TF2 Debug: Got handled GC message UpdateItemSchema
2022-03-23T10:41:05.744825612Z TF2 item schema loaded
2022-03-23T10:41:06.750084770Z TF2 Debug: Got handled GC message ClientWelcome
2022-03-23T10:41:06.750327296Z connected to TF2 GC
2022-03-23T10:41:06.751376590Z TF2 Debug: Got handled GC message SO_CacheSubscriptionCheck
2022-03-23T10:41:06.751532829Z TF2 Debug: Requesting SO cache subscription refresh
2022-03-23T10:41:06.751758024Z TF2 Debug: Sending GC message SO_CacheSubscriptionRefresh

not sure how to continue with debugging this

udisun commented 2 years ago

For anybody coming here I've solved the issue by logging out of the game first and after a short timeout logging back in:

client.on('loggedOn', () => {
        util.log(prefix + "Logged into Steam");

        // We have to log out in order to get new data from tf2
        util.log(prefix + "Logging out of games")
        client.gamesPlayed([])

        setTimeout(() => {
            util.log(prefix + "Logging into games")
            client.gamesPlayed([440, 730])
        }, 10000);
    });
DoctorMcKay commented 2 years ago

Is this happening when you restart your bot? If you don't wait long enough or properly shut down the game then the GC can seemingly never realize you disconnected at all, so you won't get all the messages that you're supposed to get when you reconnect.