Action refactoring

This commit is contained in:
Anton Vakhrushev 2020-06-27 20:35:58 +03:00
parent 3278a1071d
commit d2d076d034
15 changed files with 136 additions and 134 deletions

View File

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

View File

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

View File

@ -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<Village>): Array<ActionDefinition> {

View File

@ -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<ActionDefinition> {
return [
{
name: GoToPageAction.name,
args: {
path: path('/hero.php'),
},
},
{ name: GoToHeroVillageAction.name },
{ name: BalanceHeroResourcesAction.name },
];
}
}

View File

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

View File

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

View File

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

View File

@ -10,6 +10,6 @@ export class ResourcesToLevel extends TaskController {
defineActions(task: Task): Array<ActionDefinition> {
const villageId = task.args.villageId || taskError('No village id');
return [goToResourceViewPage(villageId), [UpgradeResourceToLevel.name]];
return [goToResourceViewPage(villageId), { name: UpgradeResourceToLevel.name }];
}
}

View File

@ -1,5 +1,4 @@
import { TaskController, ActionDefinition } from './TaskController';
import { scanAllVillagesBundle } from './ActionBundles';
import { Task } from '../Queue/TaskProvider';
import { registerTask } from './TaskMap';

View File

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

View File

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

View File

@ -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<Action> = [];
const actions: Array<Action> = [];
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;
}
}

View File

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

View File

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

View File

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