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