Rewrite hero resource action

try to detect hero village before balancing
This commit is contained in:
2020-04-11 13:46:10 +03:00
parent 8e8b358b91
commit 2d9c3f94e7
12 changed files with 279 additions and 76 deletions

View File

@ -2,7 +2,7 @@ import { elClassId, split, uniqId } from '../utils';
import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask';
import { Scheduler } from '../Scheduler';
import { TrainTroopTask } from '../Task/TrainTroopTask';
import { grabActiveVillageId } from './EveryPage';
import { grabActiveVillageId } from './VillageBlock';
const QUARTERS_ID = 19;

52
src/Page/HeroPage.ts Normal file
View File

@ -0,0 +1,52 @@
import { GrabError } from '../Errors';
import { HeroAllResources, HeroResourceType, ResourceMapping, ResourceType } from '../Game';
export function grabCurrentHeroResource(): HeroResourceType {
for (let mp of ResourceMapping) {
if (checkHeroResourceType(mp.num)) {
return mp.type;
}
}
return HeroAllResources;
}
function checkHeroResourceType(typeAsNumber: number): boolean {
const input = jQuery(`#resourceHero${typeAsNumber}`);
if (input.length !== 1) {
throw new GrabError(`Hero resource ${typeAsNumber} not found`);
}
return !!input.prop('checked');
}
export function changeHeroResource(type: HeroResourceType) {
const typeAsNumber = heroResourceTypeToNumber(type);
const input = jQuery(`#resourceHero${typeAsNumber}`);
if (input.length !== 1) {
throw new GrabError(`Hero resource ${typeAsNumber} not found`);
}
const btn = jQuery('#saveHeroAttributes');
if (btn.length !== 1) {
throw new GrabError(`Hero resource button not found`);
}
input.trigger('click');
btn.trigger('click');
}
function heroResourceTypeToNumber(type: HeroResourceType): number {
if (type === HeroAllResources) {
return 0;
}
return type as ResourceType;
}
export function grabHeroVillage(): string | undefined {
const status = jQuery('.heroStatusMessage').text();
const hrefText = jQuery('.heroStatusMessage a').text();
if (status.includes('в родной деревне')) {
return hrefText || undefined;
} else {
return undefined;
}
}

View File

@ -0,0 +1,39 @@
import { Resources, ResourceType } from '../Game';
import { GrabError } from '../Errors';
import { getNumber } from '../utils';
export function grabResources(): Resources {
const lumber = grabResource(ResourceType.Lumber);
const clay = grabResource(ResourceType.Clay);
const iron = grabResource(ResourceType.Iron);
const crop = grabResource(ResourceType.Crop);
const warehouse = grabCapacity('warehouse');
const granary = grabCapacity('granary');
return new Resources(lumber, clay, iron, crop, warehouse, granary);
}
function findStockBarElement() {
const stockBarElement = jQuery('#stockBar');
if (stockBarElement.length !== 1) {
throw new GrabError('Stock Bar not found');
}
return stockBarElement;
}
function grabResource(type: number): number {
const resElement = findStockBarElement().find(`#l${type}`);
if (resElement.length !== 1) {
throw new GrabError(`Resource #${type} not found`);
}
return getNumber(resElement.text().replace(/[^0-9]/g, ''));
}
function grabCapacity(type: string): number {
const capacityElement = findStockBarElement().find(`.${type} .capacity .value`);
if (capacityElement.length !== 1) {
throw new GrabError(`Capacity #${type} not found`);
}
return getNumber(capacityElement.text().replace(/[^0-9]/g, ''));
}

View File

@ -1,13 +1,5 @@
import * as URLParse from 'url-parse';
import { elClassId, getNumber } from '../utils';
export function grabActiveVillageId(): number {
const href = jQuery('#sidebarBoxVillagelist a.active').attr('href');
const p = new URLParse(href || '', true);
console.log('VILLAGE REF', href, p);
return getNumber(p.query.newdid);
}
interface Slot {
el: HTMLElement;
buildId: number;

40
src/Page/VillageBlock.ts Normal file
View File

@ -0,0 +1,40 @@
import { Coordinates, Village, VillageList } from '../Game';
import { GrabError } from '../Errors';
import * as URLParse from 'url-parse';
import { getNumber } from '../utils';
export function grabVillageList(): VillageList {
const villageList: VillageList = [];
const $elements = getVillageListItems();
$elements.each((idx, el) => {
villageList.push(grabVillageInfo(jQuery(el)));
});
return villageList;
}
export function grabActiveVillageId(): number {
const villageList = grabVillageList();
for (let village of villageList) {
if (village.active) {
return village.id;
}
}
return 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);
const id = getNumber(parsedHref.query.newdid);
const name = $el.find('.name').text();
const active = $el.hasClass('active');
return new Village(id, name, active, new Coordinates(0, 0));
}