Simplify action definition in tasks
This commit is contained in:
25
src/Task/ActionBundles.ts
Normal file
25
src/Task/ActionBundles.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { ActionDefinition } from './TaskController';
|
||||
import { grabVillageList } from '../Page/VillageBlock';
|
||||
import { GoToPageAction } from '../Action/GoToPageAction';
|
||||
import { path } from '../utils';
|
||||
import { MARKET_ID } from '../Core/Buildings';
|
||||
|
||||
export function scanAllVillagesBundle(): Array<ActionDefinition> {
|
||||
const actions: Array<ActionDefinition> = [];
|
||||
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 }),
|
||||
},
|
||||
]);
|
||||
}
|
||||
return actions;
|
||||
}
|
@ -1,37 +1,10 @@
|
||||
import { Args, Command } from '../Command';
|
||||
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
|
||||
import { GoToPageAction } from '../Action/GoToPageAction';
|
||||
import { path } from '../utils';
|
||||
import { TaskController, registerTask, ActionDefinition } from './TaskController';
|
||||
import { scanAllVillagesBundle } from './ActionBundles';
|
||||
import { Task } from '../Queue/TaskQueue';
|
||||
import { TaskController, registerTask } from './TaskController';
|
||||
import { grabVillageList } from '../Page/VillageBlock';
|
||||
import { MARKET_ID } from '../Core/Buildings';
|
||||
|
||||
@registerTask
|
||||
export class GrabVillageState extends TaskController {
|
||||
async run(task: Task) {
|
||||
const args: Args = { ...task.args, taskId: task.id };
|
||||
|
||||
const actions: Array<Command> = [];
|
||||
|
||||
const villages = grabVillageList();
|
||||
for (let village of villages) {
|
||||
actions.push(
|
||||
new Command(GoToPageAction.name, {
|
||||
...args,
|
||||
path: path('/dorf1.php', { newdid: village.id }),
|
||||
})
|
||||
);
|
||||
actions.push(
|
||||
new Command(GoToPageAction.name, {
|
||||
...args,
|
||||
path: path('/build.php', { newdid: village.id, gid: MARKET_ID, t: 5 }),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
actions.push(new Command(CompleteTaskAction.name, args));
|
||||
|
||||
this.scheduler.scheduleActions(actions);
|
||||
defineActions(task: Task): Array<ActionDefinition> {
|
||||
return scanAllVillagesBundle();
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,20 @@
|
||||
import { Args, Command } from '../Command';
|
||||
import { Args } from '../Command';
|
||||
import { Task } from '../Queue/TaskQueue';
|
||||
import { TaskController, registerTask } from './TaskController';
|
||||
import { TaskController, registerTask, ActionDefinition } from './TaskController';
|
||||
import { GoToPageAction } from '../Action/GoToPageAction';
|
||||
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
|
||||
import { path } from '../utils';
|
||||
import { SendResourcesAction } from '../Action/SendResourcesAction';
|
||||
import { ClickButtonAction } from '../Action/ClickButtonAction';
|
||||
import { scanAllVillagesBundle } from './ActionBundles';
|
||||
|
||||
@registerTask
|
||||
export class SendResourcesTask extends TaskController {
|
||||
async run(task: Task) {
|
||||
const args: Args = { ...task.args, taskId: task.id };
|
||||
defineActions(task: Task): Array<ActionDefinition> {
|
||||
return [...scanAllVillagesBundle(), ...this.sendResourcesActions(task.args)];
|
||||
}
|
||||
|
||||
sendResourcesActions(args: Args): Array<ActionDefinition> {
|
||||
const pathArgs = {
|
||||
newdid: args.villageId,
|
||||
gid: args.buildTypeId || undefined,
|
||||
@ -21,11 +24,11 @@ export class SendResourcesTask extends TaskController {
|
||||
|
||||
const pagePath = path('/build.php', pathArgs);
|
||||
|
||||
this.scheduler.scheduleActions([
|
||||
new Command(GoToPageAction.name, { ...args, path: pagePath }),
|
||||
new Command(SendResourcesAction.name, args),
|
||||
new Command(ClickButtonAction.name, { ...args, selector: '#enabledButton.green.sendRessources' }),
|
||||
new Command(CompleteTaskAction.name, args),
|
||||
]);
|
||||
return [
|
||||
[GoToPageAction.name, { path: pagePath }],
|
||||
[SendResourcesAction.name, {}],
|
||||
[ClickButtonAction.name, { selector: '#enabledButton.green.sendRessources' }],
|
||||
[CompleteTaskAction.name, {}],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
import { Task } from '../Queue/TaskQueue';
|
||||
import { Scheduler } from '../Scheduler';
|
||||
import { Args, Command } from '../Command';
|
||||
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
|
||||
|
||||
const taskMap: { [name: string]: Function | undefined } = {};
|
||||
|
||||
@ -16,6 +18,8 @@ export function createTask(name: string, scheduler: Scheduler): TaskController |
|
||||
return new constructor(scheduler);
|
||||
}
|
||||
|
||||
export type ActionDefinition = [string, Args];
|
||||
|
||||
export class TaskController {
|
||||
protected scheduler: Scheduler;
|
||||
|
||||
@ -23,5 +27,22 @@ export class TaskController {
|
||||
this.scheduler = scheduler;
|
||||
}
|
||||
|
||||
async run(task: Task) {}
|
||||
async run(task: Task) {
|
||||
const commands = this.createCommands(task);
|
||||
this.scheduler.scheduleActions(commands);
|
||||
}
|
||||
|
||||
defineActions(task: Task): Array<ActionDefinition> {
|
||||
return [];
|
||||
}
|
||||
|
||||
private createCommands(task: Task) {
|
||||
const args: Args = { ...task.args, taskId: task.id };
|
||||
const commands: Array<Command> = [];
|
||||
for (let def of this.defineActions(task)) {
|
||||
commands.push(new Command(def[0], { ...args, ...def[1] }));
|
||||
}
|
||||
commands.push(new Command(CompleteTaskAction.name, args));
|
||||
return commands;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user