public Task BeginBuild(int WorkerID) { Building building; Worker worker; Material[] materials; Stack stack; int quantity; Task task; LogEnter(); worker = AssertWorkerIsIdle(WorkerID); building = AssertExists(() => buildingModule.GetBuilding(worker.PlanetID, worker.X, worker.Y), $"X={worker.X}, Y={worker.Y}"); if (building.RemainingBuildSteps == 0) { Throw <PIOInvalidOperationException>(LogLevels.Warning, $"Building is already build (BuildingID={building.BuildingID})"); } materials = AssertExists(() => materialModule.GetMaterials(building.BuildingTypeID), $"BuildingTypeID={building.BuildingTypeID}"); foreach (Material material in materials) { Log(LogLevels.Information, $"Check stack quantity (ResourceTypeID={material.ResourceTypeID}, Quantity={material.Quantity})"); quantity = Try(() => stackModule.GetStackQuantity(building.BuildingID, material.ResourceTypeID)).OrThrow <PIOInternalErrorException>("Failed to check stack quantity"); if (quantity < material.Quantity) { Throw <PIONoResourcesException>(LogLevels.Warning, $"Not enough resources (BuildingID={building.BuildingID}, ResourceTypeID={material.ResourceTypeID})"); } } foreach (Material material in materials) { Log(LogLevels.Information, $"Consuming ingredient (ResourceTypeID={material.ResourceTypeID}, Quantity={material.Quantity})"); stack = Try(() => stackModule.GetStack(building.BuildingID, material.ResourceTypeID)).OrThrow <PIOInternalErrorException>("Failed to consume ingredient"); stack.Quantity -= material.Quantity; Try(() => stackModule.UpdateStack(stack.StackID, stack.Quantity)).OrThrow <PIOInternalErrorException>("Failed to update stack"); } Log(LogLevels.Information, $"Creating task (WorkerID={WorkerID})"); task = Try(() => taskModule.CreateTask(TaskTypeIDs.Build, WorkerID, worker.X, worker.Y, null, null, null, DateTime.Now.AddSeconds(10))).OrThrow <PIOInternalErrorException>("Failed to create task"); OnTasksCreated(task); return(task); }
public bool HasEnoughResourcesToBuild(int BuildingID) { Building building; Material[] materials; Stack[] stacks; Stack stack; LogEnter(); building = AssertExists(() => buildingModule.GetBuilding(BuildingID), $"BuildingID={BuildingID}"); Log(LogLevels.Information, $"Get materials (BuildingTypeID={building.BuildingTypeID})"); materials = Try(() => materialModule.GetMaterials(building.BuildingTypeID)).OrThrow <PIOInternalErrorException>("Failed to get materials"); Log(LogLevels.Information, $"Get stacks (BuildingID={building.BuildingID})"); stacks = Try(() => stackModule.GetStacks(building.BuildingID)).OrThrow <PIOInternalErrorException>("Failed to get stacks"); foreach (Material material in materials) { Log(LogLevels.Information, $"Check stack quantity (ResourceTypeID={material.ResourceTypeID}, Quantity={material.Quantity})"); stack = stacks.FirstOrDefault(item => item.ResourceTypeID == material.ResourceTypeID); if (stack == null) { Log(LogLevels.Information, $"Resource not found in stacks"); return(false); } if (stack.Quantity < material.Quantity) { Log(LogLevels.Information, $"Not enough quantity in stack (Quantity={stack.Quantity})"); return(false); } } return(true); }
public Material[] GetMaterials(BuildingTypeIDs BuildingTypeID) { LogEnter(); return(Try(() => materialModule.GetMaterials(BuildingTypeID)).OrThrow(GenerateFaultException)); }