feat(BuildCalc) Wait To button and some WIP code

This commit is contained in:
2022-04-19 23:22:19 -04:00
parent af03cae560
commit 89e12a8de0
27 changed files with 371 additions and 334 deletions
+92 -47
View File
@@ -12,11 +12,15 @@ namespace Services.Immortal;
public class BuildOrderService : IBuildOrderService
{
private BuildOrderModel buildOrder = new();
private readonly BuildOrderModel buildOrder = new();
private int lastInterval;
public BuildOrderService()
private readonly IToastService toastService;
public BuildOrderService(IToastService toastService)
{
this.toastService = toastService;
Reset();
}
@@ -47,6 +51,7 @@ public class BuildOrderService : IBuildOrderService
OnChange -= action;
}
public void Add(EntityModel entity, int atInterval)
{
if (!buildOrder.StartedOrders.ContainsKey(atInterval))
@@ -74,13 +79,23 @@ public class BuildOrderService : IBuildOrderService
else
buildOrder.UniqueCompletedCount[entity.DataType]++;
//entity.
if (!buildOrder.UniqueCompleted.ContainsKey(entity.DataType))
buildOrder.UniqueCompleted.Add(entity.DataType, new Dictionary<int, List<EntityModel>>());
if (!buildOrder.UniqueCompleted[entity.DataType].ContainsKey(completedTime))
buildOrder.UniqueCompleted[entity.DataType].Add(completedTime, new List<EntityModel>());
buildOrder.UniqueCompleted[entity.DataType][completedTime].Add(entity);
if (supply != null)
{
if (!supply.Takes.Equals(0)) buildOrder.CurrentSupplyUsed += supply.Takes;
if (!supply.Grants.Equals(0))
buildOrder.SupplyCountTimes.Add(buildOrder.SupplyCountTimes.Last().Key + supply.Grants, completedTime);
}
if (atInterval > lastInterval) lastInterval = atInterval;
NotifyDataChanged();
@@ -88,28 +103,44 @@ public class BuildOrderService : IBuildOrderService
public bool AddWait(int forInterval)
{
if (forInterval == 0)
if (forInterval < 0)
{
toastService.AddToast(new ToastModel(){SeverityType = SeverityType.Error, Title = "Wait", Message = "This should never happen."});
return false;
}
};
lastInterval += forInterval;
if (!buildOrder.StartedOrders.ContainsKey(lastInterval))
buildOrder.StartedOrders.Add(lastInterval, new List<EntityModel>());
if (!buildOrder.CompletedOrders.ContainsKey(lastInterval))
buildOrder.CompletedOrders.Add(lastInterval, new List<EntityModel>());
NotifyDataChanged();
return true;
}
public bool AddWaitTo(int interval)
{
throw new NotImplementedException();
if (interval <= lastInterval)
{
toastService.AddToast(new ToastModel(){SeverityType = SeverityType.Error, Title = "Logic Error", Message = "You cannot wait to a time that has already elapsed."});
return false;
}
lastInterval = interval;
if (!buildOrder.StartedOrders.ContainsKey(lastInterval))
buildOrder.StartedOrders.Add(lastInterval, new List<EntityModel>());
if (!buildOrder.CompletedOrders.ContainsKey(lastInterval))
buildOrder.CompletedOrders.Add(lastInterval, new List<EntityModel>());
NotifyDataChanged();
return true;
}
public int? WillMeetRequirements(EntityModel entity)
{
var requirements = entity.Requirements();
@@ -142,32 +173,15 @@ public class BuildOrderService : IBuildOrderService
return null;
}
public int? WillMeetTrainingQueue(EntityModel entity)
{
var supply = entity.Supply();
if (supply == null || supply.Takes.Equals(0)) return 0;
// TODO: Finish Training Queue Logic
foreach (var supplyAtTime in buildOrder.SupplyCountTimes)
if (supply.Takes + buildOrder.CurrentSupplyUsed < supplyAtTime.Key)
return supplyAtTime.Value;
return null;
}
public bool Add(EntityModel entity, IEconomyService withEconomy, IToastService withToasts)
public bool Add(EntityModel entity, IEconomyService withEconomy)
{
var atInterval = lastInterval;
if (!HandleSupply(entity, withToasts, ref atInterval)) return false;
if (!HandleRequirements(entity, withToasts, ref atInterval)) return false;
if (!HandleEconomy(entity, withEconomy, withToasts, ref atInterval)) return false;
if (!HandleSupply(entity, ref atInterval)) return false;
if (!HandleRequirements(entity, ref atInterval)) return false;
if (!HandleEconomy(entity, withEconomy, ref atInterval)) return false;
Add(entity, atInterval);
@@ -186,7 +200,7 @@ public class BuildOrderService : IBuildOrderService
lastInterval = buildOrder.StartedOrders.Last().Key;
return;
}
var lastStarted = buildOrder.StartedOrders.Keys.Last();
var lastCompleted = buildOrder.CompletedOrders.Keys.Last();
@@ -212,7 +226,8 @@ public class BuildOrderService : IBuildOrderService
if (entityRemoved.Supply()?.Takes > 0)
buildOrder.CurrentSupplyUsed -= entityRemoved.Supply()!.Takes;
buildOrder.UniqueCompletedCount[entityRemoved!.DataType]--;
if (buildOrder.UniqueCompletedCount[entityRemoved!.DataType] == 0)
@@ -291,7 +306,6 @@ public class BuildOrderService : IBuildOrderService
public void DeprecatedSetColor(string color)
{
}
public string GetColor()
@@ -306,10 +320,40 @@ public class BuildOrderService : IBuildOrderService
NotifyDataChanged();
}
public bool AddWaitTo(int interval, TimingService timingService)
{
if (lastInterval >= interval) return false;
if (interval > timingService.GetAttackTime()) return false;
if (!buildOrder.StartedOrders.ContainsKey(lastInterval))
buildOrder.StartedOrders.Add(lastInterval, new List<EntityModel>());
if (!buildOrder.CompletedOrders.ContainsKey(lastInterval))
buildOrder.CompletedOrders.Add(lastInterval, new List<EntityModel>());
NotifyDataChanged();
return true;
}
public int? WillMeetTrainingQueue(EntityModel entity)
{
var supply = entity.Supply();
if (supply == null || supply.Takes.Equals(0)) return 0;
foreach (var supplyAtTime in buildOrder.SupplyCountTimes)
if (supply.Takes + buildOrder.CurrentSupplyUsed < supplyAtTime.Key)
return supplyAtTime.Value;
return null;
}
private event Action OnChange = null!;
private bool HandleEconomy(EntityModel entity, IEconomyService withEconomy, IToastService withToasts,
ref int atInterval)
private bool HandleEconomy(EntityModel entity, IEconomyService withEconomy, ref int atInterval)
{
var production = entity.Production();
@@ -318,8 +362,9 @@ public class BuildOrderService : IBuildOrderService
for (var interval = atInterval; interval < withEconomy.GetOverTime().Count; interval++)
{
var economyAtSecond = withEconomy.GetOverTime()[interval];
if (economyAtSecond.Alloy >= production.Alloy && economyAtSecond.Ether >= production.Ether &&
economyAtSecond.Pyre >= production.Pyre)
if (economyAtSecond.Alloy >= production.Alloy
&& economyAtSecond.Ether >= production.Ether
&& economyAtSecond.Pyre >= production.Pyre)
{
atInterval = interval;
@@ -330,14 +375,14 @@ public class BuildOrderService : IBuildOrderService
}
if (withEconomy.GetOverTime().Last().Ether < production.Ether)
withToasts.AddToast(new ToastModel
toastService.AddToast(new ToastModel
{
Title = "Not Enough Ether", Message = "Build more ether extractors!",
SeverityType = SeverityType.Error
});
if (withEconomy.GetOverTime().Last().Alloy < production.Alloy)
withToasts.AddToast(new ToastModel
toastService.AddToast(new ToastModel
{
Title = "Not Enough Alloy", Message = "Build more bases!",
SeverityType = SeverityType.Error
@@ -346,12 +391,12 @@ public class BuildOrderService : IBuildOrderService
return false;
}
private bool HandleSupply(EntityModel entity, IToastService withToasts, ref int atInterval)
private bool HandleSupply(EntityModel entity, ref int atInterval)
{
var minSupplyInterval = WillMeetSupply(entity);
if (minSupplyInterval == null)
{
withToasts.AddToast(new ToastModel
toastService.AddToast(new ToastModel
{
Title = "Supply Cap Reached", Message = "Build more supply!",
SeverityType = SeverityType.Error
@@ -365,12 +410,12 @@ public class BuildOrderService : IBuildOrderService
return true;
}
private bool HandleTrainingQueue(EntityModel entity, IToastService withToasts, ref int atInterval)
private bool HandleTrainingQueue(EntityModel entity, ref int atInterval)
{
var minSupplyInterval = WillMeetSupply(entity);
if (minSupplyInterval == null)
{
withToasts.AddToast(new ToastModel
toastService.AddToast(new ToastModel
{
Title = "Supply Cap Reached", Message = "Build more supply!",
SeverityType = SeverityType.Error
@@ -384,13 +429,13 @@ public class BuildOrderService : IBuildOrderService
return true;
}
private bool HandleRequirements(EntityModel entity, IToastService withToasts, ref int atInterval)
private bool HandleRequirements(EntityModel entity, ref int atInterval)
{
var minRequirementInterval = WillMeetRequirements(entity);
if (minRequirementInterval == null)
{
withToasts.AddToast(new ToastModel
toastService.AddToast(new ToastModel
{
Title = "Missing Requirements", Message = "You don't have what's needed for this unit.",
SeverityType = SeverityType.Error
@@ -2,7 +2,6 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Model.BuildOrders;
using Model.Entity;
using YamlDotNet.Serialization;
namespace Services.Immortal;
+94 -109
View File
@@ -8,23 +8,98 @@ namespace Services.Immortal;
public class EconomyComparisionService : IEconomyComparisonService
{
public List<BuildToCompareModel> BuildsToCompare { get; set; }
private readonly int IntervalMax = 1024;
public EconomyComparisionService()
{
BuildsToCompare = new List<BuildToCompareModel>()
BuildsToCompare = new List<BuildToCompareModel>
{
new BuildToCompareModel { NumberOfTownHallExpansions = 0, Faction = DataType.FACTION_Aru, ChartColor = "green"},
new BuildToCompareModel { NumberOfTownHallExpansions = 0, Faction = DataType.FACTION_Aru, ChartColor = "red"}
new() { NumberOfTownHallExpansions = 0, Faction = DataType.FACTION_Aru, ChartColor = "green" },
new() { NumberOfTownHallExpansions = 0, Faction = DataType.FACTION_Aru, ChartColor = "red" }
};
BuildsToCompare[0].EconomyOverTimeModel = CalculateEconomy(BuildsToCompare[0], 0);
BuildsToCompare[1].EconomyOverTimeModel = CalculateEconomy(BuildsToCompare[1], 0);
BuildsToCompare[0].EconomyOverTimeModel = CalculateEconomy(BuildsToCompare[0]);
BuildsToCompare[1].EconomyOverTimeModel = CalculateEconomy(BuildsToCompare[1]);
}
public List<BuildToCompareModel> BuildsToCompare { get; set; }
public void ChangeNumberOfTownHalls(int forPlayer, int toCount)
{
if (BuildsToCompare[forPlayer].NumberOfTownHallExpansions == toCount) return;
BuildsToCompare[forPlayer].NumberOfTownHallExpansions = toCount;
CalculateBuildOrder(BuildsToCompare[forPlayer]);
NotifyDataChanged();
}
public void ChangeTownHallTiming(int forPlayer, int forTownHall, int toTiming)
{
if (BuildsToCompare[forPlayer].TimeToBuildTownHall[forTownHall] == toTiming) return;
BuildsToCompare[forPlayer].TimeToBuildTownHall[forTownHall] = toTiming;
CalculateBuildOrder(BuildsToCompare[forPlayer]);
NotifyDataChanged();
}
public int GetTownHallCount(int forPlayer)
{
return BuildsToCompare[forPlayer].NumberOfTownHallExpansions;
}
public int GetTownHallBuildTime(int forPlayer, int forTownHall)
{
return BuildsToCompare[forPlayer].TimeToBuildTownHall[forTownHall];
}
public List<int> GetTownHallBuildTimes(int forPlayer)
{
return BuildsToCompare[forPlayer].TimeToBuildTownHall;
}
public void ChangeFaction(int forPlayer, string toFaction)
{
if (BuildsToCompare[forPlayer].Faction.Equals(toFaction)) return;
BuildsToCompare[forPlayer].Faction = toFaction;
NotifyDataChanged();
}
public string GetFaction(int forPlayer)
{
return BuildsToCompare[forPlayer].Faction;
}
public void ChangeColor(int forPlayer, string toColor)
{
if (BuildsToCompare[forPlayer].ChartColor.Equals(toColor)) return;
BuildsToCompare[forPlayer].ChartColor = toColor;
NotifyDataChanged();
}
public string GetColor(int forPlayer)
{
return BuildsToCompare[forPlayer].ChartColor;
}
public void Subscribe(Action action)
{
OnChange += action;
}
public void Unsubscribe(Action action)
{
OnChange -= action;
}
void CalculateBuildOrder(BuildToCompareModel buildToCompare)
private void CalculateBuildOrder(BuildToCompareModel buildToCompare)
{
buildToCompare.BuildOrderModel = new BuildOrderModel(buildToCompare.Faction);
@@ -36,22 +111,23 @@ public class EconomyComparisionService : IEconomyComparisonService
Add(townHall, buildToCompare, time);
Add(townHallMining2, buildToCompare, time + townHall.Production()!.BuildTime);
Add(townHallMining3, buildToCompare, time + townHall.Production()!.BuildTime + townHallMining2.Production()!.BuildTime);
Add(townHallMining3, buildToCompare,
time + townHall.Production()!.BuildTime + townHallMining2.Production()!.BuildTime);
}
CalculateEconomy(buildToCompare, 0);
CalculateEconomy(buildToCompare);
}
public void Add(EntityModel entityModel, BuildToCompareModel buildToCompare, int atInterval)
{
BuildOrderModel buildOrder = buildToCompare.BuildOrderModel;
var buildOrder = buildToCompare.BuildOrderModel;
if (!buildOrder.StartedOrders.ContainsKey(atInterval))
buildOrder.StartedOrders.Add(atInterval, new List<EntityModel>());
var production = entityModel.Production();
var completedTime = atInterval;
if (production != null) completedTime += production.BuildTime;
@@ -64,17 +140,12 @@ public class EconomyComparisionService : IEconomyComparisonService
NotifyDataChanged();
}
private int IntervalMax = 1024;
private List<EconomyModel> CalculateEconomy(BuildToCompareModel buildToCompare, int fromInterval = 0)
{
// We don't consider things mining at zero seconds
if (fromInterval == 0)
{
fromInterval = 1;
}
BuildOrderModel buildOrder = buildToCompare.BuildOrderModel;
if (fromInterval == 0) fromInterval = 1;
var buildOrder = buildToCompare.BuildOrderModel;
List<EconomyModel> buildEconomyOverTime = buildToCompare.EconomyOverTimeModel;
@@ -195,93 +266,7 @@ public class EconomyComparisionService : IEconomyComparisonService
return buildEconomyOverTime;
}
public void ChangeNumberOfTownHalls(int forPlayer, int toCount)
{
if (BuildsToCompare[forPlayer].NumberOfTownHallExpansions == toCount)
{
return;
}
BuildsToCompare[forPlayer].NumberOfTownHallExpansions = toCount;
CalculateBuildOrder(BuildsToCompare[forPlayer]);
NotifyDataChanged();
}
public void ChangeTownHallTiming(int forPlayer, int forTownHall, int toTiming)
{
if (BuildsToCompare[forPlayer].TimeToBuildTownHall[forTownHall] == toTiming)
{
return;
}
BuildsToCompare[forPlayer].TimeToBuildTownHall[forTownHall] = toTiming;
CalculateBuildOrder(BuildsToCompare[forPlayer]);
NotifyDataChanged();
}
public int GetTownHallCount(int forPlayer)
{
return BuildsToCompare[forPlayer].NumberOfTownHallExpansions;
}
public int GetTownHallBuildTime(int forPlayer, int forTownHall)
{
return BuildsToCompare[forPlayer].TimeToBuildTownHall[forTownHall];
}
public List<int> GetTownHallBuildTimes(int forPlayer)
{
return BuildsToCompare[forPlayer].TimeToBuildTownHall;
}
public void ChangeFaction(int forPlayer, string toFaction)
{
if (BuildsToCompare[forPlayer].Faction.Equals(toFaction))
{
return;
}
BuildsToCompare[forPlayer].Faction = toFaction;
NotifyDataChanged();
}
public string GetFaction(int forPlayer)
{
return BuildsToCompare[forPlayer].Faction;
}
public void ChangeColor(int forPlayer, string toColor)
{
if (BuildsToCompare[forPlayer].ChartColor.Equals(toColor))
{
return;
}
BuildsToCompare[forPlayer].ChartColor = toColor;
NotifyDataChanged();
}
public string GetColor(int forPlayer)
{
return BuildsToCompare[forPlayer].ChartColor;
}
public void Subscribe(Action action)
{
OnChange += action;
}
public void Unsubscribe(Action action)
{
OnChange -= action;
}
private event Action OnChange = null!;
private void NotifyDataChanged()
+22 -28
View File
@@ -1,5 +1,4 @@
using Model.BuildOrders;
using Model.Economy;
using Model.Economy;
using Model.Entity;
using Model.Types;
@@ -8,7 +7,7 @@ namespace Services.Immortal;
public class EconomyService : IEconomyService
{
private List<EconomyModel> buildEconomyOverTime = null!;
private Dictionary<string, List<EconomyModel>> specficEconomiesOverTime = null!;
public List<EconomyModel> GetOverTime()
@@ -29,11 +28,8 @@ public class EconomyService : IEconomyService
public void Calculate(IBuildOrderService buildOrder, ITimingService timing, int fromInterval)
{
// We don't consider things mining at zero seconds
if (fromInterval == 0)
{
fromInterval = 1;
}
if (fromInterval == 0) fromInterval = 1;
//TODO Break all this up
if (buildEconomyOverTime == null)
{
@@ -44,7 +40,8 @@ public class EconomyService : IEconomyService
if (buildEconomyOverTime.Count > timing.GetAttackTime())
buildEconomyOverTime.RemoveRange(timing.GetAttackTime(), buildEconomyOverTime.Count - timing.GetAttackTime());
buildEconomyOverTime.RemoveRange(timing.GetAttackTime(),
buildEconomyOverTime.Count - timing.GetAttackTime());
while (buildEconomyOverTime.Count < timing.GetAttackTime())
buildEconomyOverTime.Add(new EconomyModel { Interval = buildEconomyOverTime.Count - 1 });
@@ -52,14 +49,14 @@ public class EconomyService : IEconomyService
for (var interval = fromInterval; interval < timing.GetAttackTime(); interval++)
{
buildEconomyOverTime[interval] = new EconomyModel();
var economyAtSecond = buildEconomyOverTime[interval];
CarryOverEconomyFromPreviousInterval(interval, economyAtSecond);
SetupCurrentInverval(buildOrder, economyAtSecond, interval);
AddPassivePyreGain(interval, economyAtSecond);
float freeWorkers = economyAtSecond.WorkerCount - economyAtSecond.BusyWorkerCount;
float freeWorkers = economyAtSecond.WorkerCount - economyAtSecond.BusyWorkerCount;
var workersNeeded = AddFundsFromHarvestPoints(economyAtSecond, freeWorkers);
workersNeeded -= CalculateCreatingWorkerCosts(economyAtSecond);
MakeNeededNewWorkersRequests(workersNeeded, economyAtSecond);
@@ -70,6 +67,14 @@ public class EconomyService : IEconomyService
NotifyDataChanged();
}
public EconomyModel GetEconomy(int atInterval)
{
if (atInterval >= buildEconomyOverTime.Count) return buildEconomyOverTime.Last();
return buildEconomyOverTime[atInterval];
}
private static void SetupCurrentInverval(IBuildOrderService buildOrder, EconomyModel economyAtSecond, int interval)
{
economyAtSecond.Interval = interval;
@@ -129,8 +134,8 @@ public class EconomyService : IEconomyService
*/
private static int CalculateCreatingWorkerCosts(EconomyModel economyAtSecond)
{
int createdWorkers = 0;
var createdWorkers = 0;
if (economyAtSecond.CreatingWorkerCount > 0)
for (var i = 0; i < economyAtSecond.CreatingWorkerDelays.Count; i++)
if (economyAtSecond.CreatingWorkerDelays[i] > 0)
@@ -158,8 +163,8 @@ public class EconomyService : IEconomyService
*/
private static int AddFundsFromHarvestPoints(EconomyModel economyAtSecond, float freeWorkers)
{
int workersNeeded = 0;
var workersNeeded = 0;
foreach (var entity in economyAtSecond.HarvestPoints)
{
var harvester = entity.Harvest();
@@ -188,10 +193,7 @@ public class EconomyService : IEconomyService
private static void AddPassivePyreGain(int interval, EconomyModel economyAtSecond)
{
if (interval % 3 == 0)
{
economyAtSecond.Pyre += 1;
}
if (interval % 3 == 0) economyAtSecond.Pyre += 1;
}
private void CarryOverEconomyFromPreviousInterval(int interval, EconomyModel economyAtSecond)
@@ -210,14 +212,6 @@ public class EconomyService : IEconomyService
}
public EconomyModel GetEconomy(int atInterval)
{
if (atInterval >= buildEconomyOverTime.Count) return buildEconomyOverTime.Last();
return buildEconomyOverTime[atInterval];
}
private event Action OnChange = null!;
private void NotifyDataChanged()