示例#1
0
文件: StorerModule.cs 项目: dfgs/PIO
        public void EndStore(int WorkerID, ResourceTypeIDs ResourceTypeID)
        {
            Building building;
            Stack    stack;
            Worker   worker;

            LogEnter();

            worker = AssertExists(() => workerModule.GetWorker(WorkerID), $"WorkerID={WorkerID}");

            building = AssertExists(() => buildingModule.GetBuilding(worker.PlanetID, worker.X, worker.Y), $"X={worker.X}, Y={worker.Y}");

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

            Log(LogLevels.Information, $"Adding resource (ResourceTypeID={ResourceTypeID}, Quantity={carriedQuantity})");
            if (stack == null)
            {
                Try(() => stackModule.InsertStack(building.BuildingID, ResourceTypeID, carriedQuantity)).OrThrow <PIOInternalErrorException>("Failed to insert stack");
            }
            else
            {
                stack.Quantity += carriedQuantity;
                Try(() => stackModule.UpdateStack(stack.StackID, stack.Quantity)).OrThrow <PIOInternalErrorException>("Failed to update stack");
            }
        }
示例#2
0
文件: BuilderModule.cs 项目: dfgs/PIO
        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);
        }
示例#3
0
        public Task BeginTake(int WorkerID, ResourceTypeIDs ResourceTypeID)
        {
            Building building;
            Worker   worker;
            Stack    stack;
            Task     task;

            LogEnter();

            worker = AssertWorkerIsIdle(WorkerID);

            Log(LogLevels.Information, $"Check if worker is not carrying item (WorkerID={worker.WorkerID})");
            if (worker.ResourceTypeID != null)
            {
                Throw <PIOInvalidOperationException>(LogLevels.Warning, $"Worker is already carrying item (WorkerID={worker.WorkerID}, ResourceTypeID={worker.ResourceTypeID})");
            }

            building = AssertExists(() => buildingModule.GetBuilding(worker.PlanetID, worker.X, worker.Y), $"X={worker.X}, Y={worker.Y}");
            Log(LogLevels.Information, $"Check stack quantity (BuildingID={building.BuildingID}, ResourceTypeID={ResourceTypeID})");
            stack = Try(() => stackModule.GetStack(building.BuildingID, ResourceTypeID)).OrThrow <PIOInternalErrorException>("Failed to get stack");
            if ((stack == null) || (stack.Quantity < carriedQuantity))
            {
                Throw <PIONoResourcesException>(LogLevels.Warning, $"Not enough resources (BuildingID={building.BuildingID}, ResourceTypeID={ResourceTypeID})");
            }

            Log(LogLevels.Information, $"Consuming resource (BuildingID={building.BuildingID}, ResourceTypeID={ResourceTypeID}, Quantity={carriedQuantity})");
            stack.Quantity -= carriedQuantity;
            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.Take, WorkerID, worker.X, worker.Y, null, ResourceTypeID, null, DateTime.Now.AddSeconds(5))).OrThrow <PIOInternalErrorException>("Failed to create task");

            OnTasksCreated(task);

            return(task);
        }
示例#4
0
 public Stack GetStack(int StackID)
 {
     LogEnter();
     return(Try(() => stackModule.GetStack(StackID)).OrThrow(GenerateFaultException));
 }
示例#5
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);
        }