From 9e59c7b7cc0d16fb7a8135f3ef5232edbc2de122 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sun, 17 May 2020 12:08:12 +0300 Subject: [PATCH] Improve send resources task Reduce go to page actions --- src/Action/ActionController.ts | 6 +----- src/Action/BuildBuildingAction.ts | 4 ++-- src/Action/ForgeImprovementAction.ts | 4 ++-- src/Action/SendResourcesAction.ts | 4 ++-- src/Action/TrainTrooperAction.ts | 4 ++-- src/Core/Village.ts | 5 +++++ src/Errors.ts | 4 ++++ src/Task/ActionBundles.ts | 32 +++++++++++++++++----------- src/Task/SendResourcesTask.ts | 27 +++++++---------------- 9 files changed, 46 insertions(+), 44 deletions(-) diff --git a/src/Action/ActionController.ts b/src/Action/ActionController.ts index cc16657..7c3026c 100644 --- a/src/Action/ActionController.ts +++ b/src/Action/ActionController.ts @@ -1,5 +1,5 @@ import { Scheduler } from '../Scheduler'; -import { AbortTaskError, TryLaterError } from '../Errors'; +import { taskError, TryLaterError } from '../Errors'; import { grabActiveVillageId } from '../Page/VillageBlock'; import { aroundMinutes } from '../utils'; import { Args } from '../Queue/Args'; @@ -26,10 +26,6 @@ export function createActionHandler( return new constructor(scheduler, villageStateRepository); } -export function taskError(msg: string): never { - throw new AbortTaskError(msg); -} - export class ActionController { protected scheduler: Scheduler; protected villageStateRepository: VillageStateRepository; diff --git a/src/Action/BuildBuildingAction.ts b/src/Action/BuildBuildingAction.ts index bde2703..b9fb8d9 100644 --- a/src/Action/BuildBuildingAction.ts +++ b/src/Action/BuildBuildingAction.ts @@ -1,5 +1,5 @@ -import { ActionController, taskError, registerAction } from './ActionController'; -import { GrabError, TryLaterError } from '../Errors'; +import { ActionController, registerAction } from './ActionController'; +import { GrabError, taskError, TryLaterError } from '../Errors'; import { clickBuildButton } from '../Page/BuildingPage/BuildingPage'; import { aroundMinutes } from '../utils'; import { Args } from '../Queue/Args'; diff --git a/src/Action/ForgeImprovementAction.ts b/src/Action/ForgeImprovementAction.ts index 0d8cc87..98f0128 100644 --- a/src/Action/ForgeImprovementAction.ts +++ b/src/Action/ForgeImprovementAction.ts @@ -1,5 +1,5 @@ -import { ActionController, taskError, registerAction } from './ActionController'; -import { GrabError, TryLaterError } from '../Errors'; +import { ActionController, registerAction } from './ActionController'; +import { GrabError, taskError, TryLaterError } from '../Errors'; import { aroundMinutes } from '../utils'; import { Args } from '../Queue/Args'; import { Task } from '../Queue/TaskProvider'; diff --git a/src/Action/SendResourcesAction.ts b/src/Action/SendResourcesAction.ts index 716d695..8cff452 100644 --- a/src/Action/SendResourcesAction.ts +++ b/src/Action/SendResourcesAction.ts @@ -1,5 +1,5 @@ -import { ActionController, taskError, registerAction } from './ActionController'; -import { TryLaterError } from '../Errors'; +import { ActionController, registerAction } from './ActionController'; +import { taskError, TryLaterError } from '../Errors'; import { Resources } from '../Core/Resources'; import { Coordinates } from '../Core/Village'; import { aroundMinutes, timestamp } from '../utils'; diff --git a/src/Action/TrainTrooperAction.ts b/src/Action/TrainTrooperAction.ts index 4d85f4f..f2fc8d7 100644 --- a/src/Action/TrainTrooperAction.ts +++ b/src/Action/TrainTrooperAction.ts @@ -1,5 +1,5 @@ -import { ActionController, taskError, registerAction } from './ActionController'; -import { TryLaterError } from '../Errors'; +import { ActionController, registerAction } from './ActionController'; +import { taskError, TryLaterError } from '../Errors'; import { aroundMinutes, randomInRange } from '../utils'; import { Args } from '../Queue/Args'; import { Task } from '../Queue/TaskProvider'; diff --git a/src/Core/Village.ts b/src/Core/Village.ts index b4e5033..4d0eb94 100644 --- a/src/Core/Village.ts +++ b/src/Core/Village.ts @@ -36,3 +36,8 @@ export class Village { } export type VillageList = Array; + +// export interface VillageSettings { +// id: number; +// +// } diff --git a/src/Errors.ts b/src/Errors.ts index c28e26a..fd226dc 100644 --- a/src/Errors.ts +++ b/src/Errors.ts @@ -35,3 +35,7 @@ export class TryLaterError extends Error { Object.setPrototypeOf(this, TryLaterError.prototype); } } + +export function taskError(msg: string): never { + throw new AbortTaskError(msg); +} diff --git a/src/Task/ActionBundles.ts b/src/Task/ActionBundles.ts index 9f4d0e7..ed83203 100644 --- a/src/Task/ActionBundles.ts +++ b/src/Task/ActionBundles.ts @@ -4,22 +4,30 @@ import { GoToPageAction } from '../Action/GoToPageAction'; import { MARKET_ID } from '../Core/Buildings'; import { path } from '../Helpers/Path'; +export function goToResourceViewPage(villageId: number): ActionDefinition { + return [ + GoToPageAction.name, + { + path: path('/dorf1.php', { newdid: villageId }), + }, + ]; +} + +export function goToMarketSendResourcesPage(villageId: number): ActionDefinition { + return [ + GoToPageAction.name, + { + path: path('/build.php', { newdid: villageId, gid: MARKET_ID, t: 5 }), + }, + ]; +} + 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 }), - }, - ]); + actions.push(goToResourceViewPage(village.id)); + actions.push(goToMarketSendResourcesPage(village.id)); } return actions; } diff --git a/src/Task/SendResourcesTask.ts b/src/Task/SendResourcesTask.ts index 34028d7..da04b9f 100644 --- a/src/Task/SendResourcesTask.ts +++ b/src/Task/SendResourcesTask.ts @@ -1,35 +1,24 @@ import { TaskController, ActionDefinition } from './TaskController'; -import { GoToPageAction } from '../Action/GoToPageAction'; -import { CompleteTaskAction } from '../Action/CompleteTaskAction'; import { SendResourcesAction } from '../Action/SendResourcesAction'; import { ClickButtonAction } from '../Action/ClickButtonAction'; -import { scanAllVillagesBundle } from './ActionBundles'; -import { Args } from '../Queue/Args'; +import { goToMarketSendResourcesPage, goToResourceViewPage } from './ActionBundles'; import { Task } from '../Queue/TaskProvider'; -import { path } from '../Helpers/Path'; import { registerTask } from './TaskMap'; +import { taskError } from '../Errors'; @registerTask() export class SendResourcesTask extends TaskController { defineActions(task: Task): Array { - return [...scanAllVillagesBundle(), ...this.sendResourcesActions(task.args)]; - } - - sendResourcesActions(args: Args): Array { - const pathArgs = { - newdid: args.villageId, - gid: args.buildTypeId || undefined, - id: args.buildId || undefined, - t: args.tabId, - }; - - const pagePath = path('/build.php', pathArgs); + const targetVillageId = task.args.targetVillageId || taskError('Empty target village id'); + const villageId = task.args.villageId || taskError('Empty village id'); return [ - [GoToPageAction.name, { path: pagePath }], + goToResourceViewPage(targetVillageId), + goToMarketSendResourcesPage(targetVillageId), + goToResourceViewPage(villageId), + goToMarketSendResourcesPage(villageId), [SendResourcesAction.name], [ClickButtonAction.name, { selector: '#enabledButton.green.sendRessources' }], - [CompleteTaskAction.name], ]; } }