forked from planetarium/mimir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuery.cs
258 lines (232 loc) · 10.9 KB
/
Query.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
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
using HotChocolate.AspNetCore;
using Lib9c.GraphQL.Extensions;
using Lib9c.GraphQL.InputObjects;
using Lib9c.Models.Items;
using Lib9c.Models.Market;
using Lib9c.Models.States;
using Libplanet.Crypto;
using Mimir.GraphQL.Objects;
using Mimir.MongoDB;
using Mimir.MongoDB.Bson;
using Mimir.MongoDB.Repositories;
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Extensions;
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 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) =>
(await repo.GetByAddressAsync(address)).Object;
/// <summary>
/// Get arena sub-fields.
/// </summary>
public ArenaObject GetArena() => new();
/// <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) =>
(await repo.GetByAddressAsync(address)).Object;
/// <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)
{
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.");
}
/// <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 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 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 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] WorldInformationRepository repo) =>
(await repo.GetByAddressAsync(address)).Object;
}