Refactoring

This commit is contained in:
Anton Vakhrushev 2020-04-29 19:02:56 +03:00
parent f5077005d9
commit 0b159d2397
28 changed files with 82 additions and 78 deletions

View File

@ -1,9 +1,9 @@
import { Args } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { Scheduler } from '../Scheduler';
import { ActionError, TryLaterError } from '../Errors';
import { grabActiveVillageId } from '../Page/VillageBlock';
import { aroundMinutes } from '../utils';
import { Args } from '../Args';
const actionMap: { [name: string]: Function | undefined } = {};

View File

@ -1,5 +1,4 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { grabVillageResources, grabVillageResourceStorage } from '../Page/ResourcesBlock';
import { changeHeroResource, grabCurrentHeroResource } from '../Page/HeroPage';
@ -7,6 +6,7 @@ import { grabActiveVillageId } from '../Page/VillageBlock';
import { HeroState } from '../State/HeroState';
import { calcHeroResource } from '../Core/HeroBalance';
import { HeroAllResources } from '../Core/Hero';
import { Args } from '../Args';
@registerAction
export class BalanceHeroResourcesAction extends ActionController {

View File

@ -1,9 +1,9 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Command';
import { ActionError, GrabError, TryLaterError } from '../Errors';
import { Task } from '../Queue/TaskQueue';
import { clickBuildButton } from '../Page/BuildingPage';
import { aroundMinutes } from '../utils';
import { Args } from '../Args';
@registerAction
export class BuildBuildingAction extends ActionController {

View File

@ -1,9 +1,9 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { GrabError, TryLaterError } from '../Errors';
import { grabBuildingQueueInfo } from '../Page/VillageBlock';
import { BuildingQueueInfo } from '../Game';
import { Args } from '../Args';
@registerAction
export class CheckBuildingRemainingTimeAction extends ActionController {

View File

@ -1,7 +1,7 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { AbortTaskError } from '../Errors';
import { Args } from '../Args';
@registerAction
export class ClickButtonAction extends ActionController {

View File

@ -1,6 +1,6 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { Args } from '../Args';
@registerAction
export class CompleteTaskAction extends ActionController {

View File

@ -1,10 +1,10 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { grabVillageList } from '../Page/VillageBlock';
import { grabHeroVillage } from '../Page/HeroPage';
import { path } from '../utils';
import { HeroState } from '../State/HeroState';
import { Args } from '../Args';
@registerAction
export class GoToHeroVillageAction extends ActionController {

View File

@ -1,7 +1,7 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { AbortTaskError } from '../Errors';
import { Args } from '../Args';
@registerAction
export class GoToPageAction extends ActionController {

View File

@ -1,8 +1,8 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { trimPrefix } from '../utils';
import { AbortTaskError } from '../Errors';
import { Args } from '../Args';
const CONFIG = [
{ level: 0, health: 60 },

View File

@ -1,5 +1,4 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Command';
import { AbortTaskError, ActionError, TryLaterError } from '../Errors';
import { Task } from '../Queue/TaskQueue';
import { Resources } from '../Core/Resources';
@ -10,6 +9,7 @@ import { grabActiveVillageId, grabVillageList } from '../Page/VillageBlock';
import { SendResourcesTask } from '../Task/SendResourcesTask';
import { aroundMinutes, timestamp } from '../utils';
import { VillageState } from '../State/VillageState';
import { Args } from '../Args';
function err(msg): never {
throw new ActionError(msg);

View File

@ -1,8 +1,8 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Command';
import { ActionError, TryLaterError } from '../Errors';
import { Task } from '../Queue/TaskQueue';
import { getNumber, toNumber } from '../utils';
import { Args } from '../Args';
@registerAction
export class TrainTrooperAction extends ActionController {

View File

@ -1,7 +1,7 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { grabContractResources } from '../Page/BuildingPage';
import { Args } from '../Args';
@registerAction
export class UpdateBuildingTaskResourcesAction extends ActionController {

View File

@ -1,9 +1,9 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Command';
import { GrabError, TryLaterError } from '../Errors';
import { Task } from '../Queue/TaskQueue';
import { clickUpgradeButton } from '../Page/BuildingPage';
import { aroundMinutes } from '../utils';
import { Args } from '../Args';
@registerAction
export class UpgradeBuildingAction extends ActionController {

View File

@ -1,11 +1,11 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Command';
import { AbortTaskError, ActionError, TryLaterError } from '../Errors';
import { Task } from '../Queue/TaskQueue';
import { grabResourceDeposits } from '../Page/SlotBlock';
import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask';
import { ResourceDeposit } from '../Game';
import { aroundMinutes, getNumber } from '../utils';
import { Args } from '../Args';
@registerAction
export class UpgradeResourceToLevel extends ActionController {

View File

@ -19,13 +19,3 @@ export interface Args {
selector?: string;
path?: string;
}
export class Command {
readonly name: string;
readonly args: Args;
constructor(name: string, args: Args) {
this.name = name;
this.args = args;
}
}

View File

@ -1,7 +1,6 @@
import { markPage, sleepMicro, timestamp, waitForLoad } from './utils';
import { AbortTaskError, ActionError, TryLaterError } from './Errors';
import { Task } from './Queue/TaskQueue';
import { Command } from './Command';
import { TaskQueueRenderer } from './TaskQueueRenderer';
import { createActionHandler } from './Action/ActionController';
import { createTaskHandler } from './Task/TaskController';
@ -10,6 +9,7 @@ import { GrabberManager } from './Grabber/GrabberManager';
import { Scheduler } from './Scheduler';
import { Statistics } from './Statistics';
import { ExecutionState } from './State/ExecutionState';
import { Action } from './Queue/ActionQueue';
export interface ExecutionSettings {
pauseTs: number;
@ -96,7 +96,7 @@ export class Executor {
}
}
private async processActionCommand(cmd: Command, task: Task) {
private async processActionCommand(cmd: Action, task: Task) {
const actionHandler = createActionHandler(cmd.name, this.scheduler);
this.logger.log('PROCESS ACTION', cmd.name, actionHandler);
if (cmd.args.taskId !== task.id) {

View File

@ -1,13 +1,23 @@
import { Command } from '../Command';
import { ConsoleLogger, Logger } from '../Logger';
import { DataStorage } from '../DataStorage';
import { Args } from '../Args';
const NAMESPACE = 'actions.v1';
const QUEUE_NAME = 'queue';
type ActionList = Array<Command>;
export class Action {
readonly name: string;
readonly args: Args;
export type ImmutableActionList = ReadonlyArray<Command>;
constructor(name: string, args: Args) {
this.name = name;
this.args = args;
}
}
type ActionList = Array<Action>;
export type ImmutableActionList = ReadonlyArray<Action>;
export class ActionQueue {
private storage: DataStorage;
@ -18,14 +28,14 @@ export class ActionQueue {
this.logger = new ConsoleLogger(this.constructor.name);
}
pop(): Command | undefined {
pop(): Action | undefined {
const commands = this.getCommands();
const first = commands.shift();
this.flushState(commands);
return first;
}
push(cmd: Command): void {
push(cmd: Action): void {
const commands = this.getCommands();
commands.push(cmd);
this.flushState(commands);
@ -52,7 +62,7 @@ export class ActionQueue {
const items = serialized as Array<{ [key: string]: any }>;
return items.map(i => {
const command = new Command('', {});
const command = new Action('', {});
return Object.assign(command, i);
});
}

View File

@ -1,7 +1,7 @@
import { Args } from '../Command';
import { uniqId } from '../utils';
import { ConsoleLogger, Logger } from '../Logger';
import { DataStorage } from '../DataStorage';
import { Args } from '../Args';
const NAMESPACE = 'tasks:v1';
const QUEUE_NAME = 'queue';

View File

@ -1,17 +1,17 @@
import { timestamp } from './utils';
import { UpgradeBuildingTask } from './Task/UpgradeBuildingTask';
import { ImmutableTaskList, Task, TaskId, TaskQueue } from './Queue/TaskQueue';
import { Args, Command } from './Command';
import { SendOnAdventureTask } from './Task/SendOnAdventureTask';
import { BalanceHeroResourcesTask } from './Task/BalanceHeroResourcesTask';
import { ConsoleLogger, Logger } from './Logger';
import { BuildBuildingTask } from './Task/BuildBuildingTask';
import { GrabVillageState } from './Task/GrabVillageState';
import { ActionQueue, ImmutableActionList } from './Queue/ActionQueue';
import { ActionQueue, Action, 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 './Args';
export class Scheduler {
private taskQueue: TaskQueue;
@ -106,7 +106,7 @@ export class Scheduler {
);
}
scheduleActions(actions: Array<Command>): void {
scheduleActions(actions: Array<Action>): void {
this.actionQueue.assign(actions);
}
@ -169,10 +169,6 @@ function withResources(task: Task, resources: ResourcesInterface): Task {
return new Task(task.id, task.ts, task.name, { ...task.args, resources });
}
function firstTaskTime(tasks: ImmutableTaskList, predicate: (t: Task) => boolean): number | undefined {
return tasks.find(predicate)?.ts;
}
function lastTaskTime(tasks: ImmutableTaskList, predicate: (t: Task) => boolean): number | undefined {
const queuedTaskIndex = findLastIndex(tasks, predicate);
if (queuedTaskIndex === undefined) {

View File

@ -1,4 +1,3 @@
import { Args, Command } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { TaskController, registerTask } from './TaskController';
import { GoToPageAction } from '../Action/GoToPageAction';
@ -6,19 +5,21 @@ import { CompleteTaskAction } from '../Action/CompleteTaskAction';
import { BalanceHeroResourcesAction } from '../Action/BalanceHeroResourcesAction';
import { path } from '../utils';
import { GoToHeroVillageAction } from '../Action/GoToHeroVillageAction';
import { Action } from '../Queue/ActionQueue';
import { Args } from '../Args';
@registerTask
export class BalanceHeroResourcesTask extends TaskController {
async run(task: Task) {
const args: Args = { ...task.args, taskId: task.id };
this.scheduler.scheduleActions([
new Command(GoToPageAction.name, {
new Action(GoToPageAction.name, {
...args,
path: path('/hero.php'),
}),
new Command(GoToHeroVillageAction.name, args),
new Command(BalanceHeroResourcesAction.name, args),
new Command(CompleteTaskAction.name, args),
new Action(GoToHeroVillageAction.name, args),
new Action(BalanceHeroResourcesAction.name, args),
new Action(CompleteTaskAction.name, args),
]);
}
}

View File

@ -1,4 +1,3 @@
import { Args, Command } from '../Command';
import { BuildBuildingAction } from '../Action/BuildBuildingAction';
import { CheckBuildingRemainingTimeAction } from '../Action/CheckBuildingRemainingTimeAction';
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
@ -6,23 +5,25 @@ import { GoToPageAction } from '../Action/GoToPageAction';
import { path } from '../utils';
import { Task } from '../Queue/TaskQueue';
import { TaskController, registerTask } from './TaskController';
import { Action } from '../Queue/ActionQueue';
import { Args } from '../Args';
@registerTask
export class BuildBuildingTask extends TaskController {
async run(task: Task) {
const args: Args = { ...task.args, taskId: task.id };
this.scheduler.scheduleActions([
new Command(GoToPageAction.name, {
new Action(GoToPageAction.name, {
...args,
path: path('/dorf1.php', { newdid: args.villageId }),
}),
new Command(CheckBuildingRemainingTimeAction.name, args),
new Command(GoToPageAction.name, {
new Action(CheckBuildingRemainingTimeAction.name, args),
new Action(GoToPageAction.name, {
...args,
path: path('/build.php', { newdid: args.villageId, id: args.buildId, category: args.categoryId }),
}),
new Command(BuildBuildingAction.name, args),
new Command(CompleteTaskAction.name, args),
new Action(BuildBuildingAction.name, args),
new Action(CompleteTaskAction.name, args),
]);
}
}

View File

@ -1,10 +1,11 @@
import { Args, Command } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { TaskController, registerTask, ActionDefinition } from './TaskController';
import { GoToPageAction } from '../Action/GoToPageAction';
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
import { path } from '../utils';
import { UpgradeResourceToLevel } from '../Action/UpgradeResourceToLevel';
import { Action } from '../Queue/ActionQueue';
import { Args } from '../Args';
@registerTask
export class ResourcesToLevel extends TaskController {

View File

@ -1,4 +1,3 @@
import { Args, Command } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { TaskController, registerTask } from './TaskController';
import { GoToPageAction } from '../Action/GoToPageAction';
@ -6,26 +5,28 @@ import { CompleteTaskAction } from '../Action/CompleteTaskAction';
import { SendOnAdventureAction } from '../Action/SendOnAdventureAction';
import { ClickButtonAction } from '../Action/ClickButtonAction';
import { path } from '../utils';
import { Action } from '../Queue/ActionQueue';
import { Args } from '../Args';
@registerTask
export class SendOnAdventureTask extends TaskController {
async run(task: Task) {
const args: Args = { ...task.args, taskId: task.id };
this.scheduler.scheduleActions([
new Command(GoToPageAction.name, {
new Action(GoToPageAction.name, {
...args,
path: path('/hero.php'),
}),
new Command(GoToPageAction.name, {
new Action(GoToPageAction.name, {
...args,
path: path('/hero.php', { t: 3 }),
}),
new Command(SendOnAdventureAction.name, args),
new Command(ClickButtonAction.name, {
new Action(SendOnAdventureAction.name, args),
new Action(ClickButtonAction.name, {
...args,
selector: '.adventureSendButton button',
}),
new Command(CompleteTaskAction.name, args),
new Action(CompleteTaskAction.name, args),
]);
}
}

View File

@ -1,4 +1,3 @@
import { Args } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { TaskController, registerTask, ActionDefinition } from './TaskController';
import { GoToPageAction } from '../Action/GoToPageAction';
@ -7,6 +6,7 @@ import { path } from '../utils';
import { SendResourcesAction } from '../Action/SendResourcesAction';
import { ClickButtonAction } from '../Action/ClickButtonAction';
import { scanAllVillagesBundle } from './ActionBundles';
import { Args } from '../Args';
@registerTask
export class SendResourcesTask extends TaskController {

View File

@ -1,7 +1,8 @@
import { Task } from '../Queue/TaskQueue';
import { Scheduler } from '../Scheduler';
import { Args, Command } from '../Command';
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
import { Action } from '../Queue/ActionQueue';
import { Args } from '../Args';
const taskMap: { [name: string]: Function | undefined } = {};
@ -38,11 +39,11 @@ export class TaskController {
private createCommands(task: Task) {
const args: Args = { ...task.args, taskId: task.id };
const commands: Array<Command> = [];
const commands: Array<Action> = [];
for (let def of this.defineActions(task)) {
commands.push(new Command(def[0], { ...args, ...def[1] }));
commands.push(new Action(def[0], { ...args, ...def[1] }));
}
commands.push(new Command(CompleteTaskAction.name, args));
commands.push(new Action(CompleteTaskAction.name, args));
return commands;
}
}

View File

@ -1,10 +1,11 @@
import { Args, Command } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { TaskController, registerTask } 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 '../Args';
@registerTask
export class TrainTroopTask extends TaskController {
@ -21,9 +22,9 @@ export class TrainTroopTask extends TaskController {
const pagePath = path('/build.php', pathArgs);
this.scheduler.scheduleActions([
new Command(GoToPageAction.name, { ...args, path: pagePath }),
new Command(TrainTrooperAction.name, args),
new Command(CompleteTaskAction.name, args),
new Action(GoToPageAction.name, { ...args, path: pagePath }),
new Action(TrainTrooperAction.name, args),
new Action(CompleteTaskAction.name, args),
]);
}
}

View File

@ -1,4 +1,3 @@
import { Args, Command } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { TaskController, registerTask } from './TaskController';
import { GoToPageAction } from '../Action/GoToPageAction';
@ -6,29 +5,31 @@ import { path } from '../utils';
import { UpgradeBuildingTask } from './UpgradeBuildingTask';
import { UpdateBuildingTaskResourcesAction } from '../Action/UpdateBuildingTaskResourcesAction';
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
import { Action } from '../Queue/ActionQueue';
import { Args } from '../Args';
@registerTask
export class UpdateResourceContracts extends TaskController {
async run(task: Task) {
const args: Args = { ...task.args, taskId: task.id };
const actions: Array<Command> = [];
const actions: Array<Action> = [];
const tasks = this.scheduler.getTaskItems();
for (let task of tasks) {
const { villageId, buildId } = task.args;
if (task.name === UpgradeBuildingTask.name && villageId && buildId) {
actions.push(
new Command(GoToPageAction.name, {
new Action(GoToPageAction.name, {
...args,
path: path('/build.php', { newdid: villageId, id: buildId }),
})
);
actions.push(new Command(UpdateBuildingTaskResourcesAction.name, { ...args, targetTaskId: task.id }));
actions.push(new Action(UpdateBuildingTaskResourcesAction.name, { ...args, targetTaskId: task.id }));
}
}
actions.push(new Command(CompleteTaskAction.name, args));
actions.push(new Action(CompleteTaskAction.name, args));
this.scheduler.scheduleActions(actions);
}

View File

@ -1,28 +1,29 @@
import { UpgradeBuildingAction } from '../Action/UpgradeBuildingAction';
import { Args, Command } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { TaskController, registerTask } 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 '../Args';
@registerTask
export class UpgradeBuildingTask extends TaskController {
async run(task: Task) {
const args: Args = { ...task.args, taskId: task.id };
this.scheduler.scheduleActions([
new Command(GoToPageAction.name, {
new Action(GoToPageAction.name, {
...args,
path: path('/dorf1.php', { newdid: args.villageId }),
}),
new Command(CheckBuildingRemainingTimeAction.name, args),
new Command(GoToPageAction.name, {
new Action(CheckBuildingRemainingTimeAction.name, args),
new Action(GoToPageAction.name, {
...args,
path: path('/build.php', { newdid: args.villageId, id: args.buildId }),
}),
new Command(UpgradeBuildingAction.name, args),
new Command(CompleteTaskAction.name, args),
new Action(UpgradeBuildingAction.name, args),
new Action(CompleteTaskAction.name, args),
]);
}
}