Add build building task

This commit is contained in:
2020-04-12 20:41:26 +03:00
parent 9d85d07ff5
commit 9fdc573746
8 changed files with 127 additions and 25 deletions

View File

@ -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() {

View File

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

View File

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