Change task production order
This commit is contained in:
parent
b99bbd94a6
commit
09e989a8a1
@ -10,7 +10,7 @@ export enum ProductionQueue {
|
||||
/**
|
||||
* Placed in order of execution priority. Order is important!
|
||||
*/
|
||||
export const ProductionQueueTypes: ReadonlyArray<ProductionQueue> = [
|
||||
export const OrderedProductionQueues: ReadonlyArray<ProductionQueue> = [
|
||||
ProductionQueue.TrainUnit,
|
||||
ProductionQueue.Celebration,
|
||||
ProductionQueue.UpgradeUnit,
|
||||
@ -37,7 +37,7 @@ export interface TaskNamePredicate {
|
||||
/**
|
||||
* List on non intersected task queue predicates.
|
||||
*/
|
||||
export const TASK_TYPE_PREDICATES: Array<TaskNamePredicate> = ProductionQueueTypes.map(queue => {
|
||||
const TASK_TYPE_PREDICATES: Array<TaskNamePredicate> = OrderedProductionQueues.map(queue => {
|
||||
return (taskName: string) => getProductionQueue(taskName) === queue;
|
||||
});
|
||||
|
||||
|
@ -4,7 +4,7 @@ import { VillageStorage } from './Storage/VillageStorage';
|
||||
import { calcGatheringTimings, GatheringTime } from './Core/GatheringTimings';
|
||||
import { VillageRepositoryInterface } from './VillageRepository';
|
||||
import { VillageNotFound } from './Errors';
|
||||
import { ProductionQueue, ProductionQueueTypes } from './Core/ProductionQueue';
|
||||
import { ProductionQueue, OrderedProductionQueues } from './Core/ProductionQueue';
|
||||
import { Task } from './Queue/TaskProvider';
|
||||
import { timestamp } from './utils';
|
||||
import { VillageTaskCollection } from './VillageTaskCollection';
|
||||
@ -157,7 +157,7 @@ function createProductionQueueState(
|
||||
|
||||
function createAllProductionQueueStates(storage: VillageStorage, taskCollection: VillageTaskCollection) {
|
||||
let result: { [queue: string]: VillageProductionQueueState } = {};
|
||||
for (let queue of ProductionQueueTypes) {
|
||||
for (let queue of OrderedProductionQueues) {
|
||||
result[queue] = createProductionQueueState(queue, storage, taskCollection);
|
||||
}
|
||||
return result;
|
||||
@ -165,7 +165,7 @@ function createAllProductionQueueStates(storage: VillageStorage, taskCollection:
|
||||
|
||||
function calcFrontierResources(taskCollection: VillageTaskCollection): Resources {
|
||||
let result = Resources.zero();
|
||||
for (let queue of ProductionQueueTypes) {
|
||||
for (let queue of OrderedProductionQueues) {
|
||||
const tasks = taskCollection.getTasksInProductionQueue(queue);
|
||||
const firstTaskResources = tasks.slice(0, 1).reduce(taskResourceReducer, Resources.zero());
|
||||
result = result.add(firstTaskResources);
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { VillageStorage } from './Storage/VillageStorage';
|
||||
import { Task, TaskId, uniqTaskId, withResources, withTime } from './Queue/TaskProvider';
|
||||
import { Args } from './Queue/Args';
|
||||
import { isProductionTask, ProductionQueue, ProductionQueueTypes } from './Core/ProductionQueue';
|
||||
import { isProductionTask, ProductionQueue, OrderedProductionQueues } from './Core/ProductionQueue';
|
||||
import { getProductionQueue } from './Task/TaskMap';
|
||||
import { timestamp } from './utils';
|
||||
import { Resources } from './Core/Resources';
|
||||
@ -9,6 +9,12 @@ import { ContractAttributes, ContractType } from './Core/Contract';
|
||||
import { UpgradeBuildingTask } from './Task/UpgradeBuildingTask';
|
||||
import { ForgeImprovementTask } from './Task/ForgeImprovementTask';
|
||||
|
||||
interface QueueTasks {
|
||||
queue: ProductionQueue;
|
||||
tasks: Array<Task>;
|
||||
finishTs: number;
|
||||
}
|
||||
|
||||
export class VillageTaskCollection {
|
||||
private readonly storage: VillageStorage;
|
||||
private readonly villageId: number;
|
||||
@ -52,17 +58,31 @@ export class VillageTaskCollection {
|
||||
this.removeTasks(t => t.id === taskId);
|
||||
}
|
||||
|
||||
private getQueueGroupedTasks(): Array<QueueTasks> {
|
||||
const tasks = this.storage.getTasks();
|
||||
const result: Array<QueueTasks> = [];
|
||||
for (let queue of OrderedProductionQueues) {
|
||||
const queueTasks = tasks.filter(task => getProductionQueue(task.name) === queue);
|
||||
result.push({
|
||||
queue,
|
||||
tasks: queueTasks,
|
||||
finishTs: this.storage.getQueueTaskEnding(queue),
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
getTasksInProductionQueue(queue: ProductionQueue): Array<Task> {
|
||||
return this.storage.getTasks().filter(task => getProductionQueue(task.name) === queue);
|
||||
}
|
||||
|
||||
getReadyProductionTask(): Task | undefined {
|
||||
let sortedTasks: Array<Task> = [];
|
||||
for (let queue of ProductionQueueTypes) {
|
||||
const tasks = this.getTasksInProductionQueue(queue);
|
||||
sortedTasks = sortedTasks.concat(tasks);
|
||||
const groups = this.getQueueGroupedTasks();
|
||||
const firstReadyGroup = groups.filter(g => g.finishTs <= timestamp()).shift();
|
||||
if (!firstReadyGroup) {
|
||||
return undefined;
|
||||
}
|
||||
return sortedTasks.shift();
|
||||
return firstReadyGroup.tasks.shift();
|
||||
}
|
||||
|
||||
postponeTask(taskId: TaskId, seconds: number) {
|
||||
@ -85,8 +105,7 @@ export class VillageTaskCollection {
|
||||
}
|
||||
|
||||
getVillageRequiredResources(): Resources {
|
||||
const tasks = this.storage.getTasks().filter(t => t.args.resources);
|
||||
const first = tasks.shift();
|
||||
const first = this.getReadyProductionTask();
|
||||
if (first && first.args.resources) {
|
||||
return Resources.fromObject(first.args.resources);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user