protected override CommandQueueElement RunInternal()
        {
            var             resp      = Client.IssueRequest(Client.RequestBuilder.GetPage(PageType.Fleet, PlanetId));
            PlanetResources resources = resp.GetParsedSingle <PlanetResources>();
            DetectedShip    cargo     = resp.GetParsed <DetectedShip>().FirstOrDefault(s => s.Ship == ShipType.LargeCargo);

            FleetComposition fleet = FleetComposition.ToTransport(resources.Resources);

            // if on a moon, leave 10% of deuterium, or else no other ship will be able to travel
            if (resp.GetParsedSingle <OgamePageInfo>().PlanetCoord.Type == CoordinateType.Moon)
            {
                fleet.Resources.Deuterium = (int)(fleet.Resources.Deuterium * 0.9f);
            }
            int needed    = fleet.Ships[ShipType.LargeCargo];
            int available = cargo?.Count ?? 0;

            if (needed > available)
            {
                Logger.Instance.Log(LogLevel.Error, $"Not enough Large Cargos on planet: needed {needed}, only {available} available.");
                return(null);
            }

            SendFleetCommand sendFleet = new SendFleetCommand()
            {
                Mission     = UseDeployment ? MissionType.Deployment : MissionType.Transport,
                Speed       = Speed,
                PlanetId    = PlanetId,
                Destination = Destination,
                Fleet       = fleet
            };

            sendFleet.Run();
            return(null);
        }
Beispiel #2
0
 public void UpdateData(PlanetResources resource)
 {
     lavaAmount  = resource.lava;
     waterAmount = resource.water;
     oresAmount  = resource.ores;
     fuelAmount  = resource.fuel;
     lifeAmount  = resource.life;
 }
Beispiel #3
0
    private static PlanetResources GenerateResources(PlanetType planetType)
    {
        PlanetResources generatedResources = new PlanetResources();
        PlanetResources resources          = planetType.Resources;

        generatedResources.Oxygen = GenerateResource(resources.Oxygen);
        generatedResources.Food   = GenerateResource(resources.Food);
        generatedResources.Fuel   = GenerateResource(resources.Fuel);
        return(generatedResources);
    }
Beispiel #4
0
    // Use this for initialization
    void Start()
    {
        // Create limits object
        p_limits = new PlanetLimits();

        // Set planet resources object to the one createdin editor
        p_resources = GameObject.Find("Resources").GetComponent(typeof(PlanetResources)) as PlanetResources;

        // Create the initial system
        CreateSystem();
    }
Beispiel #5
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);
            }
        }
Beispiel #6
0
        public override void Run(List <DataObject> result)
        {
            OgamePageInfo current = result.OfType <OgamePageInfo>().FirstOrDefault();

            if (current == null)
            {
                return;
            }

            var playerPlanets = result.OfType <PlanetListItem>();

            PlanetResources resources = result.OfType <PlanetResources>().FirstOrDefault();
            var             buildings = result.OfType <DetectedBuilding>().ToDictionary(s => s.Building, s => s.Level);
            var             ships     = result.OfType <DetectedShip>().ToDictionary(s => s.Ship, s => s.Count);
            var             defences  = result.OfType <DetectedDefence>().ToDictionary(s => s.Building, s => s.Count);
            var             research  = result.OfType <DetectedResearch>().ToDictionary(s => s.Research, s => s.Level);

            using (BotDb db = new BotDb())
            {
                long[] locIds = playerPlanets.Select(s => s.Coordinate.Id).ToArray();
                Dictionary <long, Planet> existing = db.Planets.Where(s => locIds.Contains(s.LocationId)).ToDictionary(s => s.LocationId);

                if (!_isPlayerSeeded)
                {
                    if (!db.Players.Where(s => s.PlayerId == current.PlayerId).Any())
                    {
                        db.Players.Add(new Player()
                        {
                            PlayerId = current.PlayerId,
                            Name     = current.PlayerName,
                            Status   = PlayerStatus.None
                        });
                        db.SaveChanges();
                    }
                    _isPlayerSeeded = true;
                }


                foreach (var playerPlanet in playerPlanets)
                {
                    Planet item;

                    if (!existing.TryGetValue(playerPlanet.Coordinate.Id, out item))
                    {
                        item = new Planet()
                        {
                            Coordinate = playerPlanet.Coordinate,
                            Name       = playerPlanet.Name,
                            PlanetId   = playerPlanet.Id,
                            PlayerId   = current.PlayerId
                        };
                        db.Planets.Add(item);
                    }

                    if (resources.Coordinate.Id == playerPlanet.Coordinate.Id)
                    {
                        item.Resources = resources.Resources;
                    }

                    if (current.PlanetId == playerPlanet.Id)
                    {
                        if (ships.Count > 0)
                        {
                            item.Ships.FromPartialResult(ships);
                            item.Ships.LastUpdated = DateTimeOffset.UtcNow;
                        }
                        else if (defences.Count > 0)
                        {
                            item.Defences.FromPartialResult(defences);
                            item.Defences.LastUpdated = DateTimeOffset.UtcNow;
                        }
                        else if (buildings.Count > 0)
                        {
                            item.Buildings.FromPartialResult(buildings);
                            item.Buildings.LastUpdated = DateTimeOffset.UtcNow;
                        }
                        else if (research.Count > 0)
                        {
                            item.Player.Research.FromPartialResult(research);
                            item.Player.Research.LastUpdated = DateTimeOffset.UtcNow;
                        }
                    }
                    item.Name     = playerPlanet.Name;
                    item.PlayerId = current.PlayerId;
                    item.PlanetId = playerPlanet.Id;
                    db.SaveChanges();
                }
            }

            List <MessageBase> messages = result.OfType <MessageBase>().ToList();

            if (!messages.Any())
            {
                return;
            }

            int[] messageIds = messages.Select(s => s.MessageId).ToArray();

            using (BotDb db = new BotDb())
            {
                HashSet <int> existing = db.Messages.Where(s => messageIds.Contains(s.MessageId)).Select(s => s.MessageId).ToHashset();

                foreach (MessageBase message in messages.Where(s => !existing.Contains(s.MessageId)))
                {
                    db.Messages.Add(new Message
                    {
                        MessageId = message.MessageId,
                        Body      = message,
                        TabType   = message.TabType
                    });
                }

                db.SaveChanges();
            }
        }
