Skip to content

Commit 52d041e

Browse files
authored
Release 2.6.0 (#214)
- 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)
1 parent 96a43a1 commit 52d041e

File tree

16 files changed

+322
-204
lines changed

16 files changed

+322
-204
lines changed

MainCore/AppDbContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ public void AddAccount(int accountId)
234234
WorkTimeMax = 380,
235235
SleepTimeMin = 480,
236236
SleepTimeMax = 600,
237+
IsSleepBetweenProxyChanging = false,
237238
IsClosedIfNoTask = false,
238239
IsDontLoadImage = false,
239240
IsMinimized = false,
@@ -338,6 +339,7 @@ public void UpdateDatabase()
338339
IsDontLoadImage = false,
339340
IsMinimized = false,
340341
IsAutoAdventure = false,
342+
IsSleepBetweenProxyChanging = false,
341343
FarmIntervalMax = 610,
342344
FarmIntervalMin = 590,
343345
});
@@ -512,6 +514,7 @@ public void AddVersionInfo()
512514
KeyValuePair.Create(2022102716038,"IgnoreRomanAdvantage"),
513515
KeyValuePair.Create(202212152155,"NPCWarehouse"),
514516
KeyValuePair.Create(202212301138,"FarmSettings"),
517+
KeyValuePair.Create(202302101011,"SleepWhenChangingProxy"),
515518
};
516519
foreach (var migration in migrations)
517520
{

MainCore/Errors/Skip.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ namespace MainCore.Errors
44
{
55
public class Skip : Error
66
{
7+
public Skip(string message) : base(message)
8+
{
9+
}
710
}
811
}

MainCore/Helper/Implementations/UpgradeBuildingHelper.cs

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,52 +43,64 @@ public Result<PlanTask> NextBuildingTask(int accountId, int villageId)
4343
var tasks = _planManager.GetList(villageId);
4444
if (tasks.Count == 0)
4545
{
46-
return Result.Fail("Queue is empty.");
46+
return Result.Fail(new Skip("Queue is empty."));
4747
}
48+
4849
using var context = _contextFactory.CreateDbContext();
49-
var currentList = context.VillagesCurrentlyBuildings.Where(x => x.VillageId == villageId).ToList();
50-
var totalBuild = currentList.Count(x => x.Level != -1);
50+
var currentList = context.VillagesCurrentlyBuildings.Where(x => x.VillageId == villageId && x.Level != -1).ToList();
51+
var totalBuild = currentList.Count;
52+
53+
if (totalBuild == 0) return GetFirstTask(villageId);
54+
55+
var accountInfo = context.AccountsInfo.Find(accountId);
56+
var tribe = accountInfo.Tribe;
57+
var hasPlusAccount = accountInfo.HasPlusAccount;
58+
var setting = context.VillagesSettings.Find(villageId);
59+
60+
var romanAdvantage = tribe == TribeEnums.Romans && !setting.IsIgnoreRomanAdvantage;
5161

52-
if (totalBuild > 0)
62+
var maxBuild = 1;
63+
if (hasPlusAccount) maxBuild++;
64+
if (romanAdvantage) maxBuild++;
65+
if (totalBuild == maxBuild)
66+
{
67+
return Result.Fail(new Skip("Amount of currently building is equal with maximum building can build in same time"));
68+
}
69+
if (romanAdvantage && totalBuild == 2 && currentList.Count(x => x.Id < 19) == 2)
5370
{
54-
var accountInfo = context.AccountsInfo.Find(accountId);
55-
var tribe = accountInfo.Tribe;
56-
var hasPlusAccount = accountInfo.HasPlusAccount;
57-
var setting = context.VillagesSettings.Find(villageId);
71+
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)"));
72+
}
5873

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

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

72-
if (numRes > numInfra)
73-
{
74-
var freeCrop = context.VillagesResources.Find(villageId).FreeCrop;
75-
if (freeCrop <= 5)
76-
{
77-
return Result.Fail("Cannot build because of lack of freecrop ( < 6 )");
78-
}
79-
return GetFirstInfrastructureTask(villageId);
80-
}
81-
else if (numInfra > numRes)
82-
{
83-
// no need check free crop, there is magic make sure this always choose crop
84-
// jk, because of how we check free crop later, first res task is always crop
85-
return GetFirstResTask(villageId);
86-
}
87-
// if same means 1 R and 1 I already, 1 ANY will be choose below
81+
var numRes = currentList.Count(x => x.Type.IsResourceField());
82+
var numBuilding = totalBuild - numRes;
83+
84+
if (numRes > numBuilding)
85+
{
86+
var freeCrop = context.VillagesResources.Find(villageId).FreeCrop;
87+
if (freeCrop < 6)
88+
{
89+
return Result.Fail(new Skip("Cannot build because of lack of freecrop ( < 6 )"));
8890
}
91+
return GetFirstBuildingTask(villageId);
92+
}
93+
else if (numBuilding > numRes)
94+
{
95+
// no need check free crop, there is magic make sure this always choose crop
96+
// jk, because of how we check free crop later, first res task is always crop
97+
return GetFirstResTask(villageId);
98+
}
99+
// if same means 1 R and 1 I already, 1 ANY will be choose below
100+
else
101+
{
102+
return GetFirstTask(villageId);
89103
}
90-
91-
return GetFirstTask(villageId);
92104
}
93105

