style(BuildCalc) Bank UI improvements and code cleanup

This commit is contained in:
2022-04-18 21:38:59 -04:00
parent 85e8f3c278
commit fb70b788bc
8 changed files with 192 additions and 127 deletions
+1 -1
View File
@@ -323,7 +323,7 @@ public interface IBuildOrderService
public int? WillMeetSupply(EntityModel entity);
public Dictionary<int, List<EntityModel>> GetOrders();
public List<EntityModel> GetCompletedBefore(int interval);
public List<EntityModel> GetHarvestersCompletedBefore(int interval);
public List<EntityModel> GetHarvestPointsCompletedBefore(int interval);
public void RemoveLast();
public void Reset();
+1 -1
View File
@@ -239,7 +239,7 @@ public class BuildOrderService : IBuildOrderService
select orders).ToList();
}
public List<EntityModel> GetHarvestersCompletedBefore(int interval)
public List<EntityModel> GetHarvestPointsCompletedBefore(int interval)
{
return (from ordersAtTime in buildOrder.StartedOrders
from orders in ordersAtTime.Value
@@ -93,7 +93,7 @@ public class EconomyComparisionService : IEconomyComparisonService
economyAtSecond.WorkerCount = buildEconomyOverTime[interval - 1].WorkerCount;
economyAtSecond.BusyWorkerCount = buildEconomyOverTime[interval - 1].BusyWorkerCount;
economyAtSecond.CreatingWorkerCount = buildEconomyOverTime[interval - 1].CreatingWorkerCount;
economyAtSecond.Harvesters = buildEconomyOverTime[interval - 1].Harvesters.ToList();
economyAtSecond.HarvestPoints = buildEconomyOverTime[interval - 1].HarvestPoints.ToList();
economyAtSecond.CreatingWorkerDelays = buildEconomyOverTime[interval - 1].CreatingWorkerDelays.ToList();
}
@@ -103,7 +103,7 @@ public class EconomyComparisionService : IEconomyComparisonService
float freeWorkers = economyAtSecond.WorkerCount - economyAtSecond.BusyWorkerCount;
var workersNeeded = 0;
economyAtSecond.Harvesters =
economyAtSecond.HarvestPoints =
(from harvester in buildOrder.GetHarvestersCompletedBefore(interval)
select harvester).ToList();
@@ -111,7 +111,7 @@ public class EconomyComparisionService : IEconomyComparisonService
economyAtSecond.Pyre += 1;
// Add funds
foreach (var entity in economyAtSecond.Harvesters)
foreach (var entity in economyAtSecond.HarvestPoints)
{
var harvester = entity.Harvest();
if (harvester.RequiresWorker)
@@ -185,7 +185,7 @@ public class EconomyComparisionService : IEconomyComparisonService
foreach (var newEntity in completedAtInterval)
{
var harvest = newEntity;
if (harvest != null) economyAtSecond.Harvesters.Add(harvest);
if (harvest != null) economyAtSecond.HarvestPoints.Add(harvest);
var production = newEntity.Production();
if (production != null && production.RequiresWorker) economyAtSecond.BusyWorkerCount -= 1;
+149 -108
View File
@@ -54,121 +54,162 @@ public class EconomyService : IEconomyService
buildEconomyOverTime[interval] = new EconomyModel();
var economyAtSecond = buildEconomyOverTime[interval];
if (interval > 0)
{
economyAtSecond.Alloy = buildEconomyOverTime[interval - 1].Alloy;
economyAtSecond.Ether = buildEconomyOverTime[interval - 1].Ether;
economyAtSecond.Pyre = buildEconomyOverTime[interval - 1].Pyre;
economyAtSecond.WorkerCount = buildEconomyOverTime[interval - 1].WorkerCount;
economyAtSecond.BusyWorkerCount = buildEconomyOverTime[interval - 1].BusyWorkerCount;
economyAtSecond.CreatingWorkerCount = buildEconomyOverTime[interval - 1].CreatingWorkerCount;
economyAtSecond.Harvesters = buildEconomyOverTime[interval - 1].Harvesters.ToList();
economyAtSecond.CreatingWorkerDelays = buildEconomyOverTime[interval - 1].CreatingWorkerDelays.ToList();
}
CarryOverEconomyFromPreviousInterval(interval, economyAtSecond);
economyAtSecond.Interval = interval;
// Add funds
float freeWorkers = economyAtSecond.WorkerCount - economyAtSecond.BusyWorkerCount;
var workersNeeded = 0;
economyAtSecond.Harvesters =
(from harvester in buildOrder.GetHarvestersCompletedBefore(interval)
select harvester).ToList();
// Add funds
if (interval % 3 == 0)
{
economyAtSecond.Pyre += 1;
}
// Add funds
foreach (var entity in economyAtSecond.Harvesters)
{
var harvester = entity.Harvest();
if (harvester.RequiresWorker)
if (harvester.Resource == ResourceType.Alloy)
{
var usedWorkers = Math.Min(harvester.Slots, freeWorkers);
economyAtSecond.Alloy += harvester.HarvestedPerInterval * usedWorkers;
freeWorkers -= usedWorkers;
if (usedWorkers < harvester.Slots) workersNeeded += 1;
}
if (harvester.RequiresWorker == false)
{
if (harvester.Resource == ResourceType.Ether)
economyAtSecond.Ether += harvester.HarvestedPerInterval * harvester.Slots;
if (harvester.Resource == ResourceType.Alloy)
economyAtSecond.Alloy += harvester.HarvestedPerInterval * harvester.Slots;
}
}
// Create new worker
if (economyAtSecond.CreatingWorkerCount > 0)
for (var i = 0; i < economyAtSecond.CreatingWorkerDelays.Count; i++)
if (economyAtSecond.CreatingWorkerDelays[i] > 0)
{
if (economyAtSecond.Alloy > 2.5f)
{
economyAtSecond.Alloy -= 2.5f;
economyAtSecond.CreatingWorkerDelays[i]--;
}
}
else
{
economyAtSecond.CreatingWorkerCount -= 1;
economyAtSecond.WorkerCount += 1;
economyAtSecond.CreatingWorkerDelays.Remove(i);
i--;
}
if (workersNeeded > economyAtSecond.CreatingWorkerCount)
{
economyAtSecond.CreatingWorkerCount += 1;
economyAtSecond.CreatingWorkerDelays.Add(50);
}
// Remove Funds from Build Order
if (buildOrder.StartedOrders.TryGetValue(interval, out var ordersAtTime))
foreach (var order in ordersAtTime)
{
var foundEntity = EntityModel.GetDictionary()[order.DataType];
var production = foundEntity.Production();
if (production != null)
{
economyAtSecond.Alloy -= production.Alloy;
economyAtSecond.Ether -= production.Ether;
economyAtSecond.Pyre -= production.Pyre;
var finishedAt = interval + production.BuildTime;
if (production.RequiresWorker) economyAtSecond.BusyWorkerCount += 1;
if (production.ConsumesWorker) economyAtSecond.WorkerCount -= 1;
}
}
// Handle new entities
if (buildOrder.CompletedOrders.TryGetValue(interval, out var completedAtInterval))
foreach (var newEntity in completedAtInterval)
{
var harvest = newEntity;
if (harvest != null) economyAtSecond.Harvesters.Add(harvest);
var production = newEntity.Production();
if (production != null && production.RequiresWorker) economyAtSecond.BusyWorkerCount -= 1;
}
SetupCurrentInverval(buildOrder, economyAtSecond, interval);
AddPassivePyreGain(interval, economyAtSecond);
float freeWorkers = economyAtSecond.WorkerCount - economyAtSecond.BusyWorkerCount;
var workersNeeded = AddFundsFromHarvestPoints(economyAtSecond, freeWorkers);
workersNeeded -= CalculateCreatingWorkerCosts(economyAtSecond);
MakeNeededNewWorkersRequests(workersNeeded, economyAtSecond);
SubtractFundsOnRequestedOrders(buildOrder, interval, economyAtSecond);
HandledAddingNewHarvestPointsAndWorkersToEconomy(buildOrder, interval, economyAtSecond);
}
NotifyDataChanged();
}
private static void SetupCurrentInverval(IBuildOrderService buildOrder, EconomyModel economyAtSecond, int interval)
{
economyAtSecond.Interval = interval;
economyAtSecond.HarvestPoints =
(from harvester in buildOrder.GetHarvestPointsCompletedBefore(interval)
select harvester).ToList();
}
private static void HandledAddingNewHarvestPointsAndWorkersToEconomy(IBuildOrderService buildOrder, int interval,
EconomyModel economyAtSecond)
{
if (buildOrder.CompletedOrders.TryGetValue(interval, out var completedAtInterval))
foreach (var newEntity in completedAtInterval)
{
var harvest = newEntity;
if (harvest != null) economyAtSecond.HarvestPoints.Add(harvest);
var production = newEntity.Production();
if (production != null && production.RequiresWorker) economyAtSecond.BusyWorkerCount -= 1;
}
}
private static void SubtractFundsOnRequestedOrders(IBuildOrderService buildOrder, int interval,
EconomyModel economyAtSecond)
{
if (buildOrder.StartedOrders.TryGetValue(interval, out var ordersAtTime))
foreach (var order in ordersAtTime)
{
var foundEntity = EntityModel.GetDictionary()[order.DataType];
var production = foundEntity.Production();
if (production != null)
{
economyAtSecond.Alloy -= production.Alloy;
economyAtSecond.Ether -= production.Ether;
economyAtSecond.Pyre -= production.Pyre;
var finishedAt = interval + production.BuildTime;
if (production.RequiresWorker) economyAtSecond.BusyWorkerCount += 1;
if (production.ConsumesWorker) economyAtSecond.WorkerCount -= 1;
}
}
}
private static void MakeNeededNewWorkersRequests(int workersNeeded, EconomyModel economyAtSecond)
{
if (workersNeeded > economyAtSecond.CreatingWorkerCount)
{
economyAtSecond.CreatingWorkerCount += 1;
economyAtSecond.CreatingWorkerDelays.Add(20);
}
}
/**
* Returns amount of workers created
*/
private static int CalculateCreatingWorkerCosts(EconomyModel economyAtSecond)
{
int createdWorkers = 0;
if (economyAtSecond.CreatingWorkerCount > 0)
for (var i = 0; i < economyAtSecond.CreatingWorkerDelays.Count; i++)
if (economyAtSecond.CreatingWorkerDelays[i] > 0)
{
if (economyAtSecond.Alloy > 2.5f)
{
economyAtSecond.Alloy -= 2.5f;
economyAtSecond.CreatingWorkerDelays[i]--;
}
}
else
{
economyAtSecond.CreatingWorkerCount -= 1;
economyAtSecond.WorkerCount += 1;
createdWorkers++;
economyAtSecond.CreatingWorkerDelays.Remove(i);
i--;
}
return createdWorkers;
}
/**
* Returns needed workers to maximize harvest points
*/
private static int AddFundsFromHarvestPoints(EconomyModel economyAtSecond, float freeWorkers)
{
int workersNeeded = 0;
foreach (var entity in economyAtSecond.HarvestPoints)
{
var harvester = entity.Harvest();
if (harvester.RequiresWorker)
if (harvester.Resource == ResourceType.Alloy)
{
var usedWorkers = Math.Min(harvester.Slots, freeWorkers);
economyAtSecond.Alloy += harvester.HarvestedPerInterval * usedWorkers;
freeWorkers -= usedWorkers;
if (usedWorkers < harvester.Slots) workersNeeded += 1;
}
if (harvester.RequiresWorker == false)
{
if (harvester.Resource == ResourceType.Ether)
economyAtSecond.Ether += harvester.HarvestedPerInterval * harvester.Slots;
if (harvester.Resource == ResourceType.Alloy)
economyAtSecond.Alloy += harvester.HarvestedPerInterval * harvester.Slots;
}
}
return workersNeeded;
}
private static void AddPassivePyreGain(int interval, EconomyModel economyAtSecond)
{
if (interval % 3 == 0)
{
economyAtSecond.Pyre += 1;
}
}
private void CarryOverEconomyFromPreviousInterval(int interval, EconomyModel economyAtSecond)
{
if (interval > 0)
{
economyAtSecond.Alloy = buildEconomyOverTime[interval - 1].Alloy;
economyAtSecond.Ether = buildEconomyOverTime[interval - 1].Ether;
economyAtSecond.Pyre = buildEconomyOverTime[interval - 1].Pyre;
economyAtSecond.WorkerCount = buildEconomyOverTime[interval - 1].WorkerCount;
economyAtSecond.BusyWorkerCount = buildEconomyOverTime[interval - 1].BusyWorkerCount;
economyAtSecond.CreatingWorkerCount = buildEconomyOverTime[interval - 1].CreatingWorkerCount;
economyAtSecond.HarvestPoints = buildEconomyOverTime[interval - 1].HarvestPoints.ToList();
economyAtSecond.CreatingWorkerDelays = buildEconomyOverTime[interval - 1].CreatingWorkerDelays.ToList();
}
}
public EconomyModel GetEconomy(int atInterval)
{
if (atInterval >= buildEconomyOverTime.Count) return buildEconomyOverTime.Last();