diff --git a/IGP/Pages/BuildCalculator/Parts/BuildChartComponent.razor b/IGP/Pages/BuildCalculator/Parts/BuildChartComponent.razor index 37e2985..9377355 100644 --- a/IGP/Pages/BuildCalculator/Parts/BuildChartComponent.razor +++ b/IGP/Pages/BuildCalculator/Parts/BuildChartComponent.razor @@ -23,10 +23,8 @@ else var x = int.Parse(point.GetInterval(chart.HighestIntervalPoint, chart.IntervalDisplayMax)); if (takenPixels.ContainsKey(x)) continue; - takenPixels.Add(x, true); -
buildEconomyOverTime = null!; - - private Dictionary> specficEconomiesOverTime = null!; + private List? buildEconomyOverTime; public List GetOverTime() { @@ -30,15 +29,13 @@ public class EconomyService : IEconomyService // We don't consider things mining at zero seconds if (fromInterval == 0) fromInterval = 1; - //TODO Break all this up if (buildEconomyOverTime == null) { - buildEconomyOverTime = new List(); + buildEconomyOverTime = []; for (var interval = 0; interval < timing.GetAttackTime(); interval++) buildEconomyOverTime.Add(new EconomyModel { Interval = interval }); } - if (buildEconomyOverTime.Count > timing.GetAttackTime()) buildEconomyOverTime.RemoveRange(timing.GetAttackTime(), buildEconomyOverTime.Count - timing.GetAttackTime()); @@ -54,7 +51,7 @@ public class EconomyService : IEconomyService CarryOverEconomyFromPreviousInterval(interval, economyAtSecond); - SetupCurrentInverval(buildOrder, economyAtSecond, interval); + SetupCurrentInterval(buildOrder, economyAtSecond, interval); AddPassivePyreGain(interval, economyAtSecond); float freeWorkers = economyAtSecond.WorkerCount - economyAtSecond.BusyWorkerCount; var workersNeeded = AddFundsFromHarvestPoints(economyAtSecond, freeWorkers); @@ -70,12 +67,12 @@ public class EconomyService : IEconomyService public EconomyModel GetEconomy(int atInterval) { - if (atInterval >= buildEconomyOverTime.Count) return buildEconomyOverTime.Last(); - - return buildEconomyOverTime[atInterval]; + return atInterval >= buildEconomyOverTime.Count + ? buildEconomyOverTime.Last() + : buildEconomyOverTime[atInterval]; } - private static void SetupCurrentInverval(IBuildOrderService buildOrder, EconomyModel economyAtSecond, int interval) + private static void SetupCurrentInterval(IBuildOrderService buildOrder, EconomyModel economyAtSecond, int interval) { economyAtSecond.Interval = interval; economyAtSecond.HarvestPoints = @@ -86,74 +83,69 @@ public class EconomyService : IEconomyService 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); + if (!buildOrder.CompletedOrders.TryGetValue(interval, out var completedAtInterval)) return; + + foreach (var newEntity in completedAtInterval) + { + economyAtSecond.HarvestPoints.Add(newEntity); - var production = newEntity.Production(); - if (production != null && production.RequiresWorker) economyAtSecond.BusyWorkerCount -= 1; - } + var production = newEntity.Production(); + if (production is { RequiresWorker: true }) 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 (!buildOrder.StartedOrders.TryGetValue(interval, out var ordersAtTime)) return; - if (production.RequiresWorker) economyAtSecond.BusyWorkerCount += 1; - - if (production.ConsumesWorker) economyAtSecond.WorkerCount -= 1; - } - } + foreach (var production in + ordersAtTime.Select(order => EntityModel.GetDictionary()[order.DataType]) + .Select(foundEntity => foundEntity.Production()) + .OfType()) + { + economyAtSecond.Alloy -= production.Alloy; + economyAtSecond.Ether -= production.Ether; + economyAtSecond.Pyre -= production.Pyre; + + 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); - } + if (workersNeeded <= economyAtSecond.CreatingWorkerCount) return; + + economyAtSecond.CreatingWorkerCount += 1; + economyAtSecond.CreatingWorkerDelays.Add(20); } /** - * Returns amount of workers created + * Returns number of workers created */ private static int CalculateCreatingWorkerCosts(EconomyModel economyAtSecond) { var 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--; - } + if (economyAtSecond.CreatingWorkerCount <= 0) return createdWorkers; + + for (var i = 0; i < economyAtSecond.CreatingWorkerDelays.Count; i++) + if (economyAtSecond.CreatingWorkerDelays[i] > 0) + { + if (!(economyAtSecond.Alloy > 2.5f)) continue; + + economyAtSecond.Alloy -= 2.5f; + economyAtSecond.CreatingWorkerDelays[i]--; + } + else + { + economyAtSecond.CreatingWorkerCount -= 1; + economyAtSecond.WorkerCount += 1; + createdWorkers++; + economyAtSecond.CreatingWorkerDelays.Remove(i); + i--; + } return createdWorkers; } @@ -165,33 +157,43 @@ public class EconomyService : IEconomyService { var workersNeeded = 0; - foreach (var entity in economyAtSecond.HarvestPoints) + foreach (var harvesterPoint in + economyAtSecond.HarvestPoints.Select(entity => entity.Harvest())) { - var harvester = entity.Harvest(); - if (harvester.RequiresWorker) - if (harvester.Resource == ResourceType.Alloy) + switch (harvesterPoint.RequiresWorker) + { + case true: { - var usedWorkers = Math.Min(harvester.Slots, freeWorkers); - economyAtSecond.Alloy += harvester.HarvestedPerInterval * usedWorkers; - economyAtSecond.AlloyIncome += harvester.HarvestedPerInterval * usedWorkers; + if (harvesterPoint.Resource == ResourceType.Alloy) + { + var usedWorkers = Math.Min(harvesterPoint.Slots, freeWorkers); + economyAtSecond.Alloy += harvesterPoint.HarvestedPerInterval * usedWorkers; + economyAtSecond.AlloyIncome += harvesterPoint.HarvestedPerInterval * usedWorkers; - freeWorkers -= usedWorkers; + freeWorkers -= usedWorkers; - if (usedWorkers < harvester.Slots) workersNeeded += 1; - } + if (usedWorkers < harvesterPoint.Slots) workersNeeded += 1; + } - if (harvester.RequiresWorker == false) - { - if (harvester.Resource == ResourceType.Ether) - { - economyAtSecond.Ether += harvester.HarvestedPerInterval * harvester.Slots; - economyAtSecond.EtherIncome += harvester.HarvestedPerInterval * harvester.Slots; + break; } - - if (harvester.Resource == ResourceType.Alloy) + case false: { - economyAtSecond.Alloy += harvester.HarvestedPerInterval * harvester.Slots; - economyAtSecond.AlloyIncome += harvester.HarvestedPerInterval * harvester.Slots; + switch (harvesterPoint.Resource) + { + case ResourceType.Ether: + economyAtSecond.Ether += harvesterPoint.HarvestedPerInterval * harvesterPoint.Slots; + economyAtSecond.EtherIncome += harvesterPoint.HarvestedPerInterval * harvesterPoint.Slots; + break; + case ResourceType.Alloy: + economyAtSecond.Alloy += harvesterPoint.HarvestedPerInterval * harvesterPoint.Slots; + economyAtSecond.AlloyIncome += harvesterPoint.HarvestedPerInterval * harvesterPoint.Slots; + break; + case ResourceType.Pyre: + break; // Pyre Miner? + } + + break; } } }