Beispiel #1
0
            public void WillTestBuildingCapacityWithLargeNumbers()
            {
                var r = new Random();

                var totalNetValue = new NetValue
                {
                    Cash   = r.Next(1, int.MaxValue),
                    Energy = r.Next(1, int.MaxValue),
                    Food   = r.Next(1, int.MaxValue),
                    Iron   = r.Next(1, int.MaxValue),
                    Mana   = r.Next(1, int.MaxValue)
                };
                var buildCosts = new BuildCosts <object>
                {
                    Cash   = 1 * r.NextDouble(),
                    Energy = 1 * r.NextDouble(),
                    Food   = 1 * r.NextDouble(),
                    Iron   = 1 * r.NextDouble(),
                    Mana   = 1 * r.NextDouble()
                };

                var netValue = buildCosts.CalculateBuildCosts(totalNetValue, 0, r.Next(250));

                Assert.GreaterOrEqual(netValue.EntityCount, 1);
            }
Beispiel #2
0
            public void WillReturnZeroBuildItems()
            {
                var totalNetValue = new NetValue();
                var buildCosts    = new BuildCosts <object>();

                var netValue = buildCosts.CalculateBuildCosts(totalNetValue, 0, 1);

                Assert.AreEqual(netValue.EntityCount, 0);
            }
Beispiel #3
0
 /// <summary>
 /// Add all resources together.
 /// </summary>
 /// <param name="netValue">
 /// The net value.
 /// </param>
 /// <param name="value">
 /// The value.
 /// </param>
 public static void Add(this NetValue netValue, NetValue value)
 {
     netValue.Cash        += value.Cash;
     netValue.Energy      += value.Energy;
     netValue.Food        += value.Food;
     netValue.Mana        += value.Mana;
     netValue.Population  += value.Population;
     netValue.Iron        += value.Iron;
     netValue.Research    += value.Research;
     netValue.EntityCount += value.EntityCount;
 }
Beispiel #4
0
 /// <summary>
 /// Subtract all resources from one another.
 /// </summary>
 /// <param name="netValue">
 /// The net value.
 /// </param>
 /// <param name="value">
 /// The value.
 /// </param>
 public static void Subtract(this NetValue netValue, NetValue value)
 {
     netValue.Cash        -= value.Cash;
     netValue.Energy      -= value.Energy;
     netValue.Food        -= value.Food;
     netValue.Mana        -= value.Mana;
     netValue.Population  -= value.Population;
     netValue.Iron        -= value.Iron;
     netValue.Research    -= value.Research;
     netValue.EntityCount -= value.EntityCount;
 }
Beispiel #5
0
            public void WillNotSubtractCorrectCostsBecauseThereAreNoBuildingsToBuild()
            {
                var player = new Player {
                    TotalNetValue = new NetValue {
                        Cash = 2, Energy = 2, Food = 2, Iron = 2, Mana = 2
                    }
                };
                var costs = new NetValue {
                    Cash = 1, Energy = 1, Food = 1, Iron = 1, Mana = 1
                };

                Assert.IsFalse(player.SubtractBuildCosts(costs));
            }
Beispiel #6
0
            public void WillTestUser()
            {
                var totalNetValue = new NetValue {
                    EntityCount = 2, Cash = 3, Energy = 3, Food = 3, Iron = 3, Mana = 3
                };
                var buildCosts = new BuildCosts <object> {
                    Cash = 1, Energy = 1, Food = 1, Iron = 1, Mana = 1
                };

                var netValue = buildCosts.CalculateBuildCosts(totalNetValue, 0, 100);

                Assert.AreEqual(netValue.EntityCount, 2);
            }
Beispiel #7
0
            public void WillReturnOneBuildItem()
            {
                var totalNetValue = new NetValue {
                    Cash = 1, Energy = 1, Food = 1, Iron = 1, Mana = 1
                };
                var buildCosts = new BuildCosts <object> {
                    Cash = 1, Energy = 1, Food = 1, Iron = 1, Mana = 1
                };

                var netValue = buildCosts.CalculateBuildCosts(totalNetValue, 0, 1);

                Assert.AreEqual(netValue.EntityCount, 1);
            }
