Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Ban Command to Fix #88 #110

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/PlayFabBuddy.Cli/Commands/Player/BanPlayersCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.Net;
using PlayFabBuddy.Lib.Aggregate;
using PlayFabBuddy.Lib.Interfaces.Adapter;
using PlayFabBuddy.Lib.UseCases.Player;
using Spectre.Console;
using Spectre.Console.Cli;

namespace PlayFabBuddy.Cli.Commands.Player;

public class BanPlayersCommand : AsyncCommand<BanPlayersCommandSettings>
{
private readonly IDataExplorerAdapter _dataExplorerAdapter;
private readonly IPlayerAccountAdapter _playerAccountAdapter;

public BanPlayersCommand(IPlayerAccountAdapter playerAccountAdapter, IDataExplorerAdapter dataExplorerAdapter)
{
_playerAccountAdapter = playerAccountAdapter;
_dataExplorerAdapter = dataExplorerAdapter;
}

public async override Task<int> ExecuteAsync(CommandContext context, BanPlayersCommandSettings settings)
{
var getPlayersUseCase = new GetPlayersByIpUseCAse(_dataExplorerAdapter, _playerAccountAdapter,
IPAddress.Parse(settings.IpAddress));

var playerList = new List<MasterPlayerAccountAggregate>();


await AnsiConsole.Status()
.Spinner(Spinner.Known.Star)
.StartAsync("Searching for Players...", async ctx =>
{
playerList = await getPlayersUseCase.ExecuteAsync();
});

var banUseCase = new BanTitlePlayerAccountsByIpUseCase(_playerAccountAdapter, playerList,
IPAddress.Parse(settings.IpAddress), " Test ");

var hasBanned = false;
await AnsiConsole.Status()
.Spinner(Spinner.Known.Star)
.StartAsync("Banning Players...", async ctx =>
{
hasBanned = await banUseCase.ExecuteAsync();
});

if (hasBanned)
{
var playerTable = new Table();
// Add some columns
playerTable.AddColumn("Master Id");
playerTable.AddColumn("Last Known IP");
playerTable.AddColumn("TitlePlayerData");

foreach (var aggregate in playerList)
{
var titlePlayer = "";

if (aggregate.MasterPlayerAccount.PlayerAccounts != null)
{
foreach (var playerAccount in aggregate.MasterPlayerAccount.PlayerAccounts)
{
titlePlayer += "ID: " + playerAccount.Id + "\n ";
}
}

playerTable.AddRow(new Text(aggregate.MasterPlayerAccount.Id!),
new Text(aggregate.MasterPlayerAccount.LastKnownIp!),
new Text(titlePlayer));
}

AnsiConsole.Write(playerTable);
}

return 0;
}
}
23 changes: 23 additions & 0 deletions src/PlayFabBuddy.Cli/Commands/Player/BanPlayersCommandSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.ComponentModel;
using System.Net;
using Spectre.Console;
using Spectre.Console.Cli;

namespace PlayFabBuddy.Cli.Commands.Player;

