fixing food tick
This commit is contained in:
@@ -104,8 +104,8 @@ public class ResourceService
|
||||
public async Task<PlayerResources> 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<bool> TrySpendGoldAsync(string username, int amount)
|
||||
@@ -310,7 +310,7 @@ public class ResourceService
|
||||
});
|
||||
}
|
||||
|
||||
private async Task<PlayerResources> ApplyProductionBuildingDeliveryAsync(PlayerResources resources)
|
||||
private async Task<PlayerResources> 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<PlayerResources> ApplyFoodUpkeepAsync(PlayerResources resources)
|
||||
private async Task<PlayerResources> 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<FoodConsumingUnit>();
|
||||
|
||||
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<PlayerResources>.Update
|
||||
.Set(x => x.FoodPerMinute, netFoodPerMinute)
|
||||
.Set(x => x.Food, food)
|
||||
.Set(x => x.FoodCapacity, foodCapacity)
|
||||
.Set(x => x.UpdatedAtUtc, now);
|
||||
|
||||
if (elapsedMinutes > 0)
|
||||
|
||||
Reference in New Issue
Block a user