Beispiel #8
0
            public void WillTestBuildingCapacityMinimum()
            {
                var totalNetValue = new NetValue {
                    Cash = 2, Energy = 2, Food = 2, Iron = 2, Mana = 2
                };
                var buildCosts = new BuildCosts <object> {
                    Cash = 1, Energy = 1, Food = 1, Iron = 1, Mana = 1
                };

                var netValue = buildCosts.CalculateBuildCosts(totalNetValue, 0, 1);

                Assert.AreEqual(netValue.EntityCount, 1);
            }
Beispiel #9
0
        /// <summary>
        /// Builds buildings on a planet for a player
        /// </summary>
        /// <param name="player">
        /// The player.
        /// </param>
        /// <param name="costs">
        /// The costs buildings to build.
        /// </param>
        /// <returns>
        /// True if the buildings were successfully built.
        /// </returns>
        public static bool SubtractBuildCosts(this Player player, NetValue costs)
        {
            if (costs.EntityCount == 0)
            {
                return(false);
            }

            player.TotalNetValue.Subtract(costs);

            // NOTE - add the buildings back to the player since the Subtract function deletes them
            player.TotalNetValue.EntityCount += costs.EntityCount;

            return(true);
        }
Beispiel #10
0
            public void WillTestBuildingCapacityMaximum()
            {
                var totalNetValue = new NetValue {
                    Cash = 3, Energy = 3, Food = 3, Iron = 3, Mana = 3
                };
                var buildCosts = new BuildCosts <object> {
                    Cash = 1, Energy = 1, Food = 1, Iron = 1, Mana = 1
                };

                var netValue = buildCosts.CalculateBuildCosts(totalNetValue, 0, 1);

                // 2 here because of the building capacity and overbuild cost (x2)
                Assert.AreEqual(netValue.EntityCount, 2);
            }
Beispiel #11
0
            public void WillBuildBuildingTest()
            {
                var scheduler      = new Mock <IScheduler>();
                var spaceScheduler = new SpaceScheduler(scheduler.Object);

                var player = new Player
                {
                    ID            = 1,
                    TotalNetValue = new NetValue
                    {
                        Cash   = 1,
                        Energy = 1,
                        Food   = 1,
                        Iron   = 1,
                        Mana   = 1
                    }
                };
                var planet = new Planet {
                    ID = 1, BuildingCapacity = 1
                };
                var buildCosts = new BuildingCosts
                {
                    Type = default(BuildingType), Cash = 1, Energy = 1, Food = 1, Iron = 1, Mana = 1, Time = 1
                };

                var costs = new NetValue
                {
                    EntityCount = 1,
                    Cash        = 1,
                    Energy      = 1,
                    Food        = 1,
                    Iron        = 1,
                    Mana        = 1
                };

                Assert.IsTrue(spaceScheduler.BuildBuildings(player, planet, buildCosts, costs, default(BuildingType)));

                // assert that this succesfully removed from player's values.
                var totalNetValue = player.TotalNetValue;

                Assert.Less(totalNetValue.Cash, 1);
                Assert.Less(totalNetValue.Energy, 1);
                Assert.Less(totalNetValue.Food, 1);
                Assert.Less(totalNetValue.Iron, 1);
                Assert.Less(totalNetValue.Mana, 1);
            }
Beispiel #12
0
            public void WillSubtractCorrectCosts()
            {
                var player = new Player
                {
                    TotalNetValue = new NetValue {
                        Cash = 2, Energy = 2, Food = 2, Iron = 2, Mana = 2
                    }
                };
                var costs = new NetValue {
                    EntityCount = 2, Cash = 2, Energy = 2, Food = 2, Iron = 2, Mana = 2
                };

                player.SubtractBuildCosts(costs);

                var totalNetValue = player.TotalNetValue;

                Assert.AreEqual(totalNetValue.Cash, 0);
                Assert.AreEqual(totalNetValue.Energy, 0);
                Assert.AreEqual(totalNetValue.Food, 0);
                Assert.AreEqual(totalNetValue.Iron, 0);
                Assert.AreEqual(totalNetValue.Mana, 0);
            }
