Improve resource contracts updates

This commit is contained in:
2020-05-02 18:40:12 +03:00
parent 53b325eee2
commit d7809818ea
12 changed files with 181 additions and 75 deletions

View File

@ -62,8 +62,16 @@ export function grabResourcesFromList($els: JQuery) {
return new Resources(grab(0), grab(1), grab(2), grab(3));
}
function findContractResourceElements() {
return jQuery('#contract .resource');
}
export function hasContractResources(): boolean {
return findContractResourceElements().length !== 0;
}
export function grabContractResources(): Resources {
const $els = jQuery('#contract .resource');
const $els = findContractResourceElements();
if ($els.length === 0) {
throw new GrabError('No resource contract element');
}

View File

@ -9,16 +9,15 @@ interface ResearchClickHandler {
export function createResearchButtons(onClickHandler: ResearchClickHandler) {
const $els = jQuery('.research');
$els.each((index, $el) => createResearchButton(jQuery($el), onClickHandler));
$els.each((index, el) => createResearchButton(jQuery(el), onClickHandler));
}
function createResearchButton($el: JQuery, onClickHandler: ResearchClickHandler) {
const unitId = grabUnitId($el);
const resElement = $el.find('.resourceWrapper .resource');
const resources = grabResourcesFromList(resElement);
function createResearchButton($researchBlockEl: JQuery, onClickHandler: ResearchClickHandler) {
const unitId = grabUnitId($researchBlockEl);
const resources = grabResources($researchBlockEl);
const id = uniqId();
$el.find('.cta').after(`<div style="padding: 8px">
$researchBlockEl.find('.cta').after(`<div style="padding: 8px">
<a id="${id}" href="#">Исследовать</a>
</div>`);
@ -28,11 +27,16 @@ function createResearchButton($el: JQuery, onClickHandler: ResearchClickHandler)
});
}
function grabUnitId($el: JQuery) {
const unitImg = $el.find('img.unit');
function grabUnitId($researchBlockEl: JQuery) {
const unitImg = $researchBlockEl.find('img.unit');
return getNumber(elClassId(unitImg.attr('class'), 'u'));
}
function grabResources($researchBlockEl: JQuery) {
const $resEls = $researchBlockEl.find('.resourceWrapper .resource');
return grabResourcesFromList($resEls);
}
export function clickResearchButton(unitId: number) {
const $blockEl = findResearchBlockByUnitId(unitId);
if ($blockEl === undefined) {
@ -57,3 +61,20 @@ function findResearchBlockByUnitId(unitId: number): JQuery | undefined {
});
return $blockEl;
}
interface ImprovementContract {
unitId: number;
resources: Resources;
}
export function grabImprovementContracts(): Array<ImprovementContract> {
const result: Array<ImprovementContract> = [];
const $els = jQuery('.research');
$els.each((index, el) => {
const $researchBlockEl = jQuery(el);
const unitId = grabUnitId($researchBlockEl);
const resources = grabResources($researchBlockEl);
result.push({ unitId, resources });
});
return result;
}

View File

@ -84,7 +84,6 @@ export class BuildingPageController {
resources: resources.scale(chunk),
trainCount: chunk,
};
console.log('TRAIN TROOP', args);
this.scheduler.scheduleTask(TrainTroopTask.name, args);
}
notify(`Training ${count} troopers scheduled`);

View File

@ -2,11 +2,12 @@ import { elClassId, getNumber, parseLocation } from '../utils';
import { FORGE_ID, MARKET_ID } from '../Core/Buildings';
export interface BuildingPageAttributes {
buildId: number;
buildTypeId: number;
categoryId: number;
sheetId: number;
tabId: number;
level: number;
buildId: number | undefined;
categoryId: number | undefined;
sheetId: number | undefined;
tabId: number | undefined;
}
export function isBuildingPage() {
@ -24,12 +25,14 @@ export function getBuildingPageAttributes(): BuildingPageAttributes {
throw Error('Not building page');
}
const p = parseLocation();
const $buildEl = jQuery('#build');
return {
buildId: getNumber(p.query.id),
buildTypeId: getNumber(elClassId(jQuery('#build').attr('class'), 'gid')),
buildTypeId: getNumber(elClassId($buildEl.attr('class'), 'gid')),
level: getNumber(elClassId($buildEl.attr('class'), 'level')),
buildId: getNumber(p.query.id) || undefined,
categoryId: getNumber(p.query.category, 1),
sheetId: getNumber(p.query.s, 0),
tabId: getNumber(p.query.t, 0),
sheetId: getNumber(p.query.s) || undefined,
tabId: getNumber(p.query.t) || undefined,
};
}