Fix trooper training

This commit is contained in:
Anton Vakhrushev 2020-05-09 21:06:08 +03:00
parent e4241346ff
commit 18f43c3931
4 changed files with 14 additions and 5 deletions

View File

@ -1,20 +1,23 @@
import { ActionController, err, registerAction } from './ActionController';
import { TryLaterError } from '../Errors';
import { aroundMinutes } from '../utils';
import { aroundMinutes, randomInRange } from '../utils';
import { Args } from '../Queue/Args';
import { Task } from '../Queue/TaskProvider';
import { clickTrainButton, fillTrainCount, getAvailableCount } from '../Page/BuildingPage/TrooperPage';
import { TrainTroopTask } from '../Task/TrainTroopTask';
import { Resources } from '../Core/Resources';
@registerAction
export class TrainTrooperAction extends ActionController {
async run(args: Args, task: Task): Promise<any> {
const troopId = args.troopId || err('No troop id');
const trainCount = args.trainCount || err('No troop train count');
const troopResources = args.troopResources || err('No troop resources');
const availableCount = getAvailableCount(troopId);
const desiredCount = randomInRange(3, 12);
const readyToTrainCount = Math.min(availableCount, trainCount);
const readyToTrainCount = Math.min(availableCount, trainCount, desiredCount);
const nextToTrainCount = trainCount - readyToTrainCount;
if (readyToTrainCount <= 0) {
@ -22,7 +25,11 @@ export class TrainTrooperAction extends ActionController {
}
if (nextToTrainCount > 0) {
this.scheduler.scheduleTask(TrainTroopTask.name, { ...task.args, trainCount: nextToTrainCount });
this.scheduler.scheduleTask(TrainTroopTask.name, {
...task.args,
trainCount: nextToTrainCount,
resources: Resources.fromObject(troopResources).scale(nextToTrainCount),
});
}
fillTrainCount(troopId, readyToTrainCount);

View File

@ -91,6 +91,7 @@ export class BuildingPageController {
sheetId: this.attributes.sheetId,
troopId,
trainCount,
troopResources: resources,
resources: resources.scale(trainCount),
};
this.scheduler.scheduleTask(TrainTroopTask.name, args);

View File

@ -13,6 +13,7 @@ export interface Args {
tabId?: number;
buildTypeId?: number;
troopId?: number;
troopResources?: ResourcesInterface;
trainCount?: number;
resources?: ResourcesInterface;
coordinates?: CoordinatesInterface;

View File

@ -4,10 +4,10 @@ export function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function randomInRange(from: number, to: number): number {
export function randomInRange(from: number, to: number): number {
const delta = to - from;
const variation = Math.random() * delta;
return from + variation;
return Math.floor(from + variation);
}
export async function sleepMicro() {