Add build building task
This commit is contained in:
		
							
								
								
									
										24
									
								
								src/Action/BuildBuildingAction.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/Action/BuildBuildingAction.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | |||||||
|  | import { ActionController, registerAction } from './ActionController'; | ||||||
|  | import { Args } from '../Common'; | ||||||
|  | import { ActionError, GrabError, TryLaterError } from '../Errors'; | ||||||
|  | import { Task } from '../Storage/TaskQueue'; | ||||||
|  | import { clickBuildButton } from '../Page/BuildingPage'; | ||||||
|  |  | ||||||
|  | @registerAction | ||||||
|  | export class BuildBuildingAction extends ActionController { | ||||||
|  |     async run(args: Args, task: Task): Promise<any> { | ||||||
|  |         const buildTypeId = args.buildTypeId; | ||||||
|  |         if (!buildTypeId) { | ||||||
|  |             throw new ActionError(task.id, 'Unknown build type id'); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         try { | ||||||
|  |             clickBuildButton(buildTypeId); | ||||||
|  |         } catch (e) { | ||||||
|  |             if (e instanceof GrabError) { | ||||||
|  |                 throw new TryLaterError(task.id, 15 * 60, 'No build button, try later'); | ||||||
|  |             } | ||||||
|  |             throw e; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -1,6 +1,8 @@ | |||||||
| export interface Args { | export interface Args { | ||||||
|     villageId?: number; |     villageId?: number; | ||||||
|     buildId?: number; |     buildId?: number; | ||||||
|  |     categoryId?: number; | ||||||
|  |     buildTypeId?: number; | ||||||
|     [name: string]: any; |     [name: string]: any; | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| import * as URLParse from 'url-parse'; | import * as URLParse from 'url-parse'; | ||||||
| import { uniqId, waitForLoad } from '../utils'; | import { getNumber, toNumber, uniqId, waitForLoad } from '../utils'; | ||||||
| import { Scheduler } from '../Scheduler'; | import { Scheduler } from '../Scheduler'; | ||||||
| import { BuildPage } from '../Page/BuildPage'; | import { BuildPage } from '../Page/BuildPage'; | ||||||
| import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask'; | import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask'; | ||||||
| @@ -77,7 +77,7 @@ export class Dashboard { | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (p.pathname === '/build.php') { |         if (p.pathname === '/build.php') { | ||||||
|             new BuildPage(this.scheduler, Number(p.query.id)).run(); |             new BuildPage(this.scheduler, getNumber(p.query.id), getNumber(p.query.category, 1)).run(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         this.createControlPanel(state); |         this.createControlPanel(state); | ||||||
|   | |||||||
| @@ -1,9 +1,11 @@ | |||||||
| import { elClassId, split, uniqId } from '../utils'; | import { elClassId, notify, split, uniqId } from '../utils'; | ||||||
| import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask'; | import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask'; | ||||||
| import { Scheduler } from '../Scheduler'; | import { Scheduler } from '../Scheduler'; | ||||||
| import { TrainTroopTask } from '../Task/TrainTroopTask'; | import { TrainTroopTask } from '../Task/TrainTroopTask'; | ||||||
| import { grabActiveVillageId } from './VillageBlock'; | import { grabActiveVillageId } from './VillageBlock'; | ||||||
| import { Logger } from '../Logger'; | import { Logger } from '../Logger'; | ||||||
|  | import { createBuildButton, createUpgradeButton } from './BuildingPage'; | ||||||
|  | import { BuildBuildingTask } from '../Task/BuildBuildingTask'; | ||||||
|  |  | ||||||
| const QUARTERS_ID = 19; | const QUARTERS_ID = 19; | ||||||
|  |  | ||||||
| @@ -11,37 +13,40 @@ export class BuildPage { | |||||||
|     private scheduler: Scheduler; |     private scheduler: Scheduler; | ||||||
|     private readonly buildId: number; |     private readonly buildId: number; | ||||||
|     private readonly logger; |     private readonly logger; | ||||||
|     constructor(scheduler: Scheduler, buildId: number) { |     private readonly categoryId: number; | ||||||
|  |  | ||||||
|  |     constructor(scheduler: Scheduler, buildId: number, categoryId: number) { | ||||||
|         this.scheduler = scheduler; |         this.scheduler = scheduler; | ||||||
|         this.buildId = buildId; |         this.buildId = buildId; | ||||||
|  |         this.categoryId = categoryId; | ||||||
|         this.logger = new Logger(this.constructor.name); |         this.logger = new Logger(this.constructor.name); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     run() { |     run() { | ||||||
|         const buildTypeId = elClassId(jQuery('#build').attr('class') || '', 'gid'); |         const buildTypeId = elClassId(jQuery('#build').attr('class') || '', 'gid'); | ||||||
|         this.logger.log('BUILD PAGE DETECTED', 'ID', this.buildId, 'TYPE', buildTypeId); |         this.logger.log('BUILD PAGE DETECTED', 'ID', this.buildId, 'TYPE', buildTypeId); | ||||||
|         this.createUpgradeButton(); |  | ||||||
|  |         createBuildButton(buildTypeId => this.onScheduleBuildBuilding(buildTypeId)); | ||||||
|  |         createUpgradeButton(() => this.onScheduleUpgradeBuilding()); | ||||||
|  |  | ||||||
|         if (buildTypeId === QUARTERS_ID) { |         if (buildTypeId === QUARTERS_ID) { | ||||||
|             this.createTrainTroopButton(); |             this.createTrainTroopButton(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private createUpgradeButton() { |     private onScheduleBuildBuilding(buildTypeId: number) { | ||||||
|         const id = uniqId(); |         const buildId = this.buildId; | ||||||
|         jQuery('.upgradeButtonsContainer .section1').append( |         const categoryId = this.categoryId; | ||||||
|             `<div style="padding: 8px"><a id="${id}" href="#">В очередь</a></div>` |         const villageId = grabActiveVillageId(); | ||||||
|         ); |         this.scheduler.scheduleTask(BuildBuildingTask.name, { villageId, buildId, categoryId, buildTypeId }); | ||||||
|         jQuery(`#${id}`).on('click', evt => { |         notify(`Building ${buildId} scheduled`); | ||||||
|             evt.preventDefault(); |  | ||||||
|             this.onScheduleBuilding(this.buildId); |  | ||||||
|         }); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private onScheduleBuilding(buildId: number) { |     private onScheduleUpgradeBuilding() { | ||||||
|  |         const buildId = this.buildId; | ||||||
|         const villageId = grabActiveVillageId(); |         const villageId = grabActiveVillageId(); | ||||||
|         this.scheduler.scheduleTask(UpgradeBuildingTask.name, { villageId, buildId }); |         this.scheduler.scheduleTask(UpgradeBuildingTask.name, { villageId, buildId }); | ||||||
|         const n = new Notification(`Building ${buildId} scheduled`); |         notify(`Upgrading ${buildId} scheduled`); | ||||||
|         setTimeout(() => n && n.close(), 4000); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private createTrainTroopButton() { |     private createTrainTroopButton() { | ||||||
|   | |||||||
| @@ -1,4 +1,31 @@ | |||||||
| import { GrabError } from '../Errors'; | import { GrabError } from '../Errors'; | ||||||
|  | import { getNumber, trimPrefix, uniqId } from '../utils'; | ||||||
|  |  | ||||||
|  | export function clickBuildButton(typeId: number) { | ||||||
|  |     const section = jQuery(`#contract_building${typeId}`); | ||||||
|  |     if (section.length !== 1) { | ||||||
|  |         throw new GrabError('No build section'); | ||||||
|  |     } | ||||||
|  |     const btn = section.find('.contractLink button.green.new'); | ||||||
|  |     if (btn.length !== 1) { | ||||||
|  |         throw new GrabError('No build button, try later'); | ||||||
|  |     } | ||||||
|  |     btn.trigger('click'); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | export function createBuildButton(onClickHandler: (buildTypeId: number) => void) { | ||||||
|  |     const $els = jQuery('[id^=contract_building]'); | ||||||
|  |     $els.each((idx, el) => { | ||||||
|  |         const $el = jQuery(el); | ||||||
|  |         const id = getNumber(trimPrefix($el.attr('id') || '', 'contract_building')); | ||||||
|  |         const btnId = uniqId(); | ||||||
|  |         $el.append(`<div style="padding: 8px"><a id="${btnId}" href="#">Построить</a></div>`); | ||||||
|  |         jQuery(`#${btnId}`).on('click', evt => { | ||||||
|  |             evt.preventDefault(); | ||||||
|  |             onClickHandler(id); | ||||||
|  |         }); | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  |  | ||||||
| export function clickUpgradeButton() { | export function clickUpgradeButton() { | ||||||
|     const btn = jQuery('.upgradeButtonsContainer .section1 button.green.build'); |     const btn = jQuery('.upgradeButtonsContainer .section1 button.green.build'); | ||||||
| @@ -7,3 +34,14 @@ export function clickUpgradeButton() { | |||||||
|     } |     } | ||||||
|     btn.trigger('click'); |     btn.trigger('click'); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | export function createUpgradeButton(onClickHandler: () => void) { | ||||||
|  |     const id = uniqId(); | ||||||
|  |     jQuery('.upgradeButtonsContainer .section1').append( | ||||||
|  |         `<div style="padding: 8px"><a id="${id}" href="#">В очередь</a></div>` | ||||||
|  |     ); | ||||||
|  |     jQuery(`#${id}`).on('click', evt => { | ||||||
|  |         evt.preventDefault(); | ||||||
|  |         onClickHandler(); | ||||||
|  |     }); | ||||||
|  | } | ||||||
|   | |||||||
| @@ -3,6 +3,14 @@ import { GrabError } from '../Errors'; | |||||||
| import * as URLParse from 'url-parse'; | import * as URLParse from 'url-parse'; | ||||||
| import { getNumber } from '../utils'; | import { getNumber } from '../utils'; | ||||||
|  |  | ||||||
|  | function getVillageListItems() { | ||||||
|  |     const $elements = jQuery('#sidebarBoxVillagelist ul li a'); | ||||||
|  |     if ($elements.length === 0) { | ||||||
|  |         throw new GrabError('Village list items not found'); | ||||||
|  |     } | ||||||
|  |     return $elements; | ||||||
|  | } | ||||||
|  |  | ||||||
| export function grabVillageList(): VillageList { | export function grabVillageList(): VillageList { | ||||||
|     const villageList: VillageList = []; |     const villageList: VillageList = []; | ||||||
|     const $elements = getVillageListItems(); |     const $elements = getVillageListItems(); | ||||||
| @@ -26,14 +34,6 @@ export function grabActiveVillageId(): number { | |||||||
|     return grabActiveVillage()?.id || 0; |     return grabActiveVillage()?.id || 0; | ||||||
| } | } | ||||||
|  |  | ||||||
| function getVillageListItems() { |  | ||||||
|     const $elements = jQuery('#sidebarBoxVillagelist ul li a'); |  | ||||||
|     if ($elements.length === 0) { |  | ||||||
|         throw new GrabError('Village list items not found'); |  | ||||||
|     } |  | ||||||
|     return $elements; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| function grabVillageInfo($el): Village { | function grabVillageInfo($el): Village { | ||||||
|     const href = $el.attr('href'); |     const href = $el.attr('href'); | ||||||
|     const parsedHref = new URLParse(href || '', true); |     const parsedHref = new URLParse(href || '', true); | ||||||
|   | |||||||
							
								
								
									
										28
									
								
								src/Task/BuildBuildingTask.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/Task/BuildBuildingTask.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | |||||||
|  | import { Args, Command } from '../Common'; | ||||||
|  | import { BuildBuildingAction } from '../Action/BuildBuildingAction'; | ||||||
|  | import { CheckBuildingRemainingTimeAction } from '../Action/CheckBuildingRemainingTimeAction'; | ||||||
|  | import { CompleteTaskAction } from '../Action/CompleteTaskAction'; | ||||||
|  | import { GoToPageAction } from '../Action/GoToPageAction'; | ||||||
|  | import { path } from '../utils'; | ||||||
|  | import { Task } from '../Storage/TaskQueue'; | ||||||
|  | import { TaskController, registerTask } from './TaskController'; | ||||||
|  |  | ||||||
|  | @registerTask | ||||||
|  | export class BuildBuildingTask extends TaskController { | ||||||
|  |     async run(task: Task) { | ||||||
|  |         const args: Args = { ...task.args, taskId: task.id }; | ||||||
|  |         this.scheduler.scheduleActions([ | ||||||
|  |             new Command(GoToPageAction.name, { | ||||||
|  |                 ...args, | ||||||
|  |                 path: path('/dorf1.php', { newdid: args.villageId }), | ||||||
|  |             }), | ||||||
|  |             new Command(CheckBuildingRemainingTimeAction.name, args), | ||||||
|  |             new Command(GoToPageAction.name, { | ||||||
|  |                 ...args, | ||||||
|  |                 path: path('/build.php', { newdid: args.villageId, id: args.buildId, category: args.categoryId }), | ||||||
|  |             }), | ||||||
|  |             new Command(BuildBuildingAction.name, args), | ||||||
|  |             new Command(CompleteTaskAction.name, args), | ||||||
|  |         ]); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -95,6 +95,11 @@ export function path(p: string, query: { [key: string]: string | number | undefi | |||||||
|     return p + (parts.length ? '?' + parts.join('&') : ''); |     return p + (parts.length ? '?' + parts.join('&') : ''); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | export function notify(msg: string): void { | ||||||
|  |     const n = new Notification(msg); | ||||||
|  |     setTimeout(() => n && n.close(), 4000); | ||||||
|  | } | ||||||
|  |  | ||||||
| export function markPage(text: string, version: string) { | export function markPage(text: string, version: string) { | ||||||
|     jQuery('body').append( |     jQuery('body').append( | ||||||
|         '<div style="' + |         '<div style="' + | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user