Adding units
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user