From f8173a90c0229324f4aa556408da72da10bcfbae Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Fri, 8 May 2020 11:28:18 +0300 Subject: [PATCH] Add task types --- src/Executor.ts | 2 +- src/Scheduler.ts | 30 ++++------------ src/Task/BalanceHeroResourcesTask.ts | 5 +-- src/Task/BuildBuildingTask.ts | 5 +-- src/Task/CelebrationTask.ts | 7 ++-- src/Task/ForgeImprovementTask.ts | 7 ++-- src/Task/GrabVillageState.ts | 5 +-- src/Task/ResourcesToLevel.ts | 5 +-- src/Task/SendOnAdventureTask.ts | 5 +-- src/Task/SendResourcesTask.ts | 5 +-- src/Task/TaskController.ts | 15 -------- src/Task/TaskMap.ts | 51 ++++++++++++++++++++++++++++ src/Task/TrainTroopTask.ts | 5 +-- src/Task/UpdateResourceContracts.ts | 5 +-- src/Task/UpgradeBuildingTask.ts | 5 +-- 15 files changed, 93 insertions(+), 64 deletions(-) create mode 100644 src/Task/TaskMap.ts diff --git a/src/Executor.ts b/src/Executor.ts index 65ea3ea..4d75f0e 100644 --- a/src/Executor.ts +++ b/src/Executor.ts @@ -2,7 +2,6 @@ import { markPage, sleepMicro, timestamp, waitForLoad } from './utils'; import { AbortTaskError, ActionError, TryLaterError } from './Errors'; import { TaskQueueRenderer } from './TaskQueueRenderer'; import { createActionHandler } from './Action/ActionController'; -import { createTaskHandler } from './Task/TaskController'; import { ConsoleLogger, Logger } from './Logger'; import { GrabberManager } from './Grabber/GrabberManager'; import { Scheduler } from './Scheduler'; @@ -10,6 +9,7 @@ import { Statistics } from './Statistics'; import { ExecutionStorage } from './Storage/ExecutionStorage'; import { Action } from './Queue/ActionQueue'; import { Task } from './Queue/TaskProvider'; +import { createTaskHandler } from './Task/TaskMap'; export interface ExecutionSettings { pauseTs: number; diff --git a/src/Scheduler.ts b/src/Scheduler.ts index 63ee375..6eda899 100644 --- a/src/Scheduler.ts +++ b/src/Scheduler.ts @@ -4,17 +4,15 @@ import { TaskQueue } from './Queue/TaskQueue'; import { SendOnAdventureTask } from './Task/SendOnAdventureTask'; import { BalanceHeroResourcesTask } from './Task/BalanceHeroResourcesTask'; import { Logger } from './Logger'; -import { BuildBuildingTask } from './Task/BuildBuildingTask'; import { GrabVillageState } from './Task/GrabVillageState'; -import { ActionQueue, Action, ImmutableActionList } from './Queue/ActionQueue'; +import { Action, ActionQueue, ImmutableActionList } from './Queue/ActionQueue'; import { UpdateResourceContracts } from './Task/UpdateResourceContracts'; -import { TrainTroopTask } from './Task/TrainTroopTask'; import { Resources, ResourcesInterface } from './Core/Resources'; import { SendResourcesTask } from './Task/SendResourcesTask'; import { Args } from './Queue/Args'; import { ImmutableTaskList, Task, TaskId } from './Queue/TaskProvider'; import { ForgeImprovementTask } from './Task/ForgeImprovementTask'; -import { CelebrationTask } from './Task/CelebrationTask'; +import { getTaskType, TaskType } from './Task/TaskMap'; export enum ContractType { UpgradeBuilding, @@ -197,32 +195,16 @@ interface TaskNamePredicate { (name: string): boolean; } -function isTrainTroopTask(taskName: string) { - return taskName === TrainTroopTask.name; -} - -function isForgeImprovementTask(taskName: string) { - return taskName === ForgeImprovementTask.name; -} - -function isBuildingTask(taskName: string) { - return taskName === BuildBuildingTask.name || taskName === UpgradeBuildingTask.name; -} - -function isCelebrationTask(taskName: string) { - return taskName === CelebrationTask.name; -} - /** * List on non intersected task type predicates. * * Placed in order of execution priority. Order is important! */ const TASK_TYPE_PREDICATES: Array = [ - isTrainTroopTask, - isForgeImprovementTask, - isBuildingTask, - isCelebrationTask, + (taskName: string) => getTaskType(taskName) === TaskType.TrainUnit, + (taskName: string) => getTaskType(taskName) === TaskType.UpgradeUnit, + (taskName: string) => getTaskType(taskName) === TaskType.Building, + (taskName: string) => getTaskType(taskName) === TaskType.Celebration, ]; function sameVillage(villageId: number | undefined, args: Args) { diff --git a/src/Task/BalanceHeroResourcesTask.ts b/src/Task/BalanceHeroResourcesTask.ts index 145bb87..720ca24 100644 --- a/src/Task/BalanceHeroResourcesTask.ts +++ b/src/Task/BalanceHeroResourcesTask.ts @@ -1,4 +1,4 @@ -import { TaskController, registerTask } from './TaskController'; +import { TaskController } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; import { CompleteTaskAction } from '../Action/CompleteTaskAction'; import { BalanceHeroResourcesAction } from '../Action/BalanceHeroResourcesAction'; @@ -7,8 +7,9 @@ import { Action } from '../Queue/ActionQueue'; import { Args } from '../Queue/Args'; import { Task } from '../Queue/TaskProvider'; import { path } from '../Helpers/Path'; +import { registerTask } from './TaskMap'; -@registerTask +@registerTask() export class BalanceHeroResourcesTask extends TaskController { async run(task: Task) { const args: Args = { ...task.args, taskId: task.id }; diff --git a/src/Task/BuildBuildingTask.ts b/src/Task/BuildBuildingTask.ts index 4837bda..8d4cd66 100644 --- a/src/Task/BuildBuildingTask.ts +++ b/src/Task/BuildBuildingTask.ts @@ -1,10 +1,11 @@ import { BuildBuildingAction } from '../Action/BuildBuildingAction'; import { GoToPageAction } from '../Action/GoToPageAction'; -import { TaskController, registerTask, ActionDefinition } from './TaskController'; +import { ActionDefinition, TaskController } from './TaskController'; import { Task } from '../Queue/TaskProvider'; import { path } from '../Helpers/Path'; +import { registerTask, TaskType } from './TaskMap'; -@registerTask +@registerTask({ type: TaskType.Building }) export class BuildBuildingTask extends TaskController { defineActions(task: Task): Array { const args = task.args; diff --git a/src/Task/CelebrationTask.ts b/src/Task/CelebrationTask.ts index 915d31e..2db2a63 100644 --- a/src/Task/CelebrationTask.ts +++ b/src/Task/CelebrationTask.ts @@ -1,10 +1,11 @@ -import { TaskController, registerTask, ActionDefinition } from './TaskController'; +import { TaskController, ActionDefinition } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; import { Task } from '../Queue/TaskProvider'; import { path } from '../Helpers/Path'; import { CelebrationAction } from '../Action/CelebrationAction'; +import { registerTask, TaskType } from './TaskMap'; -@registerTask +@registerTask({ type: TaskType.Celebration }) export class CelebrationTask extends TaskController { defineActions(task: Task): Array { const args = task.args; @@ -15,6 +16,6 @@ export class CelebrationTask extends TaskController { id: args.buildId || undefined, }; - return [[GoToPageAction.name, { ...args, path: path('/build.php', pathArgs) }], [CelebrationAction.name]]; + return [[GoToPageAction.name, { path: path('/build.php', pathArgs) }], [CelebrationAction.name]]; } } diff --git a/src/Task/ForgeImprovementTask.ts b/src/Task/ForgeImprovementTask.ts index 0d318c5..ecb5945 100644 --- a/src/Task/ForgeImprovementTask.ts +++ b/src/Task/ForgeImprovementTask.ts @@ -1,10 +1,11 @@ -import { TaskController, registerTask, ActionDefinition } from './TaskController'; +import { TaskController, ActionDefinition } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; import { Task } from '../Queue/TaskProvider'; import { ForgeImprovementAction } from '../Action/ForgeImprovementAction'; import { path } from '../Helpers/Path'; +import { registerTask, TaskType } from './TaskMap'; -@registerTask +@registerTask({ type: TaskType.UpgradeUnit }) export class ForgeImprovementTask extends TaskController { defineActions(task: Task): Array { const args = task.args; @@ -15,6 +16,6 @@ export class ForgeImprovementTask extends TaskController { id: args.buildId || undefined, }; - return [[GoToPageAction.name, { ...args, path: path('/build.php', pathArgs) }], [ForgeImprovementAction.name]]; + return [[GoToPageAction.name, { path: path('/build.php', pathArgs) }], [ForgeImprovementAction.name]]; } } diff --git a/src/Task/GrabVillageState.ts b/src/Task/GrabVillageState.ts index cf100a2..1756c3f 100644 --- a/src/Task/GrabVillageState.ts +++ b/src/Task/GrabVillageState.ts @@ -1,8 +1,9 @@ -import { TaskController, registerTask, ActionDefinition } from './TaskController'; +import { TaskController, ActionDefinition } from './TaskController'; import { scanAllVillagesBundle } from './ActionBundles'; import { Task } from '../Queue/TaskProvider'; +import { registerTask } from './TaskMap'; -@registerTask +@registerTask() export class GrabVillageState extends TaskController { defineActions(task: Task): Array { return scanAllVillagesBundle(); diff --git a/src/Task/ResourcesToLevel.ts b/src/Task/ResourcesToLevel.ts index ee73d07..73d9b31 100644 --- a/src/Task/ResourcesToLevel.ts +++ b/src/Task/ResourcesToLevel.ts @@ -1,10 +1,11 @@ -import { TaskController, registerTask, ActionDefinition } from './TaskController'; +import { TaskController, ActionDefinition } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; import { UpgradeResourceToLevel } from '../Action/UpgradeResourceToLevel'; import { Task } from '../Queue/TaskProvider'; import { path } from '../Helpers/Path'; +import { registerTask } from './TaskMap'; -@registerTask +@registerTask() export class ResourcesToLevel extends TaskController { defineActions(task: Task): Array { return [ diff --git a/src/Task/SendOnAdventureTask.ts b/src/Task/SendOnAdventureTask.ts index 6929eeb..faac11a 100644 --- a/src/Task/SendOnAdventureTask.ts +++ b/src/Task/SendOnAdventureTask.ts @@ -1,4 +1,4 @@ -import { TaskController, registerTask } from './TaskController'; +import { TaskController } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; import { CompleteTaskAction } from '../Action/CompleteTaskAction'; import { SendOnAdventureAction } from '../Action/SendOnAdventureAction'; @@ -7,8 +7,9 @@ import { Action } from '../Queue/ActionQueue'; import { Args } from '../Queue/Args'; import { Task } from '../Queue/TaskProvider'; import { path } from '../Helpers/Path'; +import { registerTask } from './TaskMap'; -@registerTask +@registerTask() export class SendOnAdventureTask extends TaskController { async run(task: Task) { const args: Args = { ...task.args, taskId: task.id }; diff --git a/src/Task/SendResourcesTask.ts b/src/Task/SendResourcesTask.ts index df4c301..34028d7 100644 --- a/src/Task/SendResourcesTask.ts +++ b/src/Task/SendResourcesTask.ts @@ -1,4 +1,4 @@ -import { TaskController, registerTask, ActionDefinition } from './TaskController'; +import { TaskController, ActionDefinition } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; import { CompleteTaskAction } from '../Action/CompleteTaskAction'; import { SendResourcesAction } from '../Action/SendResourcesAction'; @@ -7,8 +7,9 @@ import { scanAllVillagesBundle } from './ActionBundles'; import { Args } from '../Queue/Args'; import { Task } from '../Queue/TaskProvider'; import { path } from '../Helpers/Path'; +import { registerTask } from './TaskMap'; -@registerTask +@registerTask() export class SendResourcesTask extends TaskController { defineActions(task: Task): Array { return [...scanAllVillagesBundle(), ...this.sendResourcesActions(task.args)]; diff --git a/src/Task/TaskController.ts b/src/Task/TaskController.ts index 8f84303..1fe8bfe 100644 --- a/src/Task/TaskController.ts +++ b/src/Task/TaskController.ts @@ -4,21 +4,6 @@ import { Action } from '../Queue/ActionQueue'; import { Args } from '../Queue/Args'; import { Task } from '../Queue/TaskProvider'; -const taskMap: { [name: string]: Function | undefined } = {}; - -export function registerTask(constructor: Function) { - taskMap[constructor.name] = constructor; -} - -export function createTaskHandler(name: string, scheduler: Scheduler): TaskController | undefined { - const storedFunction = taskMap[name]; - if (storedFunction === undefined) { - return undefined; - } - const constructor = (storedFunction as unknown) as typeof TaskController; - return new constructor(scheduler); -} - export type ActionDefinition = [string] | [string, Args]; export class TaskController { diff --git a/src/Task/TaskMap.ts b/src/Task/TaskMap.ts new file mode 100644 index 0000000..302640c --- /dev/null +++ b/src/Task/TaskMap.ts @@ -0,0 +1,51 @@ +import { Scheduler } from '../Scheduler'; +import { TaskController } from './TaskController'; + +export enum TaskType { + Other = 1, + Building, + TrainUnit, + UpgradeUnit, + Celebration, +} + +interface TaskOptions { + type?: TaskType; +} + +interface TaskDescription { + ctor: Function; + type: TaskType; +} + +interface TaskMap { + [name: string]: TaskDescription | undefined; +} + +const taskMap: TaskMap = {}; + +export function registerTask(options: TaskOptions = {}) { + return function(ctor: Function) { + taskMap[ctor.name] = { + ctor, + type: options.type || TaskType.Other, + }; + }; +} + +export function getTaskType(name: string): TaskType | undefined { + const taskDescription = taskMap[name]; + if (taskDescription === undefined) { + return undefined; + } + return taskDescription.type; +} + +export function createTaskHandler(name: string, scheduler: Scheduler): TaskController | undefined { + const taskDescription = taskMap[name]; + if (taskDescription === undefined) { + return undefined; + } + const constructor = (taskDescription.ctor as unknown) as typeof TaskController; + return new constructor(scheduler); +} diff --git a/src/Task/TrainTroopTask.ts b/src/Task/TrainTroopTask.ts index 9b0c52f..bb9e565 100644 --- a/src/Task/TrainTroopTask.ts +++ b/src/Task/TrainTroopTask.ts @@ -1,4 +1,4 @@ -import { registerTask, TaskController } from './TaskController'; +import { TaskController } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; import { CompleteTaskAction } from '../Action/CompleteTaskAction'; import { TrainTrooperAction } from '../Action/TrainTrooperAction'; @@ -6,8 +6,9 @@ import { Action } from '../Queue/ActionQueue'; import { Args } from '../Queue/Args'; import { Task } from '../Queue/TaskProvider'; import { path } from '../Helpers/Path'; +import { registerTask, TaskType } from './TaskMap'; -@registerTask +@registerTask({ type: TaskType.TrainUnit }) export class TrainTroopTask extends TaskController { async run(task: Task) { const args: Args = { ...task.args, taskId: task.id }; diff --git a/src/Task/UpdateResourceContracts.ts b/src/Task/UpdateResourceContracts.ts index ffe63cf..24c838a 100644 --- a/src/Task/UpdateResourceContracts.ts +++ b/src/Task/UpdateResourceContracts.ts @@ -1,11 +1,12 @@ -import { TaskController, registerTask, ActionDefinition } from './TaskController'; +import { TaskController, ActionDefinition } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; import { UpgradeBuildingTask } from './UpgradeBuildingTask'; import { ImmutableTaskList, Task } from '../Queue/TaskProvider'; import { ForgeImprovementTask } from './ForgeImprovementTask'; import { path, PathList, uniqPaths } from '../Helpers/Path'; +import { registerTask } from './TaskMap'; -@registerTask +@registerTask() export class UpdateResourceContracts extends TaskController { defineActions(task: Task): Array { const tasks = this.scheduler.getTaskItems(); diff --git a/src/Task/UpgradeBuildingTask.ts b/src/Task/UpgradeBuildingTask.ts index 37a60aa..561a0ab 100644 --- a/src/Task/UpgradeBuildingTask.ts +++ b/src/Task/UpgradeBuildingTask.ts @@ -1,10 +1,11 @@ import { UpgradeBuildingAction } from '../Action/UpgradeBuildingAction'; -import { TaskController, registerTask, ActionDefinition } from './TaskController'; +import { TaskController, ActionDefinition } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; import { Task } from '../Queue/TaskProvider'; import { path } from '../Helpers/Path'; +import { registerTask, TaskType } from './TaskMap'; -@registerTask +@registerTask({ type: TaskType.Building }) export class UpgradeBuildingTask extends TaskController { defineActions(task: Task): Array { const args = task.args;