Beispiel #13
0
 /// <summary>
 /// Print a NetValue
 /// </summary>
 /// <param name="value">
 /// The value.
 /// </param>
 public static void Print(this NetValue value)
 {
     Console.WriteLine(
         string.Format(
             "{0,-15}    {1,-15}    {2,-15}    {3,-15}    {4,-15}    {5,-15}    {6,-15}",
             "Cash",
             "Food",
             "Iron",
             "Octarine",
             "Endurium",
             "Networth",
             "Attack"));
     Console.WriteLine(
         string.Format(
             "{0,-15}    {1,-15}    {2,-15}    {3,-15}    {4,-15}    {5,-15}    {6,-15}",
             value.Cash,
             value.Food,
             value.Iron,
             0,
             value.Energy,
             value.Networth,
             "100%"));
 }
Beispiel #14
0
            public void WillSubtractNetValue()
            {
                var nv1 = new NetValue
                {
                    Cash        = 1,
                    Energy      = 1,
                    Food        = 1,
                    Iron        = 1,
                    Mana        = 1,
                    Population  = 1,
                    Research    = 1,
                    EntityCount = 1
                };

                var nv2 = new NetValue
                {
                    Cash        = 1,
                    Energy      = 1,
                    Food        = 1,
                    Iron        = 1,
                    Mana        = 1,
                    Population  = 1,
                    Research    = 1,
                    EntityCount = 1
                };

                nv1.Subtract(nv2);

                Assert.AreEqual(nv1.Cash, 0, "Cash");
                Assert.AreEqual(nv1.Energy, 0, "Energy");
                Assert.AreEqual(nv1.Food, 0, "Food");
                Assert.AreEqual(nv1.Iron, 0, "Iron");
                Assert.AreEqual(nv1.Mana, 0, "Mana");
                Assert.AreEqual(nv1.Population, 0, "Population");
                Assert.AreEqual(nv1.Research, 0, "Research");
                Assert.AreEqual(nv1.EntityCount, 0, "EntityCount");
            }
Beispiel #15
0
        /// <summary>
        /// Used to schedule buildings to be built.
        /// </summary>
        /// <param name="player">
        /// The player who is building.
        /// </param>
        /// <param name="planet">
        /// The planet to build on.
        /// </param>
        /// <param name="buildingCosts">
        /// The building costs for a particular building type.
        /// </param>
        /// <param name="costs">
        /// The costs of building.
        /// </param>
        /// <param name="type">
        /// The type of building to build.
        /// </param>
        /// <returns>
        /// True if the build was added to the queue.
        /// </returns>
        public bool BuildBuildings(Player player, Planet planet, BuildCosts <BuildingType> buildingCosts, NetValue costs, BuildingType type)
        {
            if (this.scheduler == null)
            {
                Trace.WriteLine("this.scheduler is null", "SpaceScheduler.SubtractBuildCosts");
                return(false);
            }

            if (buildingCosts == null)
            {
                Trace.WriteLine("buildCosts is null", "SpaceScheduler.SubtractBuildCosts");
                return(false);
            }

            // Needs to subtract the cost here. return false if the player tries to build too many buildings.
            var totalCosts = buildingCosts.CalculateBuildCosts(costs, planet.TotalBuildings, planet.BuildingCapacity);

            player.TotalNetValue.Subtract(totalCosts);

            var jobSetup = new JobSetup <BuildBuildingsJob>(this.scheduler);

            jobSetup.Set(bbj => bbj.PlanetID, planet.ID);
            jobSetup.Set(bbj => bbj.BuildingType, type);
            jobSetup.Set(bbj => bbj.BuildingCount, totalCosts.EntityCount);

            jobSetup.Run(DateTimeOffset.UtcNow.AddMilliseconds(buildingCosts.Time));

            return(true);
        }
