Improve unit training

This commit is contained in:
Anton Vakhrushev 2020-05-09 20:44:07 +03:00
parent ea4deb6323
commit 0798e1dc3f
4 changed files with 72 additions and 68 deletions

View File

@ -1,57 +1,31 @@
import { ActionController, registerAction } from './ActionController'; import { ActionController, err, registerAction } from './ActionController';
import { ActionError, TryLaterError } from '../Errors'; import { TryLaterError } from '../Errors';
import { getNumber, toNumber } from '../utils'; import { aroundMinutes } from '../utils';
import { Args } from '../Queue/Args'; import { Args } from '../Queue/Args';
import { Task } from '../Queue/TaskProvider'; import { Task } from '../Queue/TaskProvider';
import { clickTrainButton, fillTrainCount, getAvailableCount } from '../Page/BuildingPage/TrooperPage';
import { TrainTroopTask } from '../Task/TrainTroopTask';
@registerAction @registerAction
export class TrainTrooperAction extends ActionController { export class TrainTrooperAction extends ActionController {
async run(args: Args, task: Task): Promise<any> { async run(args: Args, task: Task): Promise<any> {
const troopId = this.getTroopId(args); const troopId = args.troopId || err('No troop id');
const trainCount = this.getTrainCount(args); const trainCount = args.trainCount || err('No troop train count');
const block = jQuery(`.innerTroopWrapper[data-troopid="${troopId}"]`); const availableCount = getAvailableCount(troopId);
if (block.length !== 1) {
throw new ActionError(`Troop block not found`); const readyToTrainCount = Math.min(availableCount, trainCount);
const nextToTrainCount = trainCount - readyToTrainCount;
if (readyToTrainCount <= 0) {
throw new TryLaterError(aroundMinutes(15), 'No ready to train troops');
} }
const countLink = block.find('.cta a'); if (nextToTrainCount > 0) {
if (countLink.length !== 1) { this.scheduler.scheduleTask(TrainTroopTask.name, { ...task.args, trainCount: nextToTrainCount });
throw new ActionError(`Link with max count not found`);
} }
const maxCount = getNumber(countLink.text()); fillTrainCount(troopId, readyToTrainCount);
if (maxCount < trainCount) { clickTrainButton();
throw new TryLaterError(20 * 60, `Max count ${maxCount} less then need ${trainCount}`);
}
const input = block.find(`input[name="t${troopId}"]`);
if (input.length !== 1) {
throw new ActionError(`Input element not found`);
}
const trainButton = jQuery('.startTraining.green').first();
if (trainButton.length !== 1) {
throw new ActionError('Train button not found');
}
input.val(trainCount);
trainButton.trigger('click');
}
private getTroopId(args: Args): number {
const troopId = toNumber(args.troopId);
if (troopId === undefined) {
throw new ActionError(`Troop id must be a number, given "${args.troopId}"`);
}
return troopId;
}
private getTrainCount(args: Args): number {
const trainCount = toNumber(args.trainCount);
if (trainCount === undefined) {
throw new ActionError(`Train count must be a number, given "${args.trainCount}"`);
}
return trainCount;
} }
} }

View File

@ -30,3 +30,37 @@ export function createTrainTroopButtons(
}); });
}); });
} }
function getTroopBlock(troopId: number): JQuery {
const $block = jQuery(`.innerTroopWrapper[data-troopid="${troopId}"]`);
if ($block.length !== 1) {
throw new GrabError(`Troop block not found`);
}
return $block;
}
export function getAvailableCount(troopId: number): number {
const $block = getTroopBlock(troopId);
const $countLink = $block.find('.cta a');
if ($countLink.length !== 1) {
throw new GrabError(`Link with max count not found`);
}
return getNumber($countLink.text());
}
export function fillTrainCount(troopId: number, trainCount: number): void {
const $block = getTroopBlock(troopId);
const input = $block.find(`input[name="t${troopId}"]`);
if (input.length !== 1) {
throw new GrabError(`Input element not found`);
}
input.val(trainCount);
}
export function clickTrainButton(): void {
const $trainButton = jQuery('.startTraining.green').first();
if ($trainButton.length !== 1) {
throw new GrabError('Train button not found');
}
$trainButton.trigger('click');
}

View File

@ -83,20 +83,18 @@ export class BuildingPageController {
notify(`Upgrading ${buildId} scheduled`); notify(`Upgrading ${buildId} scheduled`);
} }
private onScheduleTrainTroopers(troopId: number, resources: Resources, count: number) { private onScheduleTrainTroopers(troopId: number, resources: Resources, trainCount: number) {
for (let chunk of split(count)) { const args = {
const args = { villageId: grabActiveVillageId(),
villageId: grabActiveVillageId(), buildId: this.attributes.buildId,
buildId: this.attributes.buildId, buildTypeId: this.attributes.buildTypeId,
buildTypeId: this.attributes.buildTypeId, sheetId: this.attributes.sheetId,
sheetId: this.attributes.sheetId, troopId,
troopId, trainCount,
resources: resources.scale(chunk), resources: resources.scale(trainCount),
trainCount: chunk, };
}; this.scheduler.scheduleTask(TrainTroopTask.name, args);
this.scheduler.scheduleTask(TrainTroopTask.name, args); notify(`Training ${trainCount} troopers scheduled`);
}
notify(`Training ${count} troopers scheduled`);
} }
private onSendResources(resources: Resources, coordinates: Coordinates) { private onSendResources(resources: Resources, coordinates: Coordinates) {

View File

@ -1,17 +1,15 @@
import { TaskController } from './TaskController'; import { ActionDefinition, TaskController } from './TaskController';
import { GoToPageAction } from '../Action/GoToPageAction'; import { GoToPageAction } from '../Action/GoToPageAction';
import { CompleteTaskAction } from '../Action/CompleteTaskAction'; import { CompleteTaskAction } from '../Action/CompleteTaskAction';
import { TrainTrooperAction } from '../Action/TrainTrooperAction'; import { TrainTrooperAction } from '../Action/TrainTrooperAction';
import { Action } from '../Queue/ActionQueue';
import { Args } from '../Queue/Args';
import { Task } from '../Queue/TaskProvider'; import { Task } from '../Queue/TaskProvider';
import { path } from '../Helpers/Path'; import { path } from '../Helpers/Path';
import { registerTask, TaskType } from './TaskMap'; import { registerTask, TaskType } from './TaskMap';
@registerTask({ type: TaskType.TrainUnit }) @registerTask({ type: TaskType.TrainUnit })
export class TrainTroopTask extends TaskController { export class TrainTroopTask extends TaskController {
async run(task: Task) { defineActions(task: Task): Array<ActionDefinition> {
const args: Args = { ...task.args, taskId: task.id }; const args = task.args;
const pathArgs = { const pathArgs = {
newdid: args.villageId, newdid: args.villageId,
@ -20,10 +18,10 @@ export class TrainTroopTask extends TaskController {
s: args.sheetId, s: args.sheetId,
}; };
this.scheduler.scheduleActions([ return [
new Action(GoToPageAction.name, { ...args, path: path('/build.php', pathArgs) }), [GoToPageAction.name, { path: path('/build.php', pathArgs) }],
new Action(TrainTrooperAction.name, args), [TrainTrooperAction.name],
new Action(CompleteTaskAction.name, args), [CompleteTaskAction.name],
]); ];
} }
} }