Improve hero balance action
This commit is contained in:
		@@ -1,10 +1,11 @@
 | 
			
		||||
import { Scheduler } from '../Scheduler';
 | 
			
		||||
import { ActionError, TryLaterError } from '../Errors';
 | 
			
		||||
import { AbortTaskError, TryLaterError } from '../Errors';
 | 
			
		||||
import { grabActiveVillageId } from '../Page/VillageBlock';
 | 
			
		||||
import { aroundMinutes } from '../utils';
 | 
			
		||||
import { Args } from '../Queue/Args';
 | 
			
		||||
import { Task } from '../Queue/TaskProvider';
 | 
			
		||||
import { VillageStorage } from '../Storage/VillageStorage';
 | 
			
		||||
import { VillageStateRepository } from '../VillageState';
 | 
			
		||||
 | 
			
		||||
const actionMap: { [name: string]: Function | undefined } = {};
 | 
			
		||||
 | 
			
		||||
@@ -12,29 +13,35 @@ export function registerAction(constructor: Function) {
 | 
			
		||||
    actionMap[constructor.name] = constructor;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function createActionHandler(name: string, scheduler: Scheduler): ActionController | undefined {
 | 
			
		||||
export function createActionHandler(
 | 
			
		||||
    name: string,
 | 
			
		||||
    scheduler: Scheduler,
 | 
			
		||||
    villageStateRepository: VillageStateRepository
 | 
			
		||||
): ActionController | undefined {
 | 
			
		||||
    const storedFunction = actionMap[name];
 | 
			
		||||
    if (storedFunction === undefined) {
 | 
			
		||||
        return undefined;
 | 
			
		||||
    }
 | 
			
		||||
    const constructor = (storedFunction as unknown) as typeof ActionController;
 | 
			
		||||
    return new constructor(scheduler);
 | 
			
		||||
    return new constructor(scheduler, villageStateRepository);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function err(msg: string): never {
 | 
			
		||||
    throw new ActionError(msg);
 | 
			
		||||
export function taskError(msg: string): never {
 | 
			
		||||
    throw new AbortTaskError(msg);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class ActionController {
 | 
			
		||||
    protected scheduler: Scheduler;
 | 
			
		||||
    constructor(scheduler: Scheduler) {
 | 
			
		||||
    protected villageStateRepository: VillageStateRepository;
 | 
			
		||||
    constructor(scheduler: Scheduler, villageStateRepository: VillageStateRepository) {
 | 
			
		||||
        this.scheduler = scheduler;
 | 
			
		||||
        this.villageStateRepository = villageStateRepository;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async run(args: Args, task: Task) {}
 | 
			
		||||
 | 
			
		||||
    ensureSameVillage(args: Args, task: Task) {
 | 
			
		||||
        let villageId = args.villageId || err('Undefined village id');
 | 
			
		||||
        let villageId = args.villageId || taskError('Undefined village id');
 | 
			
		||||
        const activeVillageId = grabActiveVillageId();
 | 
			
		||||
        if (villageId !== activeVillageId) {
 | 
			
		||||
            throw new TryLaterError(aroundMinutes(1), 'Not same village');
 | 
			
		||||
 
 | 
			
		||||
@@ -1,15 +1,11 @@
 | 
			
		||||
import { ActionController, registerAction } from './ActionController';
 | 
			
		||||
import { grabVillageResources, grabVillageResourceStorage } from '../Page/ResourcesBlock';
 | 
			
		||||
import { changeHeroResource, grabCurrentHeroResource } from '../Page/HeroPage';
 | 
			
		||||
import { grabActiveVillageId, grabVillageList } from '../Page/VillageBlock';
 | 
			
		||||
import { grabActiveVillageId } from '../Page/VillageBlock';
 | 
			
		||||
import { HeroStorage } from '../Storage/HeroStorage';
 | 
			
		||||
import { calcHeroResource } from '../Core/HeroBalance';
 | 
			
		||||
import { HeroAllResources } from '../Core/Hero';
 | 
			
		||||
import { Args } from '../Queue/Args';
 | 
			
		||||
import { Task } from '../Queue/TaskProvider';
 | 
			
		||||
import { Resources } from '../Core/Resources';
 | 
			
		||||
import { createVillageStates } from '../VillageState';
 | 
			
		||||
import { ActionError } from '../Errors';
 | 
			
		||||
 | 
			
		||||
@registerAction
 | 
			
		||||
export class BalanceHeroResourcesAction extends ActionController {
 | 
			
		||||
@@ -22,13 +18,7 @@ export class BalanceHeroResourcesAction extends ActionController {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const villages = grabVillageList();
 | 
			
		||||
        const villageStates = createVillageStates(villages, this.scheduler);
 | 
			
		||||
        const thisVillageState = villageStates.find(s => s.id === thisVillageId);
 | 
			
		||||
 | 
			
		||||
        if (!thisVillageState) {
 | 
			
		||||
            throw new ActionError(`State for village ${thisVillageId} not found`);
 | 
			
		||||
        }
 | 
			
		||||
        const thisVillageState = this.villageStateRepository.getVillageState(thisVillageId);
 | 
			
		||||
 | 
			
		||||
        const requirements = [
 | 
			
		||||
            thisVillageState.required.balance,
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
import { ActionController, err, registerAction } from './ActionController';
 | 
			
		||||
import { ActionController, taskError, registerAction } from './ActionController';
 | 
			
		||||
import { GrabError, TryLaterError } from '../Errors';
 | 
			
		||||
import { clickBuildButton } from '../Page/BuildingPage/BuildingPage';
 | 
			
		||||
import { aroundMinutes } from '../utils';
 | 
			
		||||
@@ -11,7 +11,7 @@ export class BuildBuildingAction extends ActionController {
 | 
			
		||||
        try {
 | 
			
		||||
            this.ensureSameVillage(args, task);
 | 
			
		||||
            this.ensureBuildingQueueIsEmpty();
 | 
			
		||||
            const buildTypeId = args.buildTypeId || err('Undefined build type id');
 | 
			
		||||
            const buildTypeId = args.buildTypeId || taskError('Undefined build type id');
 | 
			
		||||
            clickBuildButton(buildTypeId);
 | 
			
		||||
        } catch (e) {
 | 
			
		||||
            if (e instanceof GrabError) {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
import { ActionController, err, registerAction } from './ActionController';
 | 
			
		||||
import { ActionController, taskError, registerAction } from './ActionController';
 | 
			
		||||
import { GrabError, TryLaterError } from '../Errors';
 | 
			
		||||
import { aroundMinutes } from '../utils';
 | 
			
		||||
import { Args } from '../Queue/Args';
 | 
			
		||||
@@ -10,7 +10,7 @@ export class ForgeImprovementAction extends ActionController {
 | 
			
		||||
    async run(args: Args, task: Task): Promise<any> {
 | 
			
		||||
        try {
 | 
			
		||||
            this.ensureSameVillage(args, task);
 | 
			
		||||
            const unitId = args.unitId || err('No unitId in args');
 | 
			
		||||
            const unitId = args.unitId || taskError('No unitId in args');
 | 
			
		||||
            clickResearchButton(unitId);
 | 
			
		||||
        } catch (e) {
 | 
			
		||||
            if (e instanceof GrabError) {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
import { ActionController, err, registerAction } from './ActionController';
 | 
			
		||||
import { ActionController, taskError, registerAction } from './ActionController';
 | 
			
		||||
import { AbortTaskError, TryLaterError } from '../Errors';
 | 
			
		||||
import { Resources } from '../Core/Resources';
 | 
			
		||||
import { Coordinates, Village } from '../Core/Village';
 | 
			
		||||
@@ -15,7 +15,7 @@ const TIMEOUT = 15;
 | 
			
		||||
@registerAction
 | 
			
		||||
export class SendResourcesAction extends ActionController {
 | 
			
		||||
    async run(args: Args, task: Task): Promise<any> {
 | 
			
		||||
        const coordinates = Coordinates.fromObject(args.coordinates || err('No coordinates'));
 | 
			
		||||
        const coordinates = Coordinates.fromObject(args.coordinates || taskError('No coordinates'));
 | 
			
		||||
 | 
			
		||||
        const recipientVillage = args.targetVillageId
 | 
			
		||||
            ? this.findRecipientVillageById(args.targetVillageId)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
import { ActionController, err, registerAction } from './ActionController';
 | 
			
		||||
import { ActionController, taskError, registerAction } from './ActionController';
 | 
			
		||||
import { TryLaterError } from '../Errors';
 | 
			
		||||
import { aroundMinutes, randomInRange } from '../utils';
 | 
			
		||||
import { Args } from '../Queue/Args';
 | 
			
		||||
@@ -10,9 +10,9 @@ import { Resources } from '../Core/Resources';
 | 
			
		||||
@registerAction
 | 
			
		||||
export class TrainTrooperAction extends ActionController {
 | 
			
		||||
    async run(args: Args, task: Task): Promise<any> {
 | 
			
		||||
        const troopId = args.troopId || err('No troop id');
 | 
			
		||||
        const trainCount = args.trainCount || err('No troop train count');
 | 
			
		||||
        const troopResources = args.troopResources || err('No troop resources');
 | 
			
		||||
        const troopId = args.troopId || taskError('No troop id');
 | 
			
		||||
        const trainCount = args.trainCount || taskError('No troop train count');
 | 
			
		||||
        const troopResources = args.troopResources || taskError('No troop resources');
 | 
			
		||||
 | 
			
		||||
        const availableCount = getAvailableCount(troopId);
 | 
			
		||||
        const desiredCount = randomInRange(3, 12);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user