@@ -168,7 +168,7 @@ public static List<string> SetPrereqCombo(Account acc, Village vill)
168
168
169
169
public static void ReStartBuilding ( Account acc , Village vill )
170
170
{
171
- RemoveCompletedTasks ( vill , acc ) ;
171
+ RemoveCompletedTasks ( vill ) ;
172
172
//remove ongoing building task for this village
173
173
acc . Tasks . Remove ( typeof ( UpgradeBuilding ) , vill ) ;
174
174
@@ -198,12 +198,12 @@ public static void ReStartDemolishing(Account acc, Village vill)
198
198
//check cranny/warehouse/grannary/trapper/GG/GW
199
199
switch ( building )
200
200
{
201
- case BuildingEnum . Warehouse : return BuildingIsOnLevel ( BuildingEnum . Warehouse , 20 , vill ) ;
202
- case BuildingEnum . Granary : return BuildingIsOnLevel ( BuildingEnum . Granary , 20 , vill ) ;
203
- case BuildingEnum . GreatWarehouse : return BuildingIsOnLevel ( BuildingEnum . GreatWarehouse , 20 , vill ) ;
204
- case BuildingEnum . GreatGranary : return BuildingIsOnLevel ( BuildingEnum . GreatGranary , 20 , vill ) ;
205
- case BuildingEnum . Trapper : return BuildingIsOnLevel ( BuildingEnum . Trapper , 20 , vill ) ;
206
- case BuildingEnum . Cranny : return BuildingIsOnLevel ( BuildingEnum . Cranny , 10 , vill ) ;
201
+ case BuildingEnum . Warehouse : return BuildingAboveLevel ( BuildingEnum . Warehouse , 20 , vill ) ;
202
+ case BuildingEnum . Granary : return BuildingAboveLevel ( BuildingEnum . Granary , 20 , vill ) ;
203
+ case BuildingEnum . GreatWarehouse : return BuildingAboveLevel ( BuildingEnum . GreatWarehouse , 20 , vill ) ;
204
+ case BuildingEnum . GreatGranary : return BuildingAboveLevel ( BuildingEnum . GreatGranary , 20 , vill ) ;
205
+ case BuildingEnum . Trapper : return BuildingAboveLevel ( BuildingEnum . Trapper , 20 , vill ) ;
206
+ case BuildingEnum . Cranny : return BuildingAboveLevel ( BuildingEnum . Cranny , 10 , vill ) ;
207
207
default : return false ;
208
208
}
209
209
}
@@ -220,11 +220,13 @@ public static void ReStartDemolishing(Account acc, Village vill)
220
220
return true ;
221
221
}
222
222
223
- private static bool BuildingIsOnLevel ( BuildingEnum building , int lvl , Village vill )
223
+ /// <summary>
224
+ /// Whether there's a building above specific level or there's a task for this building
225
+ /// </summary>
226
+ public static bool BuildingAboveLevel ( BuildingEnum building , int lvl , Village vill )
224
227
{
225
- //if there already is a building on specific level or there is a task for this building
226
- // TODO: change FristOrDefault to Any
227
- return ( vill . Build . Buildings . FirstOrDefault ( x => x . Level == lvl && x . Type == building ) != null || vill . Build . Tasks . FirstOrDefault ( x => x . Level == lvl && x . Building == building ) != null ) ;
228
+ return ( vill . Build . Buildings . Any ( x => x . Type == building && lvl <= x . Level ) ||
229
+ vill . Build . Tasks . Any ( x => x . Building == building && lvl <= x . Level ) ) ;
228
230
}
229
231
230
232
/// <summary>
@@ -254,7 +256,7 @@ public static bool RemoveFinishedCB(Village vill)
254
256
}
255
257
256
258
/// <summary>
257
- /// Used by building task to get the url for navigation
259
+ /// Used by building task to get the url for navigation (for TTWars only)
258
260
/// </summary>
259
261
/// <param name="vill">Village</param>
260
262
/// <param name="task">BotTask</param>
@@ -272,7 +274,7 @@ public static (string, bool) GetUrlForBuilding(Account acc, Village vill, Buildi
272
274
return ( null , true ) ;
273
275
}
274
276
275
- public static bool IsTaskCompleted ( Village vill , Account acc , BuildingTask task )
277
+ public static bool IsTaskCompleted ( Village vill , BuildingTask task )
276
278
{
277
279
if ( vill == null ) return true ;
278
280
var building = vill . Build . Buildings . FirstOrDefault ( x => x . Id == task . BuildingId ) ;
@@ -336,7 +338,7 @@ private static (string, bool) GetUrlGeneralTask(Village vill, BuildingTask task)
336
338
}
337
339
else // there's already a building in this spot, construct a building elsewhere
338
340
{
339
- if ( ! BuildingHelper . FindBuildingId ( vill , task ) )
341
+ if ( ! FindBuildingId ( vill , task ) )
340
342
{
341
343
return ( null , false ) ;
342
344
}
@@ -548,62 +550,38 @@ public static bool IsWall(BuildingEnum building)
548
550
/// </summary>
549
551
/// <param name="vill">Village</param>
550
552
/// <param name="acc">Account</param>
551
- public static void RemoveCompletedTasks ( Village vill , Account acc ) =>
552
- vill . Build . Tasks . RemoveAll ( task => IsTaskCompleted ( vill , acc , task ) ) ;
553
-
554
- /// <summary>
555
- /// When you already build one warehouse to lv 1, you want import template but its warehouse in another poistion
556
- /// This will fix it to current
557
- /// </summary>
558
- /// <param name="vill"></param>
559
- /// <param name="acc"></param>
560
- public static void FixPositionBuilding ( Village vill , Account acc )
561
- {
562
- }
553
+ public static void RemoveCompletedTasks ( Village vill ) =>
554
+ vill . Build . Tasks . RemoveAll ( task => IsTaskCompleted ( vill , task ) ) ;
563
555
556
+
564
557
#region Functions for auto-building resource fields
565
558
566
559
public static Building FindLowestLevelBuilding ( List < Building > buildings )
567
560
{
568
- // TODO: test after implementation
569
- //return buildings
570
- // .OrderBy(x => x.Level + (x.UnderConstruction ? 1 : 0))
571
- // .FirstOrDefault();
572
-
573
- if ( buildings . Count == 0 ) return null ;
574
- int lowestLvl = 100 ;
575
- Building lowestBuilding = new Building ( ) ;
576
- for ( int i = 0 ; i < buildings . Count ; i ++ )
577
- {
578
- var buildingLevel = buildings [ i ] . Level ;
579
- if ( buildings [ i ] . UnderConstruction ) buildingLevel ++ ;
580
- if ( lowestLvl > buildingLevel )
581
- {
582
- lowestLvl = buildingLevel ;
583
- lowestBuilding = buildings [ i ] ;
584
- }
585
- }
586
- return lowestBuilding ;
561
+ return buildings
562
+ . OrderBy ( x => x . Level + ( x . UnderConstruction ? 1 : 0 ) )
563
+ . FirstOrDefault ( ) ;
587
564
}
588
565
589
- private static Building GetLowestProduction ( List < Building > buildings , Village vill )
566
+ public static Building GetLowestProduction ( List < Building > buildings , Village vill )
590
567
{
591
- //get distinct field types
568
+ // Get distinct field types
592
569
var distinct = buildings . Select ( x => x . Type ) . Distinct ( ) . ToList ( ) ;
593
- long lowestProd = long . MaxValue ;
594
- BuildingEnum toUpgrade = BuildingEnum . Cropland ;
595
570
596
- foreach ( var distinctType in distinct )
571
+ var prodArr = vill . Res . Production . ToArray ( ) ;
572
+ var dict = new Dictionary < BuildingEnum , long > ( ) ;
573
+ for ( int i = 0 ; i < 4 ; i ++ )
597
574
{
598
- if ( distinctType == BuildingEnum . Woodcutter && vill . Res . Production . Wood < lowestProd ) { lowestProd = vill . Res . Production . Wood ; toUpgrade = BuildingEnum . Woodcutter ; }
599
- if ( distinctType == BuildingEnum . ClayPit && vill . Res . Production . Clay < lowestProd ) { lowestProd = vill . Res . Production . Clay ; toUpgrade = BuildingEnum . ClayPit ; }
600
- if ( distinctType == BuildingEnum . IronMine && vill . Res . Production . Iron < lowestProd ) { lowestProd = vill . Res . Production . Iron ; toUpgrade = BuildingEnum . IronMine ; }
601
- if ( distinctType == BuildingEnum . Cropland && vill . Res . Production . Crop < lowestProd ) { lowestProd = vill . Res . Production . Crop ; toUpgrade = BuildingEnum . Cropland ; }
575
+ var resField = ( BuildingEnum ) i + 1 ;
576
+ if ( ! distinct . Any ( x => x == resField ) ) continue ;
577
+ dict . Add ( resField , prodArr [ i ] ) ;
602
578
}
579
+
580
+ var toUpgrade = dict . First ( x => x . Value == dict . Min ( y => y . Value ) ) . Key ;
603
581
return FindLowestLevelBuilding ( buildings . Where ( x => x . Type == toUpgrade ) . ToList ( ) ) ;
604
582
}
605
583
606
- private static Building GetLowestRes ( Account acc , Village vill , List < Building > buildings )
584
+ public static Building GetLowestRes ( Account acc , Village vill , List < Building > buildings )
607
585
{
608
586
//get distinct field types
609
587
var distinct = buildings . Select ( x => x . Type ) . Distinct ( ) . ToList ( ) ;
0 commit comments