Improve unit training
This commit is contained in:
		@@ -1,57 +1,31 @@
 | 
			
		||||
import { ActionController, registerAction } from './ActionController';
 | 
			
		||||
import { ActionError, TryLaterError } from '../Errors';
 | 
			
		||||
import { getNumber, toNumber } from '../utils';
 | 
			
		||||
import { ActionController, err, registerAction } from './ActionController';
 | 
			
		||||
import { TryLaterError } from '../Errors';
 | 
			
		||||
import { aroundMinutes } from '../utils';
 | 
			
		||||
import { Args } from '../Queue/Args';
 | 
			
		||||
import { Task } from '../Queue/TaskProvider';
 | 
			
		||||
import { clickTrainButton, fillTrainCount, getAvailableCount } from '../Page/BuildingPage/TrooperPage';
 | 
			
		||||
import { TrainTroopTask } from '../Task/TrainTroopTask';
 | 
			
		||||
 | 
			
		||||
@registerAction
 | 
			
		||||
export class TrainTrooperAction extends ActionController {
 | 
			
		||||
    async run(args: Args, task: Task): Promise<any> {
 | 
			
		||||
        const troopId = this.getTroopId(args);
 | 
			
		||||
        const trainCount = this.getTrainCount(args);
 | 
			
		||||
        const troopId = args.troopId || err('No troop id');
 | 
			
		||||
        const trainCount = args.trainCount || err('No troop train count');
 | 
			
		||||
 | 
			
		||||
        const block = jQuery(`.innerTroopWrapper[data-troopid="${troopId}"]`);
 | 
			
		||||
        if (block.length !== 1) {
 | 
			
		||||
            throw new ActionError(`Troop block not found`);
 | 
			
		||||
        const availableCount = getAvailableCount(troopId);
 | 
			
		||||
 | 
			
		||||
        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 (countLink.length !== 1) {
 | 
			
		||||
            throw new ActionError(`Link with max count not found`);
 | 
			
		||||
        if (nextToTrainCount > 0) {
 | 
			
		||||
            this.scheduler.scheduleTask(TrainTroopTask.name, { ...task.args, trainCount: nextToTrainCount });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const maxCount = getNumber(countLink.text());
 | 
			
		||||
        if (maxCount < trainCount) {
 | 
			
		||||
            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;
 | 
			
		||||
        fillTrainCount(troopId, readyToTrainCount);
 | 
			
		||||
        clickTrainButton();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user