-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathUseHeroResourceCommand.cs
More file actions
51 lines (42 loc) · 1.9 KB
/
Copy pathUseHeroResourceCommand.cs
File metadata and controls
51 lines (42 loc) · 1.9 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
namespace MainCore.Commands.Features.UseHeroItem
{
[Handler]
public static partial class UseHeroResourceCommand
{
public sealed record Command(AccountId AccountId, long[] Resource) : IAccountCommand;
private static async ValueTask<Result> HandleAsync(
Command command,
ToHeroInventoryCommand.Handler toHeroInventoryCommand,
UpdateInventoryCommand.Handler updateInventoryCommand,
ValidateEnoughResourceCommand.Handler validateEnoughResourceCommand,
UseHeroItemCommand.Handler useHeroItemCommand,
IDelayService delayService,
CancellationToken cancellationToken)
{
var (accountId, resource) = command;
var result = await toHeroInventoryCommand.HandleAsync(new(), cancellationToken);
if (result.IsFailed) return result;
await updateInventoryCommand.HandleAsync(new(accountId), cancellationToken);
resource = resource.Select(RoundUpTo100).ToArray();
result = await validateEnoughResourceCommand.HandleAsync(new(accountId, resource), cancellationToken);
if (result.IsFailed) return result;
var itemsToUse = new Dictionary<HeroItemEnums, long>
{
{ HeroItemEnums.Wood, resource[0] },
{ HeroItemEnums.Clay, resource[1] },
{ HeroItemEnums.Iron, resource[2] },
{ HeroItemEnums.Crop, resource[3] },
};
result = await useHeroItemCommand.HandleAsync(new(itemsToUse), cancellationToken);
if (result.IsFailed) return result;
await delayService.DelayClick(cancellationToken);
return Result.Ok();
}
private static long RoundUpTo100(long res)
{
if (res == 0) return 0;
var remainder = res % 100;
return res + (100 - remainder);
}
}
}