public class BanPlayersCommandSettings : PlayerSettings
{
[Description("The IP Address to Ban")]
[CommandArgument(0, "[IPAddress]")]
public string IpAddress { get; set; } = "";

public override ValidationResult Validate()
{
if (IpAddress.Length == 0 && !IPAddress.TryParse(IpAddress, out _))
{
return ValidationResult.Error("Please provide a valid IPAddress");
}

return ValidationResult.Success();
}
}
1 change: 1 addition & 0 deletions src/PlayFabBuddy.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public async static Task<int> Main(string[] args)
players.AddCommand<DeletePlayersCommand>("delete");
players.AddCommand<LoginPlayerCommand>("login");
players.AddCommand<ListPlayersCommand>("list");
players.AddCommand<BanPlayersCommand>("ban");
});
configurator.AddBranch<MatchmakingSettings>("matchmaking", matchmaking =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,22 @@ public async Task Delete(MasterPlayerAccountAggregate account)
{
if (account.HasMoreThanOneTitlePlayerAccount())
{
throw new Exception($"Master PlayerAccount ID \"{account.MasterPlayerAccount.Id}\" has more than one Title");
throw new Exception(
$"Master PlayerAccount ID \"{account.MasterPlayerAccount.Id}\" has more than one Title");
}

var request = new DeleteMasterPlayerAccountRequest
{
PlayFabId = account.MasterPlayerAccount.Id
};
var request = new DeleteMasterPlayerAccountRequest { PlayFabId = account.MasterPlayerAccount.Id };

await PlayFabAdminAPI.DeleteMasterPlayerAccountAsync(request);
}

public async Task<MasterPlayerAccountAggregate> LoginWithCustomId(string customId)
{
var request = new LoginWithCustomIDRequest
{
var request = new LoginWithCustomIDRequest {
CustomId = customId,
CreateAccount = true,
InfoRequestParameters = new GetPlayerCombinedInfoRequestParams
{
GetPlayerProfile = true,
GetTitleData = true,
GetUserData = true,
GetUserAccountInfo = true,
InfoRequestParameters = new GetPlayerCombinedInfoRequestParams {
GetPlayerProfile = true, GetTitleData = true, GetUserData = true, GetUserAccountInfo = true
}
};

Expand All @@ -64,15 +57,11 @@ public async Task<MasterPlayerAccountAggregate> LoginWithCustomId(string customI
throw new AddPlayerForbiddenException(customId);
}

var masterPlayerAccount = new MasterPlayerAccountEntity
{
Id = loginResult.Result.AuthenticationContext.PlayFabId,
CustomId = customId
var masterPlayerAccount = new MasterPlayerAccountEntity {
Id = loginResult.Result.AuthenticationContext.PlayFabId, CustomId = customId
};
var titlePlayerAccount = new TitlePlayerAccountEntity
{
Id = loginResult.Result.AuthenticationContext.EntityId,
TitleId = _playFabApiSettings.TitleId
var titlePlayerAccount = new TitlePlayerAccountEntity {
Id = loginResult.Result.AuthenticationContext.EntityId, TitleId = _playFabApiSettings.TitleId
};
var aggregate = new MasterPlayerAccountAggregate(masterPlayerAccount);
aggregate.AddTitlePlayerAccount(titlePlayerAccount);
Expand All @@ -82,10 +71,7 @@ public async Task<MasterPlayerAccountAggregate> LoginWithCustomId(string customI

public async Task<MasterPlayerAccountAggregate> GetTitleAccountsAndCustomId(MasterPlayerAccountAggregate account)
{
var request = new LookupUserAccountInfoRequest
{
PlayFabId = account.MasterPlayerAccount.Id
};
var request = new LookupUserAccountInfoRequest { PlayFabId = account.MasterPlayerAccount.Id };
// Result:
// Userinfo:
// CustomIdInfo:
Expand All @@ -97,14 +83,40 @@ public async Task<MasterPlayerAccountAggregate> GetTitleAccountsAndCustomId(Mast

account.MasterPlayerAccount.CustomId = response.Result.UserInfo.CustomIdInfo.CustomId;

var titlePlayerAccount = new TitlePlayerAccountEntity
{
Id = response.Result.UserInfo.TitleInfo.TitlePlayerAccount.Id,
TitleId = _playFabApiSettings.TitleId
var titlePlayerAccount = new TitlePlayerAccountEntity {
Id = response.Result.UserInfo.TitleInfo.TitlePlayerAccount.Id, TitleId = _playFabApiSettings.TitleId
};

account.AddTitlePlayerAccount(titlePlayerAccount);

return account;
}
}

public async Task<bool> BanPlayerByTitlePlayerAccount(List<MasterPlayerAccountAggregate> entityList, string reason,
uint? banDurationInHours = null, bool banByIp = false)
{
var banRequests = new List<BanRequest>();

foreach (var entity in entityList)
{
banRequests.Add(new BanRequest {
Reason = reason,
DurationInHours = banDurationInHours,
IPAddress = banByIp ? entity.MasterPlayerAccount.LastKnownIp : null,
PlayFabId = entity.MasterPlayerAccount.Id
});
}

var request = new BanUsersRequest { Bans = banRequests };

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public class TitlePlayerAccountEntity
public MasterPlayerAccountEntity? MasterAccount { get; set; }

public string? TitleId { get; set; }

public bool? IsBanned { get; set; } = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ public interface IPlayerAccountAdapter
public Task<MasterPlayerAccountAggregate> LoginWithCustomId(string customId);
public Task<MasterPlayerAccountAggregate> GetTitleAccountsAndCustomId(MasterPlayerAccountAggregate account);

}
public Task<bool> BanPlayerByTitlePlayerAccount(
List<MasterPlayerAccountAggregate> entityList,
string reason, uint? banDurationInHours = null, bool banByIp = false);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Net;
using PlayFabBuddy.Lib.Aggregate;
using PlayFabBuddy.Lib.Interfaces.Adapter;

namespace PlayFabBuddy.Lib.UseCases.Player;

public class BanTitlePlayerAccountsByIpUseCase : UseCase<bool>
{
private readonly IPAddress _ipToBan;
private readonly IPlayerAccountAdapter _playerAccountAdapter;
private readonly List<MasterPlayerAccountAggregate> _playersToBan;
private readonly string _reason;

public BanTitlePlayerAccountsByIpUseCase(IPlayerAccountAdapter playerAccountAdapter,
List<MasterPlayerAccountAggregate> playersToBan, IPAddress ipToBan, string reason)
{
_playerAccountAdapter = playerAccountAdapter;
_ipToBan = ipToBan;
_reason = reason;
_playersToBan = playersToBan;
}

public async override Task<bool> ExecuteAsync(IProgress<double>? progress = null)
{
return await _playerAccountAdapter.BanPlayerByTitlePlayerAccount(_playersToBan,
_reason + "(provided by PlayFabBuddy)");
}
}