From a23872972ed65c31724ab2ed2f7d71d07dddd8ab Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sat, 11 Apr 2020 13:54:40 +0300 Subject: [PATCH] Optimize scheduler timers --- src/Action/GrabVillageResourcesAction.ts | 36 ------------------------ src/Scheduler.ts | 7 +++-- src/utils.ts | 6 ++++ 3 files changed, 10 insertions(+), 39 deletions(-) delete mode 100644 src/Action/GrabVillageResourcesAction.ts diff --git a/src/Action/GrabVillageResourcesAction.ts b/src/Action/GrabVillageResourcesAction.ts deleted file mode 100644 index 3deb0ae..0000000 --- a/src/Action/GrabVillageResourcesAction.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { ActionController, registerAction } from './ActionController'; -import { Args } from '../Common'; -import { Task } from '../Storage/TaskQueue'; -import { getNumber } from '../utils'; -import { ActionError } from '../Errors'; - -const LUMBER = 1; -const CLAY = 2; -const IRON = 3; -const CROP = 4; - -@registerAction -export class GrabVillageResourcesAction extends ActionController { - async run(args: Args, task: Task): Promise { - const lumber = this.grabResource(task, LUMBER); - const clay = this.grabResource(task, CLAY); - const iron = this.grabResource(task, IRON); - const crop = this.grabResource(task, CROP); - - this.state.set('resources', { [LUMBER]: lumber, [CLAY]: clay, [IRON]: iron, [CROP]: crop }); - } - - private grabResource(task: Task, type: number): number { - const stockBarElement = jQuery('#stockBar'); - if (stockBarElement.length !== 1) { - throw new ActionError(task.id, 'Stock Bar not found'); - } - - const resElement = stockBarElement.find(`#l${type}`); - if (resElement.length !== 1) { - throw new ActionError(task.id, `Resource #${type} not found`); - } - - return getNumber(resElement.text().replace(/[^0-9]/g, '')); - } -} diff --git a/src/Scheduler.ts b/src/Scheduler.ts index 90979a2..7bbe5fe 100644 --- a/src/Scheduler.ts +++ b/src/Scheduler.ts @@ -1,4 +1,4 @@ -import { markPage, sleepShort, timestamp } from './utils'; +import { markPage, sleepMicro, timestamp, waitForLoad } from './utils'; import { UpgradeBuildingTask } from './Task/UpgradeBuildingTask'; import { AbortTaskError, ActionError, BuildingQueueFullError, TryLaterError } from './Errors'; import { Task, TaskId, TaskList, TaskQueue } from './Storage/TaskQueue'; @@ -25,7 +25,8 @@ export class Scheduler { } async run() { - await sleepShort(); + await waitForLoad(); + await sleepMicro(); markPage('Executor', this.version); this.renderTaskQueue(); @@ -55,7 +56,7 @@ export class Scheduler { } private async doTaskProcessingStep() { - await sleepShort(); + await sleepMicro(); const currentTs = timestamp(); const taskCommand = this.taskQueue.get(currentTs); diff --git a/src/utils.ts b/src/utils.ts index e8cdcfa..4f80e00 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -6,6 +6,12 @@ export function sleep(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)); } +export async function sleepMicro() { + let ms = 1000 + Math.random() * 1000; + console.log('SLEEP SHORT', Math.round(ms / 1000)); + return await sleep(ms); +} + export async function sleepShort() { let ms = 3000 + Math.random() * 1000; console.log('SLEEP SHORT', Math.round(ms / 1000));