Simplify action definition in tasks

This commit is contained in:
Anton Vakhrushev 2020-04-25 17:44:00 +03:00
parent 6cdb8be028
commit 28d037157f
5 changed files with 68 additions and 48 deletions

View File

@ -99,8 +99,8 @@ export class ControlPanel {
if (p.pathname === '/dorf1.php') {
showResourceSlotIds(buildingsInQueue);
onResourceSlotCtrlClick(buildId => this.onResourceSlotCtrlClick(villageId, buildId, state));
quickActions.push(...this.createDepositsQuickActions(state, villageId));
onResourceSlotCtrlClick(buildId => this.onResourceSlotCtrlClick(villageId, buildId));
quickActions.push(...this.createDepositsQuickActions(villageId));
}
if (p.pathname === '/dorf2.php') {
@ -125,7 +125,7 @@ export class ControlPanel {
});
}
private createDepositsQuickActions(state, villageId) {
private createDepositsQuickActions(villageId) {
const deposits = grabResourceDeposits();
if (deposits.length === 0) {
return [];
@ -138,16 +138,14 @@ export class ControlPanel {
label: `Ресурсы до уровня ${i}`,
cb: () => {
this.scheduler.scheduleTask(ResourcesToLevel.name, { villageId, level: i });
state.refreshTasks();
},
});
}
return quickActions;
}
private onResourceSlotCtrlClick(villageId: number, buildId: number, state) {
private onResourceSlotCtrlClick(villageId: number, buildId: number) {
this.scheduler.scheduleTask(UpgradeBuildingTask.name, { villageId, buildId });
state.refreshTasks();
const n = new Notification(`Building ${buildId} scheduled`);
setTimeout(() => n && n.close(), 4000);
}

25
src/Task/ActionBundles.ts Normal file
View 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;
}

View File

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

View File

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

View File

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