Esempio n. 1
0
        protected override CommandQueueElement RunInternal()
        {
            using (Client.EnterPlanetExclusive(this))
            {
                // Make the initial request to get a token
                HttpRequestMessage req = Client.RequestBuilder.GetPage(PageType.Resources, PlanetId);
                ResponseContainer  res = AssistedIssue(req);

                OgamePageInfo info  = res.GetParsedSingle <OgamePageInfo>();
                string        token = info.OrderToken;

                // validate resources
                int currentBuildingLevel = res.GetParsed <DetectedBuilding>()
                                           .Where(b => b.Building == BuildingToBuild)
                                           .Select(b => b.Level)
                                           .FirstOrDefault();

                PlanetResources resources = res.GetParsedSingle <PlanetResources>();

                Resources cost = Building.Get(BuildingToBuild).Cost.ForLevel(currentBuildingLevel + 1);
                DetectedOngoingConstruction underConstruction = res.GetParsedSingle <DetectedOngoingConstruction>(false);

                if (underConstruction != null)
                {
                    Logger.Instance.Log(LogLevel.Debug, $"Building {underConstruction.Building} already under construction on planet {info.PlanetName}, will finish at {underConstruction.FinishingAt}");
                    return(Reschedule(underConstruction.FinishingAt));
                }
                else if (cost.Metal > resources.Resources.Metal || cost.Crystal > resources.Resources.Crystal || cost.Deuterium > resources.Resources.Deuterium)
                {
                    double maxTimeToGetEnoughResources = new double[] {
                        (cost.Metal - resources.Resources.Metal) / (resources.ProductionPerHour.Metal / 3600.0),
                        (cost.Crystal - resources.Resources.Crystal) / (resources.ProductionPerHour.Deuterium / 3600.0),
                        (cost.Deuterium - resources.Resources.Deuterium) / (resources.ProductionPerHour.Deuterium / 3600.0)
                    }.Max();
                    Logger.Instance.Log(LogLevel.Debug, $"Not enough resources! It would cost {cost} to build {BuildingToBuild} level {currentBuildingLevel + 1}, planet {info.PlanetName} only has {resources.Resources}; will have enough in {maxTimeToGetEnoughResources} seconds");
                    return(Reschedule(DateTimeOffset.Now.AddSeconds(maxTimeToGetEnoughResources)));
                }


                HttpRequestMessage buildReq = Client.RequestBuilder.GetBuildBuildingRequest(BuildingToBuild, token);
                AssistedIssue(buildReq);

                return(null);
            }
        }
Esempio n. 2
0
        public override IEnumerable <DataObject> ProcessInternal(ClientBase client, ResponseContainer container)
        {
            HtmlDocument       doc          = container.ResponseHtml.Value;
            HtmlNodeCollection scriptBlocks = doc.DocumentNode.SelectNodes("//script[@type='text/javascript' and not(@src)]");

            var construction = new DetectedOngoingConstruction();
            var research     = new DetectedOngoingResearch();

            if (scriptBlocks != null)
            {
                foreach (HtmlNode block in scriptBlocks)
                {
                    var matches = ActivityCountdownCommanderRegex.Matches(block.InnerText);
                    if (matches.Count > 0)
                    {
                        foreach (Match match in matches)
                        {
                            string         type = match.Groups[1].Value;
                            int            remainingDuration = int.Parse(match.Groups[2].Value);
                            DateTimeOffset finishingAt       = DateTimeOffset.Now.AddSeconds(remainingDuration);

                            if (type == "research")
                            {
                                research.FinishingAt = finishingAt;
                            }
                            else
                            {
                                construction.FinishingAt = finishingAt;
                            }
                        }
                        break;
                    }
                }
            }
            HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//a[contains(@class, 'abortNow')]");

            if (nodes != null)
            {
                foreach (HtmlNode node in nodes)
                {
                    string onClick = node.GetAttributeValue("onclick", "");
                    var    match   = ActivityIdRegex.Match(onClick);
                    if (match.Success)
                    {
                        int id    = int.Parse(match.Groups[2].Value);
                        int level = int.Parse(match.Groups[3].Value);
                        // buildings
                        if (id < 100)
                        {
                            construction.Building = (BuildingType)id;
                            construction.Level    = level;
                            yield return(construction);
                        }
                        else if (id >= 100 && id <= 200)
                        {
                            research.Research = (ResearchType)id;
                            research.Level    = level;
                            yield return(research);
                        }
                    }
                }
            }
        }