94106
public PlanTask ExtractResField(int villageId, PlanTask buildingTask)
@@ -136,17 +148,17 @@ public PlanTask ExtractResField(int villageId, PlanTask buildingTask)
136148
public void RemoveFinishedCB(int villageId)
137149
{
138150
using var context = _contextFactory.CreateDbContext();
139-
var tasksDone = context.VillagesCurrentlyBuildings.Where(x => x.VillageId == villageId).Where(x => x.CompleteTime <= DateTime.Now);
151+
var tasksDone = context.VillagesCurrentlyBuildings.Where(x => x.VillageId == villageId && x.CompleteTime <= DateTime.Now);
140152

141153
if (!tasksDone.Any()) return;
142154

143155
foreach (var taskDone in tasksDone)
144156
{
145157
var building = context.VillagesBuildings.Find(villageId, taskDone.Location);
146-
if (building == null)
158+
if (building is null)
147159
{
148-
building = context.VillagesBuildings.Where(x => x.VillageId == villageId).FirstOrDefault(x => x.Type == taskDone.Type);
149-
if (building == null) continue;
160+
building = context.VillagesBuildings.FirstOrDefault(x => x.VillageId == villageId && x.Type == taskDone.Type);
161+
if (building is null) continue;
150162
}
151163

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

168-
private PlanTask GetFirstInfrastructureTask(int villageId)
180+
private PlanTask GetFirstBuildingTask(int villageId)
169181
{
170182
var tasks = _planManager.GetList(villageId);
171183
var infrastructureTasks = tasks.Where(x => x.Type == PlanTypeEnums.General && !x.Building.IsResourceField());
@@ -192,7 +204,7 @@ private bool IsInfrastructureTaskVaild(int villageId, PlanTask planTask)
192204
var buildings = context.VillagesBuildings.Where(x => x.VillageId == villageId).ToList();
193205
foreach (var prerequisiteBuilding in prerequisiteBuildings)
194206
{
195-
var building = buildings.FirstOrDefault(x => x.Type == prerequisiteBuilding.Building);
207+
var building = buildings.OrderByDescending(x => x.Level).FirstOrDefault(x => x.Type == prerequisiteBuilding.Building);
196208
if (building is null) return false;
197209
if (building.Level < prerequisiteBuilding.Level) return false;
198210
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using FluentMigrator;
2+
3+
namespace MainCore.Migrations
4+
{
5+
[Migration(202302101011)]
6+
public class SleepWhenChangingProxy : Migration
7+
{
8+
public override void Down()
9+
{
10+
Execute.Sql("ALTER TABLE 'AccountsSettings' DROP COLUMN 'IsSleepBetweenProxyChanging';");
11+
}
12+
13+
public override void Up()
14+
{
15+
Alter.Table("AccountsSettings")
16+
.AddColumn("IsSleepBetweenProxyChanging").AsBoolean().WithDefaultValue(false);
17+
}
18+
}
19+
}
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Text.Json.Serialization;
2-
3-
namespace MainCore.Models.Database
1+
namespace MainCore.Models.Database
42
{
53
public class AccountSetting
64
{
@@ -13,17 +11,12 @@ public class AccountSetting
1311
public int WorkTimeMax { get; set; }
1412
public int SleepTimeMin { get; set; }
1513
public int SleepTimeMax { get; set; }
14+
public bool IsSleepBetweenProxyChanging { get; set; }
1615
public bool IsDontLoadImage { get; set; }
1716
public bool IsMinimized { get; set; }
1817
public bool IsClosedIfNoTask { get; set; }
1918
public bool IsAutoAdventure { get; set; }
20-
21-
// these below will be on farming tab instead of settings tab
22-
23-
[JsonIgnore]
2419
public int FarmIntervalMin { get; set; }
25-
26-
[JsonIgnore]
2720
public int FarmIntervalMax { get; set; }
2821
}
2922
}

MainCore/Services/Implementations/ChromeBrowser.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ public sealed class ChromeBrowser : IChromeBrowser
1919
private readonly string[] _extensionsPath;
2020
private readonly HtmlDocument _htmlDoc = new();
2121

22-
public ChromeBrowser(string[] extensionsPath)
22+
private readonly string _pathUserData;
23+
24+
public ChromeBrowser(string[] extensionsPath, string server, string username)
2325
{
26+
_pathUserData = Path.Combine(AppContext.BaseDirectory, "Data", "Cache", server.Replace("https://", "").Replace(".", "_"), username);
27+
Directory.CreateDirectory(_pathUserData);
28+
2429
_extensionsPath = extensionsPath;
2530

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

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

6568
_driver = new ChromeDriver(_chromeService, options);
6669
if (setting.IsMinimized) _driver.Manage().Window.Minimize();

MainCore/Services/Implementations/ChromeManager.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using MainCore.Services.Interface;
2+
using Microsoft.EntityFrameworkCore;
23
using System;
34
using System.Collections.Concurrent;
45
using System.Collections.Generic;
56
using System.IO;
7+
using System.Linq;
68
using System.Reflection;
79

810
namespace MainCore.Services.Implementations
@@ -11,13 +13,22 @@ public sealed class ChromeManager : IChromeManager
1113
{
1214
private readonly ConcurrentDictionary<int, ChromeBrowser> _dictionary = new();
1315
private string[] _extensionsPath;
16+
private readonly IDbContextFactory<AppDbContext> _contextFactory;
17+
18+
public ChromeManager(IDbContextFactory<AppDbContext> contextFactory)
19+
{
20+
_contextFactory = contextFactory;
21+
}
1422

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

20-
browser = new ChromeBrowser(_extensionsPath);
28+
using var context = _contextFactory.CreateDbContext();
29+
var account = context.Accounts.FirstOrDefault(x => x.Id == id);
30+
31+
browser = new ChromeBrowser(_extensionsPath, account.Server, account.Username);
2132
_dictionary.TryAdd(id, browser);
2233
return browser;
2334
}

0 commit comments

Comments
 (0)