Skip to content

Commit

Permalink
added fleetsave command basic implementation (local system only)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinjakubowski committed Apr 21, 2017
1 parent 64cbfc2 commit afc8a9f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
68 changes: 68 additions & 0 deletions OgameBot/Engine/Commands/FleetSaveCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OgameBot.Db;
using OgameBot.Objects.Types;
using OgameBot.Engine.Parsing.Objects;
using OgameBot.Db.Parts;
using OgameBot.Objects;
using MoreLinq;
using OgameBot.Logging;

namespace OgameBot.Engine.Commands
{
public class FleetSaveCommand : CommandBase, IPlanetExclusiveOperation
{
public DateTimeOffset ReturnTime { get; set; }

public string Name => "Fleet save";
public string Progress { get; }

protected override CommandQueueElement RunInternal()
{
using (BotDb db = new BotDb())
using (Client.EnterPlanetExclusive(this))
{
var resp = Client.IssueRequest(Client.RequestBuilder.GetPage(PageType.Fleet, PlanetId));
var info = resp.GetParsedSingle<OgamePageInfo>();

PlayerResearch research = db.Players.Where(p => p.PlayerId == info.PlayerId).Select(p => p.Research).First();
FleetComposition fleet = FleetComposition.FromDetected(resp.GetParsed<DetectedShip>());
fleet.Resources = resp.GetParsedSingle<PlanetResources>().Resources;

Coordinate here = info.PlanetCoord;
TimeSpan oneWayTrip = TimeSpan.FromSeconds((ReturnTime - DateTimeOffset.Now).TotalSeconds / 2);

List<FleetSaveTarget> targets = new List<FleetSaveTarget>();

SystemCoordinate currentSystem = here;
var currentSystemPlanets = db.Planets.Where(p => p.LocationId >= currentSystem.LowerCoordinate && p.LocationId <= currentSystem.UpperCoordinate && p.LocationId != here.Id && p.LocationId != here.Id - 2)
.Select(p => p.LocationId)
.ToList();

int fleetSpeed = fleet.Speed(research);

var candidates = currentSystemPlanets.Cartesian(Enumerable.Range(1, 10), (target, speed) => new FleetSaveTarget { Target = target, Duration = here.DurationTo(target, fleetSpeed, speed, Client.Settings.Speed), Speed = speed });
var candidate = candidates.MinBy(c => (c.Duration - oneWayTrip).Duration());
targets.Add(candidate);


//Logger.Instance.Log(LogLevel.Warning, $"Best candidate for fleetsave would be {bestCandidate.Key}, would be there home in {bestCandidate.Value}");

}
return null;
}

private class FleetSaveTarget
{
public Coordinate Target { get; set; }
public int Speed { get; set; }
public TimeSpan Duration { get; set; }

public override string ToString()
{
return $"{Target} x{Speed} => {Duration}";
}
}
}
}
4 changes: 4 additions & 0 deletions OgameBot/OgameBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
<HintPath>..\packages\HtmlAgilityPack.1.4.9.5\lib\Net45\HtmlAgilityPack.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MoreLinq, Version=2.0.20029.0, Culture=neutral, PublicKeyToken=384d532d7e88985d, processorArchitecture=MSIL">
<HintPath>..\packages\morelinq.2.3.0\lib\net35\MoreLinq.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -95,6 +98,7 @@
<Compile Include="Engine\Commands\FakePlanetExclusive.cs" />
<Compile Include="Engine\Commands\Farming\FarmCommand.cs" />
<Compile Include="Engine\Commands\FindAllMessagesCommand.cs" />
<Compile Include="Engine\Commands\FleetSaveCommand.cs" />
<Compile Include="Engine\Commands\RunProxyCommand.cs" />
<Compile Include="Engine\Injects\BuildQueueInject.cs" />
<Compile Include="Engine\Injects\CustomPlanetOrderInject.cs" />
Expand Down
1 change: 1 addition & 0 deletions OgameBot/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<package id="EntityFramework" version="6.1.3" targetFramework="net461" />
<package id="EntityFramework6.Npgsql" version="3.1.1" targetFramework="net461" />
<package id="HtmlAgilityPack" version="1.4.9.5" targetFramework="net461" />
<package id="morelinq" version="2.3.0" targetFramework="net461" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net461" />
<package id="Npgsql" version="3.2.2" targetFramework="net461" />
<package id="System.Threading.Tasks.Extensions" version="4.3.0" targetFramework="net461" />
Expand Down

0 comments on commit afc8a9f

Please sign in to comment.