diff --git a/src/Action/GoToMainAction.ts b/src/Action/GoToMainAction.ts deleted file mode 100644 index 19c69d1..0000000 --- a/src/Action/GoToMainAction.ts +++ /dev/null @@ -1,7 +0,0 @@ -import Action from './Action'; - -export default class GoToMainAction extends Action { - async run(): Promise { - return Promise.resolve(null); - } -} diff --git a/src/Action/GoToResourceFieldsAction.ts b/src/Action/GoToResourceFieldsAction.ts new file mode 100644 index 0000000..290a62b --- /dev/null +++ b/src/Action/GoToResourceFieldsAction.ts @@ -0,0 +1,8 @@ +import Action from './Action'; + +export default class GoToResourceFieldsAction extends Action { + static NAME = 'go_to_resource_fields'; + async run(): Promise { + window.location.assign('/dorf1.php'); + } +} diff --git a/src/Action/StoreRemainingBuildTimeAction.ts b/src/Action/StoreRemainingBuildTimeAction.ts new file mode 100644 index 0000000..71436ec --- /dev/null +++ b/src/Action/StoreRemainingBuildTimeAction.ts @@ -0,0 +1,14 @@ +import Action from './Action'; +import { Args } from '../Common'; + +export default class StoreRemainingBuildTimeAction extends Action { + static NAME = 'store_remaining_build_time'; + + async run(args: Args): Promise { + const timer = jQuery('.buildDuration .timer'); + // if (timer.length === 1) { + // const remainingSeconds = +timer.val(); + // } + return null; + } +} diff --git a/src/Action/UpgradeBuildingAction.ts b/src/Action/UpgradeBuildingAction.ts index 3f884f0..598ab25 100644 --- a/src/Action/UpgradeBuildingAction.ts +++ b/src/Action/UpgradeBuildingAction.ts @@ -1,19 +1,26 @@ import Action from './Action'; import { Args } from '../Common'; import { TryLaterError } from '../Errors'; +import Scheduler from '../Scheduler'; export default class UpgradeBuildingAction extends Action { static NAME = 'upgrade_building'; + private scheduler: Scheduler; + + constructor(scheduler: Scheduler) { + super(); + this.scheduler = scheduler; + } async run(args: Args): Promise { const btn = jQuery( '.upgradeButtonsContainer .section1 button.green.build' ); if (btn.length === 1) { + this.scheduler.completeCurrentTask(); btn.trigger('click'); } else { - console.log('NO UPGRADE BUTTON'); - throw new TryLaterError(60); + throw new TryLaterError(60, 'No upgrade button, try later'); } return null; } diff --git a/src/Dashboard.ts b/src/Dashboard.ts index 5fb3686..c115e11 100644 --- a/src/Dashboard.ts +++ b/src/Dashboard.ts @@ -1,5 +1,5 @@ import * as URLParse from 'url-parse'; -import { markPage, sleepShort } from './utils'; +import { markPage, sleep, sleepShort } from './utils'; import { v4 as uuid } from 'uuid'; import Scheduler from './Scheduler'; import UpgradeBuildingTask from './Task/UpgradeBuildingTask'; @@ -16,14 +16,23 @@ export default class Dashboard { } async run() { + await this.load(); + await sleep(1000); + const p = new URLParse(window.location.href, true); console.log('PARSED LOCATION', p); - await sleepShort(); - markPage('Dashboard', this.version); new TaskQueueRenderer().render(this.scheduler.taskState()); + if (p.pathname === '/dorf1.php') { + this.showSlotIds('buildingSlot'); + } + + if (p.pathname === '/dorf2.php') { + this.showSlotIds('aid'); + } + if (p.pathname === '/build.php') { console.log('BUILD PAGE DETECTED'); const id = uuid(); @@ -39,4 +48,25 @@ export default class Dashboard { }); } } + + private showSlotIds(prefix: string) { + jQuery('.level.colorLayer').each((idx, el) => { + let num = ''; + el.classList.forEach(cls => { + if (cls.startsWith(prefix)) { + num = cls.substr(prefix.length); + } + }); + const t = jQuery(el) + .find('.labelLayer') + .text(); + jQuery(el) + .find('.labelLayer') + .text(num + ':' + t); + }); + } + + private async load() { + return new Promise(resolve => jQuery(resolve)); + } } diff --git a/src/Scheduler.ts b/src/Scheduler.ts index 51d54fd..9c410d8 100644 --- a/src/Scheduler.ts +++ b/src/Scheduler.ts @@ -8,15 +8,22 @@ import ActionQueue from './Storage/ActionQueue'; import { Args, Command } from './Common'; import TaskQueueRenderer from './TaskQueueRenderer'; +enum SleepType { + Long, + Short, +} + export default class Scheduler { private readonly version: string; private taskQueue: TaskQueue; private actionQueue: ActionQueue; + private sleepType: SleepType; constructor(version: string) { this.version = version; this.taskQueue = new TaskQueue(); this.actionQueue = new ActionQueue(); + this.sleepType = SleepType.Short; } async run() { @@ -24,7 +31,7 @@ export default class Scheduler { markPage('Executor', this.version); new TaskQueueRenderer().render(this.taskQueue.state()); while (true) { - await sleepLong(); + await this.sleep(); const actionItem = this.popAction(); this.log('POP ACTION ITEM', actionItem); if (actionItem !== null) { @@ -34,7 +41,7 @@ export default class Scheduler { await this.runAction(action, actionItem.args); } } else { - const taskItem = this.popTask(); + const taskItem = this.getTask(); this.log('POP TASK ITEM', taskItem); if (taskItem !== null) { const task = this.createTask(taskItem); @@ -47,6 +54,19 @@ export default class Scheduler { } } + private async sleep() { + if (this.sleepType === SleepType.Long) { + await sleepLong(); + } else { + await sleepShort(); + } + this.sleepType = SleepType.Short; + } + + private nextSleepLong() { + this.sleepType = SleepType.Long; + } + taskState(): ImmutableState { return this.taskQueue.state(); } @@ -65,7 +85,11 @@ export default class Scheduler { this.actionQueue.assign(actions); } - private popTask(): Command | null { + completeCurrentTask() { + this.taskQueue.next(); + } + + private getTask(): Command | null { return this.taskQueue.current() || this.taskQueue.next(); } @@ -92,7 +116,7 @@ export default class Scheduler { return new GoToBuildingAction(); } if (actionItem.name === UpgradeBuildingAction.NAME) { - return new UpgradeBuildingAction(); + return new UpgradeBuildingAction(this); } return null; } @@ -106,6 +130,7 @@ export default class Scheduler { console.warn('TRY AFTER', e.seconds); this.actionQueue.clear(); this.taskQueue.postpone(e.seconds); + this.nextSleepLong(); } } } diff --git a/src/utils.ts b/src/utils.ts index d481014..c40fbbc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,13 +3,13 @@ export function sleep(ms: number) { } export async function sleepShort() { - let ms = 2000 + Math.random() * 1000; + let ms = 3000 + Math.random() * 1000; console.log('SLEEP SHORT', Math.round(ms)); return await sleep(ms); } export async function sleepLong() { - let ms = 30_000 + Math.random() * 120_000; + let ms = 120_000 + Math.random() * 300_000; console.log('SLEEP LONG', Math.round(ms)); return await sleep(ms); }