Adding units

This commit is contained in:
hz
2026-05-18 15:30:46 -05:00
parent 6366ca46c6
commit 37a46a7b36
10 changed files with 529 additions and 3 deletions
@@ -51,8 +51,31 @@ public class ResourceService
public async Task<PlayerResources> GetAsync(string username)
{
var resources = await EnsureAsync(username);
return await ApplyGoldGenerationAsync(resources);
return await EnsureAsync(username);
}
public async Task<bool> TrySpendGoldAsync(string username, int amount)
{
if (amount <= 0)
{
return true;
}
await EnsureAsync(username);
var update = Builders<PlayerResources>.Update
.Inc(x => x.Gold, -amount)
.Set(x => x.UpdatedAtUtc, DateTime.UtcNow);
var updated = await _resources.FindOneAndUpdateAsync(
x => x.Username == username && x.Gold >= amount,
update,
new FindOneAndUpdateOptions<PlayerResources>
{
ReturnDocument = ReturnDocument.After
});
return updated is not null;
}
private async Task<PlayerResources> ApplyGoldGenerationAsync(PlayerResources resources)