Fix insert build tasks, so order is matter
This commit is contained in:
		| @@ -1,13 +1,14 @@ | ||||
| import { ActionController, registerAction } from './ActionController'; | ||||
| import { Args } from '../Common'; | ||||
| import { Task } from '../Storage/TaskQueue'; | ||||
| import { BuildingQueueFullError } from '../Errors'; | ||||
| import { BuildingQueueFullError, GrabError } from '../Errors'; | ||||
| import { grabActiveVillageId, grabBuildingQueueInfo } from '../Page/VillageBlock'; | ||||
| import { BuildingQueueInfo } from '../Game'; | ||||
|  | ||||
| @registerAction | ||||
| export class CheckBuildingRemainingTimeAction extends ActionController { | ||||
|     async run(args: Args, task: Task): Promise<any> { | ||||
|         const info = grabBuildingQueueInfo(); | ||||
|         const info = this.grabBuildingQueueInfoOrDefault(); | ||||
|         if (info.seconds > 0) { | ||||
|             throw new BuildingQueueFullError( | ||||
|                 task.id, | ||||
| @@ -17,4 +18,15 @@ export class CheckBuildingRemainingTimeAction extends ActionController { | ||||
|             ); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private grabBuildingQueueInfoOrDefault() { | ||||
|         try { | ||||
|             return grabBuildingQueueInfo(); | ||||
|         } catch (e) { | ||||
|             if (e instanceof GrabError) { | ||||
|                 return new BuildingQueueInfo(0); | ||||
|             } | ||||
|             throw e; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -6,6 +6,7 @@ import { clickUpgradeButton } from '../Page/BuildingPage'; | ||||
| import { grabResourceDeposits } from '../Page/SlotBlock'; | ||||
| import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask'; | ||||
| import { ResourceDeposit } from '../Game'; | ||||
| import { aroundMinutes } from '../utils'; | ||||
|  | ||||
| @registerAction | ||||
| export class UpgradeResourceToLevel extends ActionController { | ||||
| @@ -35,19 +36,16 @@ export class UpgradeResourceToLevel extends ActionController { | ||||
|                     task.args.buildId === dep.buildId | ||||
|             ); | ||||
|  | ||||
|         const available = deposits | ||||
|             .sort((x, y) => x.level - y.level) | ||||
|             .filter(dep => dep.ready) | ||||
|             .filter(isDepositTaskNotInQueue); | ||||
|         const notUpgraded = deposits.sort((x, y) => x.level - y.level).filter(isDepositTaskNotInQueue); | ||||
|  | ||||
|         if (available.length === 0) { | ||||
|             throw new TryLaterError(task.id, 10 * 60, 'No available deposits'); | ||||
|         if (notUpgraded.length === 0) { | ||||
|             throw new TryLaterError(task.id, aroundMinutes(10), 'No available deposits'); | ||||
|         } | ||||
|  | ||||
|         const targetDep = available[0]; | ||||
|         for (let dep of notUpgraded) { | ||||
|             this.scheduler.scheduleTask(UpgradeBuildingTask.name, { villageId, buildId: dep.buildId }); | ||||
|         } | ||||
|  | ||||
|         this.scheduler.scheduleTask(UpgradeBuildingTask.name, { villageId, buildId: targetDep.buildId }); | ||||
|  | ||||
|         throw new TryLaterError(task.id, 20 * 60, 'Sleep for next round'); | ||||
|         throw new TryLaterError(task.id, aroundMinutes(10), 'Sleep for next round'); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user