Fix train troop recurrent tasks

This commit is contained in:
Anton Vakhrushev 2020-10-12 13:52:02 +03:00
parent 7f3642d626
commit 33964dc99b
3 changed files with 26 additions and 14 deletions

View File

@ -30,13 +30,20 @@ export class TrainTrooperAction extends BaseAction {
} }
if (nextToTrainCount > 0) { if (nextToTrainCount > 0) {
this.scheduler.scheduleTask(TrainTroopTask.name, { this.scheduleNextTask(task, nextToTrainCount);
...task.args,
count: nextToTrainCount,
});
} }
fillTrainCount(troopId, readyToTrainCount); fillTrainCount(troopId, readyToTrainCount);
clickTrainButton(); clickTrainButton();
} }
private scheduleNextTask(prevTask: Task, nextToTrainCount: number) {
const villageId = prevTask.args.villageId || taskError('Empty village id');
const taskName = prevTask.name;
const taskCollection = this.villageFactory.getById(villageId).taskCollection();
taskCollection.addTaskAsFirst(taskName, {
...prevTask.args,
count: nextToTrainCount,
});
}
} }

View File

@ -119,8 +119,10 @@ export class Scheduler {
scheduleTask(name: string, args: Args, ts?: number | undefined): void { scheduleTask(name: string, args: Args, ts?: number | undefined): void {
if (isProductionTask(name) && args.villageId) { if (isProductionTask(name) && args.villageId) {
const controller = this.villageControllerFactory.getById(args.villageId).controller(); const taskCollection = this.villageControllerFactory
controller.addTask(name, args); .getById(args.villageId)
.taskCollection();
taskCollection.addTask(name, args);
} else { } else {
this.logger.info('Schedule task', name, args, ts); this.logger.info('Schedule task', name, args, ts);
this.taskQueue.add(new Task(uniqTaskId(), ts || timestamp(), name, args)); this.taskQueue.add(new Task(uniqTaskId(), ts || timestamp(), name, args));

View File

@ -145,8 +145,8 @@ function createProductionQueueState(
const taskEndingTimestamp = taskQueueState.finishTs; const taskEndingTimestamp = taskQueueState.finishTs;
const resources = warehouseState.resources; const resources = warehouseState.resources;
const performance = warehouseState.performance; const performance = warehouseState.performance;
const firstTaskResources = tasks.slice(0, 1).reduce(taskResourceReducer, Resources.zero()); const firstTaskResources = getTotalTaskCollectionResources(tasks.slice(0, 1));
const allTaskResources = tasks.reduce(taskResourceReducer, Resources.zero()); const allTaskResources = getTotalTaskCollectionResources(tasks);
const currentTimestamp = timestamp(); const currentTimestamp = timestamp();
@ -220,10 +220,14 @@ function getTotalTaskResources(task: TaskCore | undefined): Resources {
return Resources.zero(); return Resources.zero();
} }
function taskResourceReducer(resources: Resources, task: TaskCore) { function getTotalTaskCollectionResources(tasks: ReadonlyArray<TaskState>): Resources {
return task.args.resources const reducer = (memo: Resources, task: TaskState): Resources => {
? resources.add(Resources.fromObject(task.args.resources)) const count = task.args.count || 1;
: resources; return task.args.resources
? memo.add(Resources.fromObject(task.args.resources).scale(count))
: memo;
};
return tasks.reduce(reducer, Resources.zero());
} }
function makeTaskState(task: TaskCore, maxResourcesForTask: Resources): TaskState { function makeTaskState(task: TaskCore, maxResourcesForTask: Resources): TaskState {
@ -234,7 +238,6 @@ function makeTaskState(task: TaskCore, maxResourcesForTask: Resources): TaskStat
const isEnoughGranaryCapacity = maxResourcesForTask.allGreaterOrEqual( const isEnoughGranaryCapacity = maxResourcesForTask.allGreaterOrEqual(
new Resources(0, 0, 0, taskResources.crop) new Resources(0, 0, 0, taskResources.crop)
); );
const canBeBuilt = isEnoughWarehouseCapacity && isEnoughGranaryCapacity; const canBeBuilt = isEnoughWarehouseCapacity && isEnoughGranaryCapacity;
return { return {
@ -253,7 +256,7 @@ function createVillageState(village: Village, storage: VillageStorage): VillageS
const capacity = storage.getWarehouseCapacity(); const capacity = storage.getWarehouseCapacity();
const performance = storage.getResourcesPerformance(); const performance = storage.getResourcesPerformance();
const warehouse = makeWarehouseState(resources, capacity, performance); const warehouse = makeWarehouseState(resources, capacity, performance);
const tasks = storage.getTasks().map((t) => makeTaskState(t, warehouse.optimumFullness)); const tasks = storage.getTasks().map((task) => makeTaskState(task, warehouse.optimumFullness));
const queues = createTaskQueueStates(warehouse, tasks, storage); const queues = createTaskQueueStates(warehouse, tasks, storage);
const firstReadyTask = getReadyForProductionTask(queues); const firstReadyTask = getReadyForProductionTask(queues);
const firstTaskResources = getTotalTaskResources(firstReadyTask); const firstTaskResources = getTotalTaskResources(firstReadyTask);