コード例 #1
0
        public IActionResult Create()
        {
            List <Vehicle>  vehiclesList  = _vehicleRepository.GetAllVehicles().ToList();
            List <Worker>   workersList   = _workerRepository.GetAllWorker().ToList();
            List <Building> buildingsList = _buildingRepository.GetAllBuildings().ToList();

            var addTimetableVM = new AddTimetableVM(buildingsList, workersList, vehiclesList);


            return(View(addTimetableVM));
        }
コード例 #2
0
        public async Task <IActionResult> Buildings()
        {
            try
            {
                var model = await _buildingRepository.GetAllBuildings();

                if (model == null)
                {
                    ViewData["Error"] = "Unable to get buildings from database:";
                }
                else
                {
                    ViewData["Error"] = TempData["Error"];
                }

                model             = model.OrderBy(x => x.Name);
                ViewData["Error"] = TempData["Error"];
                return(View("~/Views/KnowledgeBase/BuildingsList.cshtml", model));
            }
            catch (Exception exc)
            {
                ViewData["Error"] = $"Unable to get buildings from database: {TempData["Error"]}";
                _logger.LogError(LoggingEvents.GetItemNotFound, exc, $"Unable to find buildings: {exc.Message}");
            }

            return(View("~/Views/KnowledgeBase/BuildingsList.cshtml"));
        }
コード例 #3
0
        //[Route("[controller]/[action]/{id}")]
        public async Task <IActionResult> Upsert(string id)
        {
            BuildingPlanViewModel model = null;

            try
            {
                var allBuildings = (await _buildingRepository.GetAllBuildings()).ToList();

                BuildingPlanModel plan;
                IEnumerable <BuildingPlanStepViewModel> queue = null;
                if (string.IsNullOrEmpty(id))
                {
                    plan = new BuildingPlanModel
                    {
                        BotUserName = User.Identity.Name
                    };
                }
                else
                {
                    plan = await _buildingPlanRepository.GetBuildingPlan(id);

                    queue = plan.BuildingSteps.Select(x => new BuildingPlanStepViewModel
                    {
                        Order    = x.Order,
                        Level    = x.Level,
                        Building = allBuildings.FirstOrDefault(y => y.BuildingId == x.BuildingId)
                    });
                }

                model = _mapper.Map <BuildingPlanViewModel>(plan);
                model.BuildingSteps   = queue?.ToList();
                ViewData["buildings"] = allBuildings;
            }
            catch (Exception exc)
            {
                _logger.LogError(LoggingEvents.GetItemException, exc, exc.Message);
                ViewBag.ErrorMessage = "Unable to find Building Plan.";
            }

            return(View(model));
        }
コード例 #4
0
        public async Task <(bool HasMoreToBuild, IEnumerable <BuildAction> Actions)> GetBuildActions(Village village, IEnumerable <BuildingModel> allBuildings = null)
        {
            var result               = new List <BuildAction>();
            var buildings            = (allBuildings ?? await _buildingRepository.GetAllBuildings()).ToList();
            var dorf1BuildingToBuild = GetResourceFieldToBuildNext(village, buildings);

            if (dorf1BuildingToBuild != null)
            {
                result.Add(new BuildAction
                {
                    BuildingId   = dorf1BuildingToBuild.BuildingId,
                    Action       = GameActionType.BUILD,
                    BuildingSlot = dorf1BuildingToBuild.Id,
                    Village      = village
                });
            }

            BuildingPlanModel plan = null;

            if (!string.IsNullOrEmpty(village.BuildingPlanId))
            {
                plan = await _buildingPlanRepository.GetBuildingPlan(village.BuildingPlanId);
            }
            if (plan == null)
            {
                plan = (await _buildingPlanRepository.GetBuildingPlans(BuildingPlanRepository.DefaultPlanUserName)).FirstOrDefault();
            }

            var hasMoreBuildingsToBuild = TryGetNextBuildingInVillageToBuildFromBuildingPlan(village, buildings, plan, out var dorf2Building);

            if (dorf2Building != null)
            {
                var slot2 = village.BuildingSlots
                            .Where(x => x.BuildingId == dorf2Building.BuildingId)
                            .OrderBy(x => x.Level)
                            .FirstOrDefault();

                result.Add(new BuildAction
                {
                    BuildingId   = dorf2Building.BuildingId,
                    Action       = GameActionType.BUILD,
                    BuildingSlot = string.IsNullOrEmpty(slot2?.Id) ? dorf2Building.PrefferedBuildingSlot : slot2.Id,
                    Village      = village
                });
            }

            return(hasMoreBuildingsToBuild || dorf1BuildingToBuild != null, result);
        }
