adding resource limits and archers

This commit is contained in:
hz
2026-05-19 05:11:57 -05:00
parent a5ecb7f585
commit e78eb24543
12 changed files with 241 additions and 39 deletions
@@ -6,12 +6,17 @@ namespace LocationsApi.Services;
public class UnitTrainingService
{
private const string ScoutUnitType = "Scout";
private const string ArcherUnitType = "Archer";
private readonly IMongoCollection<UnitTraining> _training;
private readonly IMongoCollection<Location> _locations;
private readonly ResourceService _resources;
private readonly GameSettingsService _settings;
private readonly int _scoutGoldCost;
private readonly int _archerGoldCost;
private readonly int _archerWoodCost;
private readonly int _defaultScoutTrainingSeconds;
private readonly int _defaultArcherTrainingSeconds;
public UnitTrainingService(IConfiguration cfg, ResourceService resources, GameSettingsService settings)
{
@@ -20,12 +25,18 @@ public class UnitTrainingService
var client = new MongoClient(connectionString);
var database = client.GetDatabase(databaseName);
_training = database.GetCollection<UnitTraining>("UnitTraining");
_locations = database.GetCollection<Location>("Locations");
_resources = resources;
_settings = settings;
_scoutGoldCost = int.TryParse(cfg["Units:ScoutGoldCost"], out var scoutGoldCost) ? Math.Max(0, scoutGoldCost) : 50;
_archerGoldCost = int.TryParse(cfg["Units:ArcherGoldCost"], out var archerGoldCost) ? Math.Max(0, archerGoldCost) : 100;
_archerWoodCost = int.TryParse(cfg["Units:ArcherWoodCost"], out var archerWoodCost) ? Math.Max(0, archerWoodCost) : 100;
_defaultScoutTrainingSeconds = int.TryParse(cfg["Units:ScoutTrainingSeconds"], out var scoutTrainingSeconds)
? Math.Max(1, scoutTrainingSeconds)
: 60;
_defaultArcherTrainingSeconds = int.TryParse(cfg["Units:ArcherTrainingSeconds"], out var archerTrainingSeconds)
? Math.Max(1, archerTrainingSeconds)
: 60;
_training.Indexes.CreateOne(new CreateIndexModel<UnitTraining>(
Builders<UnitTraining>.IndexKeys.Ascending(x => x.Username).Ascending(x => x.Status)));
@@ -38,6 +49,7 @@ public class UnitTrainingService
var all = await _training.Find(x => x.Username == username).ToListAsync();
var completedScouts = all.Count(x => x.UnitType == ScoutUnitType && x.Status == "Completed");
var completedArchers = all.Count(x => x.UnitType == ArcherUnitType && x.Status == "Completed");
var activeTraining = all
.Where(x => x.Status == "Training")
.OrderBy(x => x.CompletesAtUtc)
@@ -47,33 +59,47 @@ public class UnitTrainingService
return new PlayerUnitsResponse
{
CompletedScouts = completedScouts,
CompletedArchers = completedArchers,
Training = activeTraining
};
}
public async Task<UnitTraining?> TrainAsync(string username, string unitType)
{
if (!string.Equals(unitType, ScoutUnitType, StringComparison.OrdinalIgnoreCase))
var normalizedUnitType = NormalizeUnitType(unitType);
if (normalizedUnitType is null)
{
throw new ArgumentException("Unsupported unit type.", nameof(unitType));
}
var spent = await _resources.TrySpendGoldAsync(username, _scoutGoldCost);
if (normalizedUnitType == ArcherUnitType && !await HasBuiltBarracksAsync(username))
{
throw new InvalidOperationException("A completed Barracks is required to train Archers.");
}
var goldCost = normalizedUnitType == ArcherUnitType ? _archerGoldCost : _scoutGoldCost;
var woodCost = normalizedUnitType == ArcherUnitType ? _archerWoodCost : 0;
var spent = normalizedUnitType == ArcherUnitType
? await _resources.TrySpendResourcesAsync(username, goldCost, woodCost, 0)
: await _resources.TrySpendGoldAsync(username, goldCost);
if (!spent)
{
return null;
}
var now = DateTime.UtcNow;
var scoutTrainingSeconds = await _settings.GetIntAsync(GameSettingsService.ScoutTrainingSeconds, _defaultScoutTrainingSeconds);
var trainingSeconds = normalizedUnitType == ArcherUnitType
? await _settings.GetIntAsync(GameSettingsService.ArcherTrainingSeconds, _defaultArcherTrainingSeconds)
: await _settings.GetIntAsync(GameSettingsService.ScoutTrainingSeconds, _defaultScoutTrainingSeconds);
var training = new UnitTraining
{
Username = username,
UnitType = ScoutUnitType,
GoldCost = _scoutGoldCost,
UnitType = normalizedUnitType,
GoldCost = goldCost,
WoodCost = woodCost,
Status = "Training",
StartedAtUtc = now,
CompletesAtUtc = now.AddSeconds(scoutTrainingSeconds),
CompletesAtUtc = now.AddSeconds(trainingSeconds),
CreatedAtUtc = now,
UpdatedAtUtc = now
};
@@ -82,6 +108,32 @@ public class UnitTrainingService
return training;
}
private async Task<bool> HasBuiltBarracksAsync(string username)
{
await CompleteFinishedBuildingsAsync(username);
return await _locations.Find(x =>
x.OwnerUsername == username &&
x.BuildingType == "Barracks" &&
(x.BuildingStatus == "Built" || x.BuildingStatus == null || x.BuildingStatus == ""))
.AnyAsync();
}
private async Task CompleteFinishedBuildingsAsync(string username)
{
var now = DateTime.UtcNow;
var update = Builders<Location>.Update
.Set(x => x.BuildingStatus, "Built")
.Set(x => x.BuildingBuiltAtUtc, now)
.Set(x => x.UpdatedAtUtc, now);
await _locations.UpdateManyAsync(
x => x.OwnerUsername == username &&
x.BuildingType == "Barracks" &&
x.BuildingStatus == "Building" &&
x.BuildingCompletesAtUtc <= now,
update);
}
public async Task<bool> TryConsumeCompletedScoutAsync(string username)
{
await CompleteFinishedTrainingAsync(username);
@@ -114,4 +166,15 @@ public class UnitTrainingService
x => x.Username == username && x.Status == "Training" && x.CompletesAtUtc <= now,
update);
}
private static string? NormalizeUnitType(string unitType)
{
var trimmed = unitType.Trim();
return trimmed.ToLowerInvariant() switch
{
"scout" => ScoutUnitType,
"archer" => ArcherUnitType,
_ => null
};
}
}