From 53b325eee24bf2e6a677ae5626729b695b1c82e9 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sat, 2 May 2020 13:27:26 +0300 Subject: [PATCH] Improve build tasks --- src/Action/ActionController.ts | 9 +++++ src/Action/BuildBuildingAction.ts | 1 + ...rchAction.ts => ForgeImprovementAction.ts} | 2 +- src/Action/UpgradeBuildingAction.ts | 1 + src/DashboardView/VillageStateList.vue | 4 +- src/Page/BuildingPageController.ts | 4 +- src/Task/BuildBuildingTask.ts | 38 +++++++++---------- ...esearchTask.ts => ForgeImprovementTask.ts} | 10 ++--- src/Task/UpgradeBuildingTask.ts | 38 +++++++++---------- 9 files changed, 55 insertions(+), 52 deletions(-) rename src/Action/{ResearchAction.ts => ForgeImprovementAction.ts} (92%) rename src/Task/{ResearchTask.ts => ForgeImprovementTask.ts} (59%) diff --git a/src/Action/ActionController.ts b/src/Action/ActionController.ts index 9fe2ecf..e417a92 100644 --- a/src/Action/ActionController.ts +++ b/src/Action/ActionController.ts @@ -4,6 +4,7 @@ import { grabActiveVillageId } from '../Page/VillageBlock'; import { aroundMinutes } from '../utils'; import { Args } from '../Queue/Args'; import { Task } from '../Queue/TaskProvider'; +import { VillageStorage } from '../Storage/VillageStorage'; const actionMap: { [name: string]: Function | undefined } = {}; @@ -39,4 +40,12 @@ export class ActionController { throw new TryLaterError(aroundMinutes(1), 'Not same village'); } } + + ensureBuildingQueueIsEmpty() { + const storage = new VillageStorage(grabActiveVillageId()); + const info = storage.getBuildingQueueInfo(); + if (info.seconds > 0) { + throw new TryLaterError(info.seconds + 1, 'Building queue is full'); + } + } } diff --git a/src/Action/BuildBuildingAction.ts b/src/Action/BuildBuildingAction.ts index 5be3c94..00ef87e 100644 --- a/src/Action/BuildBuildingAction.ts +++ b/src/Action/BuildBuildingAction.ts @@ -10,6 +10,7 @@ export class BuildBuildingAction extends ActionController { async run(args: Args, task: Task): Promise { try { this.ensureSameVillage(args, task); + this.ensureBuildingQueueIsEmpty(); const buildTypeId = args.buildTypeId || err('Undefined build type id'); clickBuildButton(buildTypeId); } catch (e) { diff --git a/src/Action/ResearchAction.ts b/src/Action/ForgeImprovementAction.ts similarity index 92% rename from src/Action/ResearchAction.ts rename to src/Action/ForgeImprovementAction.ts index 88f621f..a6a2bbe 100644 --- a/src/Action/ResearchAction.ts +++ b/src/Action/ForgeImprovementAction.ts @@ -6,7 +6,7 @@ import { Task } from '../Queue/TaskProvider'; import { clickResearchButton } from '../Page/BuildingPage/ForgePage'; @registerAction -export class ResearchAction extends ActionController { +export class ForgeImprovementAction extends ActionController { async run(args: Args, task: Task): Promise { try { this.ensureSameVillage(args, task); diff --git a/src/Action/UpgradeBuildingAction.ts b/src/Action/UpgradeBuildingAction.ts index b16a94d..905fd6f 100644 --- a/src/Action/UpgradeBuildingAction.ts +++ b/src/Action/UpgradeBuildingAction.ts @@ -10,6 +10,7 @@ export class UpgradeBuildingAction extends ActionController { async run(args: Args, task: Task): Promise { try { this.ensureSameVillage(args, task); + this.ensureBuildingQueueIsEmpty(); clickUpgradeButton(); } catch (e) { if (e instanceof GrabError) { diff --git a/src/DashboardView/VillageStateList.vue b/src/DashboardView/VillageStateList.vue index f0151d5..d3f6563 100644 --- a/src/DashboardView/VillageStateList.vue +++ b/src/DashboardView/VillageStateList.vue @@ -69,7 +69,7 @@ - След: + След. задача: - Баланс: + Баланс задачи: diff --git a/src/Page/BuildingPageController.ts b/src/Page/BuildingPageController.ts index 85099c1..4733cdd 100644 --- a/src/Page/BuildingPageController.ts +++ b/src/Page/BuildingPageController.ts @@ -14,7 +14,7 @@ import { BuildingPageAttributes, isForgePage, isMarketSendResourcesPage } from ' import { createTrainTroopButtons } from './BuildingPage/TrooperPage'; import { createSendResourcesButton } from './BuildingPage/MarketPage'; import { createResearchButtons } from './BuildingPage/ForgePage'; -import { ResearchTask } from '../Task/ResearchTask'; +import { ForgeImprovementTask } from '../Task/ForgeImprovementTask'; export class BuildingPageController { private scheduler: Scheduler; @@ -107,7 +107,7 @@ export class BuildingPageController { private onResearch(resources: Resources, unitId: number) { const villageId = grabActiveVillageId(); - this.scheduler.scheduleTask(ResearchTask.name, { + this.scheduler.scheduleTask(ForgeImprovementTask.name, { villageId, buildTypeId: this.attributes.buildTypeId, buildId: this.attributes.buildId, diff --git a/src/Task/BuildBuildingTask.ts b/src/Task/BuildBuildingTask.ts index c6ccdc3..bf64f7a 100644 --- a/src/Task/BuildBuildingTask.ts +++ b/src/Task/BuildBuildingTask.ts @@ -1,29 +1,27 @@ 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 { TaskController, registerTask } from './TaskController'; -import { Action } from '../Queue/ActionQueue'; -import { Args } from '../Queue/Args'; +import { TaskController, registerTask, ActionDefinition } from './TaskController'; import { Task } from '../Queue/TaskProvider'; @registerTask export class BuildBuildingTask extends TaskController { - async run(task: Task) { - const args: Args = { ...task.args, taskId: task.id }; - this.scheduler.scheduleActions([ - new Action(GoToPageAction.name, { - ...args, - path: path('/dorf1.php', { newdid: args.villageId }), - }), - new Action(CheckBuildingRemainingTimeAction.name, args), - new Action(GoToPageAction.name, { - ...args, - path: path('/build.php', { newdid: args.villageId, id: args.buildId, category: args.categoryId }), - }), - new Action(BuildBuildingAction.name, args), - new Action(CompleteTaskAction.name, args), - ]); + defineActions(task: Task): Array { + const args = task.args; + return [ + [ + GoToPageAction.name, + { + path: path('/dorf1.php', { newdid: args.villageId }), + }, + ], + [ + GoToPageAction.name, + { + path: path('/build.php', { newdid: args.villageId, id: args.buildId, category: args.categoryId }), + }, + ], + [BuildBuildingAction.name], + ]; } } diff --git a/src/Task/ResearchTask.ts b/src/Task/ForgeImprovementTask.ts similarity index 59% rename from src/Task/ResearchTask.ts rename to src/Task/ForgeImprovementTask.ts index 49ba720..01d5d20 100644 --- a/src/Task/ResearchTask.ts +++ b/src/Task/ForgeImprovementTask.ts @@ -1,15 +1,11 @@ import { TaskController, registerTask, ActionDefinition } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; -import { CompleteTaskAction } from '../Action/CompleteTaskAction'; -import { TrainTrooperAction } from '../Action/TrainTrooperAction'; import { path } from '../utils'; -import { Action } from '../Queue/ActionQueue'; -import { Args } from '../Queue/Args'; import { Task } from '../Queue/TaskProvider'; -import { ResearchAction } from '../Action/ResearchAction'; +import { ForgeImprovementAction } from '../Action/ForgeImprovementAction'; @registerTask -export class ResearchTask extends TaskController { +export class ForgeImprovementTask extends TaskController { defineActions(task: Task): Array { const args = task.args; @@ -19,6 +15,6 @@ export class ResearchTask extends TaskController { id: args.buildId || undefined, }; - return [[GoToPageAction.name, { ...args, path: path('/build.php', pathArgs) }], [ResearchAction.name]]; + return [[GoToPageAction.name, { ...args, path: path('/build.php', pathArgs) }], [ForgeImprovementAction.name]]; } } diff --git a/src/Task/UpgradeBuildingTask.ts b/src/Task/UpgradeBuildingTask.ts index e335bd0..6582005 100644 --- a/src/Task/UpgradeBuildingTask.ts +++ b/src/Task/UpgradeBuildingTask.ts @@ -1,29 +1,27 @@ import { UpgradeBuildingAction } from '../Action/UpgradeBuildingAction'; -import { TaskController, registerTask } from './TaskController'; +import { TaskController, registerTask, ActionDefinition } from './TaskController'; import { GoToPageAction } from '../Action/GoToPageAction'; -import { CheckBuildingRemainingTimeAction } from '../Action/CheckBuildingRemainingTimeAction'; -import { CompleteTaskAction } from '../Action/CompleteTaskAction'; import { path } from '../utils'; -import { Action } from '../Queue/ActionQueue'; -import { Args } from '../Queue/Args'; import { Task } from '../Queue/TaskProvider'; @registerTask export class UpgradeBuildingTask extends TaskController { - async run(task: Task) { - const args: Args = { ...task.args, taskId: task.id }; - this.scheduler.scheduleActions([ - new Action(GoToPageAction.name, { - ...args, - path: path('/dorf1.php', { newdid: args.villageId }), - }), - new Action(CheckBuildingRemainingTimeAction.name, args), - new Action(GoToPageAction.name, { - ...args, - path: path('/build.php', { newdid: args.villageId, id: args.buildId }), - }), - new Action(UpgradeBuildingAction.name, args), - new Action(CompleteTaskAction.name, args), - ]); + defineActions(task: Task): Array { + const args = task.args; + return [ + [ + GoToPageAction.name, + { + path: path('/dorf1.php', { newdid: args.villageId }), + }, + ], + [ + GoToPageAction.name, + { + path: path('/build.php', { newdid: args.villageId, id: args.buildId }), + }, + ], + [UpgradeBuildingAction.name], + ]; } }