Optimize scheduler timers

This commit is contained in:
Anton Vakhrushev 2020-04-11 13:54:40 +03:00
parent 2d9c3f94e7
commit a23872972e
3 changed files with 10 additions and 39 deletions

View File

@ -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<any> {
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, ''));
}
}

View File

@ -1,4 +1,4 @@
import { markPage, sleepShort, timestamp } from './utils'; import { markPage, sleepMicro, timestamp, waitForLoad } from './utils';
import { UpgradeBuildingTask } from './Task/UpgradeBuildingTask'; import { UpgradeBuildingTask } from './Task/UpgradeBuildingTask';
import { AbortTaskError, ActionError, BuildingQueueFullError, TryLaterError } from './Errors'; import { AbortTaskError, ActionError, BuildingQueueFullError, TryLaterError } from './Errors';
import { Task, TaskId, TaskList, TaskQueue } from './Storage/TaskQueue'; import { Task, TaskId, TaskList, TaskQueue } from './Storage/TaskQueue';
@ -25,7 +25,8 @@ export class Scheduler {
} }
async run() { async run() {
await sleepShort(); await waitForLoad();
await sleepMicro();
markPage('Executor', this.version); markPage('Executor', this.version);
this.renderTaskQueue(); this.renderTaskQueue();
@ -55,7 +56,7 @@ export class Scheduler {
} }
private async doTaskProcessingStep() { private async doTaskProcessingStep() {
await sleepShort(); await sleepMicro();
const currentTs = timestamp(); const currentTs = timestamp();
const taskCommand = this.taskQueue.get(currentTs); const taskCommand = this.taskQueue.get(currentTs);

View File

@ -6,6 +6,12 @@ export function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms)); 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() { export async function sleepShort() {
let ms = 3000 + Math.random() * 1000; let ms = 3000 + Math.random() * 1000;
console.log('SLEEP SHORT', Math.round(ms / 1000)); console.log('SLEEP SHORT', Math.round(ms / 1000));