Improvements

This commit is contained in:
Anton Vakhrushev 2020-03-29 21:22:38 +03:00
parent c980abfc63
commit e215b5ca93
7 changed files with 95 additions and 18 deletions

View File

@ -1,7 +0,0 @@
import Action from './Action';
export default class GoToMainAction extends Action {
async run(): Promise<any> {
return Promise.resolve(null);
}
}

View File

@ -0,0 +1,8 @@
import Action from './Action';
export default class GoToResourceFieldsAction extends Action {
static NAME = 'go_to_resource_fields';
async run(): Promise<any> {
window.location.assign('/dorf1.php');
}
}

View File

@ -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<any> {
const timer = jQuery('.buildDuration .timer');
// if (timer.length === 1) {
// const remainingSeconds = +timer.val();
// }
return null;
}
}

View File

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

View File

@ -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));
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}