Skip to content

Commit

Permalink
update ogamed to latest official version (forked version is not neede…
Browse files Browse the repository at this point in the history
…d anymore)

fix WaitForAllExpeditions
enable expos to be sent from multiple origins
start autoharvest rework (feature is disabled atm)
  • Loading branch information
kokiddp committed Mar 25, 2021
1 parent b3392c9 commit 10bb203
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 83 deletions.
6 changes: 4 additions & 2 deletions TBot/Model/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ public enum LogSender
Tbot,
Defender,
Brain,
Expeditions
Expeditions,
Harvest
}

public enum Feature
Expand All @@ -184,6 +185,7 @@ public enum Feature
BrainAutobuildCargo = 1,
BrainAutoRepatriate = 2,
BrainAutoMine = 3,
Expeditions = 4
Expeditions = 4,
Harvest = 5
}
}
145 changes: 90 additions & 55 deletions TBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Program
* ATTENTION!!In case of adding some timers
* you need to redim the Semaphore array!!!
*/
static Semaphore[] xaSem = new Semaphore[5];
static Semaphore[] xaSem = new Semaphore[6];

static void Main(string[] args)
{
Expand Down Expand Up @@ -138,22 +138,28 @@ static void Main(string[] args)
xaSem[(int)Feature.BrainAutoRepatriate] = new Semaphore(1, 1); //Brain - AutoRepatriate
xaSem[(int)Feature.BrainAutoMine] = new Semaphore(1, 1); //Brain - Auto mine
xaSem[(int)Feature.Expeditions] = new Semaphore(1, 1); //Expeditions
xaSem[(int)Feature.Harvest] = new Semaphore(1, 1); //Harvest


if (settings.Defender.Active)
if ((bool)settings.Defender.Active)
{
InitializeDefender();
}

if (settings.Brain.Active)
if ((bool)settings.Brain.Active)
{
InitializeBrain();
}

if (settings.Expeditions.Active)
if ((bool)settings.Expeditions.Active)
{
InitializeExpeditions();
}

if ((bool)settings.AutoHarvest.Active)
{
InitializeHarvest();
}
}
else
{
Expand Down Expand Up @@ -335,7 +341,14 @@ private static void InitializeExpeditions()
{
Helpers.WriteLog(LogType.Info, LogSender.Tbot, "Initializing expeditions...");

timers.Add("ExpeditionsTimer", new Timer(HandleExpeditions, null, Helpers.CalcRandomInterval(IntervalType.SomeSeconds), Helpers.CalcRandomInterval(IntervalType.AboutAQuarterHour)));
timers.Add("ExpeditionsTimer", new Timer(HandleExpeditions, null, Helpers.CalcRandomInterval(IntervalType.SomeSeconds), Helpers.CalcRandomInterval(IntervalType.AMinuteOrTwo)));
}

private static void InitializeHarvest()
{
Helpers.WriteLog(LogType.Info, LogSender.Tbot, "Initializing Harvest...");
Helpers.WriteLog(LogType.Info, LogSender.Harvest, "This feature will be reintroduced in the next version, in a hopefully better way");
//timers.Add("HarvestTimer", new Timer(HandleExpeditions, null, Helpers.CalcRandomInterval(IntervalType.SomeSeconds), Helpers.CalcRandomInterval(IntervalType.AboutAQuarterHour)));
}

private static void Defender(object state)
Expand Down Expand Up @@ -1063,81 +1076,94 @@ private static void HandleExpeditions(object state)
{
if (slots.Free > 0)
{
Celestial origin;
if (settings.Expeditions.AutoSendExpeditions.Origin)
List<Celestial> origins = new List<Celestial>();
if (settings.Expeditions.AutoSendExpeditions.Origin.Length > 0)
{
Coordinate customOriginCoords = new Coordinate(
(int)settings.Expeditions.AutoSendExpeditions.Origin.Galaxy,
(int)settings.Expeditions.AutoSendExpeditions.Origin.System,
(int)settings.Expeditions.AutoSendExpeditions.Origin.Position,
Enum.Parse<Celestials>(settings.Expeditions.AutoSendExpeditions.Origin.Type.ToString()));
try
{

Celestial customOrigin = celestials
.Single(planet => planet.HasCoords(customOriginCoords));
origin = customOrigin;
origin = UpdatePlanet(origin, UpdateType.Ships);
foreach (var origin in settings.Expeditions.AutoSendExpeditions.Origin)
{
Coordinate customOriginCoords = new Coordinate(
(int)origin.Galaxy,
(int)origin.System,
(int)origin.Position,
Enum.Parse<Celestials>(origin.Type.ToString()));
Celestial customOrigin = celestials
.Single(planet => planet.HasCoords(customOriginCoords));
customOrigin = UpdatePlanet(customOrigin, UpdateType.Ships);
origins.Add(customOrigin);
}
}
catch (Exception e)
{
Helpers.WriteLog(LogType.Debug, LogSender.Expeditions, "Exception: " + e.Message);
Helpers.WriteLog(LogType.Warning, LogSender.Expeditions, "Unable to parse custom origin");

celestials = UpdatePlanets(UpdateType.Ships);
origin = celestials
origins.Add(celestials
.OrderBy(planet => planet.Coordinate.Type == Celestials.Moon)
.OrderByDescending(planet => Helpers.CalcFleetCapacity(planet.Ships, researches.HyperspaceTechnology, userInfo.Class))
.First();
.First()
);
}
}
else
{
celestials = UpdatePlanets(UpdateType.Ships);
origin = celestials
origins.Add(celestials
.OrderBy(planet => planet.Coordinate.Type == Celestials.Moon)
.OrderByDescending(planet => Helpers.CalcFleetCapacity(planet.Ships, researches.HyperspaceTechnology, userInfo.Class))
.First();
}
if (origin.Ships.IsEmpty())
{
Helpers.WriteLog(LogType.Warning, LogSender.Expeditions, "Unable to send expeditions: no ships available");
.First()
);
}
else
foreach (var origin in origins)
{
Buildables mainShip = Enum.Parse<Buildables>(settings.Expeditions.AutoSendExpeditions.MainShip.ToString() ?? "LargeCargo") ?? Buildables.LargeCargo;
Ships fleet = Helpers.CalcFullExpeditionShips(origin.Ships, mainShip, expsToSend, serverData, researches, userInfo.Class);

Helpers.WriteLog(LogType.Info, LogSender.Expeditions, expsToSend.ToString() + " expeditions with " + fleet.ToString() + " will be sent from " + origin.ToString());
for (int i = 0; i < expsToSend; i++)
int expsToSendFromThisOrigin = (int)Math.Round((float)expsToSend / (float)origins.Count, MidpointRounding.ToZero);
if (origin == origins.Last())
{
Coordinate destination;
if (settings.Expeditions.AutoSendExpeditions.SplitExpeditionsBetweenSystems)
{
var rand = new Random();
expsToSendFromThisOrigin = (int)Math.Round((float)expsToSend / (float)origins.Count, MidpointRounding.ToPositiveInfinity);
}
if (origin.Ships.IsEmpty())
{
Helpers.WriteLog(LogType.Warning, LogSender.Expeditions, "Unable to send expeditions: no ships available");
continue;
}
else
{
Buildables mainShip = Enum.Parse<Buildables>(settings.Expeditions.AutoSendExpeditions.MainShip.ToString() ?? "LargeCargo") ?? Buildables.LargeCargo;
Ships fleet = Helpers.CalcFullExpeditionShips(origin.Ships, mainShip, expsToSendFromThisOrigin, serverData, researches, userInfo.Class);

destination = new Coordinate
{
Galaxy = origin.Coordinate.Galaxy,
System = rand.Next(origin.Coordinate.System - 1, origin.Coordinate.System + 2),
Position = 16,
Type = Celestials.DeepSpace
};
}
else
Helpers.WriteLog(LogType.Info, LogSender.Expeditions, expsToSendFromThisOrigin.ToString() + " expeditions with " + fleet.ToString() + " will be sent from " + origin.ToString());
for (int i = 0; i < expsToSendFromThisOrigin; i++)
{
destination = new Coordinate
Coordinate destination;
if (settings.Expeditions.AutoSendExpeditions.SplitExpeditionsBetweenSystems)
{
Galaxy = origin.Coordinate.Galaxy,
System = origin.Coordinate.System,
Position = 16,
Type = Celestials.DeepSpace
};
var rand = new Random();

destination = new Coordinate
{
Galaxy = origin.Coordinate.Galaxy,
System = rand.Next(origin.Coordinate.System - 1, origin.Coordinate.System + 2),
Position = 16,
Type = Celestials.DeepSpace
};
}
else
{
destination = new Coordinate
{
Galaxy = origin.Coordinate.Galaxy,
System = origin.Coordinate.System,
Position = 16,
Type = Celestials.DeepSpace
};
}
SendFleet(origin, fleet, destination, Missions.Expedition, Speeds.HundredPercent);
Thread.Sleep((int)IntervalType.AFewSeconds);
}
SendFleet(origin, fleet, destination, Missions.Expedition, Speeds.HundredPercent);
Thread.Sleep((int)IntervalType.AFewSeconds);
}
}
}
}
else
{
Expand All @@ -1158,13 +1184,13 @@ private static void HandleExpeditions(object state)
.ToList();
if ((bool)settings.Expeditions.AutoSendExpeditions.WaitForAllExpeditions)
{
fleets = fleets
orderedFleets = orderedFleets
.OrderByDescending(fleet => fleet.BackIn)
.ToList();
}
else
{
fleets = fleets
orderedFleets = orderedFleets
.OrderBy(fleet => fleet.BackIn)
.ToList();
}
Expand All @@ -1184,6 +1210,7 @@ private static void HandleExpeditions(object state)
UpdateTitle();
}

