-
Notifications
You must be signed in to change notification settings - Fork 11
/
PersistedReservedMoneyCheckers.cs
55 lines (49 loc) · 2.61 KB
/
PersistedReservedMoneyCheckers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using TPP.Model;
namespace TPP.Persistence.MongoDB
{
/// <summary>
/// In order to prevent token overspending in a setup where both the old and new core run simultaneously,
/// they both need to know about _all_ reserved tokens (see <see cref="IBank{T}"/> for info on reserved tokens).
/// The new core does not reserve any tokens yet, and the old core saves all reserved tokens to the database.
/// This class then knows all places in the database it needs to look.
/// </summary>
public class PersistedReservedMoneyCheckers
{
private readonly IMongoDatabase _database;
public PersistedReservedMoneyCheckers(IMongoDatabase database) => _database = database;
public async Task<long> AllDatabaseReservedTokens(User user) =>
await PinballReservedTokens(user) +
await BadgeBuyOffersReservedTokens(user) +
await ItemBuyOffersReservedTokens(user) +
await SidegameReservedTokens(user);
private async Task<long> PinballReservedTokens(User user) =>
(await (
from doc in _database.GetCollection<BsonDocument>("misc").AsQueryable()
where doc["_id"] == "pinball_state" && doc["users"][user.Id] != BsonNull.Value
select doc["users"][user.Id]
).ToListAsync()).Sum(i => i.ToInt32());
private async Task<long> BadgeBuyOffersReservedTokens(User user) =>
(await (
from doc in _database.GetCollection<BsonDocument>("badgebuyoffers").AsQueryable()
where doc["user"] == user.Id
select new { Price = doc["price"], Amount = doc["amount"] }
).ToListAsync()).Sum(obj => obj.Price.ToInt32() * obj.Amount.ToInt32());
private async Task<long> ItemBuyOffersReservedTokens(User user) =>
(await (
from doc in _database.GetCollection<BsonDocument>("itembuyoffers").AsQueryable()
where doc["user_id"] == user.Id
select new { Offer = doc["offer"], Quantity = doc["quantity"] }
).ToListAsync()).Sum(obj => obj.Offer.ToInt32() * obj.Quantity.ToInt32());
private async Task<long> SidegameReservedTokens(User user) =>
(await (
from doc in _database.GetCollection<BsonDocument>("misc").AsQueryable()
where doc["_id"] == "sidegame_reserved_tokens" && doc["users"][user.Id] != BsonNull.Value
select doc["users"][user.Id]
).ToListAsync()).Sum(i => i.ToInt32());
}
}