Add build building task
This commit is contained in:
parent
9d85d07ff5
commit
9fdc573746
24
src/Action/BuildBuildingAction.ts
Normal file
24
src/Action/BuildBuildingAction.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { ActionController, registerAction } from './ActionController';
|
||||
import { Args } from '../Common';
|
||||
import { ActionError, GrabError, TryLaterError } from '../Errors';
|
||||
import { Task } from '../Storage/TaskQueue';
|
||||
import { clickBuildButton } from '../Page/BuildingPage';
|
||||
|
||||
@registerAction
|
||||
export class BuildBuildingAction extends ActionController {
|
||||
async run(args: Args, task: Task): Promise<any> {
|
||||
const buildTypeId = args.buildTypeId;
|
||||
if (!buildTypeId) {
|
||||
throw new ActionError(task.id, 'Unknown build type id');
|
||||
}
|
||||
|
||||
try {
|
||||
clickBuildButton(buildTypeId);
|
||||
} catch (e) {
|
||||
if (e instanceof GrabError) {
|
||||
throw new TryLaterError(task.id, 15 * 60, 'No build button, try later');
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
export interface Args {
|
||||
villageId?: number;
|
||||
buildId?: number;
|
||||
categoryId?: number;
|
||||
buildTypeId?: number;
|
||||
[name: string]: any;
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import * as URLParse from 'url-parse';
|
||||
import { uniqId, waitForLoad } from '../utils';
|
||||
import { getNumber, toNumber, uniqId, waitForLoad } from '../utils';
|
||||
import { Scheduler } from '../Scheduler';
|
||||
import { BuildPage } from '../Page/BuildPage';
|
||||
import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask';
|
||||
@ -77,7 +77,7 @@ export class Dashboard {
|
||||
}
|
||||
|
||||
if (p.pathname === '/build.php') {
|
||||
new BuildPage(this.scheduler, Number(p.query.id)).run();
|
||||
new BuildPage(this.scheduler, getNumber(p.query.id), getNumber(p.query.category, 1)).run();
|
||||
}
|
||||
|
||||
this.createControlPanel(state);
|
||||
|
@ -1,9 +1,11 @@
|
||||
import { elClassId, split, uniqId } from '../utils';
|
||||
import { elClassId, notify, split, uniqId } from '../utils';
|
||||
import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask';
|
||||
import { Scheduler } from '../Scheduler';
|
||||
import { TrainTroopTask } from '../Task/TrainTroopTask';
|
||||
import { grabActiveVillageId } from './VillageBlock';
|
||||
import { Logger } from '../Logger';
|
||||
import { createBuildButton, createUpgradeButton } from './BuildingPage';
|
||||
import { BuildBuildingTask } from '../Task/BuildBuildingTask';
|
||||
|
||||
const QUARTERS_ID = 19;
|
||||
|
||||
@ -11,37 +13,40 @@ export class BuildPage {
|
||||
private scheduler: Scheduler;
|
||||
private readonly buildId: number;
|
||||
private readonly logger;
|
||||
constructor(scheduler: Scheduler, buildId: number) {
|
||||
private readonly categoryId: number;
|
||||
|
||||
constructor(scheduler: Scheduler, buildId: number, categoryId: number) {
|
||||
this.scheduler = scheduler;
|
||||
this.buildId = buildId;
|
||||
this.categoryId = categoryId;
|
||||
this.logger = new Logger(this.constructor.name);
|
||||
}
|
||||
|
||||
run() {
|
||||
const buildTypeId = elClassId(jQuery('#build').attr('class') || '', 'gid');
|
||||
this.logger.log('BUILD PAGE DETECTED', 'ID', this.buildId, 'TYPE', buildTypeId);
|
||||
this.createUpgradeButton();
|
||||
|
||||
createBuildButton(buildTypeId => this.onScheduleBuildBuilding(buildTypeId));
|
||||
createUpgradeButton(() => this.onScheduleUpgradeBuilding());
|
||||
|
||||
if (buildTypeId === QUARTERS_ID) {
|
||||
this.createTrainTroopButton();
|
||||
}
|
||||
}
|
||||
|
||||
private createUpgradeButton() {
|
||||
const id = uniqId();
|
||||
jQuery('.upgradeButtonsContainer .section1').append(
|
||||
`<div style="padding: 8px"><a id="${id}" href="#">В очередь</a></div>`
|
||||
);
|
||||
jQuery(`#${id}`).on('click', evt => {
|
||||
evt.preventDefault();
|
||||
this.onScheduleBuilding(this.buildId);
|
||||
});
|
||||
private onScheduleBuildBuilding(buildTypeId: number) {
|
||||
const buildId = this.buildId;
|
||||
const categoryId = this.categoryId;
|
||||
const villageId = grabActiveVillageId();
|
||||
this.scheduler.scheduleTask(BuildBuildingTask.name, { villageId, buildId, categoryId, buildTypeId });
|
||||
notify(`Building ${buildId} scheduled`);
|
||||
}
|
||||
|
||||
private onScheduleBuilding(buildId: number) {
|
||||
private onScheduleUpgradeBuilding() {
|
||||
const buildId = this.buildId;
|
||||
const villageId = grabActiveVillageId();
|
||||
this.scheduler.scheduleTask(UpgradeBuildingTask.name, { villageId, buildId });
|
||||
const n = new Notification(`Building ${buildId} scheduled`);
|
||||
setTimeout(() => n && n.close(), 4000);
|
||||
notify(`Upgrading ${buildId} scheduled`);
|
||||
}
|
||||
|
||||
private createTrainTroopButton() {
|
||||
|
@ -1,4 +1,31 @@
|
||||
import { GrabError } from '../Errors';
|
||||
import { getNumber, trimPrefix, uniqId } from '../utils';
|
||||
|
||||
export function clickBuildButton(typeId: number) {
|
||||
const section = jQuery(`#contract_building${typeId}`);
|
||||
if (section.length !== 1) {
|
||||
throw new GrabError('No build section');
|
||||
}
|
||||
const btn = section.find('.contractLink button.green.new');
|
||||
if (btn.length !== 1) {
|
||||
throw new GrabError('No build button, try later');
|
||||
}
|
||||
btn.trigger('click');
|
||||
}
|
||||
|
||||
export function createBuildButton(onClickHandler: (buildTypeId: number) => void) {
|
||||
const $els = jQuery('[id^=contract_building]');
|
||||
$els.each((idx, el) => {
|
||||
const $el = jQuery(el);
|
||||
const id = getNumber(trimPrefix($el.attr('id') || '', 'contract_building'));
|
||||
const btnId = uniqId();
|
||||
$el.append(`<div style="padding: 8px"><a id="${btnId}" href="#">Построить</a></div>`);
|
||||
jQuery(`#${btnId}`).on('click', evt => {
|
||||
evt.preventDefault();
|
||||
onClickHandler(id);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function clickUpgradeButton() {
|
||||
const btn = jQuery('.upgradeButtonsContainer .section1 button.green.build');
|
||||
@ -7,3 +34,14 @@ export function clickUpgradeButton() {
|
||||
}
|
||||
btn.trigger('click');
|
||||
}
|
||||
|
||||
export function createUpgradeButton(onClickHandler: () => void) {
|
||||
const id = uniqId();
|
||||
jQuery('.upgradeButtonsContainer .section1').append(
|
||||
`<div style="padding: 8px"><a id="${id}" href="#">В очередь</a></div>`
|
||||
);
|
||||
jQuery(`#${id}`).on('click', evt => {
|
||||
evt.preventDefault();
|
||||
onClickHandler();
|
||||
});
|
||||
}
|
||||
|
@ -3,6 +3,14 @@ import { GrabError } from '../Errors';
|
||||
import * as URLParse from 'url-parse';
|
||||
import { getNumber } from '../utils';
|
||||
|
||||
function getVillageListItems() {
|
||||
const $elements = jQuery('#sidebarBoxVillagelist ul li a');
|
||||
if ($elements.length === 0) {
|
||||
throw new GrabError('Village list items not found');
|
||||
}
|
||||
return $elements;
|
||||
}
|
||||
|
||||
export function grabVillageList(): VillageList {
|
||||
const villageList: VillageList = [];
|
||||
const $elements = getVillageListItems();
|
||||
@ -26,14 +34,6 @@ export function grabActiveVillageId(): number {
|
||||
return grabActiveVillage()?.id || 0;
|
||||
}
|
||||
|
||||
function getVillageListItems() {
|
||||
const $elements = jQuery('#sidebarBoxVillagelist ul li a');
|
||||
if ($elements.length === 0) {
|
||||
throw new GrabError('Village list items not found');
|
||||
}
|
||||
return $elements;
|
||||
}
|
||||
|
||||
function grabVillageInfo($el): Village {
|
||||
const href = $el.attr('href');
|
||||
const parsedHref = new URLParse(href || '', true);
|
||||
|
28
src/Task/BuildBuildingTask.ts
Normal file
28
src/Task/BuildBuildingTask.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { Args, Command } from '../Common';
|
||||
import { BuildBuildingAction } from '../Action/BuildBuildingAction';
|
||||
import { CheckBuildingRemainingTimeAction } from '../Action/CheckBuildingRemainingTimeAction';
|
||||
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
|
||||
import { GoToPageAction } from '../Action/GoToPageAction';
|
||||
import { path } from '../utils';
|
||||
import { Task } from '../Storage/TaskQueue';
|
||||
import { TaskController, registerTask } from './TaskController';
|
||||
|
||||
@registerTask
|
||||
export class BuildBuildingTask extends TaskController {
|
||||
async run(task: Task) {
|
||||
const args: Args = { ...task.args, taskId: task.id };
|
||||
this.scheduler.scheduleActions([
|
||||
new Command(GoToPageAction.name, {
|
||||
...args,
|
||||
path: path('/dorf1.php', { newdid: args.villageId }),
|
||||
}),
|
||||
new Command(CheckBuildingRemainingTimeAction.name, args),
|
||||
new Command(GoToPageAction.name, {
|
||||
...args,
|
||||
path: path('/build.php', { newdid: args.villageId, id: args.buildId, category: args.categoryId }),
|
||||
}),
|
||||
new Command(BuildBuildingAction.name, args),
|
||||
new Command(CompleteTaskAction.name, args),
|
||||
]);
|
||||
}
|
||||
}
|
@ -95,6 +95,11 @@ export function path(p: string, query: { [key: string]: string | number | undefi
|
||||
return p + (parts.length ? '?' + parts.join('&') : '');
|
||||
}
|
||||
|
||||
export function notify(msg: string): void {
|
||||
const n = new Notification(msg);
|
||||
setTimeout(() => n && n.close(), 4000);
|
||||
}
|
||||
|
||||
export function markPage(text: string, version: string) {
|
||||
jQuery('body').append(
|
||||
'<div style="' +
|
||||
|
Loading…
Reference in New Issue
Block a user