Skip to content

Commit c2f891b

Browse files
authored
Merge pull request ddnet#8988 from Maslowian/cancel-swap
Cancel swap command
2 parents ad0ec42 + 458d9c9 commit c2f891b

File tree

6 files changed

+106
-1
lines changed

6 files changed

+106
-1
lines changed

src/game/server/ddracechat.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,62 @@ void CGameContext::ConSwap(IConsole::IResult *pResult, void *pUserData)
858858
Teams.SwapTeamCharacters(pPlayer, pSwapPlayer, Team);
859859
}
860860

861+
void CGameContext::ConCancelSwap(IConsole::IResult *pResult, void *pUserData)
862+
{
863+
CGameContext *pSelf = (CGameContext *)pUserData;
864+
865+
if(!CheckClientId(pResult->m_ClientId))
866+
return;
867+
868+
CPlayer *pPlayer = pSelf->m_apPlayers[pResult->m_ClientId];
869+
if(!pPlayer)
870+
return;
871+
872+
if(!g_Config.m_SvSwap)
873+
{
874+
pSelf->Console()->Print(
875+
IConsole::OUTPUT_LEVEL_STANDARD,
876+
"chatresp",
877+
"Swap is disabled on this server.");
878+
return;
879+
}
880+
881+
if(g_Config.m_SvTeam == SV_TEAM_FORCED_SOLO)
882+
{
883+
pSelf->Console()->Print(
884+
IConsole::OUTPUT_LEVEL_STANDARD,
885+
"chatresp",
886+
"Swap is not available on forced solo servers.");
887+
return;
888+
}
889+
890+
CGameTeams &Teams = pSelf->m_pController->Teams();
891+
892+
int Team = Teams.m_Core.Team(pResult->m_ClientId);
893+
894+
if(Team < TEAM_FLOCK || Team >= TEAM_SUPER)
895+
{
896+
pSelf->Console()->Print(
897+
IConsole::OUTPUT_LEVEL_STANDARD,
898+
"chatresp",
899+
"Join a team to use swap feature, which means you can swap positions with each other.");
900+
return;
901+
}
902+
903+
bool SwapPending = pPlayer->m_SwapTargetsClientId != -1 && !pSelf->Server()->ClientSlotEmpty(pPlayer->m_SwapTargetsClientId);
904+
905+
if(!SwapPending)
906+
{
907+
pSelf->Console()->Print(
908+
IConsole::OUTPUT_LEVEL_STANDARD,
909+
"chatresp",
910+
"You do not have a pending swap request.");
911+
return;
912+
}
913+
914+
Teams.CancelTeamSwap(pPlayer, Team);
915+
}
916+
861917
void CGameContext::ConSave(IConsole::IResult *pResult, void *pUserData)
862918
{
863919
CGameContext *pSelf = (CGameContext *)pUserData;

src/game/server/entities/character.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,14 @@ void CCharacter::Die(int Killer, int Weapon, bool SendKillMsg)
997997
GameServer()->m_World.m_Core.m_apCharacters[m_pPlayer->GetCid()] = 0;
998998
GameServer()->CreateDeath(m_Pos, m_pPlayer->GetCid(), TeamMask());
999999
Teams()->OnCharacterDeath(GetPlayer()->GetCid(), Weapon);
1000+
1001+
// Cancel swap requests
1002+
for(auto &pPlayer : GameServer()->m_apPlayers)
1003+
{
1004+
if(pPlayer && pPlayer->m_SwapTargetsClientId == GetPlayer()->GetCid())
1005+
pPlayer->m_SwapTargetsClientId = -1;
1006+
}
1007+
GetPlayer()->m_SwapTargetsClientId = -1;
10001008
}
10011009

10021010
bool CCharacter::TakeDamage(vec2 Force, int Dmg, int From, int Weapon)

src/game/server/gamecontext.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,6 +3787,7 @@ void CGameContext::RegisterChatCommands()
37873787
Console()->Register("practice", "?i['0'|'1']", CFGFLAG_CHAT | CFGFLAG_SERVER, ConPractice, this, "Enable cheats for your current team's run, but you can't earn a rank");
37883788
Console()->Register("practicecmdlist", "", CFGFLAG_CHAT | CFGFLAG_SERVER, ConPracticeCmdList, this, "List all commands that are avaliable in practice mode");
37893789
Console()->Register("swap", "?r[player name]", CFGFLAG_CHAT | CFGFLAG_SERVER, ConSwap, this, "Request to swap your tee with another team member");
3790+
Console()->Register("cancelswap", "", CFGFLAG_CHAT | CFGFLAG_SERVER, ConCancelSwap, this, "Cancel your swap request");
37903791
Console()->Register("save", "?r[code]", CFGFLAG_CHAT | CFGFLAG_SERVER, ConSave, this, "Save team with code r.");
37913792
Console()->Register("load", "?r[code]", CFGFLAG_CHAT | CFGFLAG_SERVER, ConLoad, this, "Load with code r. /load to check your existing saves");
37923793
Console()->Register("map", "?r[map]", CFGFLAG_CHAT | CFGFLAG_SERVER | CFGFLAG_NONTEEHISTORIC, ConMap, this, "Vote a map by name");

