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

feat: Allow the server owner to give themselves admin #211

Merged
merged 2 commits into from
Nov 4, 2024
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
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ ServerInfo__IntroAudioUrl=https://od.lk/s/Nl8yMDg4MTc0NDBf/intro-cs.mp3
# Expressed in seconds.
ServerInfo__FlagAutoReturnTime=120

ServerOwner__Name=MrDave
# Specify the secret key to give me admin.
ServerOwner__SecretKey=

# Expressed in minutes.
CommandCooldowns__Health=3
CommandCooldowns__Armour=3
Expand Down
27 changes: 27 additions & 0 deletions src/Application/Common/Resources/Messages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/Application/Common/Resources/Messages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@
<data name="OnFlagTaken" xml:space="preserve">
<value>{PlayerName} has taken the {TeamName} team's {ColorName} flag! Keep an eye on the score!</value>
</data>
<data name="OwnerNameOrSecretKeyAreNotSet" xml:space="preserve">
<value>Owner's name or secret key are not set</value>
</data>
<data name="PasswordCannotBeEmpty" xml:space="preserve">
<value>Password cannot be empty</value>
</data>
Expand Down Expand Up @@ -336,6 +339,9 @@
<data name="PlayerIsInClassSelection" xml:space="preserve">
<value>The player is in the class selection</value>
</data>
<data name="PlayerIsNotServerOwner" xml:space="preserve">
<value>You are not the owner of this server</value>
</data>
<data name="PlayerNameAlreadyExists" xml:space="preserve">
<value>That player name already exists</value>
</data>
Expand Down Expand Up @@ -459,4 +465,7 @@
<data name="WrongPassword" xml:space="preserve">
<value>The password you entered is incorrect. Please try again</value>
</data>
<data name="WrongSecretKey" xml:space="preserve">
<value>The secret key you entered is incorrect. Please try again</value>
</data>
</root>
7 changes: 7 additions & 0 deletions src/Application/Players/Accounts/ServerOwnerSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CTF.Application.Players.Accounts;

public class ServerOwnerSettings
{
public string Name { get; init; } = string.Empty;
public string SecretKey { get; init; } = string.Empty;
}
66 changes: 65 additions & 1 deletion src/Application/Players/Accounts/Systems/ChangeRoleSystem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace CTF.Application.Players.Accounts.Systems;

public class ChangeRoleSystem(IPlayerRepository playerRepository) : ISystem
public class ChangeRoleSystem(
IPlayerRepository playerRepository,
IDialogService dialogService,
ServerOwnerSettings serverOwnerSettings) : ISystem
{
[PlayerCommand("setrole")]
public void SetRole(
Expand Down Expand Up @@ -53,4 +56,65 @@ public void SetRole(
targetPlayer.GameText(gameText, 4000, 3);
currentPlayer.SendClientMessage(Color.Yellow, message);
}

[PlayerCommand("givemeadmin")]
public async void GiveMeAdmin(Player currentPlayer)
{
if (string.IsNullOrWhiteSpace(serverOwnerSettings.Name) ||
string.IsNullOrWhiteSpace(serverOwnerSettings.SecretKey))
{
currentPlayer.SendClientMessage(Color.Red, Messages.OwnerNameOrSecretKeyAreNotSet);
return;
}

var ownerName = serverOwnerSettings.Name.Trim();
bool isNotEquals = !currentPlayer.Name.Equals(ownerName, StringComparison.OrdinalIgnoreCase);
if (isNotEquals)
{
currentPlayer.SendClientMessage(Color.Red, Messages.PlayerIsNotServerOwner);
return;
}

var dialog = new InputDialog()
{
Caption = "Secret key",
Content = "Enter secret key",
Button1 = "Accept",
Button2 = "Close"
};

InputDialogResponse response = await dialogService.ShowAsync(currentPlayer, dialog);
if (response.IsRightButtonOrDisconnected())
return;

var enteredSecretKey = response.InputText;
bool isWrongSecretKey = enteredSecretKey != serverOwnerSettings.SecretKey;
if (isWrongSecretKey)
{
const int MaxFailedAttempts = 3;
var failedAttemptCount = currentPlayer.GetComponent<FailedAttemptCountComponent>();
failedAttemptCount ??= currentPlayer.AddComponent<FailedAttemptCountComponent>();
failedAttemptCount.Value++;
if (failedAttemptCount.Value == MaxFailedAttempts)
{
currentPlayer.Kick();
return;
}
currentPlayer.SendClientMessage(Color.Red, Messages.WrongSecretKey);
GiveMeAdmin(currentPlayer);
return;
}

var gameText = Smart.Format(Messages.PromotedToRole, new { RoleName = RoleId.Admin });
PlayerInfo playerInfo = currentPlayer.GetInfo();
playerInfo.SetRole(RoleId.Admin);
playerRepository.UpdateRole(playerInfo);
currentPlayer.GameText(gameText, 4000, 3);
currentPlayer.GetComponent<FailedAttemptCountComponent>()?.Destroy();
}

private class FailedAttemptCountComponent : Component
{
public int Value { get; set; } = 0;
}
}
7 changes: 6 additions & 1 deletion src/Host/Extensions/AppSettingsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ public static IServiceCollection AddSettings(
.GetRequiredSection("TopPlayers")
.Get<TopPlayersSettings>();

var serverOwnerSettings = configuration
.GetRequiredSection("ServerOwner")
.Get<ServerOwnerSettings>();

services
.AddSingleton(serverSettings)
.AddSingleton(commandCooldowns)
.AddSingleton(topPlayersSettings);
.AddSingleton(topPlayersSettings)
.AddSingleton(serverOwnerSettings);

return services;
}
Expand Down
1 change: 1 addition & 0 deletions src/Host/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
global using CTF.Application.Common.Settings;
global using CTF.Application.Common.Services;
global using CTF.Application.Players;
global using CTF.Application.Players.Accounts;
global using CTF.Application.Players.TopPlayers.Models;
global using CTF.Host.Extensions;
global using CTF.Host.Services;
Expand Down
Loading