Fix resource sending

This commit is contained in:
Anton Vakhrushev 2020-04-26 09:58:11 +03:00
parent e9e3459f83
commit 1c6ef332f5
2 changed files with 10 additions and 10 deletions

View File

@ -15,6 +15,8 @@ function err(msg): never {
throw new ActionError(msg); throw new ActionError(msg);
} }
const TIMEOUT = 15;
@registerAction @registerAction
export class SendResourcesAction extends ActionController { export class SendResourcesAction extends ActionController {
async run(args: Args, task: Task): Promise<any> { async run(args: Args, task: Task): Promise<any> {
@ -32,10 +34,6 @@ export class SendResourcesAction extends ActionController {
console.log('To transfer res', readyToTransfer); console.log('To transfer res', readyToTransfer);
console.log('Remaining res', remainingResources); console.log('Remaining res', remainingResources);
if (!remainingResources.empty() && readyToTransfer.amount() < 100) {
throw new TryLaterError(aroundMinutes(10), 'Not minimal amount');
}
if (!remainingResources.empty()) { if (!remainingResources.empty()) {
console.log('Schedule next', remainingResources); console.log('Schedule next', remainingResources);
this.scheduler.scheduleTask( this.scheduler.scheduleTask(
@ -44,7 +42,7 @@ export class SendResourcesAction extends ActionController {
...args, ...args,
resources: remainingResources, resources: remainingResources,
}, },
timestamp() + aroundMinutes(10) timestamp() + aroundMinutes(TIMEOUT)
); );
} }
@ -65,7 +63,7 @@ export class SendResourcesAction extends ActionController {
const merchants = grabMerchantsInfo(); const merchants = grabMerchantsInfo();
const capacity = merchants.available * merchants.carry; const capacity = merchants.available * merchants.carry;
if (!capacity) { if (!capacity) {
throw new TryLaterError(aroundMinutes(10), 'No merchants'); throw new TryLaterError(aroundMinutes(TIMEOUT), 'No merchants');
} }
return capacity; return capacity;
} }
@ -78,8 +76,8 @@ export class SendResourcesAction extends ActionController {
console.log('Sender res', resources); console.log('Sender res', resources);
console.log('Sender req', requirements); console.log('Sender req', requirements);
console.log('Sender free', free); console.log('Sender free', free);
if (free.empty()) { if (free.amount() < 100) {
throw new TryLaterError(aroundMinutes(10), 'No free resources'); throw new TryLaterError(aroundMinutes(TIMEOUT), 'Little free resources');
} }
return free; return free;
} }
@ -98,7 +96,7 @@ export class SendResourcesAction extends ActionController {
console.log('Recipient req', requirements); console.log('Recipient req', requirements);
console.log('Recipient missing', missing); console.log('Recipient missing', missing);
if (missing.empty()) { if (missing.empty()) {
throw new TryLaterError(aroundMinutes(10), 'No missing resources'); throw new TryLaterError(aroundMinutes(TIMEOUT), 'No missing resources');
} }
return missing; return missing;
} }

View File

@ -131,7 +131,9 @@ export class Scheduler {
} }
getTotalVillageRequiredResources(villageId): Resources { getTotalVillageRequiredResources(villageId): Resources {
const tasks = this.taskQueue.seeItems().filter(t => sameVillage(villageId, t.args) && t.args.resources); const tasks = this.taskQueue
.seeItems()
.filter(t => sameVillage(villageId, t.args) && t.args.resources && t.name !== SendResourcesTask.name);
return tasks.reduce((acc, t) => acc.add(t.args.resources!), new Resources(0, 0, 0, 0)); return tasks.reduce((acc, t) => acc.add(t.args.resources!), new Resources(0, 0, 0, 0));
} }