Add task types

This commit is contained in:
Anton Vakhrushev 2020-05-08 11:28:18 +03:00
parent 8c20b0c71a
commit f8173a90c0
15 changed files with 93 additions and 64 deletions

View File

@ -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;

View File

@ -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<TaskNamePredicate> = [
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) {

View File

@ -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 };

View File

@ -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<ActionDefinition> {
const args = task.args;

View File

@ -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<ActionDefinition> {
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]];
}
}

View File

@ -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<ActionDefinition> {
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]];
}
}

View File

@ -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<ActionDefinition> {
return scanAllVillagesBundle();

View File

@ -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<ActionDefinition> {
return [

View File

@ -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 };

View File

@ -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<ActionDefinition> {
return [...scanAllVillagesBundle(), ...this.sendResourcesActions(task.args)];

View File

@ -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 {

51
src/Task/TaskMap.ts Normal file
View File

@ -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);
}

View File

@ -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 };

View File

@ -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<ActionDefinition> {
const tasks = this.scheduler.getTaskItems();

View File

@ -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<ActionDefinition> {
const args = task.args;