Add multiple village support
This commit is contained in:
@ -1,89 +0,0 @@
|
||||
import { elClassId, split, uniqId } from '../utils';
|
||||
import { Command } from '../Common';
|
||||
import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask';
|
||||
import { Scheduler } from '../Scheduler';
|
||||
import { TrainTroopTask } from '../Task/TrainTroopTask';
|
||||
|
||||
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(id: number) {
|
||||
const queueItem = new Command(UpgradeBuildingTask.name, {
|
||||
id,
|
||||
});
|
||||
this.scheduler.scheduleTask(queueItem);
|
||||
const n = new Notification(`Building ${id} 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 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(
|
||||
new Command(TrainTroopTask.name, {
|
||||
buildId,
|
||||
troopId,
|
||||
trainCount: n,
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private log(...args) {
|
||||
console.log('BUILD PAGE:', ...args);
|
||||
}
|
||||
|
||||
private logError(...args) {
|
||||
console.error(...args);
|
||||
}
|
||||
}
|
@ -1,9 +1,10 @@
|
||||
import * as URLParse from 'url-parse';
|
||||
import { elClassId, markPage, waitForLoad } from '../utils';
|
||||
import { markPage, waitForLoad } from '../utils';
|
||||
import { Scheduler } from '../Scheduler';
|
||||
import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask';
|
||||
import { TaskQueueRenderer } from '../TaskQueueRenderer';
|
||||
import { BuildPage } from './BuildPage';
|
||||
import { BuildPage } from '../Page/BuildPage';
|
||||
import { grabActiveVillageId, showBuildingSlotIds, showFieldsSlotIds } from '../Page/EveryPage';
|
||||
import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask';
|
||||
|
||||
export class Dashboard {
|
||||
private readonly version: string;
|
||||
@ -24,12 +25,19 @@ export class Dashboard {
|
||||
this.renderTaskQueue();
|
||||
setInterval(() => this.renderTaskQueue(), 5000);
|
||||
|
||||
const villageId = grabActiveVillageId();
|
||||
|
||||
const tasks = this.scheduler.getTaskItems();
|
||||
const buildingsInQueue = tasks
|
||||
.filter(t => t.name === UpgradeBuildingTask.name && t.args.villageId === villageId)
|
||||
.map(t => t.args.buildId);
|
||||
|
||||
if (p.pathname === '/dorf1.php') {
|
||||
this.showSlotIds('buildingSlot');
|
||||
showFieldsSlotIds(buildingsInQueue);
|
||||
}
|
||||
|
||||
if (p.pathname === '/dorf2.php') {
|
||||
this.showSlotIds('aid');
|
||||
showBuildingSlotIds(buildingsInQueue);
|
||||
}
|
||||
|
||||
if (p.pathname === '/build.php') {
|
||||
@ -42,27 +50,6 @@ export class Dashboard {
|
||||
new TaskQueueRenderer().render(this.scheduler.getTaskItems());
|
||||
}
|
||||
|
||||
private showSlotIds(prefix: string) {
|
||||
const tasks = this.scheduler.getTaskItems();
|
||||
jQuery('.level.colorLayer').each((idx, el) => {
|
||||
const buildId = elClassId(jQuery(el).attr('class') || '', prefix);
|
||||
const oldLabel = jQuery(el)
|
||||
.find('.labelLayer')
|
||||
.text();
|
||||
jQuery(el)
|
||||
.find('.labelLayer')
|
||||
.text(buildId + ':' + oldLabel);
|
||||
const inQueue = tasks.find(
|
||||
t => t.cmd.name === UpgradeBuildingTask.name && Number(t.cmd.args.id) === Number(buildId)
|
||||
);
|
||||
if (inQueue !== undefined) {
|
||||
jQuery(el).css({
|
||||
'background-image': 'linear-gradient(to top, #f00, #f00 100%)',
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private log(...args) {
|
||||
console.log('SCHEDULER:', ...args);
|
||||
}
|
||||
|
Reference in New Issue
Block a user