コード例 #5
0
 public List <Building> GetAllBuildings()
 {
     return(repository.GetAllBuildings());
 }
コード例 #6
0
        public async Task <IEnumerable <BuildingResource> > GetAllBuildings()
        {
            var buildings = await repository.GetAllBuildings();

            return(mapper.Map <IEnumerable <Building>, IEnumerable <BuildingResource> >(buildings));
        }
コード例 #7
0
        protected override async Task ExecuteJob(JobExecutionData jobExecutionData)
        {
            var allVillages = await _villageRepository.GetVillages(_travianUser.UserName);

            var buildVillages = allVillages
                                .Where(x => x.IsBuildFeatureOn)
                                .ToList();

            if (!buildVillages.Any())
            {
                await _bot.SendTextMessageAsync(_botUser.ChatId, $"No villages with build feature found for player {_travianUser.UserName}");

                return;
            }

            foreach (var village in buildVillages.Where(x => x.IsWaitingForResources))
            {
                // for villages waiting for resources next execution time is the time when resources must be delivered
                // if next execution time passed resources are expected to be delivered
                village.IsWaitingForResources = village.NextBuildingPlanExecutionTime.HasValue && village.NextBuildingPlanExecutionTime.Value > DateTimeOffset.Now;
            }

            BaseScenarioResult infos = null;

            try
            {
                var updateActions = buildVillages
                                    .Select(x => new GameAction {
                    Village = _mapper.Map <Village>(x)
                });
                infos = await _gameplayClient.ExecuteActions(_travianUser, updateActions);

                var update = infos.Villages.Select(x => _mapper.Map <VillageModel>(x));
                await _villageRepository.UpdateWatchInfo(update);
            }
            catch (Exception exc)
            {
                _logger.LogError(exc, exc.Message);
                await _bot.SendTextMessageAsync(_botUser.ChatId, $"Unable to update villages info for player {_travianUser.UserName}");

                return;
            }

            if (!infos?.Villages?.Any() ?? true)
            {
                await _bot.SendTextMessageAsync(_botUser.ChatId, $"No info was found for villages of {_travianUser.UserName}");

                return;
            }

            // suits only for rome or for player with travian plus. TODO: fix for other tribes
            var freeVillages = infos.Villages
                               .Where(x => x.CanBuild)
                               .ToList();

            var busyVillages = infos.Villages
                               .Where(x => !x.CanBuild)
                               .ToList();

            if (busyVillages?.Any() ?? false)
            {
                foreach (var village in busyVillages)
                {
                    // if village has less than 10% filled warehouse we should send there more resources
                    var limit = village.Warehourse * 0.1;
                    if (!village.Dorf1BuildTimeLeft.HasValue || village.Resources.Lumber < limit || village.Resources.Clay < limit ||
                        village.Resources.Iron < limit || village.Resources.Crop < village.Granary * 0.1)
                    {
                        await SendResources(allVillages.ToList(), village);
                    }
                }
            }

            var result = new BaseScenarioResult();

            if (!freeVillages?.Any() ?? true)
            {
                await _bot.SendTextMessageAsync(_botUser.ChatId, $"All villages with building feature of player {_travianUser.UserName} are busy.");

                await _bot.SendTextMessageAsync(_botUser.ChatId, $"Calculating next build command execution time for player {_travianUser.UserName}.");
            }
            else
            {
                var i            = 0;
                var allBuidlgins = await _buildingRepository.GetAllBuildings();

                while ((freeVillages?.Any() ?? false) && i++ < buildVillages.Count)
                {
                    var allActions = new List <BuildAction>();
                    foreach (var village in freeVillages)
                    {
                        var(hasMoreToBuild, actions) = await _actionProvider.GetBuildActions(village, allBuidlgins);

                        if (!hasMoreToBuild)
                        {
                            await _bot.SendTextMessageAsync(_botUser.ChatId, $"No actions needed for village {village.Name} of player {village.PlayerName}. Swithching off the Build Feature.");

                            try
                            {
                                var villageToUpdate = await _villageRepository.GetVillage(village.CoordinateX, village.CoordinateY);

                                villageToUpdate.IsBuildFeatureOn = false;
                                await _villageRepository.UpdateInfos(new List <VillageModel> {
                                    villageToUpdate
                                });
                            }
                            catch (Exception exc)
                            {
                                _logger.LogError(exc, exc.Message);
                                await _bot.SendTextMessageAsync(_botUser.ChatId, $"Unable to update {village.Name} of player {village.PlayerName}. Check Logs.");
                            }
                        }
                        else
                        {
                            allActions.AddRange(actions);
                        }
                    }

                    if (allActions.Any())
                    {
                        result = await _gameplayClient.ExecuteActions(_travianUser, allActions);

                        var buildErrors = result.Errors
                                          .Where(x => x is BuildScenarioError)
                                          .Cast <BuildScenarioError>();
                        if (buildErrors.Any())
                        {
                            var notEnoughRes = buildErrors
                                               .Where(x => (x as BuildScenarioError).BuildErrorType == BuildErrorType.NotEnoughResources);
                            if (notEnoughRes.Any())
                            {
                                foreach (var n in notEnoughRes)
                                {
                                    var updatedVillage = result.Villages.First(x => x.CoordinateX == n.Village.CoordinateX && x.CoordinateY == n.Village.CoordinateY);
                                    await SendResources(allVillages.ToList(), updatedVillage);
                                }
                            }

                            var noSpaceInQueue = buildErrors
                                                 .Where(x => (x as BuildScenarioError).BuildErrorType == BuildErrorType.NoSpaceInQueue);
                            if (noSpaceInQueue.Any())
                            {
                                foreach (var n in noSpaceInQueue)
                                {
                                    var errorVillage = result.Villages.First(x => x.CoordinateX == n.Village.CoordinateX && x.CoordinateY == n.Village.CoordinateY);
                                    errorVillage.CanBuild = false;
                                }
                            }
                        }

                        freeVillages = result.Villages.Where(x => x.CanBuild).ToList();
                    }
                }
            }

            var      max     = infos.Villages.Count > result.Villages.Count ? infos.Villages.Count : result.Villages.Count;
            TimeSpan nextRun = TimeSpan.MaxValue;

            for (var j = 0; j < max; j++)
            {
                if (j < infos.Villages.Count)
                {
                    if (infos.Villages[j].Dorf1BuildTimeLeft.HasValue &&
                        infos.Villages[j].Dorf1BuildTimeLeft.Value > TimeSpan.Zero &&
                        infos.Villages[j].Dorf1BuildTimeLeft.Value < nextRun)
                    {
                        nextRun = infos.Villages[j].Dorf1BuildTimeLeft.Value;
                    }
                }
                if (j < result.Villages.Count)
                {
                    if (result.Villages[j].Dorf1BuildTimeLeft.HasValue &&
                        result.Villages[j].Dorf1BuildTimeLeft.Value > TimeSpan.Zero &&
                        result.Villages[j].Dorf1BuildTimeLeft.Value < nextRun)
                    {
                        nextRun = result.Villages[j].Dorf1BuildTimeLeft.Value;
                    }
                }
            }

            if (nextRun < TimeSpan.MaxValue)
            {
                var waitingForResources = buildVillages
                                          ?.Where(x => x.IsWaitingForResources && x.NextBuildingPlanExecutionTime.HasValue)
                                          ?.Select(x => x.NextBuildingPlanExecutionTime.Value)
                                          ?.ToList();

                var nextRunDateTime          = DateTimeOffset.Now + nextRun;
                var nearestResourcesDelivery = (waitingForResources?.Any() ?? false) ? waitingForResources.Min() : DateTimeOffset.MaxValue;

                var cmd = _commandFactory.GetQueueableCommand(nameof(BuildCommand), _botUser.ChatId);
                cmd.Start = (nextRunDateTime < nearestResourcesDelivery ? nextRunDateTime : nearestResourcesDelivery) + TimeSpan.FromSeconds(2);
                await cmd.Execute();
            }
            else
            {
                await _bot.SendTextMessageAsync(_botUser.ChatId, $"Unable to calculate next build command execution time for player {_travianUser.UserName}");
            }
        }
コード例 #8
0
 public IActionResult Index()
 {
     return(View(_buildingRepostiory.GetAllBuildings()));
 }