예제 #1
0
        public bool HasEnoughResourcesToProduce(int BuildingID)
        {
            Building building;

            Ingredient[] ingredients;
            Stack[]      stacks;
            Stack        stack;

            LogEnter();

            building = AssertExists(() => buildingModule.GetBuilding(BuildingID), $"BuildingID={BuildingID}");

            Log(LogLevels.Information, $"Get ingredients (BuildingTypeID={building.BuildingTypeID})");
            ingredients = Try(() => ingredientModule.GetIngredients(building.BuildingTypeID)).OrThrow <PIOInternalErrorException>("Failed to get ingredients");

            Log(LogLevels.Information, $"Get stacks (BuildingID={building.BuildingID})");
            stacks = Try(() => stackModule.GetStacks(building.BuildingID)).OrThrow <PIOInternalErrorException>("Failed to get stacks");


            foreach (Ingredient ingredient in ingredients)
            {
                Log(LogLevels.Information, $"Check stack quantity (ResourceTypeID={ingredient.ResourceTypeID}, Quantity={ingredient.Quantity})");
                stack = stacks.FirstOrDefault(item => item.ResourceTypeID == ingredient.ResourceTypeID);
                if (stack == null)
                {
                    Log(LogLevels.Information, $"Resource not found in stacks");
                    return(false);
                }

                if (stack.Quantity < ingredient.Quantity)
                {
                    Log(LogLevels.Information, $"Not enough quantity in stack (Quantity={stack.Quantity})");
                    return(false);
                }
            }

            return(true);
        }
예제 #2
0
 public Ingredient[] GetIngredients(BuildingTypeIDs BuildingTypeID)
 {
     LogEnter();
     return(Try(() => ingredientModule.GetIngredients(BuildingTypeID)).OrThrow(GenerateFaultException));
 }
예제 #3
0
        public Task BeginProduce(int WorkerID)
        {
            Building     building;
            BuildingType buildingType;
            Worker       worker;

            Ingredient[] ingredients;
            Product[]    products;
            Task         task;
            Stack        stack;
            int          quantity;

            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 building (BuildingID={building.BuildingID})");
            }

            buildingType = AssertExists(() => buildingTypeModule.GetBuildingType(building.BuildingTypeID), $"BuildingTypeID={building.BuildingTypeID}");
            if (!buildingType.IsFactory)
            {
                Throw <PIOInvalidOperationException>(LogLevels.Warning, $"Building is not a factory (BuildingID={building.BuildingID})");
            }

            ingredients = AssertExists(() => ingredientModule.GetIngredients(building.BuildingTypeID), $"BuildingTypeID={building.BuildingTypeID}");
            products    = AssertExists(() => productModule.GetProducts(building.BuildingTypeID), $"BuildingTypeID={building.BuildingTypeID}");
            if (products.Length == 0)
            {
                Log(LogLevels.Warning, $"This building has no product (BuildingTypeID={building.BuildingTypeID})");
                return(null);
            }

            foreach (Ingredient ingredient in ingredients)
            {
                Log(LogLevels.Information, $"Check stack quantity (ResourceTypeID={ingredient.ResourceTypeID}, Quantity={ingredient.Quantity})");
                quantity = Try(() => stackModule.GetStackQuantity(building.BuildingID, ingredient.ResourceTypeID)).OrThrow <PIOInternalErrorException>("Failed to check stack quantity");
                if (quantity < ingredient.Quantity)
                {
                    Throw <PIONoResourcesException>(LogLevels.Warning, $"Not enough resources (BuildingID={building.BuildingID}, ResourceTypeID={ingredient.ResourceTypeID})");
                }
            }

            foreach (Ingredient ingredient in ingredients)
            {
                Log(LogLevels.Information, $"Consuming ingredient (ResourceTypeID={ingredient.ResourceTypeID}, Quantity={ingredient.Quantity})");
                stack           = Try(() => stackModule.GetStack(building.BuildingID, ingredient.ResourceTypeID)).OrThrow <PIOInternalErrorException>("Failed to consume ingredient");
                stack.Quantity -= ingredient.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.Produce, WorkerID, worker.X, worker.Y, null, null, null, DateTime.Now.AddSeconds(products[0].Duration))).OrThrow <PIOInternalErrorException>("Failed to create task");

            OnTasksCreated(task);

            return(task);
        }