Simplify action definition in tasks
This commit is contained in:
parent
6cdb8be028
commit
28d037157f
@ -99,8 +99,8 @@ export class ControlPanel {
|
|||||||
|
|
||||||
if (p.pathname === '/dorf1.php') {
|
if (p.pathname === '/dorf1.php') {
|
||||||
showResourceSlotIds(buildingsInQueue);
|
showResourceSlotIds(buildingsInQueue);
|
||||||
onResourceSlotCtrlClick(buildId => this.onResourceSlotCtrlClick(villageId, buildId, state));
|
onResourceSlotCtrlClick(buildId => this.onResourceSlotCtrlClick(villageId, buildId));
|
||||||
quickActions.push(...this.createDepositsQuickActions(state, villageId));
|
quickActions.push(...this.createDepositsQuickActions(villageId));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p.pathname === '/dorf2.php') {
|
if (p.pathname === '/dorf2.php') {
|
||||||
@ -125,7 +125,7 @@ export class ControlPanel {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private createDepositsQuickActions(state, villageId) {
|
private createDepositsQuickActions(villageId) {
|
||||||
const deposits = grabResourceDeposits();
|
const deposits = grabResourceDeposits();
|
||||||
if (deposits.length === 0) {
|
if (deposits.length === 0) {
|
||||||
return [];
|
return [];
|
||||||
@ -138,16 +138,14 @@ export class ControlPanel {
|
|||||||
label: `Ресурсы до уровня ${i}`,
|
label: `Ресурсы до уровня ${i}`,
|
||||||
cb: () => {
|
cb: () => {
|
||||||
this.scheduler.scheduleTask(ResourcesToLevel.name, { villageId, level: i });
|
this.scheduler.scheduleTask(ResourcesToLevel.name, { villageId, level: i });
|
||||||
state.refreshTasks();
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return quickActions;
|
return quickActions;
|
||||||
}
|
}
|
||||||
|
|
||||||
private onResourceSlotCtrlClick(villageId: number, buildId: number, state) {
|
private onResourceSlotCtrlClick(villageId: number, buildId: number) {
|
||||||
this.scheduler.scheduleTask(UpgradeBuildingTask.name, { villageId, buildId });
|
this.scheduler.scheduleTask(UpgradeBuildingTask.name, { villageId, buildId });
|
||||||
state.refreshTasks();
|
|
||||||
const n = new Notification(`Building ${buildId} scheduled`);
|
const n = new Notification(`Building ${buildId} scheduled`);
|
||||||
setTimeout(() => n && n.close(), 4000);
|
setTimeout(() => n && n.close(), 4000);
|
||||||
}
|
}
|
||||||
|
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 { TaskController, registerTask, ActionDefinition } from './TaskController';
|
||||||
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
|
import { scanAllVillagesBundle } from './ActionBundles';
|
||||||
import { GoToPageAction } from '../Action/GoToPageAction';
|
|
||||||
import { path } from '../utils';
|
|
||||||
import { Task } from '../Queue/TaskQueue';
|
import { Task } from '../Queue/TaskQueue';
|
||||||
import { TaskController, registerTask } from './TaskController';
|
|
||||||
import { grabVillageList } from '../Page/VillageBlock';
|
|
||||||
import { MARKET_ID } from '../Core/Buildings';
|
|
||||||
|
|
||||||
@registerTask
|
@registerTask
|
||||||
export class GrabVillageState extends TaskController {
|
export class GrabVillageState extends TaskController {
|
||||||
async run(task: Task) {
|
defineActions(task: Task): Array<ActionDefinition> {
|
||||||
const args: Args = { ...task.args, taskId: task.id };
|
return scanAllVillagesBundle();
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,20 @@
|
|||||||
import { Args, Command } from '../Command';
|
import { Args } from '../Command';
|
||||||
import { Task } from '../Queue/TaskQueue';
|
import { Task } from '../Queue/TaskQueue';
|
||||||
import { TaskController, registerTask } from './TaskController';
|
import { TaskController, registerTask, ActionDefinition } from './TaskController';
|
||||||
import { GoToPageAction } from '../Action/GoToPageAction';
|
import { GoToPageAction } from '../Action/GoToPageAction';
|
||||||
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
|
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
|
||||||
import { path } from '../utils';
|
import { path } from '../utils';
|
||||||
import { SendResourcesAction } from '../Action/SendResourcesAction';
|
import { SendResourcesAction } from '../Action/SendResourcesAction';
|
||||||
import { ClickButtonAction } from '../Action/ClickButtonAction';
|
import { ClickButtonAction } from '../Action/ClickButtonAction';
|
||||||
|
import { scanAllVillagesBundle } from './ActionBundles';
|
||||||
|
|
||||||
@registerTask
|
@registerTask
|
||||||
export class SendResourcesTask extends TaskController {
|
export class SendResourcesTask extends TaskController {
|
||||||
async run(task: Task) {
|
defineActions(task: Task): Array<ActionDefinition> {
|
||||||
const args: Args = { ...task.args, taskId: task.id };
|
return [...scanAllVillagesBundle(), ...this.sendResourcesActions(task.args)];
|
||||||
|
}
|
||||||
|
|
||||||
|
sendResourcesActions(args: Args): Array<ActionDefinition> {
|
||||||
const pathArgs = {
|
const pathArgs = {
|
||||||
newdid: args.villageId,
|
newdid: args.villageId,
|
||||||
gid: args.buildTypeId || undefined,
|
gid: args.buildTypeId || undefined,
|
||||||
@ -21,11 +24,11 @@ export class SendResourcesTask extends TaskController {
|
|||||||
|
|
||||||
const pagePath = path('/build.php', pathArgs);
|
const pagePath = path('/build.php', pathArgs);
|
||||||
|
|
||||||
this.scheduler.scheduleActions([
|
return [
|
||||||
new Command(GoToPageAction.name, { ...args, path: pagePath }),
|
[GoToPageAction.name, { path: pagePath }],
|
||||||
new Command(SendResourcesAction.name, args),
|
[SendResourcesAction.name, {}],
|
||||||
new Command(ClickButtonAction.name, { ...args, selector: '#enabledButton.green.sendRessources' }),
|
[ClickButtonAction.name, { selector: '#enabledButton.green.sendRessources' }],
|
||||||
new Command(CompleteTaskAction.name, args),
|
[CompleteTaskAction.name, {}],
|
||||||
]);
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import { Task } from '../Queue/TaskQueue';
|
import { Task } from '../Queue/TaskQueue';
|
||||||
import { Scheduler } from '../Scheduler';
|
import { Scheduler } from '../Scheduler';
|
||||||
|
import { Args, Command } from '../Command';
|
||||||
|
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
|
||||||
|
|
||||||
const taskMap: { [name: string]: Function | undefined } = {};
|
const taskMap: { [name: string]: Function | undefined } = {};
|
||||||
|
|
||||||
@ -16,6 +18,8 @@ export function createTask(name: string, scheduler: Scheduler): TaskController |
|
|||||||
return new constructor(scheduler);
|
return new constructor(scheduler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ActionDefinition = [string, Args];
|
||||||
|
|
||||||
export class TaskController {
|
export class TaskController {
|
||||||
protected scheduler: Scheduler;
|
protected scheduler: Scheduler;
|
||||||
|
|
||||||
@ -23,5 +27,22 @@ export class TaskController {
|
|||||||
this.scheduler = scheduler;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user