Refactoring: simplify troop training
This commit is contained in:
parent
1b053de16a
commit
25969b8c0f
@ -17,13 +17,12 @@ import { Task } from '../../Queue/Task';
|
|||||||
export class TrainTrooperAction extends BaseAction {
|
export class TrainTrooperAction extends BaseAction {
|
||||||
async run(args: Args, task: Task): Promise<any> {
|
async run(args: Args, task: Task): Promise<any> {
|
||||||
const troopId = args.troopId || taskError('No troop id');
|
const troopId = args.troopId || taskError('No troop id');
|
||||||
const trainCount = args.trainCount || taskError('No troop train count');
|
const trainCount = args.count || taskError('No count');
|
||||||
const troopResources = args.troopResources || taskError('No troop resources');
|
|
||||||
|
|
||||||
const availableCount = getAvailableCount(troopId);
|
const availableCount = getAvailableCount(troopId);
|
||||||
const desiredCount = randomInRange(3, 12);
|
const desiredCount = randomInRange(3, 12);
|
||||||
|
|
||||||
const readyToTrainCount = Math.min(availableCount, trainCount, desiredCount);
|
const readyToTrainCount = Math.min(trainCount, availableCount, desiredCount);
|
||||||
const nextToTrainCount = trainCount - readyToTrainCount;
|
const nextToTrainCount = trainCount - readyToTrainCount;
|
||||||
|
|
||||||
if (readyToTrainCount <= 0) {
|
if (readyToTrainCount <= 0) {
|
||||||
@ -33,8 +32,7 @@ export class TrainTrooperAction extends BaseAction {
|
|||||||
if (nextToTrainCount > 0) {
|
if (nextToTrainCount > 0) {
|
||||||
this.scheduler.scheduleTask(TrainTroopTask.name, {
|
this.scheduler.scheduleTask(TrainTroopTask.name, {
|
||||||
...task.args,
|
...task.args,
|
||||||
trainCount: nextToTrainCount,
|
count: nextToTrainCount,
|
||||||
resources: Resources.fromObject(troopResources).scale(nextToTrainCount),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ import { createCelebrationButtons } from './BuildingPage/GuildHallPage';
|
|||||||
import { CelebrationTask } from '../Handler/Task/CelebrationTask';
|
import { CelebrationTask } from '../Handler/Task/CelebrationTask';
|
||||||
import { VillageController } from '../Village/VillageController';
|
import { VillageController } from '../Village/VillageController';
|
||||||
import { notify } from '../Helpers/Browser';
|
import { notify } from '../Helpers/Browser';
|
||||||
|
import { Args } from '../Queue/Args';
|
||||||
|
|
||||||
export class BuildingPageController {
|
export class BuildingPageController {
|
||||||
private scheduler: Scheduler;
|
private scheduler: Scheduler;
|
||||||
@ -109,19 +110,18 @@ export class BuildingPageController {
|
|||||||
notify(`Upgrading ${buildId} scheduled`);
|
notify(`Upgrading ${buildId} scheduled`);
|
||||||
}
|
}
|
||||||
|
|
||||||
private onScheduleTrainTroopers(troopId: number, resources: Resources, trainCount: number) {
|
private onScheduleTrainTroopers(troopId: number, resources: Resources, count: number) {
|
||||||
const args = {
|
const args: Args = {
|
||||||
villageId: grabActiveVillageId(),
|
villageId: grabActiveVillageId(),
|
||||||
buildId: this.attributes.buildId,
|
buildId: this.attributes.buildId,
|
||||||
buildTypeId: this.attributes.buildTypeId,
|
buildTypeId: this.attributes.buildTypeId,
|
||||||
sheetId: this.attributes.sheetId,
|
sheetId: this.attributes.sheetId,
|
||||||
|
resources,
|
||||||
troopId,
|
troopId,
|
||||||
trainCount,
|
count,
|
||||||
troopResources: resources,
|
|
||||||
resources: resources.scale(trainCount),
|
|
||||||
};
|
};
|
||||||
this.villageController.addTask(TrainTroopTask.name, args);
|
this.villageController.addTask(TrainTroopTask.name, args);
|
||||||
notify(`Training ${trainCount} troopers scheduled`);
|
notify(`Training ${count} troopers scheduled`);
|
||||||
}
|
}
|
||||||
|
|
||||||
private onSendResources(coordinates: Coordinates) {
|
private onSendResources(coordinates: Coordinates) {
|
||||||
|
@ -13,8 +13,6 @@ export interface Args {
|
|||||||
tabId?: number;
|
tabId?: number;
|
||||||
buildTypeId?: number;
|
buildTypeId?: number;
|
||||||
troopId?: number;
|
troopId?: number;
|
||||||
troopResources?: ResourcesInterface;
|
|
||||||
trainCount?: number;
|
|
||||||
count?: number;
|
count?: number;
|
||||||
resources?: ResourcesInterface;
|
resources?: ResourcesInterface;
|
||||||
coordinates?: CoordinatesInterface;
|
coordinates?: CoordinatesInterface;
|
||||||
|
@ -3,7 +3,6 @@ import { Resources } from '../Core/Resources';
|
|||||||
import { VillageStorage } from '../Storage/VillageStorage';
|
import { VillageStorage } from '../Storage/VillageStorage';
|
||||||
import { calcGatheringTimings, GatheringTime } from '../Core/GatheringTimings';
|
import { calcGatheringTimings, GatheringTime } from '../Core/GatheringTimings';
|
||||||
import { OrderedProductionQueues, ProductionQueue } from '../Core/ProductionQueue';
|
import { OrderedProductionQueues, ProductionQueue } from '../Core/ProductionQueue';
|
||||||
import { TrainTroopTask } from '../Handler/Task/TrainTroopTask';
|
|
||||||
import { Args } from '../Queue/Args';
|
import { Args } from '../Queue/Args';
|
||||||
import { timestamp } from '../Helpers/Time';
|
import { timestamp } from '../Helpers/Time';
|
||||||
import { isInQueue, TaskCore } from '../Queue/Task';
|
import { isInQueue, TaskCore } from '../Queue/Task';
|
||||||
@ -221,9 +220,7 @@ function getReadyForProductionTask(
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
return firstReadyQueue.tasks.find(
|
return firstReadyQueue.tasks.find((task) => task.canBeBuilt);
|
||||||
(task) => task.name === TrainTroopTask.name || task.canBeBuilt
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTaskResources(task: TaskCore | undefined): Resources {
|
function getTaskResources(task: TaskCore | undefined): Resources {
|
||||||
|
Loading…
Reference in New Issue
Block a user