I created the class with some functions, but its not working well. In the running time it is showing the this.pickRandomName
is undefined. When I call it this.createBot
.
class HostBotManager {
this.xmpp.on('online', async (address) => {
// Makes itself available
console.log('online as ', address.toString())
// Let the action begin!
// for (var i=0; i<this.instanceCount; i++) {
// this.createBot()
this.runBetween(0, 1000, this.instanceCount);
// }
})
// no need to listen for stanzas
}
// Load list of possible bot names from file
loadNames(filename) {
let data = fs.readFileSync(filename, 'utf-8')
return data.split(/r?n/)
}
pickRandomName() {
console.log("what is this ", this)
if (!this.names) return unnamed
let nameIndex = Math.floor(Math.random() * this.names.length)
let prefix = this.names[nameIndex]
let suffix = Math.random().toString(36).substr(2)
return `${this.botPrefix}${prefix}-${suffix}`
}
runBetween(start, end, instanceCount) {
const delay = (end - start) / instanceCount;
console.log("delay time ", delay)
for (let i = 0; i < instanceCount; i++) {
setTimeout(this.createBot, start + i * delay);
}
}
async createBot() {
console.info("create bot has called!", this)
// don't do this if we're winding down
if (this.windingDown) return
// Randomly create the bot
let bot = {}
// Choose a name
let name = this.pickRandomName()
bot.jid = jid(`${name}@${this.domain}`)
// Choose a table
let suffix = Date.now().toString(36)
bot.table = jid(`${name}-${suffix}@${this.tableService}`)
console.log(`Sending ${bot.jid} to ${bot.table}`)
// Create the room
this.xmpp.send(stanza(`
<presence from="${bot.jid}" to="${bot.table}">
<game var="http://hool.org/protocol/mug/hool">
<item role="kibitzer"/>
</game>
</presence>
`))
this.activeBots.push(bot)
// invite player bots
for (let seat of ['N', 'E', 'S', 'W']) {
this.xmpp.send(stanza(`
<message from="${bot.jid}" to="${bot.table}">
<game xmlns="http://jabber.org/protocol/mug#user">
<invite to="${this.botService}"/>
<item role="${seat}"/>
</game>
</message>
`))
await wait()
}
}
If I call a this.createBot()
straightly from the for loop its working fine. But if I call it via this.runBetween
its showing some error with this.pickRandomname
is not a function.
This is the error
let name = this.pickRandomName()
^
TypeError: this.pickRandomName is not a function
at Timeout.createBot [as _onTimeout] (C:bk-hoolbot-testhobotindex.js:140:21)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7)