Adding barracks

This commit is contained in:
hz
2026-05-19 04:42:57 -05:00
parent 34d406e993
commit 78513603ab
10 changed files with 556 additions and 152 deletions
@@ -82,6 +82,35 @@ public class ResourceService
return updated is not null;
}
public async Task<bool> TrySpendResourcesAsync(string username, int gold, int wood, int ore)
{
gold = Math.Max(0, gold);
wood = Math.Max(0, wood);
ore = Math.Max(0, ore);
if (gold == 0 && wood == 0 && ore == 0)
{
return true;
}
await EnsureAsync(username);
var update = Builders<PlayerResources>.Update
.Inc(x => x.Gold, -gold)
.Inc(x => x.Wood, -wood)
.Inc(x => x.Ore, -ore)
.Set(x => x.UpdatedAtUtc, DateTime.UtcNow);
var updated = await _resources.FindOneAndUpdateAsync(
x => x.Username == username && x.Gold >= gold && x.Wood >= wood && x.Ore >= ore,
update,
new FindOneAndUpdateOptions<PlayerResources>
{
ReturnDocument = ReturnDocument.After
});
return updated is not null;
}
public async Task AddWoodGenerationAsync(string username, int woodPerMinute)
{
if (woodPerMinute <= 0)