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 NAMESPACE = 'actions.v1';
const QUEUE_NAME = 'queue'; const QUEUE_NAME = 'queue';
export class Action { export interface Action {
readonly name: string; name: string;
readonly args: Args; args: Args;
constructor(name: string, args: Args) {
this.name = name;
this.args = args;
}
} }
type ActionList = Array<Action>; type ActionList = Array<Action>;
@ -29,20 +24,20 @@ export class ActionQueue {
} }
pop(): Action | undefined { pop(): Action | undefined {
const commands = this.getCommands(); const actions = this.getActions();
const first = commands.shift(); const first = actions.shift();
this.flushState(commands); this.flushState(actions);
return first; return first;
} }
push(cmd: Action): void { push(cmd: Action): void {
const commands = this.getCommands(); const actions = this.getActions();
commands.push(cmd); actions.push(cmd);
this.flushState(commands); this.flushState(actions);
} }
assign(commands: ActionList): void { assign(actions: ActionList): void {
this.flushState(commands); this.flushState(actions);
} }
clear(): void { clear(): void {
@ -50,10 +45,10 @@ export class ActionQueue {
} }
seeItems(): ImmutableActionList { seeItems(): ImmutableActionList {
return this.getCommands(); return this.getActions();
} }
private getCommands(): ActionList { private getActions(): ActionList {
const serialized = this.storage.get(QUEUE_NAME); const serialized = this.storage.get(QUEUE_NAME);
if (!Array.isArray(serialized)) { if (!Array.isArray(serialized)) {
return []; return [];
@ -62,7 +57,7 @@ export class ActionQueue {
const items = serialized as Array<{ [key: string]: any }>; const items = serialized as Array<{ [key: string]: any }>;
return items.map(i => { return items.map(i => {
const command = new Action('', {}); const command = { name: '', args: {} };
return Object.assign(command, i); return Object.assign(command, i);
}); });
} }

View File

@ -1,13 +1,5 @@
import { Logger } from '../Logger'; import { Logger } from '../Logger';
import { Args } from './Args'; import { ImmutableTaskList, Task, TaskId, TaskList, TaskProvider } from './TaskProvider';
import {
ImmutableTaskList,
Task,
TaskId,
TaskList,
TaskProvider,
uniqTaskId,
} from './TaskProvider';
export class TaskQueue { export class TaskQueue {
private provider: TaskProvider; private provider: TaskProvider;

View File

@ -5,39 +5,39 @@ import { path } from '../Helpers/Path';
import { Village } from '../Core/Village'; import { Village } from '../Core/Village';
export function goToResourceViewPage(villageId: number): ActionDefinition { export function goToResourceViewPage(villageId: number): ActionDefinition {
return [ return {
GoToPageAction.name, name: GoToPageAction.name,
{ args: {
path: path('/dorf1.php', { newdid: villageId }), path: path('/dorf1.php', { newdid: villageId }),
}, },
]; };
} }
export function goToMarketSendResourcesPage(villageId: number): ActionDefinition { export function goToMarketSendResourcesPage(villageId: number): ActionDefinition {
return [ return {
GoToPageAction.name, name: GoToPageAction.name,
{ args: {
path: path('/build.php', { newdid: villageId, gid: MARKET_ID, t: 5 }), path: path('/build.php', { newdid: villageId, gid: MARKET_ID, t: 5 }),
}, },
]; };
} }
export function goToForgePage(villageId: number): ActionDefinition { export function goToForgePage(villageId: number): ActionDefinition {
return [ return {
GoToPageAction.name, name: GoToPageAction.name,
{ args: {
path: path('/build.php', { newdid: villageId, gid: FORGE_ID }), path: path('/build.php', { newdid: villageId, gid: FORGE_ID }),
}, },
]; };
} }
export function goToGuildHallPage(villageId: number): ActionDefinition { export function goToGuildHallPage(villageId: number): ActionDefinition {
return [ return {
GoToPageAction.name, name: GoToPageAction.name,
{ args: {
path: path('/build.php', { newdid: villageId, gid: GUILD_HALL_ID }), path: path('/build.php', { newdid: villageId, gid: GUILD_HALL_ID }),
}, },
]; };
} }
export function scanAllVillagesBundle(villages: Array<Village>): Array<ActionDefinition> { 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 { GoToPageAction } from '../Action/GoToPageAction';
import { CompleteTaskAction } from '../Action/CompleteTaskAction'; import { CompleteTaskAction } from '../Action/CompleteTaskAction';
import { BalanceHeroResourcesAction } from '../Action/BalanceHeroResourcesAction'; import { BalanceHeroResourcesAction } from '../Action/BalanceHeroResourcesAction';
import { GoToHeroVillageAction } from '../Action/GoToHeroVillageAction'; import { GoToHeroVillageAction } from '../Action/GoToHeroVillageAction';
import { Action } from '../Queue/ActionQueue';
import { Args } from '../Queue/Args';
import { Task } from '../Queue/TaskProvider'; import { Task } from '../Queue/TaskProvider';
import { path } from '../Helpers/Path'; import { path } from '../Helpers/Path';
import { registerTask } from './TaskMap'; import { registerTask } from './TaskMap';
@registerTask() @registerTask()
export class BalanceHeroResourcesTask extends TaskController { export class BalanceHeroResourcesTask extends TaskController {
async run(task: Task) { defineActions(task: Task): Array<ActionDefinition> {
const args: Args = { ...task.args, taskId: task.id }; return [
this.scheduler.scheduleActions([ {
new Action(GoToPageAction.name, { name: GoToPageAction.name,
...args, args: {
path: path('/hero.php'), path: path('/hero.php'),
}), },
new Action(GoToHeroVillageAction.name, args), },
new Action(BalanceHeroResourcesAction.name, args), { name: GoToHeroVillageAction.name },
new Action(CompleteTaskAction.name, args), { name: BalanceHeroResourcesAction.name },
]); ];
} }
} }

View File

@ -16,17 +16,19 @@ export class BuildBuildingTask extends TaskController {
return [ return [
goToResourceViewPage(villageId), goToResourceViewPage(villageId),
[
GoToPageAction.name,
{ {
name: GoToPageAction.name,
args: {
path: path('/build.php', { path: path('/build.php', {
newdid: args.villageId, newdid: args.villageId,
id: args.buildId, id: args.buildId,
category: args.categoryId, 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 { GoToPageAction } from '../Action/GoToPageAction';
import { Task } from '../Queue/TaskProvider'; import { Task } from '../Queue/TaskProvider';
import { path } from '../Helpers/Path'; import { path } from '../Helpers/Path';
@ -11,15 +11,18 @@ export class CelebrationTask extends TaskController {
defineActions(task: Task): Array<ActionDefinition> { defineActions(task: Task): Array<ActionDefinition> {
const args = task.args; const args = task.args;
const pathArgs = {
newdid: args.villageId,
gid: args.buildTypeId || undefined,
id: args.buildId || undefined,
};
return [ 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 { GoToPageAction } from '../Action/GoToPageAction';
import { Task } from '../Queue/TaskProvider'; import { Task } from '../Queue/TaskProvider';
import { ForgeImprovementAction } from '../Action/ForgeImprovementAction'; import { ForgeImprovementAction } from '../Action/ForgeImprovementAction';
@ -11,15 +11,18 @@ export class ForgeImprovementTask extends TaskController {
defineActions(task: Task): Array<ActionDefinition> { defineActions(task: Task): Array<ActionDefinition> {
const args = task.args; const args = task.args;
const pathArgs = {
newdid: args.villageId,
gid: args.buildTypeId || undefined,
id: args.buildId || undefined,
};
return [ 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> { defineActions(task: Task): Array<ActionDefinition> {
const villageId = task.args.villageId || taskError('No village id'); 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 { TaskController, ActionDefinition } from './TaskController';
import { scanAllVillagesBundle } from './ActionBundles';
import { Task } from '../Queue/TaskProvider'; import { Task } from '../Queue/TaskProvider';
import { registerTask } from './TaskMap'; 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 { GoToPageAction } from '../Action/GoToPageAction';
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
import { SendOnAdventureAction } from '../Action/SendOnAdventureAction'; import { SendOnAdventureAction } from '../Action/SendOnAdventureAction';
import { ClickButtonAction } from '../Action/ClickButtonAction'; import { ClickButtonAction } from '../Action/ClickButtonAction';
import { Action } from '../Queue/ActionQueue';
import { Args } from '../Queue/Args';
import { Task } from '../Queue/TaskProvider'; import { Task } from '../Queue/TaskProvider';
import { path } from '../Helpers/Path'; import { path } from '../Helpers/Path';
import { registerTask } from './TaskMap'; import { registerTask } from './TaskMap';
@registerTask() @registerTask()
export class SendOnAdventureTask extends TaskController { export class SendOnAdventureTask extends TaskController {
async run(task: Task) { defineActions(task: Task): Array<ActionDefinition> {
const args: Args = { ...task.args, taskId: task.id }; return [
this.scheduler.scheduleActions([ {
new Action(GoToPageAction.name, { name: GoToPageAction.name,
...args, args: {
path: path('/hero.php'), path: path('/hero.php'),
}), },
new Action(GoToPageAction.name, { },
...args, {
name: GoToPageAction.name,
args: {
path: path('/hero.php', { t: 3 }), path: path('/hero.php', { t: 3 }),
}), },
new Action(SendOnAdventureAction.name, args), },
new Action(ClickButtonAction.name, { { name: SendOnAdventureAction.name },
...args, {
name: ClickButtonAction.name,
args: {
selector: '.adventureSendButton button', selector: '.adventureSendButton button',
}), },
new Action(CompleteTaskAction.name, args), },
]); ];
} }
} }

View File

@ -17,9 +17,12 @@ export class SendResourcesTask extends TaskController {
actions.push(goToMarketSendResourcesPage(village.id)); actions.push(goToMarketSendResourcesPage(village.id));
} }
actions.push([FindSendResourcesPath.name]); actions.push({ name: FindSendResourcesPath.name });
actions.push([SendResourcesAction.name]); actions.push({ name: SendResourcesAction.name });
actions.push([ClickButtonAction.name, { selector: '#enabledButton.green.sendRessources' }]); actions.push({
name: ClickButtonAction.name,
args: { selector: '#enabledButton.green.sendRessources' },
});
return actions; return actions;
} }

View File

@ -5,7 +5,10 @@ import { Args } from '../Queue/Args';
import { Task } from '../Queue/TaskProvider'; import { Task } from '../Queue/TaskProvider';
import { VillageFactory } from '../VillageFactory'; import { VillageFactory } from '../VillageFactory';
export type ActionDefinition = [string] | [string, Args]; export interface ActionDefinition {
name: string;
args?: Args;
}
export class TaskController { export class TaskController {
protected readonly scheduler: Scheduler; protected readonly scheduler: Scheduler;
@ -27,15 +30,14 @@ export class TaskController {
private createCommands(task: Task) { private createCommands(task: Task) {
const args: Args = { ...task.args, taskId: task.id }; const args: Args = { ...task.args, taskId: task.id };
const commands: Array<Action> = []; const actions: Array<Action> = [];
for (let def of this.defineActions(task)) { for (let def of this.defineActions(task)) {
if (def.length === 1) { actions.push({
commands.push(new Action(def[0], args)); name: def.name,
} else { args: def.args ? { ...args, ...def.args } : args,
commands.push(new Action(def[0], { ...args, ...def[1] })); });
} }
} actions.push({ name: CompleteTaskAction.name, args });
commands.push(new Action(CompleteTaskAction.name, args)); return actions;
return commands;
} }
} }

View File

@ -1,6 +1,5 @@
import { ActionDefinition, TaskController } from './TaskController'; import { ActionDefinition, TaskController } from './TaskController';
import { GoToPageAction } from '../Action/GoToPageAction'; import { GoToPageAction } from '../Action/GoToPageAction';
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
import { TrainTrooperAction } from '../Action/TrainTrooperAction'; import { TrainTrooperAction } from '../Action/TrainTrooperAction';
import { Task } from '../Queue/TaskProvider'; import { Task } from '../Queue/TaskProvider';
import { path } from '../Helpers/Path'; import { path } from '../Helpers/Path';
@ -12,17 +11,19 @@ export class TrainTroopTask extends TaskController {
defineActions(task: Task): Array<ActionDefinition> { defineActions(task: Task): Array<ActionDefinition> {
const args = task.args; const args = task.args;
const pathArgs = {
newdid: args.villageId,
gid: args.buildTypeId || undefined,
id: args.buildId || undefined,
s: args.sheetId,
};
return [ return [
[GoToPageAction.name, { path: path('/build.php', pathArgs) }], {
[TrainTrooperAction.name], name: GoToPageAction.name,
[CompleteTaskAction.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), ...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 { private walkUpgradeTasks(tasks: ImmutableTaskList): PathList {

View File

@ -16,13 +16,13 @@ export class UpgradeBuildingTask extends TaskController {
return [ return [
goToResourceViewPage(villageId), goToResourceViewPage(villageId),
[
GoToPageAction.name,
{ {
name: GoToPageAction.name,
args: {
path: path('/build.php', { newdid: args.villageId, id: args.buildId }), path: path('/build.php', { newdid: args.villageId, id: args.buildId }),
}, },
], },
[UpgradeBuildingAction.name], { name: UpgradeBuildingAction.name },
]; ];
} }
} }