Optimize postpone error handler

This commit is contained in:
2020-04-19 11:53:18 +03:00
parent 84ac56d052
commit 24ceb04307
7 changed files with 49 additions and 55 deletions

View File

@ -1,7 +1,9 @@
import { Args } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { DataStorage } from '../DataStorage';
import { Scheduler } from '../Scheduler';
import { ActionError, TryLaterError } from '../Errors';
import { grabActiveVillageId } from '../Page/VillageBlock';
import { aroundMinutes } from '../utils';
const actionMap: { [name: string]: Function | undefined } = {};
@ -25,4 +27,16 @@ export class ActionController {
}
async run(args: Args, task: Task) {}
ensureSameVillage(args: Args, task: Task) {
let villageId = args.villageId;
if (villageId === undefined) {
throw new ActionError(task.id, 'Undefined village id');
}
const activeVillageId = grabActiveVillageId();
if (villageId !== activeVillageId) {
throw new TryLaterError(task.id, aroundMinutes(1), 'Not same village');
}
}
}

View File

@ -3,20 +3,23 @@ import { Args } from '../Command';
import { ActionError, GrabError, TryLaterError } from '../Errors';
import { Task } from '../Queue/TaskQueue';
import { clickBuildButton } from '../Page/BuildingPage';
import { aroundMinutes } from '../utils';
@registerAction
export class BuildBuildingAction extends ActionController {
async run(args: Args, task: Task): Promise<any> {
this.ensureSameVillage(args, task);
const buildTypeId = args.buildTypeId;
if (!buildTypeId) {
throw new ActionError(task.id, 'Unknown build type id');
if (buildTypeId === undefined) {
throw new ActionError(task.id, 'Undefined build type id');
}
try {
clickBuildButton(buildTypeId);
} catch (e) {
if (e instanceof GrabError) {
throw new TryLaterError(task.id, 15 * 60, 'No build button, try later');
throw new TryLaterError(task.id, aroundMinutes(5), 'No upgrade button, try later');
}
throw e;
}

View File

@ -1,8 +1,8 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Command';
import { Task } from '../Queue/TaskQueue';
import { PostponeAllBuildingsError, GrabError } from '../Errors';
import { grabActiveVillageId, grabBuildingQueueInfo } from '../Page/VillageBlock';
import { GrabError, TryLaterError } from '../Errors';
import { grabBuildingQueueInfo } from '../Page/VillageBlock';
import { BuildingQueueInfo } from '../Game';
@registerAction
@ -10,12 +10,7 @@ export class CheckBuildingRemainingTimeAction extends ActionController {
async run(args: Args, task: Task): Promise<any> {
const info = this.grabBuildingQueueInfoOrDefault();
if (info.seconds > 0) {
throw new PostponeAllBuildingsError(
task.id,
grabActiveVillageId(),
info.seconds + 1,
'Building queue is full'
);
throw new TryLaterError(task.id, info.seconds + 1, 'Building queue is full');
}
}

View File

@ -1,22 +1,20 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Command';
import { ActionError, GrabError, PostponeAllBuildingsError } from '../Errors';
import { GrabError, TryLaterError } from '../Errors';
import { Task } from '../Queue/TaskQueue';
import { clickUpgradeButton } from '../Page/BuildingPage';
import { aroundMinutes } from '../utils';
@registerAction
export class UpgradeBuildingAction extends ActionController {
async run(args: Args, task: Task): Promise<any> {
let villageId = args.villageId;
if (villageId === undefined) {
throw new ActionError(task.id, 'No village id');
}
this.ensureSameVillage(args, task);
try {
clickUpgradeButton();
} catch (e) {
if (e instanceof GrabError) {
throw new PostponeAllBuildingsError(task.id, villageId, 15 * 60, 'No upgrade button, try later');
throw new TryLaterError(task.id, aroundMinutes(5), 'No upgrade button, try later');
}
throw e;
}