Fix trooper training
This commit is contained in:
parent
e4241346ff
commit
18f43c3931
@ -1,20 +1,23 @@
|
|||||||
import { ActionController, err, registerAction } from './ActionController';
|
import { ActionController, err, registerAction } from './ActionController';
|
||||||
import { TryLaterError } from '../Errors';
|
import { TryLaterError } from '../Errors';
|
||||||
import { aroundMinutes } from '../utils';
|
import { aroundMinutes, randomInRange } from '../utils';
|
||||||
import { Args } from '../Queue/Args';
|
import { Args } from '../Queue/Args';
|
||||||
import { Task } from '../Queue/TaskProvider';
|
import { Task } from '../Queue/TaskProvider';
|
||||||
import { clickTrainButton, fillTrainCount, getAvailableCount } from '../Page/BuildingPage/TrooperPage';
|
import { clickTrainButton, fillTrainCount, getAvailableCount } from '../Page/BuildingPage/TrooperPage';
|
||||||
import { TrainTroopTask } from '../Task/TrainTroopTask';
|
import { TrainTroopTask } from '../Task/TrainTroopTask';
|
||||||
|
import { Resources } from '../Core/Resources';
|
||||||
|
|
||||||
@registerAction
|
@registerAction
|
||||||
export class TrainTrooperAction extends ActionController {
|
export class TrainTrooperAction extends ActionController {
|
||||||
async run(args: Args, task: Task): Promise<any> {
|
async run(args: Args, task: Task): Promise<any> {
|
||||||
const troopId = args.troopId || err('No troop id');
|
const troopId = args.troopId || err('No troop id');
|
||||||
const trainCount = args.trainCount || err('No troop train count');
|
const trainCount = args.trainCount || err('No troop train count');
|
||||||
|
const troopResources = args.troopResources || err('No troop resources');
|
||||||
|
|
||||||
const availableCount = getAvailableCount(troopId);
|
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;
|
const nextToTrainCount = trainCount - readyToTrainCount;
|
||||||
|
|
||||||
if (readyToTrainCount <= 0) {
|
if (readyToTrainCount <= 0) {
|
||||||
@ -22,7 +25,11 @@ export class TrainTrooperAction extends ActionController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (nextToTrainCount > 0) {
|
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);
|
fillTrainCount(troopId, readyToTrainCount);
|
||||||
|
@ -91,6 +91,7 @@ export class BuildingPageController {
|
|||||||
sheetId: this.attributes.sheetId,
|
sheetId: this.attributes.sheetId,
|
||||||
troopId,
|
troopId,
|
||||||
trainCount,
|
trainCount,
|
||||||
|
troopResources: resources,
|
||||||
resources: resources.scale(trainCount),
|
resources: resources.scale(trainCount),
|
||||||
};
|
};
|
||||||
this.scheduler.scheduleTask(TrainTroopTask.name, args);
|
this.scheduler.scheduleTask(TrainTroopTask.name, args);
|
||||||
|
@ -13,6 +13,7 @@ export interface Args {
|
|||||||
tabId?: number;
|
tabId?: number;
|
||||||
buildTypeId?: number;
|
buildTypeId?: number;
|
||||||
troopId?: number;
|
troopId?: number;
|
||||||
|
troopResources?: ResourcesInterface;
|
||||||
trainCount?: number;
|
trainCount?: number;
|
||||||
resources?: ResourcesInterface;
|
resources?: ResourcesInterface;
|
||||||
coordinates?: CoordinatesInterface;
|
coordinates?: CoordinatesInterface;
|
||||||
|
@ -4,10 +4,10 @@ export function sleep(ms: number) {
|
|||||||
return new Promise(resolve => setTimeout(resolve, ms));
|
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 delta = to - from;
|
||||||
const variation = Math.random() * delta;
|
const variation = Math.random() * delta;
|
||||||
return from + variation;
|
return Math.floor(from + variation);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function sleepMicro() {
|
export async function sleepMicro() {
|
||||||
|
Loading…
Reference in New Issue
Block a user