@ -14,18 +14,20 @@ public class BuildOrderService : IBuildOrderService
{
{
private readonly BuildOrderModel _ buildOrder = new ( ) ;
private readonly BuildOrderModel _ buildOrder = new ( ) ;
private readonly ITimingService _ timingService ;
private readonly IToastService _ toastService ;
private readonly IToastService _ toastService ;
private int _l astInterval ;
private int _l astInterval ;
public BuildOrderService ( IToastService toastService )
public BuildOrderService ( IToastService toastService , ITimingService timingService )
{
{
_ toastService = toastService ;
_ toastService = toastService ;
_ timingService = timingService ;
Reset ( ) ;
Reset ( ) ;
}
}
public int BuildingInputDelay { get ; set ; } = 2 ;
public Dictionary < int , List < EntityModel > > StartedOrders = > _ buildOrder . StartedOrders ;
public Dictionary < int , List < EntityModel > > StartedOrders = > _ buildOrder . StartedOrders ;
public Dictionary < int , List < EntityModel > > CompletedOrders = > _ buildOrder . CompletedOrders ;
public Dictionary < int , List < EntityModel > > CompletedOrders = > _ buildOrder . CompletedOrders ;
public Dictionary < string , int > UniqueCompletedTimes = > _ buildOrder . UniqueCompletedTimes ;
public Dictionary < string , int > UniqueCompletedTimes = > _ buildOrder . UniqueCompletedTimes ;
@ -241,14 +243,12 @@ public class BuildOrderService : IBuildOrderService
_ buildOrder . UniqueCompleted [ entityRemoved . DataType ]
_ buildOrder . UniqueCompleted [ entityRemoved . DataType ]
. Remove ( _ buildOrder . UniqueCompleted [ entityRemoved . DataType ] . Last ( ) ) ;
. Remove ( _ buildOrder . UniqueCompleted [ entityRemoved . DataType ] . Last ( ) ) ;
if ( entityRemoved . Production ( ) ! = null
if ( entityRemoved . Production ( ) ! = null
& & entityRemoved . Production ( ) ! . ProducedBy ! = null
& & entityRemoved . Production ( ) ! . ProducedBy ! = null
& & entityRemoved . Supply ( ) ! = null
& & entityRemoved . Supply ( ) ! = null
& & entityRemoved . Supply ( ) ! . Takes > 0 )
& & entityRemoved . Supply ( ) ! . Takes > 0 )
{
_ buildOrder . TrainingCapacityUsed . Remove ( _ buildOrder . TrainingCapacityUsed . Last ( ) ) ;
_ buildOrder . TrainingCapacityUsed . Remove ( _ buildOrder . TrainingCapacityUsed . Last ( ) ) ;
}
if ( entityRemoved . Info ( ) . Descriptive = = DescriptiveType . Worker )
if ( entityRemoved . Info ( ) . Descriptive = = DescriptiveType . Worker )
{
{
RemoveLast ( ) ;
RemoveLast ( ) ;
@ -339,7 +339,7 @@ public class BuildOrderService : IBuildOrderService
public int? WillMeetTrainingQueue ( EntityModel entity )
public int? WillMeetTrainingQueue ( EntityModel entity )
{
{
Console . WriteLine ( $"WillMeetTrainingQueue {entity.Info().Name}" ) ;
Console . WriteLine ( $"WillMeetTrainingQueue {entity.Info().Name}" ) ;
var supply = entity . Supply ( ) ;
var supply = entity . Supply ( ) ;
var production = entity . Production ( ) ;
var production = entity . Production ( ) ;
@ -348,7 +348,7 @@ public class BuildOrderService : IBuildOrderService
if ( supply = = null | | production = = null | | supply . Takes . Equals ( 0 ) )
if ( supply = = null | | production = = null | | supply . Takes . Equals ( 0 ) )
{
{
Console . WriteLine ( supply = = null ? "Was Null" : supply . Takes ) ;
Console . WriteLine ( supply = = null ? "Was Null" : supply . Takes ) ;
return 1 ;
return 1 ;
}
}
@ -358,47 +358,50 @@ public class BuildOrderService : IBuildOrderService
Console . WriteLine ( "Produced by Nothing" ) ;
Console . WriteLine ( "Produced by Nothing" ) ;
return 1 ;
return 1 ;
}
}
var uniqueCompleted = _ buildOrder . UniqueCompleted [ producedBy ] ;
var uniqueCompleted = _ buildOrder . UniqueCompleted [ producedBy ] ;
var shortestIncrement = int . MaxValue ;
var shortestIncrement = int . MaxValue ;
var trainingSlots = 0 ;
var trainingSlots = 0 ;
bool didDelay = false ;
var didDelay = false ;
foreach ( var productionEntity in uniqueCompleted ) trainingSlots + = productionEntity . Supply ( ) ! . Grants ;
foreach ( var productionEntity in uniqueCompleted ) trainingSlots + = productionEntity . Supply ( ) ! . Grants ;
while ( true )
while ( true )
{
{
var usedSlots = 0 ;
var usedSlots = 0 ;
foreach ( var used in _ buildOrder . TrainingCapacityUsed )
foreach ( var used in _ buildOrder . TrainingCapacityUsed )
if ( checkedInterval > = used . StartingUsageTime & & checkedInterval < used . StopUsageTime )
if ( checkedInterval > = used . StartingUsageTime & & checkedInterval < used . StopUsageTime )
{
{
usedSlots + = used . UsedSlots ;
usedSlots + = used . UsedSlots ;
var duration = used . StopUsageTime - used . StartingUsageTime ;
var duration = used . StopUsageTime - used . StartingUsageTime ;
if ( duration < shortestIncrement ) shortestIncrement = duration ;
if ( duration < shortestIncrement ) shortestIncrement = duration ;
Console . WriteLine ( $"Used slots {used.UsedSlots} Duration {duration} Start {used.StartingUsageTime} Stop {used.StopUsageTime} " ) ;
Console . WriteLine (
$"Used slots {used.UsedSlots} Duration {duration} Start {used.StartingUsageTime} Stop {used.StopUsageTime} " ) ;
}
}
if ( usedSlots + supply . Takes < = trainingSlots )
if ( usedSlots + supply . Takes < = trainingSlots )
{
{
if ( didDelay )
if ( didDelay )
{
_ toastService . AddToast ( new ToastModel
_ toastService . AddToast ( new ToastModel { Title = "Waited" , SeverityType = SeverityType . Information , Message = $"Had to wait {checkedInterval - _lastInterval}s for Training Queue." } ) ;
{
}
Title = "Waited" , SeverityType = SeverityType . Information ,
Message = $"Had to wait {checkedInterval - _lastInterval}s for Training Queue."
} ) ;
Console . WriteLine ( $"Time {checkedInterval} did Delay {didDelay}" ) ;
Console . WriteLine ( $"Time {checkedInterval} did Delay {didDelay}" ) ;
return checkedInterval ;
return checkedInterval ;
}
}
checkedInterval + = shortestIncrement ;
checkedInterval + = shortestIncrement ;
didDelay = true ;
didDelay = true ;
if ( shortestIncrement = = int . MaxValue )
if ( shortestIncrement = = int . MaxValue )
{
{
Console . WriteLine ( "MaxValue" ) ;
Console . WriteLine ( $"MaxValue" ) ;
return null ;
return null ;
}
}
}
}
@ -421,7 +424,7 @@ public class BuildOrderService : IBuildOrderService
{
{
atInterval = interval ;
atInterval = interval ;
if ( entity . EntityType ! = EntityType . Army ) atInterval + = BuildingInputDelay ;
if ( entity . EntityType ! = EntityType . Army ) atInterval + = _ timingService . BuildingInputDelay ;
return true ;
return true ;
}
}