コード例 #1
0
        public Task BeginHarvest(int WorkerID)
        {
            Building     building;
            BuildingType buildingType;
            Worker       worker;

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

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


            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);
            }



            Log(LogLevels.Information, $"Creating task (WorkerID={WorkerID})");
            task = Try(() => taskModule.CreateTask(TaskTypeIDs.Harvest, WorkerID, worker.X, worker.Y, null, null, null, DateTime.Now.AddSeconds(products[0].Duration))).OrThrow <PIOInternalErrorException>("Failed to create task");

            OnTasksCreated(task);

            return(task);
        }
コード例 #2
0
 public Product[] GetProducts(BuildingTypeIDs BuildingTypeID)
 {
     LogEnter();
     return(Try(() => productModule.GetProducts(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);
        }