6178: Persist AFK state of players on map change, fix players always being considered AFK with `sv_max_afk_time 0 ` r=def- a=Robyt3



## Checklist

- [X] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test (especially base/) or added coverage to integration test
- [ ] Considered possible null pointers and out of bounds array indexing
- [X] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
This commit is contained in:
bors[bot] 2022-12-22 23:48:40 +00:00 committed by GitHub
commit 674b5f919d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 10 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

@ -686,16 +686,24 @@ void CPlayer::UpdatePlaytime()
void CPlayer::AfkTimer()
{
if(g_Config.m_SvMaxAfkTime == 0)
return;
m_Afk = g_Config.m_SvMaxAfkTime != 0 && m_LastPlaytime < time_get() - time_freq() * g_Config.m_SvMaxAfkTime;
}
if(m_LastPlaytime < time_get() - time_freq() * g_Config.m_SvMaxAfkTime)
void CPlayer::SetAfk(bool Afk)
{
if(g_Config.m_SvMaxAfkTime == 0)
{
m_Afk = true;
m_Afk = false;
return;
}
m_Afk = false;
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

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;