/*
if ((bool)settings.Expeditions.AutoHarvest.Active)
{
slots = UpdateSlots();
Expand Down Expand Up @@ -1254,10 +1281,18 @@ private static void HandleExpeditions(object state)
}
}
}
*/
}

catch (Exception e)
{
Helpers.WriteLog(LogType.Warning, LogSender.Expeditions, "HandleExpeditions exception: " + e.Message);
int interval = (int)(Helpers.CalcRandomInterval(IntervalType.AMinuteOrTwo));
var time = GetDateTime();
DateTime newTime = time.AddMilliseconds(interval);
timers.GetValueOrDefault("ExpeditionsTimer").Change(interval, Timeout.Infinite);
Helpers.WriteLog(LogType.Info, LogSender.Expeditions, "Next check at " + newTime.ToString());
UpdateTitle();
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion TBot/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="ogamed_win64" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\ogamed-win64.exe;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>..\Resources\ogamed_win64.exe;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>
Binary file not shown.
6 changes: 3 additions & 3 deletions TBot/Services/OgamedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class OgamedService
public OgamedService(Credentials credentials, string host = "127.0.0.1", int port = 8080)
{
ExecuteOgamedExecutable(credentials, host, port);
this.Url = "http://" + host + ":" + port;
this.Url = "http://localhost:" + port;
this.Client = new RestClient(this.Url);
}

Expand Down Expand Up @@ -358,7 +358,7 @@ public Classes GetUserClass()
{
var request = new RestRequest
{
Resource = "/bot/user-class",
Resource = "/bot/character-class",
Method = Method.GET
};
var result = JsonConvert.DeserializeObject<OgamedResponse>(Client.Execute(request).Content);
Expand Down Expand Up @@ -647,7 +647,7 @@ public Model.Resources GetResourceProduction(Planet planet)
{
var request = new RestRequest
{
Resource = "/bot/planets/" + planet.ID + "/resource-production",
Resource = "/bot/planets/" + planet.ID + "/resource-details",
Method = Method.GET,
};
var result = JsonConvert.DeserializeObject<OgamedResponse>(Client.Execute(request).Content);
Expand Down
4 changes: 2 additions & 2 deletions TBot/TBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<Nullable>disable</Nullable>
<Authors>ELK-Lab</Authors>
<Version>0.1.8</Version>
<Version>0.1.9</Version>
<StartupObject>Tbot.Program</StartupObject>
<Platforms>AnyCPU;x64;x86</Platforms>
<Copyright>2021 © ELK-Lab</Copyright>
<PackageLicenseExpression>https://licenses.nuget.org/MIT</PackageLicenseExpression>
<PackageProjectUrl>https://www.ogame-tbot.net</PackageProjectUrl>
<RepositoryUrl>https://github.com/ogame-tbot/TBot</RepositoryUrl>
<AssemblyVersion>0.1.8.0</AssemblyVersion>
<AssemblyVersion>0.1.9.0</AssemblyVersion>
<FileVersion>0.1.8.0</FileVersion>
<Description>OGame Bot</Description>
<SignAssembly>false</SignAssembly>
Expand Down
Loading

0 comments on commit 10bb203

Please sign in to comment.