Improve village state management
This commit is contained in:
		@@ -5,7 +5,7 @@ import { aroundMinutes } from '../utils';
 | 
			
		||||
import { Args } from '../Queue/Args';
 | 
			
		||||
import { Task } from '../Queue/TaskProvider';
 | 
			
		||||
import { VillageStorage } from '../Storage/VillageStorage';
 | 
			
		||||
import { VillageStateRepository } from '../VillageState';
 | 
			
		||||
import { VillageFactory } from '../VillageFactory';
 | 
			
		||||
 | 
			
		||||
const actionMap: { [name: string]: Function | undefined } = {};
 | 
			
		||||
 | 
			
		||||
@@ -16,22 +16,22 @@ export function registerAction(constructor: Function) {
 | 
			
		||||
export function createActionHandler(
 | 
			
		||||
    name: string,
 | 
			
		||||
    scheduler: Scheduler,
 | 
			
		||||
    villageStateRepository: VillageStateRepository
 | 
			
		||||
    villageFactory: VillageFactory
 | 
			
		||||
): ActionController | undefined {
 | 
			
		||||
    const storedFunction = actionMap[name];
 | 
			
		||||
    if (storedFunction === undefined) {
 | 
			
		||||
        return undefined;
 | 
			
		||||
    }
 | 
			
		||||
    const constructor = (storedFunction as unknown) as typeof ActionController;
 | 
			
		||||
    return new constructor(scheduler, villageStateRepository);
 | 
			
		||||
    return new constructor(scheduler, villageFactory);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class ActionController {
 | 
			
		||||
    protected scheduler: Scheduler;
 | 
			
		||||
    protected villageStateRepository: VillageStateRepository;
 | 
			
		||||
    constructor(scheduler: Scheduler, villageStateRepository: VillageStateRepository) {
 | 
			
		||||
    protected readonly scheduler: Scheduler;
 | 
			
		||||
    protected readonly villageFactory: VillageFactory;
 | 
			
		||||
    constructor(scheduler: Scheduler, villageFactory: VillageFactory) {
 | 
			
		||||
        this.scheduler = scheduler;
 | 
			
		||||
        this.villageStateRepository = villageStateRepository;
 | 
			
		||||
        this.villageFactory = villageFactory;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async run(args: Args, task: Task) {}
 | 
			
		||||
 
 | 
			
		||||
@@ -18,7 +18,7 @@ export class BalanceHeroResourcesAction extends ActionController {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const thisVillageState = this.villageStateRepository.getVillageState(thisVillageId);
 | 
			
		||||
        const thisVillageState = this.villageFactory.createState(thisVillageId);
 | 
			
		||||
 | 
			
		||||
        const requirements = [
 | 
			
		||||
            thisVillageState.required.balance,
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,5 @@
 | 
			
		||||
import { ActionController, registerAction } from './ActionController';
 | 
			
		||||
import { AbortTaskError, taskError } from '../Errors';
 | 
			
		||||
import { taskError } from '../Errors';
 | 
			
		||||
import { Args } from '../Queue/Args';
 | 
			
		||||
import { Task } from '../Queue/TaskProvider';
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -18,8 +18,8 @@ export class SendResourcesAction extends ActionController {
 | 
			
		||||
 | 
			
		||||
        const coordinates = Coordinates.fromObject(args.coordinates || taskError('No coordinates'));
 | 
			
		||||
 | 
			
		||||
        const senderVillage = this.villageStateRepository.getVillageState(senderVillageId);
 | 
			
		||||
        const recipientVillage = this.villageStateRepository.getVillageState(targetVillageId);
 | 
			
		||||
        const senderVillage = this.villageFactory.createState(senderVillageId);
 | 
			
		||||
        const recipientVillage = this.villageFactory.createState(targetVillageId);
 | 
			
		||||
 | 
			
		||||
        const readyToTransfer = this.getResourcesForTransfer(senderVillage, recipientVillage);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,5 @@
 | 
			
		||||
import { ActionController, registerAction } from './ActionController';
 | 
			
		||||
import { AbortTaskError, ActionError, taskError, TryLaterError } from '../Errors';
 | 
			
		||||
import { ActionError, taskError, TryLaterError } from '../Errors';
 | 
			
		||||
import { grabResourceDeposits } from '../Page/SlotBlock';
 | 
			
		||||
import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask';
 | 
			
		||||
import { ResourceDeposit } from '../Game';
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user