Add auto training
This commit is contained in:
		| @@ -4,8 +4,14 @@ import { Task } from '../Storage/TaskQueue'; | ||||
| import { trimPrefix } from '../utils'; | ||||
| import { AbortTaskError } from '../Errors'; | ||||
|  | ||||
| const HARD = 0; | ||||
| const NORMAL = 3; | ||||
| const CONFIG = [ | ||||
|     { level: 0, health: 60 }, | ||||
|     { level: 1, health: 50 }, | ||||
|     { level: 2, health: 40 }, | ||||
|     { level: 3, health: 30 }, | ||||
|     { level: 4, health: 30 }, | ||||
|     { level: 5, health: 30 }, | ||||
| ]; | ||||
|  | ||||
| interface Adventure { | ||||
|     el: HTMLElement; | ||||
| @@ -28,19 +34,20 @@ export class SendOnAdventureAction extends ActionController { | ||||
|         console.log('HERO', hero); | ||||
|  | ||||
|         if (easiest && hero.health) { | ||||
|             if (easiest.level === NORMAL && hero.health >= 30) { | ||||
|                 return jQuery(easiest.el) | ||||
|                     .find('td.goTo .gotoAdventure') | ||||
|                     .trigger('click'); | ||||
|             } | ||||
|             if (easiest.level === HARD && hero.health >= 50) { | ||||
|                 return jQuery(easiest.el) | ||||
|             this.checkConfig(easiest, Number(hero.health)); | ||||
|         } | ||||
|  | ||||
|         throw new AbortTaskError(task.id, 'No suitable adventure'); | ||||
|     } | ||||
|  | ||||
|     private checkConfig(adventure: Adventure, health: number) { | ||||
|         for (let conf of CONFIG) { | ||||
|             if (adventure.level === conf.level && health >= conf.health) { | ||||
|                 return jQuery(adventure.el) | ||||
|                     .find('td.goTo .gotoAdventure') | ||||
|                     .trigger('click'); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         throw new AbortTaskError(task.id, 'No suitable adventure'); | ||||
|     } | ||||
|  | ||||
|     private findAdventures(): Array<Adventure> { | ||||
|   | ||||
							
								
								
									
										57
									
								
								src/Action/TrainTrooperAction.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								src/Action/TrainTrooperAction.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,57 @@ | ||||
| import { ActionController, registerAction } from './ActionController'; | ||||
| import { Args } from '../Common'; | ||||
| import { ActionError, TryLaterError } from '../Errors'; | ||||
| import { Task } from '../Storage/TaskQueue'; | ||||
| import { getNumber, toNumber } from '../utils'; | ||||
|  | ||||
| @registerAction | ||||
| export class TrainTrooperAction extends ActionController { | ||||
|     async run(args: Args, task: Task): Promise<any> { | ||||
|         const troopId = this.getTroopId(args, task); | ||||
|         const trainCount = this.getTrainCount(args, task); | ||||
|  | ||||
|         const block = jQuery(`#nonFavouriteTroops .innerTroopWrapper.troop${troopId}`); | ||||
|         if (block.length !== 1) { | ||||
|             throw new ActionError(task.id, `Troop block not found`); | ||||
|         } | ||||
|  | ||||
|         const countLink = block.find('.cta a'); | ||||
|         if (countLink.length !== 1) { | ||||
|             throw new ActionError(task.id, `Link with max count not found`); | ||||
|         } | ||||
|  | ||||
|         const maxCount = getNumber(countLink.text()); | ||||
|         if (maxCount < trainCount) { | ||||
|             throw new TryLaterError(task.id, 10 * 60, `Max count ${maxCount} less then need ${trainCount}`); | ||||
|         } | ||||
|  | ||||
|         const input = block.find(`input[name="t${troopId}"]`); | ||||
|         if (input.length !== 1) { | ||||
|             throw new ActionError(task.id, `Input element not found`); | ||||
|         } | ||||
|  | ||||
|         const trainButton = jQuery('.startTraining.green').first(); | ||||
|         if (trainButton.length !== 1) { | ||||
|             throw new ActionError(task.id, 'Train button not found'); | ||||
|         } | ||||
|  | ||||
|         input.val(trainCount); | ||||
|         trainButton.trigger('click'); | ||||
|     } | ||||
|  | ||||
|     private getTroopId(args: Args, task: Task): number { | ||||
|         const troopId = toNumber(args.troopId); | ||||
|         if (troopId === undefined) { | ||||
|             throw new ActionError(task.id, `Troop id must be a number, given "${args.troopId}"`); | ||||
|         } | ||||
|         return troopId; | ||||
|     } | ||||
|  | ||||
|     private getTrainCount(args: Args, task: Task): number { | ||||
|         const trainCount = toNumber(args.trainCount); | ||||
|         if (trainCount === undefined) { | ||||
|             throw new ActionError(task.id, `Train count must be a number, given "${args.trainCount}"`); | ||||
|         } | ||||
|         return trainCount; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user