From 28d037157f03b6a63f61bd97744d1b869494b69a Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sat, 25 Apr 2020 17:44:00 +0300 Subject: [PATCH] Simplify action definition in tasks --- src/ControlPanel.ts | 10 ++++------ src/Task/ActionBundles.ts | 25 +++++++++++++++++++++++++ src/Task/GrabVillageState.ts | 35 ++++------------------------------- src/Task/SendResourcesTask.ts | 23 +++++++++++++---------- src/Task/TaskController.ts | 23 ++++++++++++++++++++++- 5 files changed, 68 insertions(+), 48 deletions(-) create mode 100644 src/Task/ActionBundles.ts diff --git a/src/ControlPanel.ts b/src/ControlPanel.ts index 62f7dcc..5e2e061 100644 --- a/src/ControlPanel.ts +++ b/src/ControlPanel.ts @@ -99,8 +99,8 @@ export class ControlPanel { if (p.pathname === '/dorf1.php') { showResourceSlotIds(buildingsInQueue); - onResourceSlotCtrlClick(buildId => this.onResourceSlotCtrlClick(villageId, buildId, state)); - quickActions.push(...this.createDepositsQuickActions(state, villageId)); + onResourceSlotCtrlClick(buildId => this.onResourceSlotCtrlClick(villageId, buildId)); + quickActions.push(...this.createDepositsQuickActions(villageId)); } if (p.pathname === '/dorf2.php') { @@ -125,7 +125,7 @@ export class ControlPanel { }); } - private createDepositsQuickActions(state, villageId) { + private createDepositsQuickActions(villageId) { const deposits = grabResourceDeposits(); if (deposits.length === 0) { return []; @@ -138,16 +138,14 @@ export class ControlPanel { label: `Ресурсы до уровня ${i}`, cb: () => { this.scheduler.scheduleTask(ResourcesToLevel.name, { villageId, level: i }); - state.refreshTasks(); }, }); } return quickActions; } - private onResourceSlotCtrlClick(villageId: number, buildId: number, state) { + private onResourceSlotCtrlClick(villageId: number, buildId: number) { this.scheduler.scheduleTask(UpgradeBuildingTask.name, { villageId, buildId }); - state.refreshTasks(); const n = new Notification(`Building ${buildId} scheduled`); setTimeout(() => n && n.close(), 4000); } diff --git a/src/Task/ActionBundles.ts b/src/Task/ActionBundles.ts new file mode 100644 index 0000000..3d0aa9f --- /dev/null +++ b/src/Task/ActionBundles.ts @@ -0,0 +1,25 @@ +import { ActionDefinition } from './TaskController'; +import { grabVillageList } from '../Page/VillageBlock'; +import { GoToPageAction } from '../Action/GoToPageAction'; +import { path } from '../utils'; +import { MARKET_ID } from '../Core/Buildings'; + +export function scanAllVillagesBundle(): Array { + const actions: Array = []; + const villages = grabVillageList(); + for (let village of villages) { + actions.push([ + GoToPageAction.name, + { + path: path('/dorf1.php', { newdid: village.id }), + }, + ]); + actions.push([ + GoToPageAction.name, + { + path: path('/build.php', { newdid: village.id, gid: MARKET_ID, t: 5 }), + }, + ]); + } + return actions; +} diff --git a/src/Task/GrabVillageState.ts b/src/Task/GrabVillageState.ts index 7870a2e..192976d 100644 --- a/src/Task/GrabVillageState.ts +++ b/src/Task/GrabVillageState.ts @@ -1,37 +1,10 @@ -import { Args, Command } from '../Command'; -import { CompleteTaskAction } from '../Action/CompleteTaskAction'; -import { GoToPageAction } from '../Action/GoToPageAction'; -import { path } from '../utils'; +import { TaskController, registerTask, ActionDefinition } from './TaskController'; +import { scanAllVillagesBundle } from './ActionBundles'; import { Task } from '../Queue/TaskQueue'; -import { TaskController, registerTask } from './TaskController'; -import { grabVillageList } from '../Page/VillageBlock'; -import { MARKET_ID } from '../Core/Buildings'; @registerTask export class GrabVillageState extends TaskController { - async run(task: Task) { - const args: Args = { ...task.args, taskId: task.id }; - - const actions: Array = []; - - const villages = grabVillageList(); - for (let village of villages) { - actions.push( - new Command(GoToPageAction.name, { - ...args, - path: path('/dorf1.php', { newdid: village.id }), - }) - ); - actions.push( - new Command(GoToPageAction.name, { - ...args, - path: path('/build.php', { newdid: village.id, gid: MARKET_ID, t: 5 }), - }) - ); - } - - actions.push(new Command(CompleteTaskAction.name, args)); - - this.scheduler.scheduleActions(actions); + defineActions(task: Task): Array { + return scanAllVillagesBundle(); } } diff --git a/src/Task/SendResourcesTask.ts b/src/Task/SendResourcesTask.ts index 1eec42a..2dbcc62 100644 --- a/src/Task/SendResourcesTask.ts +++ b/src/Task/SendResourcesTask.ts @@ -1,17 +1,20 @@ -import { Args, Command } from '../Command'; +import { Args } from '../Command'; import { Task } from '../Queue/TaskQueue'; -import { TaskController, registerTask } from './TaskController'; +import { TaskController, registerTask, ActionDefinition } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; import { CompleteTaskAction } from '../Action/CompleteTaskAction'; import { path } from '../utils'; import { SendResourcesAction } from '../Action/SendResourcesAction'; import { ClickButtonAction } from '../Action/ClickButtonAction'; +import { scanAllVillagesBundle } from './ActionBundles'; @registerTask export class SendResourcesTask extends TaskController { - async run(task: Task) { - const args: Args = { ...task.args, taskId: task.id }; + defineActions(task: Task): Array { + return [...scanAllVillagesBundle(), ...this.sendResourcesActions(task.args)]; + } + sendResourcesActions(args: Args): Array { const pathArgs = { newdid: args.villageId, gid: args.buildTypeId || undefined, @@ -21,11 +24,11 @@ export class SendResourcesTask extends TaskController { const pagePath = path('/build.php', pathArgs); - this.scheduler.scheduleActions([ - new Command(GoToPageAction.name, { ...args, path: pagePath }), - new Command(SendResourcesAction.name, args), - new Command(ClickButtonAction.name, { ...args, selector: '#enabledButton.green.sendRessources' }), - new Command(CompleteTaskAction.name, args), - ]); + return [ + [GoToPageAction.name, { path: pagePath }], + [SendResourcesAction.name, {}], + [ClickButtonAction.name, { selector: '#enabledButton.green.sendRessources' }], + [CompleteTaskAction.name, {}], + ]; } } diff --git a/src/Task/TaskController.ts b/src/Task/TaskController.ts index fd887a2..edc9274 100644 --- a/src/Task/TaskController.ts +++ b/src/Task/TaskController.ts @@ -1,5 +1,7 @@ import { Task } from '../Queue/TaskQueue'; import { Scheduler } from '../Scheduler'; +import { Args, Command } from '../Command'; +import { CompleteTaskAction } from '../Action/CompleteTaskAction'; const taskMap: { [name: string]: Function | undefined } = {}; @@ -16,6 +18,8 @@ export function createTask(name: string, scheduler: Scheduler): TaskController | return new constructor(scheduler); } +export type ActionDefinition = [string, Args]; + export class TaskController { protected scheduler: Scheduler; @@ -23,5 +27,22 @@ export class TaskController { this.scheduler = scheduler; } - async run(task: Task) {} + async run(task: Task) { + const commands = this.createCommands(task); + this.scheduler.scheduleActions(commands); + } + + defineActions(task: Task): Array { + return []; + } + + private createCommands(task: Task) { + const args: Args = { ...task.args, taskId: task.id }; + const commands: Array = []; + for (let def of this.defineActions(task)) { + commands.push(new Command(def[0], { ...args, ...def[1] })); + } + commands.push(new Command(CompleteTaskAction.name, args)); + return commands; + } }