Skip to content

Commit

Permalink
Merge pull request #111 from MrDave1999/feat/ChangeTeam
Browse files Browse the repository at this point in the history
feat: Allow players to change teams
  • Loading branch information
MrDave1999 authored Sep 28, 2024
2 parents fbf4c8e + 8d8d472 commit 32534ca
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
45 changes: 45 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.

15 changes: 15 additions & 0 deletions src/Application/Common/Resources/Messages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@
<data name="NoPermissions" xml:space="preserve">
<value>You do not have permissions to use this command</value>
</data>
<data name="NoTeam" xml:space="preserve">
<value>You do not belong to any team</value>
</data>
<data name="OnFlagAtBasePosition" xml:space="preserve">
<value>~n~~n~~n~{GameText}Defend this flag from enemy capture!</value>
</data>
Expand Down Expand Up @@ -240,6 +243,12 @@
<data name="PasswordSuccessfullyChanged" xml:space="preserve">
<value>Password successfully changed. New password: {NewPassword}</value>
</data>
<data name="PlayerHasChangedTeams" xml:space="preserve">
<value>Attention, team! {PlayerName} has made the switch and is now part of the {TeamName} team</value>
</data>
<data name="PlayerIsAlreadyInTeam" xml:space="preserve">
<value>You are already a member of that team</value>
</data>
<data name="PlayerNameAlreadyExists" xml:space="preserve">
<value>That player name already exists</value>
</data>
Expand Down Expand Up @@ -267,6 +276,12 @@
<data name="SuccessfulLogin" xml:space="preserve">
<value>You have successfully logged in</value>
</data>
<data name="TeamIsFull" xml:space="preserve">
<value>The team is currently full</value>
</data>
<data name="TeamsAreEqualInMembers" xml:space="preserve">
<value>Both teams have the same number of members</value>
</data>
<data name="TiedTeams" xml:space="preserve">
<value>It's a tie! Both teams couldn't beat each other this time</value>
</data>
Expand Down
1 change: 1 addition & 0 deletions src/Application/Players/PlayerSpawnSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public void OnPlayerSpawn(Player player)
player.Interior = currentMap.Interior;
player.Color = playerInfo.Team.ColorHex;
player.Team = (int)playerInfo.Team.Id;
player.Skin = (int)playerInfo.Team.SkinId;
if (playerInfo.HasSkin())
{
player.Skin = playerInfo.SkinId;
Expand Down
95 changes: 95 additions & 0 deletions src/Application/Teams/Systems/ChangeTeamSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
namespace CTF.Application.Teams.Systems;

public class ChangeTeamSystem(
IWorldService worldService,
IDialogService dialogService,
TeamTextDrawRenderer teamTextDrawRenderer,
OnFlagDropped onFlagDropped) : ISystem
{
[PlayerCommand("team")]
public async void ShowTeams(Player player)
{
PlayerInfo playerInfo = player.GetInfo();
if (playerInfo.Team == Team.None)
{
player.SendClientMessage(Color.Red, Messages.NoTeam);
return;
}

var columnHeaders = new[]
{
"Name",
"Members"
};
var tablistDialog = new TablistDialog(
caption: "Select a team",
button1: "Select",
button2: "Close",
columnHeaders);

Team alphaTeam = Team.Alpha;
tablistDialog.Add(columns:
[
$"{alphaTeam.ColorHex}{alphaTeam.Name}",
$"{alphaTeam.ColorHex}{alphaTeam.Members.Count}"
], tag: alphaTeam);

Team betaTeam = Team.Beta;
tablistDialog.Add(columns:
[
$"{betaTeam.ColorHex}{betaTeam.Name}",
$"{betaTeam.ColorHex}{betaTeam.Members.Count}"
], tag: betaTeam);

TablistDialogResponse response = await dialogService.ShowAsync(player, tablistDialog);
if (response.IsRightButtonOrDisconnected())
return;

Team selectedTeam = response.Item.Tag as Team;
ChangeTeam(player, selectedTeam);
}

private void ChangeTeam(Player player, Team selectedTeam)
{
Team alphaTeam = Team.Alpha;
Team betaTeam = Team.Beta;
PlayerInfo playerInfo = player.GetInfo();
if (playerInfo.Team == selectedTeam)
{
player.SendClientMessage(Color.Red, Messages.PlayerIsAlreadyInTeam);
return;
}

if (alphaTeam.Members.Count == betaTeam.Members.Count)
{
player.SendClientMessage(Color.Red, Messages.TeamsAreEqualInMembers);
return;
}

if (selectedTeam.IsFull())
{
player.SendClientMessage(Color.Red, Messages.TeamIsFull);
return;
}

if (playerInfo.HasCapturedFlag())
{
onFlagDropped.Handle(selectedTeam, player);
}

Team rivalTeam = selectedTeam.RivalTeam;
selectedTeam.Members.Add(player);
rivalTeam.Members.Remove(player);
teamTextDrawRenderer.UpdateTeamMembers(selectedTeam);
teamTextDrawRenderer.UpdateTeamMembers(rivalTeam);
var message = Smart.Format(Messages.PlayerHasChangedTeams, new
{
PlayerName = player.Name,
TeamName = selectedTeam.Name
});
worldService.SendClientMessage(selectedTeam.ColorHex, message);
playerInfo.SetTeam(selectedTeam.Id);
player.Team = (int)selectedTeam.Id;
player.Spawn();
}
}

0 comments on commit 32534ca

Please sign in to comment.