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) {
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')
;(async () => {
const args = await Travian.generateArgs({
......@@ -32,10 +48,15 @@ console.log('starting')
const travian = impl(...args)
try {
await travian.login(await readCredentials())
// eslint-disable-next-line no-constant-condition
while (true) {
delete require.cache[require.resolve('./travian')]
delete require.cache[require.resolve('./bot')]
if ((await reload('./bot')) || (await reload('./travian'))) {
// eslint-disable-next-line global-require
await require('./travian')
.travianImpl(...args)
.refresh()
}
try {
// eslint-disable-next-line global-require
await require('./bot').bot(require('./travian').travianImpl(...args))
......@@ -43,7 +64,11 @@ console.log('starting')
} catch (e) {
console.error('Detected problem while running bot...')
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)
}
}
......
......@@ -10,6 +10,7 @@ const travianImpl = (
) => ({
async goto(path, opts = {}) {
if (state.path !== path) {
console.log('travian: navigating to', path)
await page.goto(server + path, {
waitUntil: 'networkidle',
...opts,
......@@ -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 }) {
console.log(
`travian: logging in ${name}:${password
.split('')
.map(() => '*')
.join('')}`,
)
await this.goto('dorf1.php')
if ((await page.$('.outerLoginBox')) !== null) {
await page.click('.account input')
......@@ -51,7 +65,7 @@ const travianImpl = (
return {
level: Number.parseInt(parts[parts.length - 1], 10),
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)
......@@ -78,6 +92,7 @@ const travianImpl = (
})
},
async upgradeBuilding(id) {
console.log(`travian: upgrading building ${id}`)
await this.goto(`build.php?id=${id}`)
await page.click('.upgradeButtonsContainer .section1 button')
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