Skip to content

Commit 1ad6317

Browse files
authored
Fix a lot of things (#162)
- fix negative min setting value - pause account before shutdown bot - fix building list in build single section in building tab - fix start adventures when hero not at home - fix bot crash when missing chrome - fix version form
1 parent e980c02 commit 1ad6317

17 files changed

+252
-114
lines changed

MainCore/Helper/BuildingsHelper.cs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public static class BuildingsHelper
1414

1515
public static List<BuildingEnums> GetCanBuild(AppDbContext context, IPlanManager planManager, int accountId, int villageId)
1616
{
17-
var result = new List<BuildingEnums>(); var tribe = context.AccountsInfo.Find(accountId).Tribe;
17+
var result = new List<BuildingEnums>();
18+
var tribe = context.AccountsInfo.Find(accountId).Tribe;
1819
for (var i = BuildingEnums.Sawmill; i <= BuildingEnums.Hospital; i++)
1920
{
2021
if (CanBuild(context, planManager, villageId, tribe, i))
@@ -27,8 +28,7 @@ public static List<BuildingEnums> GetCanBuild(AppDbContext context, IPlanManager
2728

2829
public static bool CanBuild(AppDbContext context, IPlanManager planManager, int villageId, TribeEnums tribe, BuildingEnums building)
2930
{
30-
bool exists = (context.VillagesBuildings.Where(x => x.VillageId == villageId).FirstOrDefault(x => x.Type == building) is not null); //there is already a building of this type in the vill
31-
if (exists)
31+
if (IsExists(context, planManager, villageId, building))
3232
{
3333
//check cranny/warehouse/grannary/trapper/GG/GW
3434
return building switch
@@ -47,15 +47,52 @@ public static bool CanBuild(AppDbContext context, IPlanManager planManager, int
4747
if (reqTribe != TribeEnums.Any && reqTribe != tribe) return false;
4848
foreach (var prerequisite in prerequisites)
4949
{
50+
if (prerequisite.Building.IsResourceField())
51+
{
52+
if (prerequisite.Building == BuildingEnums.Cropland)
53+
{
54+
if (IsAutoCropFieldAboveLevel(planManager, villageId, prerequisite.Level)) return true;
55+
}
56+
else
57+
{
58+
if (IsAutoResourceFieldAboveLevel(planManager, villageId, prerequisite.Level)) return true;
59+
}
60+
}
5061
if (!IsBuildingAboveLevel(context, planManager, villageId, prerequisite.Building, prerequisite.Level)) return false;
5162
}
5263
return true;
5364
}
5465

55-
public static bool IsBuildingAboveLevel(AppDbContext context, IPlanManager planManager, int villageId, BuildingEnums building, int lvl)
66+
private static bool IsExists(AppDbContext context, IPlanManager planManager, int villageId, BuildingEnums building)
67+
{
68+
var b = context.VillagesBuildings.Where(x => x.VillageId == villageId).FirstOrDefault(x => x.Type == building);
69+
if (b is not null) return true;
70+
var c = context.VillagesCurrentlyBuildings.Where(x => x.VillageId == villageId).FirstOrDefault(x => x.Type == building);
71+
if (c is not null) return true;
72+
var q = planManager.GetList(villageId).FirstOrDefault(x => x.Building == building);
73+
if (q is not null) return true;
74+
return false;
75+
}
76+
77+
private static bool IsBuildingAboveLevel(AppDbContext context, IPlanManager planManager, int villageId, BuildingEnums building, int lvl)
78+
{
79+
var b = context.VillagesBuildings.Where(x => x.VillageId == villageId).Any(x => x.Type == building && lvl <= x.Level);
80+
if (b) return true;
81+
var c = context.VillagesCurrentlyBuildings.Where(x => x.VillageId == villageId).Any(x => x.Type == building && lvl <= x.Level);
82+
if (c) return true;
83+
var q = planManager.GetList(villageId).Any(x => x.Building == building && lvl <= x.Level);
84+
if (q) return true;
85+
return false;
86+
}
87+
88+
private static bool IsAutoResourceFieldAboveLevel(IPlanManager planManager, int villageId, int lvl)
89+
{
90+
return planManager.GetList(villageId).Any(x => (x.ResourceType == ResTypeEnums.AllResources || x.ResourceType == ResTypeEnums.ExcludeCrop) && lvl <= x.Level);
91+
}
92+
93+
private static bool IsAutoCropFieldAboveLevel(IPlanManager planManager, int villageId, int lvl)
5694
{
57-
return (context.VillagesBuildings.Where(x => x.VillageId == villageId).Any(x => x.Type == building && lvl <= x.Level) ||
58-
planManager.GetList(villageId).Any(x => x.Building == building && lvl <= x.Level));
95+
return planManager.GetList(villageId).Any(x => (x.ResourceType == ResTypeEnums.AllResources || x.ResourceType == ResTypeEnums.OnlyCrop) && lvl <= x.Level);
5996
}
6097

6198
public static bool IsResourceField(this BuildingEnums building)

MainCore/Tasks/Misc/LoginTask.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ private void AddTask()
146146
using var context = _contextFactory.CreateDbContext();
147147
var villages = context.Villages.Where(x => x.AccountId == AccountId);
148148
var listTask = _taskManager.GetList(AccountId);
149-
var upgradeBuildingList = listTask.Where(x => x.GetType() == typeof(UpgradeBuilding)).OfType<UpgradeBuilding>();
149+
var upgradeBuildingList = listTask.OfType<UpgradeBuilding>();
150+
var updateList = listTask.OfType<UpdateDorf1>();
150151
foreach (var village in villages)
151152
{
152153
var queue = _planManager.GetList(village.Id);
@@ -158,6 +159,15 @@ private void AddTask()
158159
_taskManager.Add(AccountId, new UpgradeBuilding(village.Id, AccountId));
159160
}
160161
}
162+
var setting = context.VillagesSettings.Find(village.Id);
163+
if (setting.IsAutoRefresh)
164+
{
165+
var update = updateList.FirstOrDefault(x => x.VillageId == village.Id);
166+
if (update is null)
167+
{
168+
_taskManager.Add(AccountId, new UpdateDorf1(village.Id, AccountId));
169+
}
170+
}
161171
}
162172
}
163173
}

MainCore/Tasks/Sim/InstantUpgrade.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public override void Execute()
3838
NavigateHelper.AfterClicking(_chromeBrowser, context, AccountId);
3939

4040
var tasks = _taskManager.GetList(AccountId);
41-
var upgradeTask = tasks.Where(x => x.GetType() == typeof(UpgradeBuilding)).OfType<UpgradeBuilding>().FirstOrDefault(x => x.VillageId == VillageId);
41+
var upgradeTask = tasks.OfType<UpgradeBuilding>().FirstOrDefault(x => x.VillageId == VillageId);
4242
if (upgradeTask is not null)
4343
{
4444
upgradeTask.ExecuteAt = DateTime.Now;

MainCore/Tasks/Update/UpdateAdventures.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public override void Execute()
2626
var setting = context.AccountsSettings.Find(AccountId);
2727
if (setting.IsAutoAdventure)
2828
{
29+
var hero = context.Heroes.Find(AccountId);
30+
if (hero.Status != Enums.HeroStatusEnums.Home)
31+
{
32+
_logManager.Warning(AccountId, "Hero is not at home. Cannot start adventures");
33+
return;
34+
}
2935
var adventures = context.Adventures.Where(a => a.AccountId == AccountId);
3036
if (adventures.Any())
3137
{

MainCore/Tasks/Update/UpdateDorf1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private void NextExcute()
2828
var tasks = _taskManager.GetList(AccountId);
2929
var updateTasks = tasks.OfType<UpdateDorf1>().OrderByDescending(x => x.ExecuteAt);
3030
var updateTask = updateTasks.FirstOrDefault();
31-
31+
if (updateTask is null) return;
3232
using var context = _contextFactory.CreateDbContext();
3333
var setting = context.VillagesSettings.Find(VillageId);
3434
var rand = new Random(DateTime.Now.Second);

MainCore/Tasks/Update/UpdateInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private void UpdateVillageList()
7777
});
7878
context.AddVillage(newVill.Id);
7979

80-
var tasks = _taskManager.GetList(AccountId).Where(x => x.GetType() == typeof(UpdateBothDorf)).Cast<UpdateVillage>().ToList();
80+
var tasks = _taskManager.GetList(AccountId).OfType<UpdateVillage>().ToList();
8181
var task = tasks.FirstOrDefault(x => x.VillageId == newVill.Id);
8282
if (task is null)
8383
{

WPFUI/App.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:local="clr-namespace:WPFUI"
5-
Startup="Application_Startup" Exit="Application_Exit">
5+
Startup="Application_Startup">
66
<Application.Resources>
77
</Application.Resources>
88
</Application>

WPFUI/App.xaml.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ namespace WPFUI
1919
public partial class App : Application
2020
{
2121
private static ServiceProvider _provider;
22-
public static IServiceProvider Provider => _provider;
22+
public static ServiceProvider Provider => _provider;
2323

2424
private static WaitingWindow _waitingWindow;
2525
private static MainWindow _mainWindow;
26+
private static VersionWindow _versionWindow;
2627

2728
public static T GetService<T>()
2829
{
2930
if (typeof(T) == typeof(WaitingWindow)) return (T)Convert.ChangeType(_waitingWindow, typeof(T));
31+
if (typeof(T) == typeof(VersionWindow)) return (T)Convert.ChangeType(_versionWindow, typeof(T));
3032
if (typeof(T) == typeof(MainWindow)) return (T)Convert.ChangeType(_mainWindow, typeof(T));
3133

3234
return Provider.GetRequiredService<T>();
@@ -35,15 +37,23 @@ public static T GetService<T>()
3537
private async void Application_Startup(object sender, StartupEventArgs e)
3638
{
3739
_provider = new ServiceCollection().ConfigureServices().BuildServiceProvider();
40+
_versionWindow = new();
3841
_waitingWindow = new();
3942
_mainWindow = new();
4043

4144
var waitingWindow = GetService<WaitingWindow>();
4245
waitingWindow.ViewModel.Show("loading data");
46+
try
47+
{
48+
await ChromeDriverInstaller.Install();
49+
}
50+
catch (Exception ex)
51+
{
52+
MessageBox.Show(ex.Message, "Error");
53+
}
4354

4455
var tasks = new List<Task>
4556
{
46-
ChromeDriverInstaller.Install(),
4757
Task.Run(() =>
4858
{
4959
var chromeManager = GetService<IChromeManager>();
@@ -79,18 +89,15 @@ private async void Application_Startup(object sender, StartupEventArgs e)
7989

8090
await Task.WhenAll(tasks);
8191

82-
var versionWindow = new VersionWindow();
92+
var versionWindow = GetService<VersionWindow>();
93+
8394
await versionWindow.ViewModel.Load();
84-
if (versionWindow.ViewModel.IsNewVersion) versionWindow.Show();
8595

8696
var mainWindow = GetService<MainWindow>();
8797
mainWindow.Show();
8898
waitingWindow.ViewModel.Close();
89-
}
9099

91-
private void Application_Exit(object sender, ExitEventArgs e)
92-
{
93-
_provider.Dispose();
100+
if (versionWindow.ViewModel.IsNewVersion) versionWindow.Show();
94101
}
95102
}
96103

WPFUI/Models/AccountSetting.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using ReactiveUI;
1+
using MainCore.Helper;
2+
using ReactiveUI;
3+
using System.Windows;
24

35
namespace WPFUI.Models
46
{
@@ -25,21 +27,25 @@ public void CopyTo(MainCore.Models.Database.AccountSetting settings)
2527
var clickDelay = int.Parse(ClickDelay);
2628
var clickDelayRange = int.Parse(ClickDelayRange);
2729
settings.ClickDelayMin = clickDelay - clickDelayRange;
30+
if (settings.ClickDelayMin < 0) settings.ClickDelayMin = 0;
2831
settings.ClickDelayMax = clickDelay + clickDelayRange;
2932

3033
var taskDelay = int.Parse(TaskDelay);
3134
var taskDelayRange = int.Parse(TaskDelayRange);
3235
settings.TaskDelayMin = taskDelay - taskDelayRange;
36+
if (settings.TaskDelayMin < 0) settings.TaskDelayMin = 0;
3337
settings.TaskDelayMax = taskDelay + taskDelayRange;
3438

3539
var workTime = int.Parse(WorkTime);
3640
var workTimeRange = int.Parse(WorkTimeRange);
3741
settings.WorkTimeMin = workTime - workTimeRange;
42+
if (settings.WorkTimeMin < 0) settings.WorkTimeMin = 0;
3843
settings.WorkTimeMax = workTime + workTimeRange;
3944

4045
var sleepTime = int.Parse(SleepTime);
4146
var sleepTimeRange = int.Parse(SleepTimeRange);
4247
settings.SleepTimeMin = sleepTime - sleepTimeRange;
48+
if (settings.SleepTimeMin < 0) settings.SleepTimeMin = 0;
4349
settings.SleepTimeMax = sleepTime + sleepTimeRange;
4450

4551
settings.IsDontLoadImage = IsDontLoadImage;
@@ -48,6 +54,51 @@ public void CopyTo(MainCore.Models.Database.AccountSetting settings)
4854
settings.IsAutoAdventure = IsAutoStartAdventure;
4955
}
5056

57+
public bool IsVaild()
58+
{
59+
if (!ClickDelay.IsNumeric())
60+
{
61+
MessageBox.Show("Click delay is not a number.", "Warning");
62+
return false;
63+
}
64+
if (!ClickDelayRange.IsNumeric())
65+
{
66+
MessageBox.Show("Click delay range is not a number.", "Warning");
67+
return false;
68+
}
69+
if (!TaskDelay.IsNumeric())
70+
{
71+
MessageBox.Show("Task delay is not a number.", "Warning");
72+
return false;
73+
}
74+
if (!TaskDelayRange.IsNumeric())
75+
{
76+
MessageBox.Show("Task delay range is not a number.", "Warning");
77+
return false;
78+
}
79+
if (!WorkTime.IsNumeric())
80+
{
81+
MessageBox.Show("Work time is not a number.", "Warning");
82+
return false;
83+
}
84+
if (!WorkTimeRange.IsNumeric())
85+
{
86+
MessageBox.Show("Work time range is not a number.", "Warning");
87+
return false;
88+
}
89+
if (!SleepTime.IsNumeric())
90+
{
91+
MessageBox.Show("Sleep time is not a number.", "Warning");
92+
return false;
93+
}
94+
if (!SleepTimeRange.IsNumeric())
95+
{
96+
MessageBox.Show("Sleep time range is not a number.", "Warning");
97+
return false;
98+
}
99+
return true;
100+
}
101+
51102
private string _clickDelay;
52103

53104
public string ClickDelay

WPFUI/Models/VillageSetting.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,21 @@ public void CopyTo(MainCore.Models.Database.VillageSetting settings)
3131
settings.IsUseHeroRes = IsUseHeroRes;
3232
settings.IsInstantComplete = IsInstantComplete;
3333
settings.InstantCompleteTime = int.Parse(InstantCompleteTime);
34+
if (settings.InstantCompleteTime < 0) settings.InstantCompleteTime = 0;
3435
settings.IsAdsUpgrade = IsAdsUpgrade;
3536
settings.AdsUpgradeTime = int.Parse(AdsUpgradeTime);
37+
if (settings.AdsUpgradeTime < 0) settings.AdsUpgradeTime = 0;
3638

3739
settings.IsAutoRefresh = IsAutoRefresh;
3840
var autoRefreshTime = int.Parse(AutoRefreshTime);
3941
var autoRefreshTimeTolerance = int.Parse(AutoRefreshTimeTolerance);
4042
settings.AutoRefreshTimeMin = autoRefreshTime - autoRefreshTimeTolerance;
43+
if (settings.AutoRefreshTimeMin < 0) settings.AutoRefreshTimeMin = 0;
4144
settings.AutoRefreshTimeMax = autoRefreshTime + autoRefreshTimeTolerance;
4245

4346
settings.IsAutoNPC = IsAutoNPC;
4447
settings.AutoNPCPercent = int.Parse(AutoNPCPercent);
48+
if (settings.AutoRefreshTimeMin < 4) settings.AutoRefreshTimeMin = 4;
4549
settings.AutoNPCWood = int.Parse(AutoNPCWood);
4650
settings.AutoNPCClay = int.Parse(AutoNPCClay);
4751
settings.AutoNPCIron = int.Parse(AutoNPCIron);

0 commit comments

Comments
 (0)