@@ -43,52 +43,64 @@ public Result<PlanTask> NextBuildingTask(int accountId, int villageId)
43
43
var tasks = _planManager . GetList ( villageId ) ;
44
44
if ( tasks . Count == 0 )
45
45
{
46
- return Result . Fail ( "Queue is empty." ) ;
46
+ return Result . Fail ( new Skip ( "Queue is empty." ) ) ;
47
47
}
48
+
48
49
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 ;
51
61
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 )
53
70
{
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
+ }
58
73
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 ) ;
66
76
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 ) ;
71
80
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 )" ) ) ;
88
90
}
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 ) ;
89
103
}
90
-
91
- return GetFirstTask ( villageId ) ;
92
104
}
93
105
94
106
public PlanTask ExtractResField ( int villageId , PlanTask buildingTask )
@@ -136,17 +148,17 @@ public PlanTask ExtractResField(int villageId, PlanTask buildingTask)
136
148
public void RemoveFinishedCB ( int villageId )
137
149
{
138
150
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 ) ;
140
152
141
153
if ( ! tasksDone . Any ( ) ) return ;
142
154
143
155
foreach ( var taskDone in tasksDone )
144
156
{
145
157
var building = context . VillagesBuildings . Find ( villageId , taskDone . Location ) ;
146
- if ( building == null )
158
+ if ( building is null )
147
159
{
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 ;
150
162
}
151
163
152
164
if ( building . Level < taskDone . Level ) building . Level = taskDone . Level ;
@@ -165,7 +177,7 @@ private PlanTask GetFirstResTask(int villageId)
165
177
return task ;
166
178
}
167
179
168
- private PlanTask GetFirstInfrastructureTask ( int villageId )
180
+ private PlanTask GetFirstBuildingTask ( int villageId )
169
181
{
170
182
var tasks = _planManager . GetList ( villageId ) ;
171
183
var infrastructureTasks = tasks . Where ( x => x . Type == PlanTypeEnums . General && ! x . Building . IsResourceField ( ) ) ;
@@ -192,7 +204,7 @@ private bool IsInfrastructureTaskVaild(int villageId, PlanTask planTask)
192
204
var buildings = context . VillagesBuildings . Where ( x => x . VillageId == villageId ) . ToList ( ) ;
193
205
foreach ( var prerequisiteBuilding in prerequisiteBuildings )
194
206
{
195
- var building = buildings . FirstOrDefault ( x => x . Type == prerequisiteBuilding . Building ) ;
207
+ var building = buildings . OrderByDescending ( x => x . Level ) . FirstOrDefault ( x => x . Type == prerequisiteBuilding . Building ) ;
196
208
if ( building is null ) return false ;
197
209
if ( building . Level < prerequisiteBuilding . Level ) return false ;
198
210
}
0 commit comments