diff --git a/src/Task/ActionBundles.ts b/src/Task/ActionBundles.ts index bc34103..d13d77b 100644 --- a/src/Task/ActionBundles.ts +++ b/src/Task/ActionBundles.ts @@ -1,8 +1,8 @@ import { ActionDefinition } from './TaskController'; -import { grabVillageList } from '../Page/VillageBlock'; import { GoToPageAction } from '../Action/GoToPageAction'; import { FORGE_ID, GUILD_HALL_ID, MARKET_ID } from '../Core/Buildings'; import { path } from '../Helpers/Path'; +import { Village } from '../Core/Village'; export function goToResourceViewPage(villageId: number): ActionDefinition { return [ @@ -40,9 +40,8 @@ export function goToGuildHallPage(villageId: number): ActionDefinition { ]; } -export function scanAllVillagesBundle(): Array { +export function scanAllVillagesBundle(villages: Array): Array { const actions: Array = []; - const villages = grabVillageList(); for (let village of villages) { actions.push(goToResourceViewPage(village.id)); actions.push(goToMarketSendResourcesPage(village.id)); diff --git a/src/Task/GrabVillageState.ts b/src/Task/GrabVillageState.ts index 1756c3f..5f03be9 100644 --- a/src/Task/GrabVillageState.ts +++ b/src/Task/GrabVillageState.ts @@ -6,6 +6,7 @@ import { registerTask } from './TaskMap'; @registerTask() export class GrabVillageState extends TaskController { defineActions(task: Task): Array { - return scanAllVillagesBundle(); + const villages = this.factory.getAllVillages(); + return scanAllVillagesBundle(villages); } } diff --git a/src/Task/UpdateResourceContracts.ts b/src/Task/UpdateResourceContracts.ts index 5185f17..9367802 100644 --- a/src/Task/UpdateResourceContracts.ts +++ b/src/Task/UpdateResourceContracts.ts @@ -9,7 +9,11 @@ import { registerTask } from './TaskMap'; @registerTask() export class UpdateResourceContracts extends TaskController { defineActions(task: Task): Array { - const tasks = this.scheduler.getTaskItems(); + const villages = this.factory.getAllVillages(); + + const tasks = villages + .map(v => this.factory.createTaskCollection(v.id).getTasks()) + .reduce((acc, tasks) => acc.concat(tasks), []); const paths = uniqPaths([...this.walkUpgradeTasks(tasks), ...this.walkImprovementTask(tasks)]); diff --git a/src/VillageRepository.ts b/src/VillageRepository.ts index f124d6c..fefe0fa 100644 --- a/src/VillageRepository.ts +++ b/src/VillageRepository.ts @@ -1,4 +1,4 @@ -import { Village } from './Core/Village'; +import { Coordinates, Village } from './Core/Village'; import { grabVillageList } from './Page/VillageBlock'; import { VillageNotFound } from './Errors'; @@ -20,6 +20,14 @@ export class VillageRepository implements VillageRepositoryInterface { return village; } + getByCrd(crd: Coordinates): Village { + const village = this.all().find(vlg => vlg.crd.eq(crd)); + if (!village) { + throw new VillageNotFound('Village not found'); + } + return village; + } + getActive(): Village { const village = this.all().find(vlg => vlg.active); if (!village) { diff --git a/src/VillageTaskCollection.ts b/src/VillageTaskCollection.ts index 68c8c25..289c184 100644 --- a/src/VillageTaskCollection.ts +++ b/src/VillageTaskCollection.ts @@ -1,5 +1,5 @@ import { VillageStorage } from './Storage/VillageStorage'; -import { Task, TaskId, TaskList, uniqTaskId, withResources, withTime } from './Queue/TaskProvider'; +import { Task, TaskId, uniqTaskId, withResources, withTime } from './Queue/TaskProvider'; import { Args } from './Queue/Args'; import { isProductionTask, ProductionQueue, ProductionQueueTypes } from './Core/ProductionQueue'; import { getProductionQueue } from './Task/TaskMap'; @@ -22,32 +22,16 @@ export class VillageTaskCollection { return this.storage.getTasks(); } - private modifyTasks(predicate: (t: Task) => boolean, modifier: (t: Task) => Task): number { - const [matched, other] = this.split(predicate); - const modified = matched.map(modifier); - const modifiedCount = modified.length; - this.storage.storeTaskList(modified.concat(other)); - return modifiedCount; + private modifyTasks(predicate: (t: Task) => boolean, modifier: (t: Task) => Task): void { + const tasks = this.getTasks(); + const modified = tasks.map(t => (predicate(t) ? modifier(t) : t)); + this.storage.storeTaskList(modified); } - private removeTasks(predicate: (t: Task) => boolean): number { - const [_, other] = this.split(predicate); - const result = other.length; - this.storage.storeTaskList(other); - return result; - } - - private split(predicate: (t: Task) => boolean): [TaskList, TaskList] { - const matched: TaskList = []; - const other: TaskList = []; - this.getTasks().forEach(t => { - if (predicate(t)) { - matched.push(t); - } else { - other.push(t); - } - }); - return [matched, other]; + private removeTasks(predicate: (t: Task) => boolean): void { + const tasks = this.getTasks(); + const remaining = tasks.filter(t => !predicate(t)); + this.storage.storeTaskList(remaining); } addTask(name: string, args: Args) {