adding mines
This commit is contained in:
@@ -12,7 +12,9 @@ public class LocationService
|
||||
private readonly int _homeSearchRadius;
|
||||
private readonly int _visibilityDepth;
|
||||
private readonly int _logCampWoodPerMinute;
|
||||
private readonly int _mineOrePerMinute;
|
||||
private readonly int _defaultLogCampBuildSeconds;
|
||||
private readonly int _defaultMineBuildSeconds;
|
||||
|
||||
public LocationService(IConfiguration cfg, ResourceService resources, GameSettingsService settings)
|
||||
{
|
||||
@@ -29,9 +31,15 @@ public class LocationService
|
||||
_logCampWoodPerMinute = int.TryParse(cfg["Buildings:LogCampWoodPerMinute"], out var logCampWoodPerMinute)
|
||||
? Math.Max(1, logCampWoodPerMinute)
|
||||
: 5;
|
||||
_mineOrePerMinute = int.TryParse(cfg["Buildings:MineOrePerMinute"], out var mineOrePerMinute)
|
||||
? Math.Max(1, mineOrePerMinute)
|
||||
: 5;
|
||||
_defaultLogCampBuildSeconds = int.TryParse(cfg["Buildings:LogCampBuildSeconds"], out var logCampBuildSeconds)
|
||||
? Math.Max(1, logCampBuildSeconds)
|
||||
: 60;
|
||||
_defaultMineBuildSeconds = int.TryParse(cfg["Buildings:MineBuildSeconds"], out var mineBuildSeconds)
|
||||
? Math.Max(1, mineBuildSeconds)
|
||||
: 60;
|
||||
|
||||
var coordinateIndex = Builders<Location>.IndexKeys
|
||||
.Ascending(x => x.Q)
|
||||
@@ -123,7 +131,8 @@ public class LocationService
|
||||
|
||||
public async Task<Location?> BuildAsync(BuildLocationRequest request, string username)
|
||||
{
|
||||
if (!string.Equals(request.BuildingType, "LogCamp", StringComparison.OrdinalIgnoreCase))
|
||||
var buildingType = NormalizeBuildingType(request.BuildingType);
|
||||
if (buildingType is null)
|
||||
{
|
||||
throw new ArgumentException("Unsupported building type.", nameof(request.BuildingType));
|
||||
}
|
||||
@@ -140,17 +149,24 @@ public class LocationService
|
||||
throw new InvalidOperationException("This location already has a building or construction in progress.");
|
||||
}
|
||||
|
||||
if (!string.Equals(location.Terrain, "Forest", StringComparison.OrdinalIgnoreCase))
|
||||
var requiredTerrain = buildingType == "LogCamp" ? "Forest" : "Ore";
|
||||
if (!string.Equals(location.Terrain, requiredTerrain, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new InvalidOperationException("Log Camps can only be built on Forest territory.");
|
||||
throw new InvalidOperationException($"{buildingType} can only be built on {requiredTerrain} territory.");
|
||||
}
|
||||
|
||||
await CompleteFinishedBuildingsAsync(username);
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var buildSeconds = await _settings.GetIntAsync(GameSettingsService.LogCampBuildSeconds, _defaultLogCampBuildSeconds);
|
||||
var settingKey = buildingType == "LogCamp"
|
||||
? GameSettingsService.LogCampBuildSeconds
|
||||
: GameSettingsService.MineBuildSeconds;
|
||||
var defaultBuildSeconds = buildingType == "LogCamp"
|
||||
? _defaultLogCampBuildSeconds
|
||||
: _defaultMineBuildSeconds;
|
||||
var buildSeconds = await _settings.GetIntAsync(settingKey, defaultBuildSeconds);
|
||||
var update = Builders<Location>.Update
|
||||
.Set(x => x.BuildingType, "LogCamp")
|
||||
.Set(x => x.BuildingType, buildingType)
|
||||
.Set(x => x.BuildingStatus, "Building")
|
||||
.Set(x => x.BuildingStartedAtUtc, now)
|
||||
.Set(x => x.BuildingCompletesAtUtc, now.AddSeconds(buildSeconds))
|
||||
@@ -177,7 +193,7 @@ public class LocationService
|
||||
var now = DateTime.UtcNow;
|
||||
var readyBuildings = await _locations.Find(x =>
|
||||
x.OwnerUsername == username &&
|
||||
x.BuildingType == "LogCamp" &&
|
||||
(x.BuildingType == "LogCamp" || x.BuildingType == "Mine") &&
|
||||
x.BuildingStatus == "Building" &&
|
||||
x.BuildingCompletesAtUtc <= now)
|
||||
.ToListAsync();
|
||||
@@ -199,7 +215,14 @@ public class LocationService
|
||||
|
||||
if (updated is not null)
|
||||
{
|
||||
await _resources.AddWoodGenerationAsync(username, _logCampWoodPerMinute);
|
||||
if (updated.BuildingType == "LogCamp")
|
||||
{
|
||||
await _resources.AddWoodGenerationAsync(username, _logCampWoodPerMinute);
|
||||
}
|
||||
else if (updated.BuildingType == "Mine")
|
||||
{
|
||||
await _resources.AddOreGenerationAsync(username, _mineOrePerMinute);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -551,4 +574,15 @@ public class LocationService
|
||||
_ => "Field"
|
||||
};
|
||||
}
|
||||
|
||||
private static string? NormalizeBuildingType(string buildingType)
|
||||
{
|
||||
var trimmed = buildingType.Trim();
|
||||
return trimmed.ToLowerInvariant() switch
|
||||
{
|
||||
"logcamp" or "log camp" => "LogCamp",
|
||||
"mine" => "Mine",
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user