Example #1
0
    private void TryCompleteProjects(Rocket rocket)
    {
        numCompletedProjectsNextTurn = 0;
        foreach (Project project in projects)
        {
            // Figure out what resources still need to be launched.
            IEnumerable <ResourceDelta> remainingCosts = from cost in project.Cost
                                                         let remaining = cost.Amount - ResourcesStorage.NextAmount(cost.Type)
                                                                         where remaining > 0
                                                                         select new ResourceDelta(cost.Type, remaining);

            // If the rocket doesn't have room for all this, we can't complete this project
            if (!rocket.TryAddCargo(remainingCosts))
            {
                break;
            }

            // This project will be completed this tick.
            numCompletedProjectsNextTurn++;

            // Its resources will be included in the rocket's cargo
            // TODO and hopefully there's space in space for all of it!
            if (!ResourcesStorage.TryAddDeltas(remainingCosts))
            {
                throw new Exception();
            }

            // And they'll be deducted from the storage after that
            ResourcesStorage.TryAddDeltas(project.Cost.Select(cost => new ResourceDelta(cost.Type, -cost.Amount)));
        }
    }
    public void TryAddRejectsAllDeltas()
    {
        var storages = new ResourcesStorage();

        storages.TryAddDeltas(new[]
        {
            new ResourceDelta(ResourceType.Air, 20), new ResourceDelta(ResourceType.Air, -30),
            new ResourceDelta(ResourceType.Food, 20)
        });

        Assert.AreEqual(0, storages.NextAmount(ResourceType.Air));
        Assert.AreEqual(0, storages.NextAmount(ResourceType.Food));
    }