GameContext: Extract OnSetTeamNetMessage()

This commit is contained in:
Alexander Akulich 2023-08-15 23:23:01 +03:00
parent a1ec904596
commit ce5371b038
2 changed files with 54 additions and 50 deletions

View file

@ -1937,56 +1937,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID)
else if(MsgID == NETMSGTYPE_CL_VOTE) else if(MsgID == NETMSGTYPE_CL_VOTE)
OnVoteNetMessage(static_cast<CNetMsg_Cl_Vote *>(pRawMsg), ClientID); OnVoteNetMessage(static_cast<CNetMsg_Cl_Vote *>(pRawMsg), ClientID);
else if(MsgID == NETMSGTYPE_CL_SETTEAM && !m_World.m_Paused) else if(MsgID == NETMSGTYPE_CL_SETTEAM && !m_World.m_Paused)
{ OnSetTeamNetMessage(static_cast<CNetMsg_Cl_SetTeam *>(pRawMsg), ClientID);
CNetMsg_Cl_SetTeam *pMsg = (CNetMsg_Cl_SetTeam *)pRawMsg;
if(pPlayer->GetTeam() == pMsg->m_Team || (g_Config.m_SvSpamprotection && pPlayer->m_LastSetTeam && pPlayer->m_LastSetTeam + Server()->TickSpeed() * g_Config.m_SvTeamChangeDelay > Server()->Tick()))
return;
//Kill Protection
CCharacter *pChr = pPlayer->GetCharacter();
if(pChr)
{
int CurrTime = (Server()->Tick() - pChr->m_StartTime) / Server()->TickSpeed();
if(g_Config.m_SvKillProtection != 0 && CurrTime >= (60 * g_Config.m_SvKillProtection) && pChr->m_DDRaceState == DDRACE_STARTED)
{
SendChatTarget(ClientID, "Kill Protection enabled. If you really want to join the spectators, first type /kill");
return;
}
}
if(pPlayer->m_TeamChangeTick > Server()->Tick())
{
pPlayer->m_LastSetTeam = Server()->Tick();
int TimeLeft = (pPlayer->m_TeamChangeTick - Server()->Tick()) / Server()->TickSpeed();
char aTime[32];
str_time((int64_t)TimeLeft * 100, TIME_HOURS, aTime, sizeof(aTime));
char aBuf[128];
str_format(aBuf, sizeof(aBuf), "Time to wait before changing team: %s", aTime);
SendBroadcast(aBuf, ClientID);
return;
}
// Switch team on given client and kill/respawn them
if(m_pController->CanJoinTeam(pMsg->m_Team, ClientID))
{
if(pPlayer->IsPaused())
SendChatTarget(ClientID, "Use /pause first then you can kill");
else
{
if(pPlayer->GetTeam() == TEAM_SPECTATORS || pMsg->m_Team == TEAM_SPECTATORS)
m_VoteUpdate = true;
m_pController->DoTeamChange(pPlayer, pMsg->m_Team);
pPlayer->m_TeamChangeTick = Server()->Tick();
}
}
else
{
char aBuf[128];
str_format(aBuf, sizeof(aBuf), "Only %d active players are allowed", Server()->MaxClients() - g_Config.m_SvSpectatorSlots);
SendBroadcast(aBuf, ClientID);
}
}
else if(MsgID == NETMSGTYPE_CL_ISDDNETLEGACY) else if(MsgID == NETMSGTYPE_CL_ISDDNETLEGACY)
{ {
IServer::CClientInfo Info; IServer::CClientInfo Info;
@ -2703,6 +2654,58 @@ void CGameContext::OnVoteNetMessage(const CNetMsg_Cl_Vote *pMsg, int ClientID)
m_VoteUpdate = true; m_VoteUpdate = true;
} }
void CGameContext::OnSetTeamNetMessage(const CNetMsg_Cl_SetTeam *pMsg, int ClientID)
{
CPlayer *pPlayer = m_apPlayers[ClientID];
if(pPlayer->GetTeam() == pMsg->m_Team || (g_Config.m_SvSpamprotection && pPlayer->m_LastSetTeam && pPlayer->m_LastSetTeam + Server()->TickSpeed() * g_Config.m_SvTeamChangeDelay > Server()->Tick()))
return;
// Kill Protection
CCharacter *pChr = pPlayer->GetCharacter();
if(pChr)
{
int CurrTime = (Server()->Tick() - pChr->m_StartTime) / Server()->TickSpeed();
if(g_Config.m_SvKillProtection != 0 && CurrTime >= (60 * g_Config.m_SvKillProtection) && pChr->m_DDRaceState == DDRACE_STARTED)
{
SendChatTarget(ClientID, "Kill Protection enabled. If you really want to join the spectators, first type /kill");
return;
}
}
if(pPlayer->m_TeamChangeTick > Server()->Tick())
{
pPlayer->m_LastSetTeam = Server()->Tick();
int TimeLeft = (pPlayer->m_TeamChangeTick - Server()->Tick()) / Server()->TickSpeed();
char aTime[32];
str_time((int64_t)TimeLeft * 100, TIME_HOURS, aTime, sizeof(aTime));
char aBuf[128];
str_format(aBuf, sizeof(aBuf), "Time to wait before changing team: %s", aTime);
SendBroadcast(aBuf, ClientID);
return;
}
// Switch team on given client and kill/respawn them
if(m_pController->CanJoinTeam(pMsg->m_Team, ClientID))
{
if(pPlayer->IsPaused())
SendChatTarget(ClientID, "Use /pause first then you can kill");
else
{
if(pPlayer->GetTeam() == TEAM_SPECTATORS || pMsg->m_Team == TEAM_SPECTATORS)
m_VoteUpdate = true;
m_pController->DoTeamChange(pPlayer, pMsg->m_Team);
pPlayer->m_TeamChangeTick = Server()->Tick();
}
}
else
{
char aBuf[128];
str_format(aBuf, sizeof(aBuf), "Only %d active players are allowed", Server()->MaxClients() - g_Config.m_SvSpectatorSlots);
SendBroadcast(aBuf, ClientID);
}
}
void CGameContext::ConTuneParam(IConsole::IResult *pResult, void *pUserData) void CGameContext::ConTuneParam(IConsole::IResult *pResult, void *pUserData)
{ {
CGameContext *pSelf = (CGameContext *)pUserData; CGameContext *pSelf = (CGameContext *)pUserData;

View file

@ -291,6 +291,7 @@ public:
void OnSayNetMessage(const CNetMsg_Cl_Say *pMsg, int ClientID, const CUnpacker *pUnpacker); void OnSayNetMessage(const CNetMsg_Cl_Say *pMsg, int ClientID, const CUnpacker *pUnpacker);
void OnCallVoteNetMessage(const CNetMsg_Cl_CallVote *pMsg, int ClientID); void OnCallVoteNetMessage(const CNetMsg_Cl_CallVote *pMsg, int ClientID);
void OnVoteNetMessage(const CNetMsg_Cl_Vote *pMsg, int ClientID); void OnVoteNetMessage(const CNetMsg_Cl_Vote *pMsg, int ClientID);
void OnSetTeamNetMessage(const CNetMsg_Cl_SetTeam *pMsg, int ClientID);
bool OnClientDataPersist(int ClientID, void *pData) override; bool OnClientDataPersist(int ClientID, void *pData) override;
void OnClientConnected(int ClientID, void *pData) override; void OnClientConnected(int ClientID, void *pData) override;