Unverified Commit 92950d30 authored by Isabella Skořepová's avatar Isabella Skořepová
Browse files

Refresh webpage on file reload and add

parent 3060b867
...@@ -21,6 +21,22 @@ function timeout(ms) { ...@@ -21,6 +21,22 @@ function timeout(ms) {
return new Promise(res => setTimeout(res, ms)) return new Promise(res => setTimeout(res, ms))
} }
const reload = (() => {
const mtimeCache = {}
return async function reloadImpl(name) {
const path = require.resolve(name)
const stat = await promisify(fs.stat)(path)
const mtime = `${stat.mtime}`
if (mtimeCache[path] && mtimeCache[path] !== mtime) {
delete require.cache[path]
mtimeCache[path] = mtime
return true
}
mtimeCache[path] = mtime
return false
}
})()
console.log('starting') console.log('starting')
;(async () => { ;(async () => {
const args = await Travian.generateArgs({ const args = await Travian.generateArgs({
...@@ -32,10 +48,15 @@ console.log('starting') ...@@ -32,10 +48,15 @@ console.log('starting')
const travian = impl(...args) const travian = impl(...args)
try { try {
await travian.login(await readCredentials()) await travian.login(await readCredentials())
// eslint-disable-next-line no-constant-condition // eslint-disable-next-line no-constant-condition
while (true) { while (true) {
delete require.cache[require.resolve('./travian')] if ((await reload('./bot')) || (await reload('./travian'))) {
delete require.cache[require.resolve('./bot')] // eslint-disable-next-line global-require
await require('./travian')
.travianImpl(...args)
.refresh()
}
try { try {
// eslint-disable-next-line global-require // eslint-disable-next-line global-require
await require('./bot').bot(require('./travian').travianImpl(...args)) await require('./bot').bot(require('./travian').travianImpl(...args))
...@@ -43,7 +64,11 @@ console.log('starting') ...@@ -43,7 +64,11 @@ console.log('starting')
} catch (e) { } catch (e) {
console.error('Detected problem while running bot...') console.error('Detected problem while running bot...')
console.error(e) console.error(e)
console.error("Sleeping for 5 seconds so that it doesn't use whole CPU") console.log()
console.error(
new Date(),
"Sleeping for 5 seconds so that it doesn't use whole CPU",
)
await timeout(5000) await timeout(5000)
} }
} }
......
...@@ -10,6 +10,7 @@ const travianImpl = ( ...@@ -10,6 +10,7 @@ const travianImpl = (
) => ({ ) => ({
async goto(path, opts = {}) { async goto(path, opts = {}) {
if (state.path !== path) { if (state.path !== path) {
console.log('travian: navigating to', path)
await page.goto(server + path, { await page.goto(server + path, {
waitUntil: 'networkidle', waitUntil: 'networkidle',
...opts, ...opts,
...@@ -23,7 +24,20 @@ const travianImpl = ( ...@@ -23,7 +24,20 @@ const travianImpl = (
}) })
}, },
async refresh() {
console.log('travian: Pressing F5')
await page.goto(server + state.path, {
waitUntil: 'networkidle',
})
},
async login({ name, password }) { async login({ name, password }) {
console.log(
`travian: logging in ${name}:${password
.split('')
.map(() => '*')
.join('')}`,
)
await this.goto('dorf1.php') await this.goto('dorf1.php')
if ((await page.$('.outerLoginBox')) !== null) { if ((await page.$('.outerLoginBox')) !== null) {
await page.click('.account input') await page.click('.account input')
...@@ -51,7 +65,7 @@ const travianImpl = ( ...@@ -51,7 +65,7 @@ const travianImpl = (
return { return {
level: Number.parseInt(parts[parts.length - 1], 10), level: Number.parseInt(parts[parts.length - 1], 10),
type: parts.slice(0, parts.length - 2).join(' '), type: parts.slice(0, parts.length - 2).join(' '),
id: href.replace(`${server}build.php?id=`, ''), id: Number.parseInt(href.replace(`${server}build.php?id=`, ''), 10),
} }
}) })
.filter(a => a.type) .filter(a => a.type)
...@@ -78,6 +92,7 @@ const travianImpl = ( ...@@ -78,6 +92,7 @@ const travianImpl = (
}) })
}, },
async upgradeBuilding(id) { async upgradeBuilding(id) {
console.log(`travian: upgrading building ${id}`)
await this.goto(`build.php?id=${id}`) await this.goto(`build.php?id=${id}`)
await page.click('.upgradeButtonsContainer .section1 button') await page.click('.upgradeButtonsContainer .section1 button')
await this.networkIdle() await this.networkIdle()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment