From d2d076d034e8a8441955868a4d83a2ff7f97259b Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sat, 27 Jun 2020 20:35:58 +0300 Subject: [PATCH] Action refactoring --- src/Queue/ActionQueue.ts | 33 +++++++++----------- src/Queue/TaskQueue.ts | 10 +------ src/Task/ActionBundles.ts | 32 ++++++++++---------- src/Task/BalanceHeroResourcesTask.ts | 26 ++++++++-------- src/Task/BuildBuildingTask.ts | 12 ++++---- src/Task/CelebrationTask.ts | 21 +++++++------ src/Task/ForgeImprovementTask.ts | 21 +++++++------ src/Task/ResourcesToLevel.ts | 2 +- src/Task/RunVillageProductionTask.ts | 1 - src/Task/SendOnAdventureTask.ts | 45 ++++++++++++++-------------- src/Task/SendResourcesTask.ts | 9 ++++-- src/Task/TaskController.ts | 20 +++++++------ src/Task/TrainTroopTask.ts | 23 +++++++------- src/Task/UpdateResourceContracts.ts | 5 +++- src/Task/UpgradeBuildingTask.ts | 10 +++---- 15 files changed, 136 insertions(+), 134 deletions(-) diff --git a/src/Queue/ActionQueue.ts b/src/Queue/ActionQueue.ts index d46e7ca..1ebff1e 100644 --- a/src/Queue/ActionQueue.ts +++ b/src/Queue/ActionQueue.ts @@ -5,14 +5,9 @@ import { Args } from './Args'; const NAMESPACE = 'actions.v1'; const QUEUE_NAME = 'queue'; -export class Action { - readonly name: string; - readonly args: Args; - - constructor(name: string, args: Args) { - this.name = name; - this.args = args; - } +export interface Action { + name: string; + args: Args; } type ActionList = Array; @@ -29,20 +24,20 @@ export class ActionQueue { } pop(): Action | undefined { - const commands = this.getCommands(); - const first = commands.shift(); - this.flushState(commands); + const actions = this.getActions(); + const first = actions.shift(); + this.flushState(actions); return first; } push(cmd: Action): void { - const commands = this.getCommands(); - commands.push(cmd); - this.flushState(commands); + const actions = this.getActions(); + actions.push(cmd); + this.flushState(actions); } - assign(commands: ActionList): void { - this.flushState(commands); + assign(actions: ActionList): void { + this.flushState(actions); } clear(): void { @@ -50,10 +45,10 @@ export class ActionQueue { } seeItems(): ImmutableActionList { - return this.getCommands(); + return this.getActions(); } - private getCommands(): ActionList { + private getActions(): ActionList { const serialized = this.storage.get(QUEUE_NAME); if (!Array.isArray(serialized)) { return []; @@ -62,7 +57,7 @@ export class ActionQueue { const items = serialized as Array<{ [key: string]: any }>; return items.map(i => { - const command = new Action('', {}); + const command = { name: '', args: {} }; return Object.assign(command, i); }); } diff --git a/src/Queue/TaskQueue.ts b/src/Queue/TaskQueue.ts index d576906..fa09b9d 100644 --- a/src/Queue/TaskQueue.ts +++ b/src/Queue/TaskQueue.ts @@ -1,13 +1,5 @@ import { Logger } from '../Logger'; -import { Args } from './Args'; -import { - ImmutableTaskList, - Task, - TaskId, - TaskList, - TaskProvider, - uniqTaskId, -} from './TaskProvider'; +import { ImmutableTaskList, Task, TaskId, TaskList, TaskProvider } from './TaskProvider'; export class TaskQueue { private provider: TaskProvider; diff --git a/src/Task/ActionBundles.ts b/src/Task/ActionBundles.ts index d13d77b..e100d12 100644 --- a/src/Task/ActionBundles.ts +++ b/src/Task/ActionBundles.ts @@ -5,39 +5,39 @@ import { path } from '../Helpers/Path'; import { Village } from '../Core/Village'; export function goToResourceViewPage(villageId: number): ActionDefinition { - return [ - GoToPageAction.name, - { + return { + name: GoToPageAction.name, + args: { path: path('/dorf1.php', { newdid: villageId }), }, - ]; + }; } export function goToMarketSendResourcesPage(villageId: number): ActionDefinition { - return [ - GoToPageAction.name, - { + return { + name: GoToPageAction.name, + args: { path: path('/build.php', { newdid: villageId, gid: MARKET_ID, t: 5 }), }, - ]; + }; } export function goToForgePage(villageId: number): ActionDefinition { - return [ - GoToPageAction.name, - { + return { + name: GoToPageAction.name, + args: { path: path('/build.php', { newdid: villageId, gid: FORGE_ID }), }, - ]; + }; } export function goToGuildHallPage(villageId: number): ActionDefinition { - return [ - GoToPageAction.name, - { + return { + name: GoToPageAction.name, + args: { path: path('/build.php', { newdid: villageId, gid: GUILD_HALL_ID }), }, - ]; + }; } export function scanAllVillagesBundle(villages: Array): Array { diff --git a/src/Task/BalanceHeroResourcesTask.ts b/src/Task/BalanceHeroResourcesTask.ts index 720ca24..a9330a2 100644 --- a/src/Task/BalanceHeroResourcesTask.ts +++ b/src/Task/BalanceHeroResourcesTask.ts @@ -1,26 +1,24 @@ -import { TaskController } from './TaskController'; +import { ActionDefinition, TaskController } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; import { CompleteTaskAction } from '../Action/CompleteTaskAction'; import { BalanceHeroResourcesAction } from '../Action/BalanceHeroResourcesAction'; import { GoToHeroVillageAction } from '../Action/GoToHeroVillageAction'; -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() export class BalanceHeroResourcesTask extends TaskController { - async run(task: Task) { - const args: Args = { ...task.args, taskId: task.id }; - this.scheduler.scheduleActions([ - new Action(GoToPageAction.name, { - ...args, - path: path('/hero.php'), - }), - new Action(GoToHeroVillageAction.name, args), - new Action(BalanceHeroResourcesAction.name, args), - new Action(CompleteTaskAction.name, args), - ]); + defineActions(task: Task): Array { + return [ + { + name: GoToPageAction.name, + args: { + path: path('/hero.php'), + }, + }, + { name: GoToHeroVillageAction.name }, + { name: BalanceHeroResourcesAction.name }, + ]; } } diff --git a/src/Task/BuildBuildingTask.ts b/src/Task/BuildBuildingTask.ts index 341ec96..c586cdd 100644 --- a/src/Task/BuildBuildingTask.ts +++ b/src/Task/BuildBuildingTask.ts @@ -16,17 +16,19 @@ export class BuildBuildingTask extends TaskController { return [ goToResourceViewPage(villageId), - [ - GoToPageAction.name, - { + { + name: GoToPageAction.name, + args: { path: path('/build.php', { newdid: args.villageId, id: args.buildId, category: args.categoryId, }), }, - ], - [BuildBuildingAction.name], + }, + { + name: BuildBuildingAction.name, + }, ]; } } diff --git a/src/Task/CelebrationTask.ts b/src/Task/CelebrationTask.ts index 2b4f84e..b6ae24a 100644 --- a/src/Task/CelebrationTask.ts +++ b/src/Task/CelebrationTask.ts @@ -1,4 +1,4 @@ -import { TaskController, ActionDefinition } from './TaskController'; +import { ActionDefinition, TaskController } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; import { Task } from '../Queue/TaskProvider'; import { path } from '../Helpers/Path'; @@ -11,15 +11,18 @@ export class CelebrationTask extends TaskController { defineActions(task: Task): Array { const args = task.args; - const pathArgs = { - newdid: args.villageId, - gid: args.buildTypeId || undefined, - id: args.buildId || undefined, - }; - return [ - [GoToPageAction.name, { path: path('/build.php', pathArgs) }], - [CelebrationAction.name], + { + name: GoToPageAction.name, + args: { + path: path('/build.php', { + newdid: args.villageId, + gid: args.buildTypeId, + id: args.buildId, + }), + }, + }, + { name: CelebrationAction.name }, ]; } } diff --git a/src/Task/ForgeImprovementTask.ts b/src/Task/ForgeImprovementTask.ts index 9688efc..4db083c 100644 --- a/src/Task/ForgeImprovementTask.ts +++ b/src/Task/ForgeImprovementTask.ts @@ -1,4 +1,4 @@ -import { TaskController, ActionDefinition } from './TaskController'; +import { ActionDefinition, TaskController } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; import { Task } from '../Queue/TaskProvider'; import { ForgeImprovementAction } from '../Action/ForgeImprovementAction'; @@ -11,15 +11,18 @@ export class ForgeImprovementTask extends TaskController { defineActions(task: Task): Array { const args = task.args; - const pathArgs = { - newdid: args.villageId, - gid: args.buildTypeId || undefined, - id: args.buildId || undefined, - }; - return [ - [GoToPageAction.name, { path: path('/build.php', pathArgs) }], - [ForgeImprovementAction.name], + { + name: GoToPageAction.name, + args: { + path: path('/build.php', { + newdid: args.villageId, + gid: args.buildTypeId, + id: args.buildId, + }), + }, + }, + { name: ForgeImprovementAction.name }, ]; } } diff --git a/src/Task/ResourcesToLevel.ts b/src/Task/ResourcesToLevel.ts index 44a87ca..f19b132 100644 --- a/src/Task/ResourcesToLevel.ts +++ b/src/Task/ResourcesToLevel.ts @@ -10,6 +10,6 @@ export class ResourcesToLevel extends TaskController { defineActions(task: Task): Array { const villageId = task.args.villageId || taskError('No village id'); - return [goToResourceViewPage(villageId), [UpgradeResourceToLevel.name]]; + return [goToResourceViewPage(villageId), { name: UpgradeResourceToLevel.name }]; } } diff --git a/src/Task/RunVillageProductionTask.ts b/src/Task/RunVillageProductionTask.ts index f548b10..90c84cb 100644 --- a/src/Task/RunVillageProductionTask.ts +++ b/src/Task/RunVillageProductionTask.ts @@ -1,5 +1,4 @@ import { TaskController, ActionDefinition } from './TaskController'; -import { scanAllVillagesBundle } from './ActionBundles'; import { Task } from '../Queue/TaskProvider'; import { registerTask } from './TaskMap'; diff --git a/src/Task/SendOnAdventureTask.ts b/src/Task/SendOnAdventureTask.ts index faac11a..2200e87 100644 --- a/src/Task/SendOnAdventureTask.ts +++ b/src/Task/SendOnAdventureTask.ts @@ -1,33 +1,34 @@ -import { TaskController } from './TaskController'; +import { ActionDefinition, TaskController } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; -import { CompleteTaskAction } from '../Action/CompleteTaskAction'; import { SendOnAdventureAction } from '../Action/SendOnAdventureAction'; import { ClickButtonAction } from '../Action/ClickButtonAction'; -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() export class SendOnAdventureTask extends TaskController { - async run(task: Task) { - const args: Args = { ...task.args, taskId: task.id }; - this.scheduler.scheduleActions([ - new Action(GoToPageAction.name, { - ...args, - path: path('/hero.php'), - }), - new Action(GoToPageAction.name, { - ...args, - path: path('/hero.php', { t: 3 }), - }), - new Action(SendOnAdventureAction.name, args), - new Action(ClickButtonAction.name, { - ...args, - selector: '.adventureSendButton button', - }), - new Action(CompleteTaskAction.name, args), - ]); + defineActions(task: Task): Array { + return [ + { + name: GoToPageAction.name, + args: { + path: path('/hero.php'), + }, + }, + { + name: GoToPageAction.name, + args: { + path: path('/hero.php', { t: 3 }), + }, + }, + { name: SendOnAdventureAction.name }, + { + name: ClickButtonAction.name, + args: { + selector: '.adventureSendButton button', + }, + }, + ]; } } diff --git a/src/Task/SendResourcesTask.ts b/src/Task/SendResourcesTask.ts index e4ecb9b..881a1a3 100644 --- a/src/Task/SendResourcesTask.ts +++ b/src/Task/SendResourcesTask.ts @@ -17,9 +17,12 @@ export class SendResourcesTask extends TaskController { actions.push(goToMarketSendResourcesPage(village.id)); } - actions.push([FindSendResourcesPath.name]); - actions.push([SendResourcesAction.name]); - actions.push([ClickButtonAction.name, { selector: '#enabledButton.green.sendRessources' }]); + actions.push({ name: FindSendResourcesPath.name }); + actions.push({ name: SendResourcesAction.name }); + actions.push({ + name: ClickButtonAction.name, + args: { selector: '#enabledButton.green.sendRessources' }, + }); return actions; } diff --git a/src/Task/TaskController.ts b/src/Task/TaskController.ts index b87af97..2ddbe4f 100644 --- a/src/Task/TaskController.ts +++ b/src/Task/TaskController.ts @@ -5,7 +5,10 @@ import { Args } from '../Queue/Args'; import { Task } from '../Queue/TaskProvider'; import { VillageFactory } from '../VillageFactory'; -export type ActionDefinition = [string] | [string, Args]; +export interface ActionDefinition { + name: string; + args?: Args; +} export class TaskController { protected readonly scheduler: Scheduler; @@ -27,15 +30,14 @@ export class TaskController { private createCommands(task: Task) { const args: Args = { ...task.args, taskId: task.id }; - const commands: Array = []; + const actions: Array = []; for (let def of this.defineActions(task)) { - if (def.length === 1) { - commands.push(new Action(def[0], args)); - } else { - commands.push(new Action(def[0], { ...args, ...def[1] })); - } + actions.push({ + name: def.name, + args: def.args ? { ...args, ...def.args } : args, + }); } - commands.push(new Action(CompleteTaskAction.name, args)); - return commands; + actions.push({ name: CompleteTaskAction.name, args }); + return actions; } } diff --git a/src/Task/TrainTroopTask.ts b/src/Task/TrainTroopTask.ts index 77b7010..9f17d5b 100644 --- a/src/Task/TrainTroopTask.ts +++ b/src/Task/TrainTroopTask.ts @@ -1,6 +1,5 @@ import { ActionDefinition, TaskController } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; -import { CompleteTaskAction } from '../Action/CompleteTaskAction'; import { TrainTrooperAction } from '../Action/TrainTrooperAction'; import { Task } from '../Queue/TaskProvider'; import { path } from '../Helpers/Path'; @@ -12,17 +11,19 @@ export class TrainTroopTask extends TaskController { defineActions(task: Task): Array { const args = task.args; - const pathArgs = { - newdid: args.villageId, - gid: args.buildTypeId || undefined, - id: args.buildId || undefined, - s: args.sheetId, - }; - return [ - [GoToPageAction.name, { path: path('/build.php', pathArgs) }], - [TrainTrooperAction.name], - [CompleteTaskAction.name], + { + name: GoToPageAction.name, + args: { + path: path('/build.php', { + newdid: args.villageId, + gid: args.buildTypeId, + id: args.buildId, + s: args.sheetId, + }), + }, + }, + { name: TrainTrooperAction.name }, ]; } } diff --git a/src/Task/UpdateResourceContracts.ts b/src/Task/UpdateResourceContracts.ts index 4b3c8fb..f34a759 100644 --- a/src/Task/UpdateResourceContracts.ts +++ b/src/Task/UpdateResourceContracts.ts @@ -20,7 +20,10 @@ export class UpdateResourceContracts extends TaskController { ...this.walkImprovementTask(tasks), ]); - return paths.map(p => [GoToPageAction.name, { path: path(p.name, p.query) }]); + return paths.map(p => ({ + name: GoToPageAction.name, + args: { path: path(p.name, p.query) }, + })); } private walkUpgradeTasks(tasks: ImmutableTaskList): PathList { diff --git a/src/Task/UpgradeBuildingTask.ts b/src/Task/UpgradeBuildingTask.ts index 8299119..3d8e1c5 100644 --- a/src/Task/UpgradeBuildingTask.ts +++ b/src/Task/UpgradeBuildingTask.ts @@ -16,13 +16,13 @@ export class UpgradeBuildingTask extends TaskController { return [ goToResourceViewPage(villageId), - [ - GoToPageAction.name, - { + { + name: GoToPageAction.name, + args: { path: path('/build.php', { newdid: args.villageId, id: args.buildId }), }, - ], - [UpgradeBuildingAction.name], + }, + { name: UpgradeBuildingAction.name }, ]; } }