Improvements
This commit is contained in:
parent
c980abfc63
commit
e215b5ca93
@ -1,7 +0,0 @@
|
|||||||
import Action from './Action';
|
|
||||||
|
|
||||||
export default class GoToMainAction extends Action {
|
|
||||||
async run(): Promise<any> {
|
|
||||||
return Promise.resolve(null);
|
|
||||||
}
|
|
||||||
}
|
|
8
src/Action/GoToResourceFieldsAction.ts
Normal file
8
src/Action/GoToResourceFieldsAction.ts
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
14
src/Action/StoreRemainingBuildTimeAction.ts
Normal file
14
src/Action/StoreRemainingBuildTimeAction.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,19 +1,26 @@
|
|||||||
import Action from './Action';
|
import Action from './Action';
|
||||||
import { Args } from '../Common';
|
import { Args } from '../Common';
|
||||||
import { TryLaterError } from '../Errors';
|
import { TryLaterError } from '../Errors';
|
||||||
|
import Scheduler from '../Scheduler';
|
||||||
|
|
||||||
export default class UpgradeBuildingAction extends Action {
|
export default class UpgradeBuildingAction extends Action {
|
||||||
static NAME = 'upgrade_building';
|
static NAME = 'upgrade_building';
|
||||||
|
private scheduler: Scheduler;
|
||||||
|
|
||||||
|
constructor(scheduler: Scheduler) {
|
||||||
|
super();
|
||||||
|
this.scheduler = scheduler;
|
||||||
|
}
|
||||||
|
|
||||||
async run(args: Args): Promise<any> {
|
async run(args: Args): Promise<any> {
|
||||||
const btn = jQuery(
|
const btn = jQuery(
|
||||||
'.upgradeButtonsContainer .section1 button.green.build'
|
'.upgradeButtonsContainer .section1 button.green.build'
|
||||||
);
|
);
|
||||||
if (btn.length === 1) {
|
if (btn.length === 1) {
|
||||||
|
this.scheduler.completeCurrentTask();
|
||||||
btn.trigger('click');
|
btn.trigger('click');
|
||||||
} else {
|
} else {
|
||||||
console.log('NO UPGRADE BUTTON');
|
throw new TryLaterError(60, 'No upgrade button, try later');
|
||||||
throw new TryLaterError(60);
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import * as URLParse from 'url-parse';
|
import * as URLParse from 'url-parse';
|
||||||
import { markPage, sleepShort } from './utils';
|
import { markPage, sleep, sleepShort } from './utils';
|
||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
import Scheduler from './Scheduler';
|
import Scheduler from './Scheduler';
|
||||||
import UpgradeBuildingTask from './Task/UpgradeBuildingTask';
|
import UpgradeBuildingTask from './Task/UpgradeBuildingTask';
|
||||||
@ -16,14 +16,23 @@ export default class Dashboard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async run() {
|
async run() {
|
||||||
|
await this.load();
|
||||||
|
await sleep(1000);
|
||||||
|
|
||||||
const p = new URLParse(window.location.href, true);
|
const p = new URLParse(window.location.href, true);
|
||||||
console.log('PARSED LOCATION', p);
|
console.log('PARSED LOCATION', p);
|
||||||
|
|
||||||
await sleepShort();
|
|
||||||
|
|
||||||
markPage('Dashboard', this.version);
|
markPage('Dashboard', this.version);
|
||||||
new TaskQueueRenderer().render(this.scheduler.taskState());
|
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') {
|
if (p.pathname === '/build.php') {
|
||||||
console.log('BUILD PAGE DETECTED');
|
console.log('BUILD PAGE DETECTED');
|
||||||
const id = uuid();
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,15 +8,22 @@ import ActionQueue from './Storage/ActionQueue';
|
|||||||
import { Args, Command } from './Common';
|
import { Args, Command } from './Common';
|
||||||
import TaskQueueRenderer from './TaskQueueRenderer';
|
import TaskQueueRenderer from './TaskQueueRenderer';
|
||||||
|
|
||||||
|
enum SleepType {
|
||||||
|
Long,
|
||||||
|
Short,
|
||||||
|
}
|
||||||
|
|
||||||
export default class Scheduler {
|
export default class Scheduler {
|
||||||
private readonly version: string;
|
private readonly version: string;
|
||||||
private taskQueue: TaskQueue;
|
private taskQueue: TaskQueue;
|
||||||
private actionQueue: ActionQueue;
|
private actionQueue: ActionQueue;
|
||||||
|
private sleepType: SleepType;
|
||||||
|
|
||||||
constructor(version: string) {
|
constructor(version: string) {
|
||||||
this.version = version;
|
this.version = version;
|
||||||
this.taskQueue = new TaskQueue();
|
this.taskQueue = new TaskQueue();
|
||||||
this.actionQueue = new ActionQueue();
|
this.actionQueue = new ActionQueue();
|
||||||
|
this.sleepType = SleepType.Short;
|
||||||
}
|
}
|
||||||
|
|
||||||
async run() {
|
async run() {
|
||||||
@ -24,7 +31,7 @@ export default class Scheduler {
|
|||||||
markPage('Executor', this.version);
|
markPage('Executor', this.version);
|
||||||
new TaskQueueRenderer().render(this.taskQueue.state());
|
new TaskQueueRenderer().render(this.taskQueue.state());
|
||||||
while (true) {
|
while (true) {
|
||||||
await sleepLong();
|
await this.sleep();
|
||||||
const actionItem = this.popAction();
|
const actionItem = this.popAction();
|
||||||
this.log('POP ACTION ITEM', actionItem);
|
this.log('POP ACTION ITEM', actionItem);
|
||||||
if (actionItem !== null) {
|
if (actionItem !== null) {
|
||||||
@ -34,7 +41,7 @@ export default class Scheduler {
|
|||||||
await this.runAction(action, actionItem.args);
|
await this.runAction(action, actionItem.args);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const taskItem = this.popTask();
|
const taskItem = this.getTask();
|
||||||
this.log('POP TASK ITEM', taskItem);
|
this.log('POP TASK ITEM', taskItem);
|
||||||
if (taskItem !== null) {
|
if (taskItem !== null) {
|
||||||
const task = this.createTask(taskItem);
|
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 {
|
taskState(): ImmutableState {
|
||||||
return this.taskQueue.state();
|
return this.taskQueue.state();
|
||||||
}
|
}
|
||||||
@ -65,7 +85,11 @@ export default class Scheduler {
|
|||||||
this.actionQueue.assign(actions);
|
this.actionQueue.assign(actions);
|
||||||
}
|
}
|
||||||
|
|
||||||
private popTask(): Command | null {
|
completeCurrentTask() {
|
||||||
|
this.taskQueue.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
private getTask(): Command | null {
|
||||||
return this.taskQueue.current() || this.taskQueue.next();
|
return this.taskQueue.current() || this.taskQueue.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,7 +116,7 @@ export default class Scheduler {
|
|||||||
return new GoToBuildingAction();
|
return new GoToBuildingAction();
|
||||||
}
|
}
|
||||||
if (actionItem.name === UpgradeBuildingAction.NAME) {
|
if (actionItem.name === UpgradeBuildingAction.NAME) {
|
||||||
return new UpgradeBuildingAction();
|
return new UpgradeBuildingAction(this);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -106,6 +130,7 @@ export default class Scheduler {
|
|||||||
console.warn('TRY AFTER', e.seconds);
|
console.warn('TRY AFTER', e.seconds);
|
||||||
this.actionQueue.clear();
|
this.actionQueue.clear();
|
||||||
this.taskQueue.postpone(e.seconds);
|
this.taskQueue.postpone(e.seconds);
|
||||||
|
this.nextSleepLong();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,13 @@ export function sleep(ms: number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function sleepShort() {
|
export async function sleepShort() {
|
||||||
let ms = 2000 + Math.random() * 1000;
|
let ms = 3000 + Math.random() * 1000;
|
||||||
console.log('SLEEP SHORT', Math.round(ms));
|
console.log('SLEEP SHORT', Math.round(ms));
|
||||||
return await sleep(ms);
|
return await sleep(ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function sleepLong() {
|
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));
|
console.log('SLEEP LONG', Math.round(ms));
|
||||||
return await sleep(ms);
|
return await sleep(ms);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user