From 4a3d3af41c07fff09d0dac92f7a2ed4bc1821035 Mon Sep 17 00:00:00 2001 From: hz Date: Wed, 20 May 2026 19:23:20 -0500 Subject: [PATCH] fixing food tick --- .../LocationsApi/Services/ResourceService.cs | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/microservices/LocationsApi/Services/ResourceService.cs b/microservices/LocationsApi/Services/ResourceService.cs index 6405d71..3f90c5d 100644 --- a/microservices/LocationsApi/Services/ResourceService.cs +++ b/microservices/LocationsApi/Services/ResourceService.cs @@ -104,8 +104,8 @@ public class ResourceService public async Task GetAsync(string username) { var resources = await EnsureAsync(username); - resources = await ApplyProductionBuildingDeliveryAsync(resources); - return await ApplyFoodUpkeepAsync(resources); + resources = await ApplyFoodTickAsync(resources); + return await ApplyProductionBuildingDeliveryAsync(resources, includeFood: false); } public async Task TrySpendGoldAsync(string username, int amount) @@ -310,7 +310,7 @@ public class ResourceService }); } - private async Task ApplyProductionBuildingDeliveryAsync(PlayerResources resources) + private async Task ApplyProductionBuildingDeliveryAsync(PlayerResources resources, bool includeFood = true) { var now = DateTime.UtcNow; await CompletePendingWorkerAssignmentsAsync(resources.Username, now); @@ -320,13 +320,15 @@ public class ResourceService var buildingCapacity = await _settings.GetNonNegativeIntAsync(GameSettingsService.ProductionBuildingResourceCapacity, _productionBuildingResourceCapacity); var logCampWoodPerMinute = await _settings.GetNonNegativeIntAsync(GameSettingsService.LogCampWoodPerMinute, 5); var mineOrePerMinute = await _settings.GetNonNegativeIntAsync(GameSettingsService.MineOrePerMinute, 5); - var farmFoodPerMinute = await _settings.GetNonNegativeIntAsync(GameSettingsService.FarmFoodPerMinute, 5); + var farmFoodPerMinute = includeFood + ? await _settings.GetNonNegativeIntAsync(GameSettingsService.FarmFoodPerMinute, 5) + : 0; var buildings = await _locations.Find(x => x.OwnerUsername == resources.Username && !string.IsNullOrWhiteSpace(x.WorkerUnitId) && (x.BuildingStatus == "Built" || x.BuildingStatus == null || x.BuildingStatus == "") && - (x.BuildingType == "LogCamp" || x.BuildingType == "Mine" || x.BuildingType == "Farm")) + (x.BuildingType == "LogCamp" || x.BuildingType == "Mine" || (includeFood && x.BuildingType == "Farm"))) .ToListAsync(); var wood = Math.Min(resources.Wood, woodCapacity); @@ -392,7 +394,7 @@ public class ResourceService }); } - private async Task ApplyFoodUpkeepAsync(PlayerResources resources) + private async Task ApplyFoodTickAsync(PlayerResources resources) { var now = DateTime.UtcNow; var unitFoodPerMinute = await _settings.GetNonNegativeIntAsync(GameSettingsService.UnitFoodPerMinute, _unitFoodPerMinute); @@ -400,15 +402,26 @@ public class ResourceService var units = await GetFoodConsumingUnitsAsync(resources.Username); var upkeepPerMinute = units.Count * unitFoodPerMinute; var netFoodPerMinute = foodProductionPerMinute - upkeepPerMinute; - var elapsedMinutes = upkeepPerMinute <= 0 || resources.LastFoodGeneratedAtUtc.Year < 2000 + var elapsedMinutes = resources.LastFoodGeneratedAtUtc.Year < 2000 ? 0 : (int)Math.Floor((now - resources.LastFoodGeneratedAtUtc).TotalMinutes); - var food = resources.Food; + var foodCapacity = await _settings.GetNonNegativeIntAsync(GameSettingsService.TownHallFoodCapacity, _townHallFoodCapacity); + var food = Math.Min(resources.Food, foodCapacity); var removedUnits = new List(); - for (var minute = 0; minute < elapsedMinutes && units.Count > 0; minute++) + for (var minute = 0; minute < elapsedMinutes; minute++) { + if (foodProductionPerMinute > 0) + { + food = Math.Min(foodCapacity, food + foodProductionPerMinute); + } + var neededFood = units.Count * unitFoodPerMinute; + if (neededFood <= 0) + { + continue; + } + if (food >= neededFood) { food -= neededFood; @@ -427,7 +440,7 @@ public class ResourceService } netFoodPerMinute = foodProductionPerMinute - (units.Count * unitFoodPerMinute); - if (elapsedMinutes <= 0 && removedUnits.Count == 0 && resources.Food == food && resources.FoodPerMinute == netFoodPerMinute) + if (elapsedMinutes <= 0 && removedUnits.Count == 0 && resources.Food == food && resources.FoodPerMinute == netFoodPerMinute && resources.FoodCapacity == foodCapacity) { return resources; } @@ -437,6 +450,7 @@ public class ResourceService var update = Builders.Update .Set(x => x.FoodPerMinute, netFoodPerMinute) .Set(x => x.Food, food) + .Set(x => x.FoodCapacity, foodCapacity) .Set(x => x.UpdatedAtUtc, now); if (elapsedMinutes > 0)