Beispiel #16
0
        /// <summary>
        /// Builds buildings on a planet.
        /// </summary>
        /// <param name="planet">
        /// The planet.
        /// </param>
        /// <param name="player">
        /// The player.
        /// </param>
        /// <param name="spaceScheduler">
        /// The space Scheduler.
        /// </param>
        private static void BuildBuildings(Planet planet, Player player, ISpaceScheduler spaceScheduler)
        {
            do
            {
                Console.WriteLine("\r\n******");
                Console.WriteLine("Select building type");

                var buildingTypes = SystemTypes.EnumToList <BuildingType>();
                if (buildingTypes == null)
                {
                    return;
                }

                for (var i = 0; i < buildingTypes.Count(); i += 1)
                {
                    Console.WriteLine("{0} - {1}", i, buildingTypes.ElementAt(i));
                }

                Console.WriteLine("-1 -- Finished building");
                Console.WriteLine("******");

                var input = Console.ReadLine();

                int buildingType;
                if (!int.TryParse(input, out buildingType))
                {
                    continue;
                }

                if (buildingType == -1)
                {
                    break;
                }

                // The maximum buildings for the BuildingType.
                var buildType =
                    player.Galaxy.GalaxySettings.BuildingCosts.FirstOrDefault(
                        bc => bc.Type == (BuildingType)buildingType);

                // Get the total value and set the number of buildings to zero.
                var totalValue = new NetValue();
                totalValue.Add(player.TotalNetValue);
                totalValue.EntityCount = 0;

                var buildingCosts = buildType.CalculateBuildCosts(totalValue, planet.TotalBuildings, planet.BuildingCapacity);

                Console.WriteLine("Number of buildings - (max is {0}):", buildingCosts.EntityCount);

                input = Console.ReadLine();
                int buildingCount;
                if (!int.TryParse(input, out buildingCount))
                {
                    Console.WriteLine("Invalid input.");
                    continue;
                }


                // build buildings on planet.
                if (!spaceScheduler.BuildBuildings(player, planet, buildType, buildingCosts, buildType.Type))
                {
                    Console.WriteLine(
                        "Cannot build {0} buildings.  The maximum is {1}", buildingCount, buildingCosts.EntityCount);
                }
            }while (true);
        }
Beispiel #17
0
        /// <summary>
        /// Calculates the maximum number of buildings a player can build on a particular planet.
        /// </summary>
        /// <typeparam name="TType">
        /// The type of build cost.
        /// </typeparam>
        /// <param name="buildingCost">
        /// The building Cost.
        /// </param>
        /// <param name="totalNetValue">
        /// The total Net Value.
        /// </param>
        /// <param name="planetTotalBuildings">
        /// The total number of buildings on the planet.  This is at the time of the start of the build.
        /// </param>
        /// <param name="planetBuildCapacity">
        /// The number of buildings the planet can natively hold.
        /// </param>
        /// <returns>
        /// The maximum buildings and the cost.
        /// </returns>
        public static NetValue CalculateBuildCosts <TType>(this BuildCosts <TType> buildingCost, NetValue totalNetValue, int planetTotalBuildings, int planetBuildCapacity)
        {
            int output         = planetTotalBuildings;
            int maximumTobuild = totalNetValue.EntityCount == 0 ? int.MaxValue : totalNetValue.EntityCount + output;

            // TODO -- include empire size in calculations
            var calculationArray = new[]
            {
                totalNetValue.Cash, buildingCost.Cash, totalNetValue.Energy, buildingCost.Energy,
                totalNetValue.Food, buildingCost.Food, totalNetValue.Iron, buildingCost.Iron,
                totalNetValue.Mana, buildingCost.Mana
            };

            var maxFound = false;
            var totals   = new double[calculationArray.Length / 2];

            // Make sure that we actually gave the item building costs or this could be inifinity
            if (buildingCost.Cash > 0 || buildingCost.Energy > 0 || buildingCost.Food > 0 || buildingCost.Iron > 0 || buildingCost.Mana > 0)
            {
                do
                {
                    output++;

                    // This will tell us to kick out of the loop if the totals never get incremented.
                    for (var i = 0; i < calculationArray.Length; i += 2)
                    {
                        // Find the total cost for the resource taking into account the planet building max.
                        var costForItem = calculationArray[i + 1] * Math.Max(1, (output / planetBuildCapacity));

                        // Make sure that one of the resource values hasn't gone past the maximum cash value.
                        if (totals[i / 2] + costForItem <= calculationArray[i])
                        {
                            totals[i / 2] += costForItem;
                            continue;
                        }

                        maxFound = true;
                        break;
                    }
                }while (!maxFound && output <= maximumTobuild);
            }

            return(new NetValue
            {
                // Always return 0 or greater - subtract 1 for the do-while
                EntityCount = Math.Max(0, output - planetTotalBuildings - 1),
                Cash = totals[0],
                Energy = totals[1],
                Food = totals[2],
                Iron = totals[3],
                Mana = totals[4]
            });
        }