This commit is contained in:
Anton Vakhrushev 2020-05-25 09:14:49 +03:00
parent faed76df4d
commit b99bbd94a6
5 changed files with 27 additions and 31 deletions

View File

@ -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<ActionDefinition> {
export function scanAllVillagesBundle(villages: Array<Village>): Array<ActionDefinition> {
const actions: Array<ActionDefinition> = [];
const villages = grabVillageList();
for (let village of villages) {
actions.push(goToResourceViewPage(village.id));
actions.push(goToMarketSendResourcesPage(village.id));

View File

@ -6,6 +6,7 @@ import { registerTask } from './TaskMap';
@registerTask()
export class GrabVillageState extends TaskController {
defineActions(task: Task): Array<ActionDefinition> {
return scanAllVillagesBundle();
const villages = this.factory.getAllVillages();
return scanAllVillagesBundle(villages);
}
}

View File

@ -9,7 +9,11 @@ import { registerTask } from './TaskMap';
@registerTask()
export class UpdateResourceContracts extends TaskController {
defineActions(task: Task): Array<ActionDefinition> {
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)]);

View File

@ -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) {

View File

@ -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) {