diff --git a/src/PlayFabBuddy.Cli/Commands/Player/CreateNewPlayersCommand.cs b/src/PlayFabBuddy.Cli/Commands/Player/CreateNewPlayersCommand.cs index a47333c..db760fc 100644 --- a/src/PlayFabBuddy.Cli/Commands/Player/CreateNewPlayersCommand.cs +++ b/src/PlayFabBuddy.Cli/Commands/Player/CreateNewPlayersCommand.cs @@ -1,23 +1,16 @@ -using PlayFabBuddy.Lib.Aggregate; +using PlayFabBuddy.Infrastructure.Adapter.PlayFab; +using PlayFabBuddy.Infrastructure.Exceptions; using PlayFabBuddy.Lib.Interfaces.Adapter; -using PlayFabBuddy.Lib.Interfaces.Repositories; using PlayFabBuddy.Lib.UseCases.Player; using Spectre.Console; using Spectre.Console.Cli; namespace PlayFabBuddy.Cli.Commands.Player; -public class CreateNewPlayersCommand : AsyncCommand +public class CreateNewPlayersCommand( + IPlayerAccountAdapter playerAccountAdapter) + : AsyncCommand { - private readonly IRepository _repository; - private readonly IPlayerAccountAdapter _playerAccountAdapter; - - public CreateNewPlayersCommand(IPlayerAccountAdapter playerAccountAdapter, IRepository repo) - { - _playerAccountAdapter = playerAccountAdapter; - _repository = repo; - } - public async override Task ExecuteAsync(CommandContext context, CreateNewPlayersCommandSettings settings) { await AnsiConsole.Progress().StartAsync(async ctx => @@ -35,17 +28,29 @@ await AnsiConsole.Progress().StartAsync(async ctx => return 0; } - private async Task CreateUsers(int concurrentUsers, ProgressTask task) + private async Task CreateUsers(int numberOfUsersToCreate, ProgressTask task) { - var commandList = new List>(); - for (var i = 0; i < concurrentUsers; i++) + task.MaxValue(numberOfUsersToCreate); + for (var i = 0; i < numberOfUsersToCreate; i++) { - task.Increment(i % 10); - commandList.Add(new RegisterNewPlayerUseCase(_playerAccountAdapter).ExecuteAsync()); + task.Increment(1); + try + { + await new RegisterNewPlayerUseCase(playerAccountAdapter).ExecuteAsync(); + } + catch (AddPlayerRateLimitException e) + { + AnsiConsole.MarkupLine($"[red]Rate Limit Exceeded, waiting for {e.RetryInSeconds} seconds[/]"); + + var retry = 0; + if (e.RetryInSeconds is not null) + { + retry = (int)e.RetryInSeconds; + } + Thread.Sleep(1000 * retry); + + await new RegisterNewPlayerUseCase(playerAccountAdapter).ExecuteAsync(); + } } - - var results = await Task.WhenAll(commandList); - - await _repository.Append(results.ToList()); } } \ No newline at end of file diff --git a/src/PlayFabBuddy.Infrastructure/Adapter/PlayFab/PlayerAccountAdapter.cs b/src/PlayFabBuddy.Infrastructure/Adapter/PlayFab/PlayerAccountAdapter.cs index 0ca1ddb..54f9101 100644 --- a/src/PlayFabBuddy.Infrastructure/Adapter/PlayFab/PlayerAccountAdapter.cs +++ b/src/PlayFabBuddy.Infrastructure/Adapter/PlayFab/PlayerAccountAdapter.cs @@ -52,9 +52,12 @@ public async Task LoginWithCustomId(string customI */ var loginResult = await PlayFabClientAPI.LoginWithCustomIDAsync(request); - if (loginResult.Error != null && loginResult.Error.HttpStatus == "Forbidden") + switch (loginResult.Error) { - throw new AddPlayerForbiddenException(customId); + case { HttpStatus: "Forbidden" }: + throw new AddPlayerForbiddenException(customId); + case {HttpStatus:"TooManyRequests"}: + throw new AddPlayerRateLimitException(customId, loginResult.Error.RetryAfterSeconds); } var masterPlayerAccount = new MasterPlayerAccountEntity { @@ -112,11 +115,6 @@ public async Task BanPlayerByTitlePlayerAccount(List