forked from planetarium/mimir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery.cs
More file actions
418 lines (385 loc) · 15.4 KB
/
Copy pathQuery.cs
File metadata and controls
418 lines (385 loc) · 15.4 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
using Hangfire;
using HotChocolate.AspNetCore;
using Lib9c.GraphQL.Extensions;
using Lib9c.GraphQL.InputObjects;
using Lib9c.Models.Block;
using Lib9c.Models.Items;
using Lib9c.Models.Market;
using Lib9c.Models.States;
using Libplanet.Crypto;
using Mimir.GraphQL.Objects;
using Mimir.GraphQL.Types;
using Mimir.MongoDB;
using Mimir.MongoDB.Bson;
using Mimir.MongoDB.Models;
using Mimir.MongoDB.Repositories;
using Mimir.Services;
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Extensions;
using Nekoyume.Model.EnumType;
using Nekoyume.TableData;
namespace Mimir.GraphQL.Queries;
// NOTE: Sort methods in alphabetical order.
public class Query
{
/// <summary>
/// Get an action point by address.
/// </summary>
/// <param name="address">The address of the avatar.</param>
/// <returns>The action point.</returns>
public async Task<int> GetActionPointAsync(
Address address,
[Service] IActionPointRepository repo
) => (await repo.GetByAddressAsync(address)).Object;
/// <summary>
/// Get all action types.
/// </summary>
/// <returns>All action types.</returns>
public async Task<IEnumerable<ActionTypeDocument>> GetActionTypesAsync(
[Service] IActionTypeRepository repo
) => (await repo.GetAllAsync()).OrderBy(x => x.Id);
/// <summary>
/// Get an agent state by address.
/// </summary>
/// <param name="address">The address of the agent.</param>
/// <returns>The agent state</returns>
public async Task<AgentState> GetAgentAsync(
Address address,
[Service] IAgentRepository repo,
[Service] IBackgroundJobClient jobClient
)
{
try
{
var document = await repo.GetByAddressAsync(address);
if (document.Metadata.StoredBlockIndex != 0)
{
jobClient.Enqueue<IStateRecoveryService>(service =>
service.TryRecoverAgentStateAsync(address)
);
}
return document.Object;
}
catch (Mimir.MongoDB.Exceptions.DocumentNotFoundInMongoCollectionException)
{
jobClient.Enqueue<IStateRecoveryService>(service =>
service.TryRecoverAgentStateAsync(address)
);
throw;
}
}
/// <summary>
/// Get an avatar state by address.
/// </summary>
/// <param name="address">The address of the avatar.</param>
/// <returns>The avatar state</returns>
public async Task<AvatarState> GetAvatarAsync(
Address address,
[Service] IAvatarRepository repo,
[Service] IBackgroundJobClient jobClient
)
{
try
{
return (await repo.GetByAddressAsync(address)).Object;
}
catch (Mimir.MongoDB.Exceptions.DocumentNotFoundInMongoCollectionException)
{
jobClient.Enqueue<IStateRecoveryService>(service =>
service.TryRecoverAvatarStateAsync(address)
);
throw;
}
}
/// <summary>
/// Get the balance of a specific currency for a given address.
/// Choose one of the following parameters to specify the currency: currency, currencyTicker
/// </summary>
/// <param name="currency">The currency object.</param>
/// <param name="currencyTicker">The ticker of the currency.</param>
/// <param name="address">The address of the balance.</param>
/// <exception cref="GraphQLRequestException"></exception>
public async Task<string> GetBalanceAsync(
CurrencyInput? currency,
string? currencyTicker,
Address address,
[Service] IBalanceRepository repo,
[Service] IBackgroundJobClient jobClient
)
{
try
{
if (currency is not null)
{
return (await repo.GetByAddressAsync(currency.ToCurrency(), address)).Object;
}
if (currencyTicker is not null)
{
return (await repo.GetByAddressAsync(currencyTicker.ToCurrency(), address)).Object;
}
throw new GraphQLRequestException(
"Either currency or currencyTicker must be provided."
);
}
catch (Mimir.MongoDB.Exceptions.DocumentNotFoundInMongoCollectionException)
{
if (currencyTicker?.ToUpper() == "NCG")
{
jobClient.Enqueue<IStateRecoveryService>(service =>
service.TryRecoverNCGBalanceAsync(address)
);
}
throw;
}
}
/// <summary>
/// Get a collection state by avatar address.
/// </summary>
/// <param name="address">The address of the avatar.</param>
/// <returns>The collection state for the specified avatar address.</returns>
public async Task<CollectionState> GetCollectionAsync(
Address address,
[Service] ICollectionRepository repo
) => (await repo.GetByAddressAsync(address)).Object;
/// <summary>
/// Get combination slot states for a specific avatar address.
/// </summary>
/// <param name="avatarAddress">The address of the avatar</param>
/// <returns>Combination slot states for the specified avatar address.</returns>
public async Task<Dictionary<int, CombinationSlotState>> GetCombinationSlotsAsync(
Address avatarAddress,
[Service] IAllCombinationSlotStateRepository repo
) => (await repo.GetByAddressAsync(avatarAddress)).Object.CombinationSlots;
/// <summary>
/// Get daily active users count grouped by date.
/// </summary>
/// <param name="startDate">Optional start date filter</param>
/// <param name="endDate">Optional end date filter</param>
/// <returns>List of daily active users with date and count</returns>
public async Task<List<DailyActiveUser>> GetDailyActiveUsersAsync(
DateTime? startDate,
DateTime? endDate,
[Service] ITransactionRepository repo
) => await repo.GetDailyActiveUsersAsync(startDate, endDate);
/// <summary>
/// Get the daily reward received block index by address.
/// </summary>
/// <param name="address">The address of the avatar.</param>
/// <returns>The daily reward received block index.</returns>
public async Task<long> GetDailyRewardReceivedBlockIndexAsync(
Address address,
[Service] IDailyRewardRepository repo
) => (await repo.GetByAddressAsync(address)).Object;
/// <summary>
/// Get the inventory state by avatar address.
/// </summary>
/// <param name="address">The address of the avatar.</param>
/// <returns>The inventory state for the specified avatar address.</returns>
public async Task<Inventory> GetInventoryAsync(
Address address,
[Service] IInventoryRepository repo
) => (await repo.GetByAddressAsync(address)).Object;
/// <summary>
/// Get metadata by collection name.
/// </summary>
/// <param name="collectionName">The name of the collection.</param>
/// <returns>The metadata</returns>
public async Task<MetadataDocument> GetMetadataAsync(
string collectionName,
[Service] IMetadataRepository repo
) => await repo.GetByCollectionAsync(collectionName);
/// <summary>
/// Get Block by index.
/// </summary>
/// <param name="index">Block index.</param>
/// <returns>The Block Information</returns>
public async Task<BlockDocument> GetBlockAsync(long index, [Service] IBlockRepository repo) =>
await repo.GetByIndexAsync(index);
/// <summary>
/// Get Transaction by transaction ID.
/// </summary>
/// <param name="txId">Transaction ID.</param>
/// <returns>The Transaction Information</returns>
public async Task<TransactionDocument> GetTransactionAsync(
string txId,
[Service] ITransactionRepository repo
) => await repo.GetByTxIdAsync(txId);
/// <summary>
/// Get an pet state by avatar address.
/// </summary>
/// <param name="avatarAddress">The address of the avatar.</param>
/// <returns>The agent state</returns>
public async Task<PetState> GetPetAsync(Address avatarAddress, [Service] IPetRepository repo) =>
(await repo.GetByAvatarAddressAsync(avatarAddress)).Object;
/// <summary>
/// Get the pledge state for a given agent address.
/// </summary>
public async Task<PledgeDocument> GetPledgeAsync(
Address agentAddress,
[Service] IPledgeRepository repo
) => await repo.GetByAddressAsync(agentAddress.GetPledgeAddress());
/// <summary>
/// Get the product by product ID.
/// </summary>
/// <param name="productId">The product ID</param>
/// <returns>The product.</returns>
public async Task<Product> GetProductAsync(Guid productId, [Service] IProductRepository repo) =>
(await repo.GetByProductIdAsync(productId)).Object;
/// <summary>
/// Get the product ids that are contained in the products state for a specific avatar address.
/// </summary>
/// <param name="avatarAddress">The address of the avatar.</param>
/// <returns>The product ids that contained in the products state for the specified avatar address.</returns>
public async Task<List<Guid>> GetProductIdsAsync(
Address avatarAddress,
[Service] IProductsRepository repo
) => (await repo.GetByAvatarAddressAsync(avatarAddress)).Object.ProductIds;
/// <summary>
/// Get the runes for a specific avatar address.
/// </summary>
/// <param name="avatarAddress">The address of the avatar.</param>
/// <returns>The runes for a specific avatar address.</returns>
public async Task<RuneState[]> GetRunesAsync(
Address avatarAddress,
[Service] AllRuneRepository repo
) => (await repo.GetByAddressAsync(avatarAddress)).Object.Runes.Values.ToArray();
/// <summary>
/// Get a stake state by agent address.
/// </summary>
/// <param name="address">The address of the agent.</param>
/// <returns>The stake state.</returns>
public async Task<StakeState?> GetStakeAsync(
Address address,
[Service] IStakeRepository repo
) => (await repo.GetByAgentAddressAsync(address)).Object;
/// <summary>
/// Get a itemSlot state by avatar address.
/// </summary>
/// <param name="address">The address of the avatar.</param>
/// <param name="battleType">The battleType.</param>
/// <returns>The stake state.</returns>
public async Task<ItemSlotState?> GetItemSlotAsync(
Address address,
BattleType battleType,
[Service] IItemSlotRepository repo
) => (await repo.GetByAddressAsync(address, battleType)).Object;
/// <summary>
/// Get a runeSlot state by avatar address.
/// </summary>
/// <param name="address">The address of the avatar.</param>
/// <param name="battleType">The battleType.</param>
/// <returns>The rune slot state.</returns>
public async Task<RuneSlotState?> GetRuneSlotAsync(
Address address,
BattleType battleType,
[Service] IRuneSlotRepository repo
) => (await repo.GetByAddressAsync(address, battleType)).Object;
/// <summary>
/// Get the world boss.
/// </summary>
public async Task<WorldBossState> GetWorldBossAsync(
[Service] MetadataRepository metadataRepo,
[Service] TableSheetsRepository tableSheetsRepo,
[Service] WorldBossRepository worldBossRepo
)
{
var collectionName = CollectionNames.GetCollectionName<WorldBossStateDocument>();
var metadataDocument = await metadataRepo.GetByCollectionAsync(collectionName);
var blockIndex = metadataDocument.LatestBlockIndex;
var worldBossListSheet = await tableSheetsRepo.GetSheetAsync<WorldBossListSheet>();
WorldBossListSheet.Row row;
try
{
row = worldBossListSheet.FindRowByBlockIndex(blockIndex);
}
catch (InvalidOperationException)
{
throw new GraphQLException(
$"Failed to find the world boss row by block index, {blockIndex}"
);
}
var raidId = row.Id;
var worldBossAddress = Addresses.GetWorldBossAddress(raidId);
return (await worldBossRepo.GetByAddressAsync(worldBossAddress)).Object;
}
/// <summary>
/// Get the kill reward record of world boss.
/// </summary>
public async Task<WorldBossKillRewardRecord> GetWorldBossKillRewardRecordAsync(
Address avatarAddress,
[Service] MetadataRepository metadataRepo,
[Service] TableSheetsRepository tableSheetsRepo,
[Service] WorldBossKillRewardRecordRepository worldBossKillRewardRecordRepo
)
{
var collectionName = CollectionNames.GetCollectionName<WorldBossStateDocument>();
var metadataDocument = await metadataRepo.GetByCollectionAsync(collectionName);
var blockIndex = metadataDocument.LatestBlockIndex;
var worldBossListSheet = await tableSheetsRepo.GetSheetAsync<WorldBossListSheet>();
WorldBossListSheet.Row row;
try
{
row = worldBossListSheet.FindRowByBlockIndex(blockIndex);
}
catch (InvalidOperationException)
{
throw new GraphQLException(
$"Failed to find the world boss row by block index, {blockIndex}"
);
}
var raidId = row.Id;
var worldBossKillRewardRecordAddress = Addresses.GetWorldBossKillRewardRecordAddress(
avatarAddress,
raidId
);
return (
await worldBossKillRewardRecordRepo.GetByAddressAsync(worldBossKillRewardRecordAddress)
).Object;
}
/// <summary>
/// Get the raider of world boss.
/// </summary>
public async Task<RaiderState> GetWorldBossRaiderAsync(
Address avatarAddress,
[Service] MetadataRepository metadataRepo,
[Service] TableSheetsRepository tableSheetsRepo,
[Service] WorldBossRaiderRepository worldBossRaiderRepo
)
{
var collectionName = CollectionNames.GetCollectionName<WorldBossStateDocument>();
var metadataDocument = await metadataRepo.GetByCollectionAsync(collectionName);
var blockIndex = metadataDocument.LatestBlockIndex;
var worldBossListSheet = await tableSheetsRepo.GetSheetAsync<WorldBossListSheet>();
WorldBossListSheet.Row row;
try
{
row = worldBossListSheet.FindRowByBlockIndex(blockIndex);
}
catch (InvalidOperationException)
{
throw new GraphQLException(
$"Failed to find the world boss row by block index, {blockIndex}"
);
}
var raidId = row.Id;
var raiderAddress = Addresses.GetRaiderAddress(avatarAddress, raidId);
return (await worldBossRaiderRepo.GetByAddressAsync(raiderAddress)).Object;
}
/// <summary>
/// Get a world information state by avatar address.
/// </summary>
/// <param name="address">The address of the avatar.</param>
/// <returns>The world information state.</returns>
public async Task<WorldInformationState> GetWorldInformationAsync(
Address address,
[Service] IWorldInformationRepository repo
) => (await repo.GetByAddressAsync(address)).Object;
/// <summary>
/// Get WNCG price from CoinMarketCap API.
/// </summary>
/// <returns>The WNCG price information.</returns>
public async Task<WncgPriceType?> GetWncgPriceAsync(
[Service] IWncgPriceService wncgPriceService
) => await wncgPriceService.GetWncgPriceAsync();
}