Skip to content

Commit

Permalink
fixed player create progress bar, accounting for PlayFab new API Rate…
Browse files Browse the repository at this point in the history
… limiting
  • Loading branch information
Annonator committed Dec 31, 2023
1 parent 2cb20e5 commit d4a5649
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
47 changes: 26 additions & 21 deletions src/PlayFabBuddy.Cli/Commands/Player/CreateNewPlayersCommand.cs
Original file line number Diff line number Diff line change
@@ -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<CreateNewPlayersCommandSettings>
public class CreateNewPlayersCommand(
IPlayerAccountAdapter playerAccountAdapter)
: AsyncCommand<CreateNewPlayersCommandSettings>
{
private readonly IRepository<MasterPlayerAccountAggregate> _repository;
private readonly IPlayerAccountAdapter _playerAccountAdapter;

public CreateNewPlayersCommand(IPlayerAccountAdapter playerAccountAdapter, IRepository<MasterPlayerAccountAggregate> repo)
{
_playerAccountAdapter = playerAccountAdapter;
_repository = repo;
}

public async override Task<int> ExecuteAsync(CommandContext context, CreateNewPlayersCommandSettings settings)
{
await AnsiConsole.Progress().StartAsync(async ctx =>
Expand All @@ -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<Task<MasterPlayerAccountAggregate>>();
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ public async Task<MasterPlayerAccountAggregate> 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 {
Expand Down Expand Up @@ -112,11 +115,6 @@ public async Task<bool> BanPlayerByTitlePlayerAccount(List<MasterPlayerAccountAg
var response = await _playFabAdminInstanceApi.BanUsersAsync(request);

// TO make it simple for now, check if we have banned the same amount of players as we requested. Can be optimized in the future.
if (response.Result.BanData.Count == entityList.Count)
{
return true;
}

return false;
return response.Result.BanData.Count == entityList.Count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PlayFabBuddy.Infrastructure.Exceptions;

public class AddPlayerRateLimitException(string CustomId, uint? retryInSeconds) : Exception
{
public string CustomId { get; init; } = CustomId;

public uint? RetryInSeconds { get; init; } = retryInSeconds;
}

0 comments on commit d4a5649

Please sign in to comment.