From d4ad7ec40caa3a7da5449cdcb63699ca2543726e Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sat, 10 Oct 2020 16:41:19 +0300 Subject: [PATCH] Simplify some code --- src/Core/Village.ts | 2 -- src/Executor.ts | 25 ++++++-------------- src/Page/VillageBlock.ts | 6 ++--- src/Scheduler.ts | 34 +++++++++------------------- src/Village/VillageFactory.ts | 6 +---- src/Village/VillageTaskCollection.ts | 8 +------ 6 files changed, 23 insertions(+), 58 deletions(-) diff --git a/src/Core/Village.ts b/src/Core/Village.ts index 0c6f148..6edfce6 100644 --- a/src/Core/Village.ts +++ b/src/Core/Village.ts @@ -40,8 +40,6 @@ export enum ReceiveResourcesMode { Optimum = 'optimum', } -export type VillageList = Array; - export interface VillageSettings { sendResourcesThreshold: number; sendResourcesMultiplier: number; diff --git a/src/Executor.ts b/src/Executor.ts index 3d89d18..734a131 100644 --- a/src/Executor.ts +++ b/src/Executor.ts @@ -25,35 +25,24 @@ export interface ExecutionSettings { } export class Executor { - private readonly version: string; - private readonly scheduler: Scheduler; - private readonly villageFactory: VillageFactory; - private readonly grabberManager: GrabberManager; - private readonly statistics: Statistics; private readonly executionState: ExecutionStorage; - private readonly logger: Logger; constructor( - version: string, - scheduler: Scheduler, - villageFactory: VillageFactory, - grabberManager: GrabberManager, - statistics: Statistics, - logger: Logger + private readonly version: string, + private readonly scheduler: Scheduler, + private readonly villageFactory: VillageFactory, + private readonly grabberManager: GrabberManager, + private readonly statistics: Statistics, + private readonly logger: Logger ) { - this.version = version; - this.scheduler = scheduler; - this.villageFactory = villageFactory; - this.grabberManager = grabberManager; - this.statistics = statistics; this.executionState = new ExecutionStorage(); - this.logger = logger; } async run() { await waitForLoad(); await sleepMicro(); + this.scheduler.scheduleRegularTasks(); this.renderInfo(); const sleep = createExecutionLoopSleeper(); diff --git a/src/Page/VillageBlock.ts b/src/Page/VillageBlock.ts index 01feb9e..638190d 100644 --- a/src/Page/VillageBlock.ts +++ b/src/Page/VillageBlock.ts @@ -1,6 +1,6 @@ import { GrabError } from '../Errors'; import { Resources } from '../Core/Resources'; -import { Coordinates, Village, VillageList } from '../Core/Village'; +import { Coordinates, Village } from '../Core/Village'; import { BuildingQueueInfo } from '../Core/BuildingQueueInfo'; import { getNumber } from '../Helpers/Convert'; import { parseLocation } from '../Helpers/Browser'; @@ -13,8 +13,8 @@ function getVillageListItems() { return $elements; } -export function grabVillageList(): VillageList { - const villageList: VillageList = []; +export function grabVillageList(): Array { + const villageList: Array = []; const $elements = getVillageListItems(); $elements.each((idx, el) => { villageList.push(grabVillageInfo(jQuery(el))); diff --git a/src/Scheduler.ts b/src/Scheduler.ts index f27ec5f..57ceade 100644 --- a/src/Scheduler.ts +++ b/src/Scheduler.ts @@ -22,29 +22,15 @@ interface NextExecution { } export class Scheduler { - private readonly taskQueue: TaskQueue; - private readonly actionQueue: ActionQueue; - private readonly villageRepository: VillageRepositoryInterface; - private readonly villageControllerFactory: VillageFactory; - private readonly logger: Logger; - constructor( - taskQueue: TaskQueue, - actionQueue: ActionQueue, - villageRepository: VillageRepositoryInterface, - villageControllerFactory: VillageFactory, - logger: Logger - ) { - this.taskQueue = taskQueue; - this.actionQueue = actionQueue; - this.villageRepository = villageRepository; - this.villageControllerFactory = villageControllerFactory; - this.logger = logger; - - // this.taskQueue.push(GrabVillageStateTask.name, {}, timestamp()); - // this.taskQueue.push(UpdateResourceContractsTask.name, {}, timestamp()); - // this.taskQueue.push(BalanceHeroResourcesTask.name, {}, timestamp()); + private readonly taskQueue: TaskQueue, + private readonly actionQueue: ActionQueue, + private readonly villageRepository: VillageRepositoryInterface, + private readonly villageControllerFactory: VillageFactory, + private readonly logger: Logger + ) {} + scheduleRegularTasks() { const villages = this.villageRepository.all(); for (let village of villages) { this.createUniqTaskTimer(5 * 60, RunVillageProductionTask.name, { @@ -53,10 +39,12 @@ export class Scheduler { } this.createUniqTaskTimer(10 * 60, GrabVillageStateTask.name); - // @todo Только если деревень больше одной - // this.createUniqTaskTimer(10 * 60, SendResourcesTask.name); + if (villages.length > 1) { + this.createUniqTaskTimer(10 * 60, SendResourcesTask.name); + } this.createUniqTaskTimer(10 * 60, BalanceHeroResourcesTask.name); this.createUniqTaskTimer(20 * 60, UpdateResourceContractsTask.name); + // @todo Нужна отдельная настройка для запуска задачи // this.createUniqTaskTimer(60 * 60, SendOnAdventureTask.name); } diff --git a/src/Village/VillageFactory.ts b/src/Village/VillageFactory.ts index 2eecb8e..eee90cb 100644 --- a/src/Village/VillageFactory.ts +++ b/src/Village/VillageFactory.ts @@ -6,11 +6,7 @@ import { VillageState, VillageStateFactory } from './VillageState'; import { Village } from '../Core/Village'; export class VillageFactory { - private readonly villageRepository: VillageRepositoryInterface; - - constructor(villageRepository: VillageRepositoryInterface) { - this.villageRepository = villageRepository; - } + constructor(private readonly villageRepository: VillageRepositoryInterface) {} getById(villageId: number): IntVillageFactory { return this.makeInternalFactory(this.villageRepository.getById(villageId)); diff --git a/src/Village/VillageTaskCollection.ts b/src/Village/VillageTaskCollection.ts index b3bc186..28e365a 100644 --- a/src/Village/VillageTaskCollection.ts +++ b/src/Village/VillageTaskCollection.ts @@ -10,13 +10,7 @@ import { Task, withResources, withTime } from '../Queue/Task'; import { TaskId, uniqTaskId } from '../Queue/TaskId'; export class VillageTaskCollection { - private readonly storage: VillageStorage; - private readonly villageId: number; - - constructor(villageId: number, storage: VillageStorage) { - this.villageId = villageId; - this.storage = storage; - } + constructor(private readonly villageId: number, private readonly storage: VillageStorage) {} getTasks(): Array { return this.storage.getTasks();