src/game/server/gamecontext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ class CGameContext : public IGameServer
447447
static void ConPractice(IConsole::IResult *pResult, void *pUserData);
448448
static void ConPracticeCmdList(IConsole::IResult *pResult, void *pUserData);
449449
static void ConSwap(IConsole::IResult *pResult, void *pUserData);
450+
static void ConCancelSwap(IConsole::IResult *pResult, void *pUserData);
450451
static void ConSave(IConsole::IResult *pResult, void *pUserData);
451452
static void ConLoad(IConsole::IResult *pResult, void *pUserData);
452453
static void ConMap(IConsole::IResult *pResult, void *pUserData);

src/game/server/teams.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ void CGameTeams::RequestTeamSwap(CPlayer *pPlayer, CPlayer *pTargetPlayer, int T
877877

878878
// Notification for the swap initiator
879879
str_format(aBuf, sizeof(aBuf),
880-
"You have requested to swap with %s.",
880+
"You have requested to swap with %s. Use /cancelswap to cancel the request.",
881881
Server()->ClientName(pTargetPlayer->GetCid()));
882882
GameServer()->SendChatTarget(pPlayer->GetCid(), aBuf);
883883

@@ -978,6 +978,44 @@ void CGameTeams::SwapTeamCharacters(CPlayer *pPrimaryPlayer, CPlayer *pTargetPla
978978
GameServer()->SendChatTeam(Team, aBuf);
979979
}
980980

981+
void CGameTeams::CancelTeamSwap(CPlayer *pPlayer, int Team)
982+
{
983+
if(!pPlayer)
984+
return;
985+
986+
char aBuf[128];
987+
988+
// Notification for the swap initiator
989+
str_format(aBuf, sizeof(aBuf),
990+
"You have canceled swap with %s.",
991+
Server()->ClientName(pPlayer->m_SwapTargetsClientId));
992+
GameServer()->SendChatTarget(pPlayer->GetCid(), aBuf);
993+
994+
// Notification to the target swap player
995+
str_format(aBuf, sizeof(aBuf),
996+
"%s has canceled swap with you.",
997+
Server()->ClientName(pPlayer->GetCid()));
998+
GameServer()->SendChatTarget(pPlayer->m_SwapTargetsClientId, aBuf);
999+
1000+
// Notification for the remaining team
1001+
str_format(aBuf, sizeof(aBuf),
1002+
"%s has canceled swap with %s.",
1003+
Server()->ClientName(pPlayer->GetCid()), Server()->ClientName(pPlayer->m_SwapTargetsClientId));
1004+
// Do not send the team notification for team 0
1005+
if(Team != 0)
1006+
{
1007+
for(int i = 0; i < MAX_CLIENTS; i++)
1008+
{
1009+
if(m_Core.Team(i) == Team && i != pPlayer->m_SwapTargetsClientId && i != pPlayer->GetCid())
1010+
{
1011+
GameServer()->SendChatTarget(i, aBuf);
1012+
}
1013+
}
1014+
}
1015+
1016+
pPlayer->m_SwapTargetsClientId = -1;
1017+
}
1018+
9811019
void CGameTeams::ProcessSaveTeam()
9821020
{
9831021
for(int Team = 0; Team < NUM_DDRACE_TEAMS; Team++)

src/game/server/teams.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class CGameTeams
124124
void ResetSavedTeam(int ClientId, int Team);
125125
void RequestTeamSwap(CPlayer *pPlayer, CPlayer *pTargetPlayer, int Team);
126126
void SwapTeamCharacters(CPlayer *pPrimaryPlayer, CPlayer *pTargetPlayer, int Team);
127+
void CancelTeamSwap(CPlayer *pPlayer, int Team);
127128
void ProcessSaveTeam();
128129

129130
int GetFirstEmptyTeam() const;

0 commit comments

Comments
 (0)