Add multiple village support
This commit is contained in:
87
src/Page/BuildPage.ts
Normal file
87
src/Page/BuildPage.ts
Normal file
@ -0,0 +1,87 @@
|
||||
import { elClassId, split, uniqId } from '../utils';
|
||||
import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask';
|
||||
import { Scheduler } from '../Scheduler';
|
||||
import { TrainTroopTask } from '../Task/TrainTroopTask';
|
||||
import { grabActiveVillageId } from './EveryPage';
|
||||
|
||||
const QUARTERS_ID = 19;
|
||||
|
||||
export class BuildPage {
|
||||
private scheduler: Scheduler;
|
||||
private readonly buildId: number;
|
||||
constructor(scheduler: Scheduler, buildId: number) {
|
||||
this.scheduler = scheduler;
|
||||
this.buildId = buildId;
|
||||
}
|
||||
|
||||
run() {
|
||||
const buildTypeId = elClassId(jQuery('#build').attr('class') || '', 'gid');
|
||||
this.log('BUILD PAGE DETECTED', 'ID', this.buildId, 'TYPE', buildTypeId);
|
||||
this.createUpgradeButton();
|
||||
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 onScheduleBuilding(buildId: number) {
|
||||
const villageId = grabActiveVillageId();
|
||||
this.scheduler.scheduleTask(UpgradeBuildingTask.name, { villageId, buildId });
|
||||
const n = new Notification(`Building ${buildId} scheduled`);
|
||||
setTimeout(() => n && n.close(), 4000);
|
||||
}
|
||||
|
||||
private createTrainTroopButton() {
|
||||
const troopBlocks = jQuery('#nonFavouriteTroops .action.troop:not(.empty) .innerTroopWrapper');
|
||||
troopBlocks.each((idx, el) => {
|
||||
const troopId = elClassId(jQuery(el).attr('class') || '', 'troop');
|
||||
console.log('TROOP', troopId);
|
||||
if (troopId) {
|
||||
const id = uniqId();
|
||||
jQuery(el)
|
||||
.find('.details')
|
||||
.append(`<div style="padding: 8px 0"><a id="${id}" href="#">Обучить</a></div>`);
|
||||
jQuery(`#${id}`).on('click', evt => {
|
||||
evt.preventDefault();
|
||||
this.onTrainTroopClick(this.buildId, troopId, el);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private onTrainTroopClick(buildId: Number, troopId: Number, el: HTMLElement) {
|
||||
console.log('TRAIN TROOPERS', 'TROOP ID', troopId, 'BUILDING ID', buildId);
|
||||
const villageId = grabActiveVillageId();
|
||||
const input = jQuery(el).find(`input[name="t${troopId}"]`);
|
||||
const count = Number(input.val());
|
||||
if (!isNaN(count) && count > 0) {
|
||||
console.log('PREPARE TO TRAIN', count, 'TROOPERS');
|
||||
for (let n of split(count)) {
|
||||
this.scheduler.scheduleTask(TrainTroopTask.name, {
|
||||
villageId,
|
||||
buildId,
|
||||
troopId,
|
||||
trainCount: n,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private log(...args) {
|
||||
console.log('BUILD PAGE:', ...args);
|
||||
}
|
||||
|
||||
private logError(...args) {
|
||||
console.error(...args);
|
||||
}
|
||||
}
|
35
src/Page/EveryPage.ts
Normal file
35
src/Page/EveryPage.ts
Normal file
@ -0,0 +1,35 @@
|
||||
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);
|
||||
}
|
||||
|
||||
function showSlotIds(prefix: string, buildingIds: number[]): void {
|
||||
jQuery('.level.colorLayer').each((idx, el) => {
|
||||
const buildId = getNumber(elClassId(jQuery(el).attr('class') || '', prefix));
|
||||
const oldLabel = jQuery(el)
|
||||
.find('.labelLayer')
|
||||
.text();
|
||||
jQuery(el)
|
||||
.find('.labelLayer')
|
||||
.text(buildId + ':' + oldLabel);
|
||||
const inQueue = buildingIds.includes(buildId);
|
||||
if (inQueue) {
|
||||
jQuery(el).css({
|
||||
'background-image': 'linear-gradient(to top, #f00, #f00 100%)',
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function showFieldsSlotIds(buildingIds: number[]) {
|
||||
showSlotIds('buildingSlot', buildingIds);
|
||||
}
|
||||
|
||||
export function showBuildingSlotIds(buildingIds: number[]) {
|
||||
showSlotIds('aid', buildingIds);
|
||||
}
|
Reference in New Issue
Block a user