Beispiel #7
0
 public override void SetUpPlanet(PlanetInfo p)
 {
     base.SetUpPlanet(p);
     pResources = GetComponent <PlanetResources>();
     pResources.Setup(p);
 }
        public override IEnumerable <DataObject> ProcessInternal(ClientBase client, ResponseContainer container)
        {
            HtmlDocument doc     = container.ResponseHtml.Value;
            HtmlNode     infoBox = doc.DocumentNode.SelectSingleNode("//div[@id='info']");

            if (infoBox == null)
            {
                // Requirement for this parser
                yield break;
            }

            Coordinate coord = container.GetParsedSingle <OgamePageInfo>().PlanetCoord;

            string metBoxHtml = infoBox.SelectSingleNode(".//li[@id='metal_box']").GetAttributeValue("title", null).Split('|').Last();
            string cryBoxHtml = infoBox.SelectSingleNode(".//li[@id='crystal_box']").GetAttributeValue("title", null).Split('|').Last();
            string deuBoxHtml = infoBox.SelectSingleNode(".//li[@id='deuterium_box']").GetAttributeValue("title", null).Split('|').Last();
            string eneBoxHtml = infoBox.SelectSingleNode(".//li[@id='energy_box']").GetAttributeValue("title", null).Split('|').Last();

            HtmlDocument metBox = new HtmlDocument();

            metBox.LoadHtml(WebUtility.HtmlDecode(metBoxHtml));
            HtmlDocument cryBox = new HtmlDocument();

            cryBox.LoadHtml(WebUtility.HtmlDecode(cryBoxHtml));
            HtmlDocument deuBox = new HtmlDocument();

            deuBox.LoadHtml(WebUtility.HtmlDecode(deuBoxHtml));
            HtmlDocument eneBox = new HtmlDocument();

            eneBox.LoadHtml(WebUtility.HtmlDecode(eneBoxHtml));

            HtmlNodeCollection metTds = metBox.DocumentNode.SelectNodes("//td");
            HtmlNodeCollection cryTds = cryBox.DocumentNode.SelectNodes("//td");
            HtmlNodeCollection deuTds = deuBox.DocumentNode.SelectNodes("//td");
            HtmlNodeCollection eneTds = eneBox.DocumentNode.SelectNodes("//td");

            PlanetResources result = new PlanetResources();

            result.Coordinate = coord;
            result.Resources  = new Resources
            {
                Metal     = int.Parse(metTds[0].InnerText, NumberStyles.AllowThousands | NumberStyles.Integer, client.ServerCulture),
                Crystal   = int.Parse(cryTds[0].InnerText, NumberStyles.AllowThousands | NumberStyles.Integer, client.ServerCulture),
                Deuterium = int.Parse(deuTds[0].InnerText, NumberStyles.AllowThousands | NumberStyles.Integer, client.ServerCulture),
                Energy    = int.Parse(eneTds[0].InnerText, NumberStyles.AllowThousands | NumberStyles.Integer, client.ServerCulture)
            };

            result.Capacities = new Resources
            {
                Metal     = int.Parse(metTds[1].InnerText, NumberStyles.AllowThousands | NumberStyles.Integer, client.ServerCulture),
                Crystal   = int.Parse(cryTds[1].InnerText, NumberStyles.AllowThousands | NumberStyles.Integer, client.ServerCulture),
                Deuterium = int.Parse(deuTds[1].InnerText, NumberStyles.AllowThousands | NumberStyles.Integer, client.ServerCulture),
                Energy    = int.Parse(eneTds[1].InnerText, NumberStyles.AllowThousands | NumberStyles.Integer, client.ServerCulture)
            };

            result.ProductionPerHour = new Resources
            {
                Metal     = int.Parse(metTds[2].InnerText, NumberStyles.AllowThousands | NumberStyles.Integer, client.ServerCulture),
                Crystal   = int.Parse(cryTds[2].InnerText, NumberStyles.AllowThousands | NumberStyles.Integer, client.ServerCulture),
                Deuterium = int.Parse(deuTds[2].InnerText, NumberStyles.AllowThousands | NumberStyles.Integer, client.ServerCulture),
                Energy    = int.Parse(eneTds[2].InnerText, NumberStyles.AllowThousands | NumberStyles.Integer, client.ServerCulture)
            };

            yield return(result);
        }
Beispiel #9
0
 private void AddResources(PlanetResources planetResource)
 {
     this.AddResource(this.Oxygen, planetResource.Oxygen);
     this.AddResource(this.Food, planetResource.Food);
     this.AddResource(this.Fuel, planetResource.Fuel);
 }