Persist AFK state of players on map change

Store AFK state of players in `CPersistentClientData` and restore it when a client is connected.

In order for this to work, `m_LastPlaytime` must be adjusted as well to ensure that the AFK state is not reset again automatically by the `AfkTimer` function.

The AFK state is encapsulated using `IsAfk` and `GetAfk`.

Closes #1966.
This commit is contained in:
Robert Müller 2022-12-23 00:06:29 +01:00
parent 6723910d38
commit 4439cbd7a1
5 changed files with 30 additions and 5 deletions

View file

@ -1028,7 +1028,7 @@ void CCharacter::SnapCharacter(int SnappingClient, int ID)
AmmoCount = (!m_FreezeTime) ? m_Core.m_aWeapons[m_Core.m_ActiveWeapon].m_Ammo : 0;
}
if(GetPlayer()->m_Afk || GetPlayer()->IsPaused())
if(GetPlayer()->IsAfk() || GetPlayer()->IsPaused())
{
if(m_FreezeTime > 0 || m_FreezeTime == -1 || m_Core.m_DeepFrozen || m_Core.m_LiveFrozen)
Emote = EMOTE_NORMAL;

View file

@ -922,7 +922,7 @@ void CGameContext::OnTick()
GetPlayerChar(m_VoteCreator)->Team() != GetPlayerChar(i)->Team())))
continue;
if(m_apPlayers[i]->m_Afk && i != m_VoteCreator)
if(m_apPlayers[i]->IsAfk() && i != m_VoteCreator)
continue;
// can't vote in kick and spec votes in the beginning after joining
@ -973,7 +973,7 @@ void CGameContext::OnTick()
if(i != j && (!m_apPlayers[j] || str_comp(aaBuf[j], aaBuf[i]) != 0))
continue;
if(m_apPlayers[j] && !m_apPlayers[j]->m_Afk && m_apPlayers[j]->GetTeam() != TEAM_SPECTATORS &&
if(m_apPlayers[j] && !m_apPlayers[j]->IsAfk() && m_apPlayers[j]->GetTeam() != TEAM_SPECTATORS &&
((Server()->Tick() - m_apPlayers[j]->m_JoinTick) / (Server()->TickSpeed() * 60) > g_Config.m_SvVoteVetoTime ||
(m_apPlayers[j]->GetCharacter() && m_apPlayers[j]->GetCharacter()->m_DDRaceState == DDRACE_STARTED &&
(Server()->Tick() - m_apPlayers[j]->GetCharacter()->m_StartTime) / (Server()->TickSpeed() * 60) > g_Config.m_SvVoteVetoTime)))
@ -1488,6 +1488,7 @@ bool CGameContext::OnClientDataPersist(int ClientID, void *pData)
return false;
}
pPersistent->m_IsSpectator = m_apPlayers[ClientID]->GetTeam() == TEAM_SPECTATORS;
pPersistent->m_IsAfk = m_apPlayers[ClientID]->IsAfk();
return true;
}
@ -1495,9 +1496,11 @@ void CGameContext::OnClientConnected(int ClientID, void *pData)
{
CPersistentClientData *pPersistentData = (CPersistentClientData *)pData;
bool Spec = false;
bool Afk = true;
if(pPersistentData)
{
Spec = pPersistentData->m_IsSpectator;
Afk = pPersistentData->m_IsAfk;
}
{
@ -1522,6 +1525,7 @@ void CGameContext::OnClientConnected(int ClientID, void *pData)
if(m_apPlayers[ClientID])
delete m_apPlayers[ClientID];
m_apPlayers[ClientID] = new(ClientID) CPlayer(this, NextUniqueClientID, ClientID, StartTeam);
m_apPlayers[ClientID]->SetAfk(Afk);
NextUniqueClientID += 1;
#ifdef CONF_DEBUG
@ -4361,6 +4365,6 @@ void CGameContext::OnUpdatePlayerServerInfo(char *aBuf, int BufSize, int ID)
"\"afk\":%s,"
"\"team\":%d",
aJsonSkin,
JsonBool(m_apPlayers[ID]->m_Afk),
JsonBool(m_apPlayers[ID]->IsAfk()),
m_apPlayers[ID]->GetTeam());
}

View file

@ -127,6 +127,7 @@ class CGameContext : public IGameServer
struct CPersistentClientData
{
bool m_IsSpectator;
bool m_IsAfk;
};
public:

View file

@ -689,6 +689,23 @@ void CPlayer::AfkTimer()
m_Afk = g_Config.m_SvMaxAfkTime != 0 && m_LastPlaytime < time_get() - time_freq() * g_Config.m_SvMaxAfkTime;
}
void CPlayer::SetAfk(bool Afk)
{
if(g_Config.m_SvMaxAfkTime == 0)
{
m_Afk = false;
return;
}
m_Afk = Afk;
// Ensure that the AFK state is not reset again automatically
if(Afk)
m_LastPlaytime = time_get() - time_freq() * g_Config.m_SvMaxAfkTime - 1;
else
m_LastPlaytime = time_get();
}
int CPlayer::GetDefaultEmote() const
{
if(m_OverrideEmoteReset >= 0)

View file

@ -140,6 +140,7 @@ private:
int m_Paused;
int64_t m_ForcePauseTime;
int64_t m_LastPause;
bool m_Afk;
int m_DefEmote;
int m_OverrideEmote;
@ -181,7 +182,6 @@ public:
vec2 m_ShowDistance;
bool m_SpecTeam;
bool m_NinjaJetpack;
bool m_Afk;
bool m_HasFinishScore;
int m_ChatScore;
@ -190,6 +190,9 @@ public:
void UpdatePlaytime();
void AfkTimer();
void SetAfk(bool Afk);
bool IsAfk() const { return m_Afk; }
int64_t m_LastPlaytime;
int64_t m_LastEyeEmote;
int64_t m_LastBroadcast;