Fix pushing new build tasks

This commit is contained in:
2020-04-20 22:21:26 +03:00
parent 4ff55b5850
commit 58b65aff82
3 changed files with 45 additions and 27 deletions

View File

@ -16,16 +16,18 @@ export function clickBuildButton(typeId: number) {
btn.trigger('click');
}
export function createBuildButton(onClickHandler: (buildTypeId: number) => void) {
export function createBuildButton(onClickHandler: (buildTypeId: number, resources: Resources) => 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 buildTypeId = getNumber(trimPrefix($el.attr('id') || '', 'contract_building'));
const btnId = uniqId();
const resElement = $el.find('.resourceWrapper .resource');
const resources = grabResourcesFromList(resElement);
$el.append(`<div style="padding: 8px"><a id="${btnId}" href="#">Построить</a></div>`);
jQuery(`#${btnId}`).on('click', evt => {
evt.preventDefault();
onClickHandler(id);
onClickHandler(buildTypeId, resources);
});
});
}
@ -43,14 +45,15 @@ export function clickUpgradeButton() {
btn.trigger('click');
}
export function createUpgradeButton(onClickHandler: () => void) {
export function createUpgradeButton(onClickHandler: (resources: Resources) => void) {
const id = uniqId();
jQuery('.upgradeButtonsContainer .section1').append(
`<div style="padding: 8px"><a id="${id}" href="#">В очередь</a></div>`
);
const resources = grabContractResources();
jQuery(`#${id}`).on('click', evt => {
evt.preventDefault();
onClickHandler();
onClickHandler(resources);
});
}

View File

@ -1,10 +1,10 @@
import { elClassId, notify, split, uniqId } from '../utils';
import { notify, split } from '../utils';
import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask';
import { Scheduler } from '../Scheduler';
import { TrainTroopTask } from '../Task/TrainTroopTask';
import { grabActiveVillageId } from './VillageBlock';
import { ConsoleLogger, Logger } from '../Logger';
import { createBuildButton, createTrainTroopButtons, createUpgradeButton, grabContractResources } from './BuildingPage';
import { ConsoleLogger } from '../Logger';
import { createBuildButton, createTrainTroopButtons, createUpgradeButton } from './BuildingPage';
import { BuildBuildingTask } from '../Task/BuildBuildingTask';
import { Resources } from '../Game';
@ -34,8 +34,11 @@ export class BuildingPageController {
const buildTypeId = this.attributes.buildTypeId;
this.logger.log('BUILD PAGE DETECTED', 'ID', this.attributes.buildId, 'TYPE', buildTypeId);
createBuildButton(buildTypeId => this.onScheduleBuildBuilding(buildTypeId));
createUpgradeButton(() => this.onScheduleUpgradeBuilding());
if (buildTypeId) {
createUpgradeButton(res => this.onScheduleUpgradeBuilding(res));
} else {
createBuildButton((buildTypeId, res) => this.onScheduleBuildBuilding(buildTypeId, res));
}
if (buildTypeId === QUARTERS_ID) {
createTrainTroopButtons((troopId, res, count) => this.onScheduleTrainTroopers(troopId, res, count));
@ -50,19 +53,17 @@ export class BuildingPageController {
}
}
private onScheduleBuildBuilding(buildTypeId: number) {
private onScheduleBuildBuilding(buildTypeId: number, resources: Resources) {
const buildId = this.attributes.buildId;
const categoryId = this.attributes.categoryId;
const villageId = grabActiveVillageId();
const resources = grabContractResources();
this.scheduler.scheduleTask(BuildBuildingTask.name, { villageId, buildId, categoryId, buildTypeId, resources });
notify(`Building ${buildId} scheduled`);
}
private onScheduleUpgradeBuilding() {
private onScheduleUpgradeBuilding(resources: Resources) {
const buildId = this.attributes.buildId;
const villageId = grabActiveVillageId();
const resources = grabContractResources();
this.scheduler.scheduleTask(UpgradeBuildingTask.name, { villageId, buildId, resources });
notify(`Upgrading ${buildId} scheduled`);
}