adding resource limits and archers
This commit is contained in:
@@ -8,6 +8,9 @@ public class ResourceService
|
||||
private readonly IMongoCollection<PlayerResources> _resources;
|
||||
private readonly int _startingGold;
|
||||
private readonly int _headquartersGoldPerMinute;
|
||||
private readonly int _townHallGoldCapacity;
|
||||
private readonly int _townHallWoodCapacity;
|
||||
private readonly int _townHallOreCapacity;
|
||||
|
||||
public ResourceService(IConfiguration cfg)
|
||||
{
|
||||
@@ -20,6 +23,15 @@ public class ResourceService
|
||||
_headquartersGoldPerMinute = int.TryParse(cfg["Resources:HeadquartersGoldPerMinute"], out var goldPerMinute)
|
||||
? Math.Max(0, goldPerMinute)
|
||||
: 10;
|
||||
_townHallGoldCapacity = int.TryParse(cfg["Resources:TownHallGoldCapacity"], out var goldCapacity)
|
||||
? Math.Max(0, goldCapacity)
|
||||
: 500;
|
||||
_townHallWoodCapacity = int.TryParse(cfg["Resources:TownHallWoodCapacity"], out var woodCapacity)
|
||||
? Math.Max(0, woodCapacity)
|
||||
: 100;
|
||||
_townHallOreCapacity = int.TryParse(cfg["Resources:TownHallOreCapacity"], out var oreCapacity)
|
||||
? Math.Max(0, oreCapacity)
|
||||
: 100;
|
||||
|
||||
_resources.Indexes.CreateOne(new CreateIndexModel<PlayerResources>(
|
||||
Builders<PlayerResources>.IndexKeys.Ascending(x => x.Username),
|
||||
@@ -31,7 +43,7 @@ public class ResourceService
|
||||
var now = DateTime.UtcNow;
|
||||
var update = Builders<PlayerResources>.Update
|
||||
.SetOnInsert(x => x.Username, username)
|
||||
.SetOnInsert(x => x.Gold, _startingGold)
|
||||
.SetOnInsert(x => x.Gold, Math.Min(_startingGold, _townHallGoldCapacity))
|
||||
.SetOnInsert(x => x.Wood, 0)
|
||||
.SetOnInsert(x => x.Ore, 0)
|
||||
.SetOnInsert(x => x.CreatedAtUtc, now)
|
||||
@@ -39,6 +51,9 @@ public class ResourceService
|
||||
.SetOnInsert(x => x.LastWoodGeneratedAtUtc, now)
|
||||
.SetOnInsert(x => x.LastOreGeneratedAtUtc, now)
|
||||
.Set(x => x.GoldPerMinute, _headquartersGoldPerMinute)
|
||||
.Set(x => x.GoldCapacity, _townHallGoldCapacity)
|
||||
.Set(x => x.WoodCapacity, _townHallWoodCapacity)
|
||||
.Set(x => x.OreCapacity, _townHallOreCapacity)
|
||||
.Set(x => x.UpdatedAtUtc, now);
|
||||
|
||||
var resources = await _resources.FindOneAndUpdateAsync(
|
||||
@@ -167,34 +182,52 @@ public class ResourceService
|
||||
var oreElapsedMinutes = resources.OrePerMinute <= 0 || resources.LastOreGeneratedAtUtc.Year < 2000
|
||||
? 0
|
||||
: (int)Math.Floor((now - resources.LastOreGeneratedAtUtc).TotalMinutes);
|
||||
var cappedGold = Math.Min(resources.Gold, _townHallGoldCapacity);
|
||||
var cappedWood = Math.Min(resources.Wood, _townHallWoodCapacity);
|
||||
var cappedOre = Math.Min(resources.Ore, _townHallOreCapacity);
|
||||
var isOverCapacity = resources.Gold != cappedGold || resources.Wood != cappedWood || resources.Ore != cappedOre;
|
||||
|
||||
if (goldElapsedMinutes <= 0 && woodElapsedMinutes <= 0 && oreElapsedMinutes <= 0)
|
||||
if (goldElapsedMinutes <= 0 && woodElapsedMinutes <= 0 && oreElapsedMinutes <= 0 && !isOverCapacity)
|
||||
{
|
||||
return resources;
|
||||
}
|
||||
|
||||
var update = Builders<PlayerResources>.Update
|
||||
.Set(x => x.GoldPerMinute, _headquartersGoldPerMinute)
|
||||
.Set(x => x.GoldCapacity, _townHallGoldCapacity)
|
||||
.Set(x => x.WoodCapacity, _townHallWoodCapacity)
|
||||
.Set(x => x.OreCapacity, _townHallOreCapacity)
|
||||
.Set(x => x.UpdatedAtUtc, now);
|
||||
|
||||
if (isOverCapacity)
|
||||
{
|
||||
update = update
|
||||
.Set(x => x.Gold, cappedGold)
|
||||
.Set(x => x.Wood, cappedWood)
|
||||
.Set(x => x.Ore, cappedOre);
|
||||
}
|
||||
|
||||
if (goldElapsedMinutes > 0)
|
||||
{
|
||||
var generatedGold = Math.Min(_townHallGoldCapacity, cappedGold + (goldElapsedMinutes * resources.GoldPerMinute));
|
||||
update = update
|
||||
.Inc(x => x.Gold, goldElapsedMinutes * resources.GoldPerMinute)
|
||||
.Set(x => x.Gold, generatedGold)
|
||||
.Set(x => x.LastGoldGeneratedAtUtc, resources.LastGoldGeneratedAtUtc.AddMinutes(goldElapsedMinutes));
|
||||
}
|
||||
|
||||
if (woodElapsedMinutes > 0)
|
||||
{
|
||||
var generatedWood = Math.Min(_townHallWoodCapacity, cappedWood + (woodElapsedMinutes * resources.WoodPerMinute));
|
||||
update = update
|
||||
.Inc(x => x.Wood, woodElapsedMinutes * resources.WoodPerMinute)
|
||||
.Set(x => x.Wood, generatedWood)
|
||||
.Set(x => x.LastWoodGeneratedAtUtc, resources.LastWoodGeneratedAtUtc.AddMinutes(woodElapsedMinutes));
|
||||
}
|
||||
|
||||
if (oreElapsedMinutes > 0)
|
||||
{
|
||||
var generatedOre = Math.Min(_townHallOreCapacity, cappedOre + (oreElapsedMinutes * resources.OrePerMinute));
|
||||
update = update
|
||||
.Inc(x => x.Ore, oreElapsedMinutes * resources.OrePerMinute)
|
||||
.Set(x => x.Ore, generatedOre)
|
||||
.Set(x => x.LastOreGeneratedAtUtc, resources.LastOreGeneratedAtUtc.AddMinutes(oreElapsedMinutes));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user