Add auto training
This commit is contained in:
89
src/Dashboard/BuildPage.ts
Normal file
89
src/Dashboard/BuildPage.ts
Normal file
@ -0,0 +1,89 @@
|
||||
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);
|
||||
}
|
||||
}
|
73
src/Dashboard/Dashboard.ts
Normal file
73
src/Dashboard/Dashboard.ts
Normal file
@ -0,0 +1,73 @@
|
||||
import * as URLParse from 'url-parse';
|
||||
import { elClassId, markPage, waitForLoad } from '../utils';
|
||||
import { Scheduler } from '../Scheduler';
|
||||
import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask';
|
||||
import { TaskQueueRenderer } from '../TaskQueueRenderer';
|
||||
import { BuildPage } from './BuildPage';
|
||||
|
||||
export class Dashboard {
|
||||
private readonly version: string;
|
||||
private scheduler: Scheduler;
|
||||
|
||||
constructor(version: string, scheduler: Scheduler) {
|
||||
this.version = version;
|
||||
this.scheduler = scheduler;
|
||||
}
|
||||
|
||||
async run() {
|
||||
await waitForLoad();
|
||||
|
||||
const p = new URLParse(window.location.href, true);
|
||||
this.log('PARSED LOCATION', p);
|
||||
|
||||
markPage('Dashboard', this.version);
|
||||
this.renderTaskQueue();
|
||||
setInterval(() => this.renderTaskQueue(), 5000);
|
||||
|
||||
if (p.pathname === '/dorf1.php') {
|
||||
this.showSlotIds('buildingSlot');
|
||||
}
|
||||
|
||||
if (p.pathname === '/dorf2.php') {
|
||||
this.showSlotIds('aid');
|
||||
}
|
||||
|
||||
if (p.pathname === '/build.php') {
|
||||
new BuildPage(this.scheduler, Number(p.query.id)).run();
|
||||
}
|
||||
}
|
||||
|
||||
private renderTaskQueue() {
|
||||
this.log('RENDER TASK QUEUE');
|
||||
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);
|
||||
}
|
||||
|
||||
private logError(...args) {
|
||||
console.error(...args);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user