Add task for building upgrade
This commit is contained in:
@ -1,3 +1,3 @@
|
||||
export default abstract class Action {
|
||||
abstract async run();
|
||||
abstract async run(args);
|
||||
}
|
||||
|
10
src/Action/GoToBuildingAction.ts
Normal file
10
src/Action/GoToBuildingAction.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import Action from './Action';
|
||||
|
||||
export default class GoToBuildingAction extends Action {
|
||||
static NAME = 'go_to_building';
|
||||
|
||||
async run(args): Promise<any> {
|
||||
window.location.assign('/build.php?id=' + args.id);
|
||||
return null;
|
||||
}
|
||||
}
|
17
src/Action/UpgradeBuildingAction.ts
Normal file
17
src/Action/UpgradeBuildingAction.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import Action from './Action';
|
||||
|
||||
export default class UpgradeBuildingAction extends Action {
|
||||
static NAME = 'upgrade_building';
|
||||
|
||||
async run(args): Promise<any> {
|
||||
const btn = jQuery(
|
||||
'.upgradeButtonsContainer .section1 button.green.build'
|
||||
);
|
||||
if (btn.length === 1) {
|
||||
btn.trigger('click');
|
||||
} else {
|
||||
console.log('NO UPGRADE BUTTON');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
40
src/Dashboard.ts
Normal file
40
src/Dashboard.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import * as URLParse from 'url-parse';
|
||||
import { sleep } from './utils';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import { QueueItem } from './Queue';
|
||||
import Scheduler from './Scheduler';
|
||||
import UpgradeBuildingTask from './Task/UpgradeBuildingTask';
|
||||
|
||||
export default class Dashboard {
|
||||
private scheduler: Scheduler;
|
||||
|
||||
constructor(scheduler: Scheduler) {
|
||||
this.scheduler = scheduler;
|
||||
}
|
||||
|
||||
async run() {
|
||||
jQuery('body').append(
|
||||
'<div style="position: absolute; top: 0; left: 0; background-color: white">Dashboard</div>'
|
||||
);
|
||||
|
||||
const p = new URLParse(window.location.href, true);
|
||||
console.log('PARSED LOCATION', p);
|
||||
|
||||
await sleep(5000);
|
||||
|
||||
if (p.pathname === '/build.php') {
|
||||
console.log('BUILD PAGE DETECTED');
|
||||
const id = uuid();
|
||||
jQuery('.upgradeButtonsContainer .section1').append(
|
||||
`<div style="padding: 8px"><a id="${id}" href="#">В очередь</a></div>`
|
||||
);
|
||||
jQuery(`#${id}`).on('click', () => {
|
||||
const queueItem = new QueueItem(UpgradeBuildingTask.NAME, {
|
||||
id: p.query['id'],
|
||||
});
|
||||
this.scheduler.pushTask(queueItem);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -27,7 +27,7 @@ export class Queue {
|
||||
}
|
||||
const first = items.shift();
|
||||
|
||||
localStorage.setItem(this.name, JSON.stringify(items));
|
||||
this.flush(items);
|
||||
|
||||
if (first === undefined) {
|
||||
return null;
|
||||
@ -41,7 +41,12 @@ export class Queue {
|
||||
const items = serialized
|
||||
? (JSON.parse(serialized) as Array<QueueItem>)
|
||||
: [];
|
||||
const first = items.push(item);
|
||||
items.push(item);
|
||||
this.flush(items);
|
||||
}
|
||||
|
||||
private flush(items) {
|
||||
console.log('SET NEW QUEUE', this.name, items);
|
||||
localStorage.setItem(this.name, JSON.stringify(items));
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
import { sleep } from './utils';
|
||||
import { Queue } from './Queue';
|
||||
import GoToMainAction from './Action/GoToMainAction';
|
||||
import { sleepLong } from './utils';
|
||||
import { Queue, QueueItem } from './Queue';
|
||||
import UpgradeBuildingTask from './Task/UpgradeBuildingTask';
|
||||
import GoToBuildingAction from './Action/GoToBuildingAction';
|
||||
import UpgradeBuildingAction from './Action/UpgradeBuildingAction';
|
||||
|
||||
const ACTION_QUEUE = 'action_queue';
|
||||
const TASK_QUEUE = 'task_queue';
|
||||
@ -16,39 +18,76 @@ export default class Scheduler {
|
||||
|
||||
async run() {
|
||||
while (true) {
|
||||
const action = this.popAction();
|
||||
console.log('POP ACTION', action);
|
||||
if (action !== null) {
|
||||
await action.run();
|
||||
await sleepLong();
|
||||
const actionItem = this.popAction();
|
||||
this.log('POP ACTION ITEM', actionItem);
|
||||
if (actionItem !== null) {
|
||||
const action = this.createAction(actionItem);
|
||||
this.log('POP ACTION', action);
|
||||
if (action) {
|
||||
await action.run(actionItem.args);
|
||||
}
|
||||
} else {
|
||||
const task = this.popTask();
|
||||
console.log('POP TASK', task);
|
||||
if (task !== null) {
|
||||
// do task
|
||||
const taskItem = this.popTask();
|
||||
this.log('POP TASK ITEM', taskItem);
|
||||
if (taskItem !== null) {
|
||||
const task = this.createTask(taskItem);
|
||||
this.log('POP TASK', task);
|
||||
if (task !== null) {
|
||||
task.run(taskItem.args);
|
||||
}
|
||||
}
|
||||
}
|
||||
const waitTime = Math.random() * 5000;
|
||||
console.log('WAIT', waitTime);
|
||||
await sleep(waitTime);
|
||||
}
|
||||
}
|
||||
|
||||
pushTask(task: QueueItem): void {
|
||||
this.log('PUSH TASK', task);
|
||||
this.taskQueue.push(task);
|
||||
}
|
||||
|
||||
pushAction(action: QueueItem): void {
|
||||
this.log('PUSH ACTION', action);
|
||||
this.actionQueue.push(action);
|
||||
}
|
||||
|
||||
private popTask() {
|
||||
const item = this.taskQueue.pop();
|
||||
if (item === null) {
|
||||
const taskItem = this.taskQueue.pop();
|
||||
if (taskItem === null) {
|
||||
return null;
|
||||
}
|
||||
return taskItem;
|
||||
}
|
||||
|
||||
private createTask(taskItem: QueueItem) {
|
||||
switch (taskItem.name) {
|
||||
case UpgradeBuildingTask.NAME:
|
||||
return new UpgradeBuildingTask(this);
|
||||
}
|
||||
this.log('UNKNOWN TASK', taskItem.name);
|
||||
return null;
|
||||
}
|
||||
|
||||
private popAction() {
|
||||
const item = this.actionQueue.pop();
|
||||
if (item === null) {
|
||||
const actionItem = this.actionQueue.pop();
|
||||
if (actionItem === null) {
|
||||
return null;
|
||||
}
|
||||
if (item.name === 'go_to_main') {
|
||||
return new GoToMainAction();
|
||||
this.log('UNKNOWN ACTION', actionItem.name);
|
||||
return actionItem;
|
||||
}
|
||||
|
||||
private createAction(actionItem: QueueItem) {
|
||||
if (actionItem.name === GoToBuildingAction.NAME) {
|
||||
return new GoToBuildingAction();
|
||||
}
|
||||
if (actionItem.name === UpgradeBuildingAction.NAME) {
|
||||
return new UpgradeBuildingAction();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private log(...args) {
|
||||
console.log('SCHEDULER', ...args);
|
||||
}
|
||||
}
|
||||
|
21
src/Task/UpgradeBuildingTask.ts
Normal file
21
src/Task/UpgradeBuildingTask.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import Scheduler from '../Scheduler';
|
||||
import GoToBuildingAction from '../Action/GoToBuildingAction';
|
||||
import UpgradeBuildingAction from '../Action/UpgradeBuildingAction';
|
||||
import { QueueItem } from '../Queue';
|
||||
|
||||
export default class UpgradeBuildingTask {
|
||||
static NAME = 'upgrade_building';
|
||||
private scheduler: Scheduler;
|
||||
|
||||
constructor(scheduler: Scheduler) {
|
||||
this.scheduler = scheduler;
|
||||
}
|
||||
|
||||
run(args) {
|
||||
console.log('RUN', UpgradeBuildingTask.NAME, 'with', args);
|
||||
this.scheduler.pushAction(new QueueItem(GoToBuildingAction.NAME, args));
|
||||
this.scheduler.pushAction(
|
||||
new QueueItem(UpgradeBuildingAction.NAME, args)
|
||||
);
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import ModeDetector from './ModeDetector';
|
||||
import Scheduler from './Scheduler';
|
||||
import Dashboard from './Dashboard';
|
||||
|
||||
const md = new ModeDetector();
|
||||
if (md.isAuto()) {
|
||||
@ -9,4 +10,6 @@ if (md.isAuto()) {
|
||||
scheduler.run();
|
||||
} else {
|
||||
console.log('NORMAL MODE');
|
||||
const dashboard = new Dashboard(new Scheduler());
|
||||
dashboard.run();
|
||||
}
|
||||
|
14
src/utils.ts
14
src/utils.ts
@ -1,5 +1,15 @@
|
||||
function sleep(ms: number) {
|
||||
export function sleep(ms: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
export { sleep };
|
||||
export async function sleepShort() {
|
||||
let ms = 2000 + Math.random() * 10000;
|
||||
console.log('SLEEp SHORT', Math.round(ms));
|
||||
return await sleep(ms);
|
||||
}
|
||||
|
||||
export async function sleepLong() {
|
||||
let ms = 10000 + Math.random() * 10000;
|
||||
console.log('SLEEP LONG', Math.round(ms));
|
||||
return await sleep(ms);
|
||||
}
|
||||
|
Reference in New Issue
Block a user