Example #1
0
        /// <summary>
        /// Upgrades specified building for exactly one level. Will upgrade the lowest level building.
        /// </summary>
        /// <param name="acc">Account</param>
        /// <param name="vill">Village</param>
        /// <param name="building">Building to be upgraded by one</param>
        /// <param name="bottom">Whether to insert the building task on the bottom of the build list</param>
        /// <returns>Whether the method executed successfully</returns>
        internal static bool UpgradeBuildingForOneLvl(Account acc, Village vill, BuildingEnum building, bool bottom = true)
        {
            // We already have a build task
            if (!bottom && vill.Build.Tasks.FirstOrDefault()?.Building == building)
            {
                return(true);
            }
            if (bottom && vill.Build.Tasks.LastOrDefault()?.Building == building)
            {
                return(true);
            }

            var upgrade = vill.Build
                          .Buildings
                          .OrderBy(x => x.Level)
                          .FirstOrDefault(x => x.Type == building);

            // We don't have this building in the village yet
            if (upgrade == null)
            {
                return(AddBuildingTask(acc, vill, new BuildingTask()
                {
                    TaskType = BuildingType.General,
                    Building = building,
                    Level = 1,
                }, bottom));
            }

            var currentLvl = (int)upgrade.Level;

            RemoveFinishedCB(vill);
            currentLvl += vill.Build.CurrentlyBuilding.Count(x => x.Building == building);

            if (BuildingsData.MaxBuildingLevel(acc, building) == currentLvl)
            {
                // Building is on max level, construct new building if possible
                if (!BuildingsData.CanHaveMultipleBuildings(building))
                {
                    return(false);
                }

                return(AddBuildingTask(acc, vill, new BuildingTask()
                {
                    TaskType = BuildingType.General,
                    Building = building,
                    Level = 1,
                }, bottom));
            }
            else // Upgrade the defined building
            {
                return(AddBuildingTask(acc, vill, new BuildingTask()
                {
                    TaskType = BuildingType.General,
                    Building = building,
                    Level = currentLvl + 1,
                    BuildingId = upgrade.Id
                }, bottom));
            }
        }
Example #2
0
        /// <summary>
        /// Adds the building task to the village list of building tasks. Restarts BotTask UpgradeBuilding if needed.
        /// </summary>
        /// <param name="acc">Account</param>
        /// <param name="vill">Village</param>
        /// <param name="task">BuildingTask to add</param>
        /// <param name="bottom">Whether to insert the BuildingTask on the bottom of the list</param>
        /// <returns>Whether the method completed successfully</returns>
        public static bool AddBuildingTask(Account acc, Village vill, BuildingTask task, bool bottom = true, bool restart = true)
        {
            if (vill == null)
            {
                return(false);
            }

            //check wall
            if (IsWall(task.Building))
            {
                var wall = BuildingsData.GetTribesWall(acc.AccInfo.Tribe);
                // check type
                if (task.Building != wall)
                {
                    task.Building = wall;
                }
                // check position
                if (task.BuildingId != 40)
                {
                    task.BuildingId = 40;
                }
            }
            // check rally point
            else if (task.Building == BuildingEnum.Site)
            {
                // check position
                if (task.BuildingId != 39)
                {
                    task.BuildingId = 39;
                }
            }
            // other building
            else if (task.BuildingId == null ||
                     vill.Build.Buildings.Any(x => x.Id == task.BuildingId &&
                                              x.Type != task.Building &&
                                              x.Type != BuildingEnum.Site))
            {
                //Check if bot has any space to build new buildings, otherwise return
                if (!FindBuildingId(vill, task))
                {
                    return(false);
                }
            }

            // checking multiple building
            // you need at least one at level 20 before building other
            if (BuildingsData.CanHaveMultipleBuildings(task.Building))
            {
                if (task.ConstructNew)
                {
                    // Highest level building
                    var highestLvl = vill.Build
                                     .Buildings
                                     .Where(x => x.Type == task.Building)
                                     .OrderByDescending(x => x.Level)
                                     .FirstOrDefault();

                    if (highestLvl != null &&
                        highestLvl.Level != BuildingsData.MaxBuildingLevel(acc, task.Building))
                    {
                        task.BuildingId = highestLvl.Id;
                    }
                }
            }
            else if (!IsResourceField(task.Building))
            {
                var buildings = vill.Build.Buildings.Where(x => x.Type == task.Building);
                if (buildings.Count() > 0)
                {
                    var id = buildings.First().Id;
                    if (id != task.BuildingId)
                    {
                        task.BuildingId = id;
                    }
                }
            }

            if (bottom)
            {
                vill.Build.Tasks.Add(task);
            }
            else
            {
                vill.Build.Tasks.Insert(0, task);
            }

            if (acc.Wb != null && restart)
            {
                ReStartBuilding(acc, vill);
            }
            return(true);
        }