Skip to content

Commit

Permalink
Release 2.6.0 (#214)
Browse files Browse the repository at this point in the history
- fix upgrade building task crash when choosing the building to build
- add force sleep setting (now when changing proxy bot have to sleep based on the setting instead of 15 mins, untick will make the bot sleep about 15mins between 2 proxy)
- optimize sleep task
- fix bot don't know there is level 10 available to build resource bonus building (credit to Lasseroenn)

If you like my work, you can donate to me through [ko-fi.com/vinaghost](https://ko-fi.com/vinaghost)
  • Loading branch information
vinaghost authored Mar 14, 2023
1 parent 96a43a1 commit 52d041e
Show file tree
Hide file tree
Showing 16 changed files with 322 additions and 204 deletions.
3 changes: 3 additions & 0 deletions MainCore/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ public void AddAccount(int accountId)
WorkTimeMax = 380,
SleepTimeMin = 480,
SleepTimeMax = 600,
IsSleepBetweenProxyChanging = false,
IsClosedIfNoTask = false,
IsDontLoadImage = false,
IsMinimized = false,
Expand Down Expand Up @@ -338,6 +339,7 @@ public void UpdateDatabase()
IsDontLoadImage = false,
IsMinimized = false,
IsAutoAdventure = false,
IsSleepBetweenProxyChanging = false,
FarmIntervalMax = 610,
FarmIntervalMin = 590,
});
Expand Down Expand Up @@ -512,6 +514,7 @@ public void AddVersionInfo()
KeyValuePair.Create(2022102716038,"IgnoreRomanAdvantage"),
KeyValuePair.Create(202212152155,"NPCWarehouse"),
KeyValuePair.Create(202212301138,"FarmSettings"),
KeyValuePair.Create(202302101011,"SleepWhenChangingProxy"),
};
foreach (var migration in migrations)
{
Expand Down
3 changes: 3 additions & 0 deletions MainCore/Errors/Skip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ namespace MainCore.Errors
{
public class Skip : Error
{
public Skip(string message) : base(message)
{
}
}
}
98 changes: 55 additions & 43 deletions MainCore/Helper/Implementations/UpgradeBuildingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,52 +43,64 @@ public Result<PlanTask> NextBuildingTask(int accountId, int villageId)
var tasks = _planManager.GetList(villageId);
if (tasks.Count == 0)
{
return Result.Fail("Queue is empty.");
return Result.Fail(new Skip("Queue is empty."));
}

using var context = _contextFactory.CreateDbContext();
var currentList = context.VillagesCurrentlyBuildings.Where(x => x.VillageId == villageId).ToList();
var totalBuild = currentList.Count(x => x.Level != -1);
var currentList = context.VillagesCurrentlyBuildings.Where(x => x.VillageId == villageId && x.Level != -1).ToList();
var totalBuild = currentList.Count;

if (totalBuild == 0) return GetFirstTask(villageId);

var accountInfo = context.AccountsInfo.Find(accountId);
var tribe = accountInfo.Tribe;
var hasPlusAccount = accountInfo.HasPlusAccount;
var setting = context.VillagesSettings.Find(villageId);

var romanAdvantage = tribe == TribeEnums.Romans && !setting.IsIgnoreRomanAdvantage;

if (totalBuild > 0)
var maxBuild = 1;
if (hasPlusAccount) maxBuild++;
if (romanAdvantage) maxBuild++;
if (totalBuild == maxBuild)
{
return Result.Fail(new Skip("Amount of currently building is equal with maximum building can build in same time"));
}
if (romanAdvantage && totalBuild == 2 && currentList.Count(x => x.Id < 19) == 2)
{
var accountInfo = context.AccountsInfo.Find(accountId);
var tribe = accountInfo.Tribe;
var hasPlusAccount = accountInfo.HasPlusAccount;
var setting = context.VillagesSettings.Find(villageId);
if (GetFirstBuildingTask(villageId) is null) return Result.Fail(new Skip("Amount of currently building is equal with maximum building can build in same time (there is only resource field in queue)"));
}

var maxBuild = 1;
if (hasPlusAccount) maxBuild++;
if (tribe == TribeEnums.Romans && !setting.IsIgnoreRomanAdvantage) maxBuild++;
if (totalBuild == maxBuild)
{
return Result.Fail("Amount of currently building is equal with maximum building can build in same time");
}
// roman will have another method to select task
if (!romanAdvantage) return GetFirstTask(villageId);

if (tribe == TribeEnums.Romans && !setting.IsIgnoreRomanAdvantage && maxBuild - totalBuild == 1)
{
var numRes = currentList.Count(x => x.Type.IsResourceField());
var numInfra = totalBuild - numRes;
// there is atleast 2 slot free
// roman can build both building or resource field
if (maxBuild - totalBuild >= 2) return GetFirstTask(villageId);

if (numRes > numInfra)
{
var freeCrop = context.VillagesResources.Find(villageId).FreeCrop;
if (freeCrop <= 5)
{
return Result.Fail("Cannot build because of lack of freecrop ( < 6 )");
}
return GetFirstInfrastructureTask(villageId);
}
else if (numInfra > numRes)
{
// no need check free crop, there is magic make sure this always choose crop
// jk, because of how we check free crop later, first res task is always crop
return GetFirstResTask(villageId);
}
// if same means 1 R and 1 I already, 1 ANY will be choose below
var numRes = currentList.Count(x => x.Type.IsResourceField());
var numBuilding = totalBuild - numRes;

if (numRes > numBuilding)
{
var freeCrop = context.VillagesResources.Find(villageId).FreeCrop;
if (freeCrop < 6)
{
return Result.Fail(new Skip("Cannot build because of lack of freecrop ( < 6 )"));
}
return GetFirstBuildingTask(villageId);
}
else if (numBuilding > numRes)
{
// no need check free crop, there is magic make sure this always choose crop
// jk, because of how we check free crop later, first res task is always crop
return GetFirstResTask(villageId);
}
// if same means 1 R and 1 I already, 1 ANY will be choose below
else
{
return GetFirstTask(villageId);
}

return GetFirstTask(villageId);
}

public PlanTask ExtractResField(int villageId, PlanTask buildingTask)
Expand Down Expand Up @@ -136,17 +148,17 @@ public PlanTask ExtractResField(int villageId, PlanTask buildingTask)
public void RemoveFinishedCB(int villageId)
{
using var context = _contextFactory.CreateDbContext();
var tasksDone = context.VillagesCurrentlyBuildings.Where(x => x.VillageId == villageId).Where(x => x.CompleteTime <= DateTime.Now);
var tasksDone = context.VillagesCurrentlyBuildings.Where(x => x.VillageId == villageId && x.CompleteTime <= DateTime.Now);

if (!tasksDone.Any()) return;

foreach (var taskDone in tasksDone)
{
var building = context.VillagesBuildings.Find(villageId, taskDone.Location);
if (building == null)
if (building is null)
{
building = context.VillagesBuildings.Where(x => x.VillageId == villageId).FirstOrDefault(x => x.Type == taskDone.Type);
if (building == null) continue;
building = context.VillagesBuildings.FirstOrDefault(x => x.VillageId == villageId && x.Type == taskDone.Type);
if (building is null) continue;
}

if (building.Level < taskDone.Level) building.Level = taskDone.Level;
Expand All @@ -165,7 +177,7 @@ private PlanTask GetFirstResTask(int villageId)
return task;
}

private PlanTask GetFirstInfrastructureTask(int villageId)
private PlanTask GetFirstBuildingTask(int villageId)
{
var tasks = _planManager.GetList(villageId);
var infrastructureTasks = tasks.Where(x => x.Type == PlanTypeEnums.General && !x.Building.IsResourceField());
Expand All @@ -192,7 +204,7 @@ private bool IsInfrastructureTaskVaild(int villageId, PlanTask planTask)
var buildings = context.VillagesBuildings.Where(x => x.VillageId == villageId).ToList();
foreach (var prerequisiteBuilding in prerequisiteBuildings)
{
var building = buildings.FirstOrDefault(x => x.Type == prerequisiteBuilding.Building);
var building = buildings.OrderByDescending(x => x.Level).FirstOrDefault(x => x.Type == prerequisiteBuilding.Building);
if (building is null) return false;
if (building.Level < prerequisiteBuilding.Level) return false;
}
Expand Down
19 changes: 19 additions & 0 deletions MainCore/Migrations/202302101011_SleepWhenChangingProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using FluentMigrator;

namespace MainCore.Migrations
{
[Migration(202302101011)]
public class SleepWhenChangingProxy : Migration
{
public override void Down()
{
Execute.Sql("ALTER TABLE 'AccountsSettings' DROP COLUMN 'IsSleepBetweenProxyChanging';");
}

public override void Up()
{
Alter.Table("AccountsSettings")
.AddColumn("IsSleepBetweenProxyChanging").AsBoolean().WithDefaultValue(false);
}
}
}
11 changes: 2 additions & 9 deletions MainCore/Models/Database/AccountSetting.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text.Json.Serialization;

namespace MainCore.Models.Database
namespace MainCore.Models.Database
{
public class AccountSetting
{
Expand All @@ -13,17 +11,12 @@ public class AccountSetting
public int WorkTimeMax { get; set; }
public int SleepTimeMin { get; set; }
public int SleepTimeMax { get; set; }
public bool IsSleepBetweenProxyChanging { get; set; }
public bool IsDontLoadImage { get; set; }
public bool IsMinimized { get; set; }
public bool IsClosedIfNoTask { get; set; }
public bool IsAutoAdventure { get; set; }

// these below will be on farming tab instead of settings tab

[JsonIgnore]
public int FarmIntervalMin { get; set; }

[JsonIgnore]
public int FarmIntervalMax { get; set; }
}
}
11 changes: 7 additions & 4 deletions MainCore/Services/Implementations/ChromeBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ public sealed class ChromeBrowser : IChromeBrowser
private readonly string[] _extensionsPath;
private readonly HtmlDocument _htmlDoc = new();

public ChromeBrowser(string[] extensionsPath)
private readonly string _pathUserData;

public ChromeBrowser(string[] extensionsPath, string server, string username)
{
_pathUserData = Path.Combine(AppContext.BaseDirectory, "Data", "Cache", server.Replace("https://", "").Replace(".", "_"), username);
Directory.CreateDirectory(_pathUserData);

_extensionsPath = extensionsPath;

_chromeService = ChromeDriverService.CreateDefaultService();
Expand Down Expand Up @@ -58,9 +63,7 @@ public void Setup(Access access, AccountSetting setting)
options.AddArgument("--mute-audio");
if (setting.IsDontLoadImage) options.AddArguments("--blink-settings=imagesEnabled=false"); //--disable-images

var path = Path.Combine(AppContext.BaseDirectory, "Data", "Cache", access.ProxyHost ?? "default");
Directory.CreateDirectory(path);
options.AddArguments($"user-data-dir={path}");
options.AddArguments($"user-data-dir={_pathUserData}");

_driver = new ChromeDriver(_chromeService, options);
if (setting.IsMinimized) _driver.Manage().Window.Minimize();
Expand Down
13 changes: 12 additions & 1 deletion MainCore/Services/Implementations/ChromeManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using MainCore.Services.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace MainCore.Services.Implementations
Expand All @@ -11,13 +13,22 @@ public sealed class ChromeManager : IChromeManager
{
private readonly ConcurrentDictionary<int, ChromeBrowser> _dictionary = new();
private string[] _extensionsPath;
private readonly IDbContextFactory<AppDbContext> _contextFactory;

public ChromeManager(IDbContextFactory<AppDbContext> contextFactory)
{
_contextFactory = contextFactory;
}

public IChromeBrowser Get(int id)
{
var result = _dictionary.TryGetValue(id, out ChromeBrowser browser);
if (result) return browser;

browser = new ChromeBrowser(_extensionsPath);
using var context = _contextFactory.CreateDbContext();
var account = context.Accounts.FirstOrDefault(x => x.Id == id);

browser = new ChromeBrowser(_extensionsPath, account.Server, account.Username);
_dictionary.TryAdd(id, browser);
return browser;
}
Expand Down
Loading

0 comments on commit 52d041e

Please sign in to comment.