add time played support to server query results

This commit is contained in:
Kae 2025-01-05 15:21:27 +11:00
parent 9bc12f5f97
commit c47bceb3f3
6 changed files with 31 additions and 9 deletions

View File

@ -63,6 +63,8 @@ ServerClientContext::ServerClientContext(ConnectionId clientId, Maybe<HostAddres
m_netGroup.addNetElement(&m_teamNetState); m_netGroup.addNetElement(&m_teamNetState);
m_netGroup.addNetElement(&m_shipUpgrades); m_netGroup.addNetElement(&m_shipUpgrades);
m_netGroup.addNetElement(&m_shipCoordinate); m_netGroup.addNetElement(&m_shipCoordinate);
m_creationTime = Time::monotonicMilliseconds();
} }
ConnectionId ServerClientContext::clientId() const { ConnectionId ServerClientContext::clientId() const {
@ -289,4 +291,8 @@ Json ServerClientContext::storeServerData() {
return store; return store;
} }
int64_t ServerClientContext::creationTime() const {
return m_creationTime;
}
} }

View File

@ -85,6 +85,8 @@ public:
void loadServerData(Json const& store); void loadServerData(Json const& store);
Json storeServerData(); Json storeServerData();
int64_t creationTime() const;
private: private:
ConnectionId const m_clientId; ConnectionId const m_clientId;
Maybe<HostAddress> const m_remoteAddress; Maybe<HostAddress> const m_remoteAddress;
@ -109,6 +111,7 @@ private:
NetElementTopGroup m_netGroup; NetElementTopGroup m_netGroup;
uint64_t m_netVersion = 0; uint64_t m_netVersion = 0;
int64_t m_creationTime;
NetElementData<Maybe<pair<WarpAction, WarpMode>>> m_orbitWarpActionNetState; NetElementData<Maybe<pair<WarpAction, WarpMode>>> m_orbitWarpActionNetState;
NetElementData<WorldId> m_playerWorldIdNetState; NetElementData<WorldId> m_playerWorldIdNetState;

View File

@ -173,6 +173,15 @@ List<ConnectionId> UniverseServer::clientIds() const {
return m_clients.keys(); return m_clients.keys();
} }
List<pair<ConnectionId, int64_t>> UniverseServer::clientIdsAndCreationTime() const {
List<pair<ConnectionId, int64_t>> result;
ReadLocker clientsLocker(m_clientsLock);
result.reserve(m_clients.size());
for (auto& pair : m_clients)
result.emplaceAppend(pair.first, pair.second->creationTime());
return result;
}
size_t UniverseServer::numberOfClients() const { size_t UniverseServer::numberOfClients() const {
ReadLocker clientsLocker(m_clientsLock); ReadLocker clientsLocker(m_clientsLock);
return m_clients.size(); return m_clients.size();

View File

@ -55,6 +55,7 @@ public:
bool isWorldActive(WorldId const& worldId) const; bool isWorldActive(WorldId const& worldId) const;
List<ConnectionId> clientIds() const; List<ConnectionId> clientIds() const;
List<pair<ConnectionId, int64_t>> clientIdsAndCreationTime() const;
size_t numberOfClients() const; size_t numberOfClients() const;
uint32_t maxClients() const; uint32_t maxClients() const;
bool isConnectedClient(ConnectionId clientId) const; bool isConnectedClient(ConnectionId clientId) const;

View File

@ -107,6 +107,8 @@ bool ServerQueryThread::processPacket(HostAddressWithPort const& address, char c
<< A2S_TYPE_DEDICATED // dedicated << A2S_TYPE_DEDICATED // dedicated
#ifdef STAR_SYSTEM_FAMILY_WINDOWS #ifdef STAR_SYSTEM_FAMILY_WINDOWS
<< A2S_ENV_WINDOWS // os << A2S_ENV_WINDOWS // os
#elif defined(STAR_SYSTEM_MACOS)
<< A2S_ENV_MAC // os
#else #else
<< A2S_ENV_LINUX // os << A2S_ENV_LINUX // os
#endif #endif
@ -153,17 +155,17 @@ void ServerQueryThread::buildPlayerResponse() {
return; return;
} }
auto clientIds = m_universe->clientIds(); auto clientIds = m_universe->clientIdsAndCreationTime();
uint8_t cnt = (uint8_t)clientIds.count(); uint8_t cnt = (uint8_t)clientIds.count();
int32_t kills = 0; // Not currently supported int32_t kills = 0; // Not currently supported
float timeConnected = 60; // Not supported defaults to 1min
m_playersResponse.clear(); m_playersResponse.clear();
m_playersResponse << A2S_HEAD_INT << A2S_PLAYER_REPLY << cnt; m_playersResponse << A2S_HEAD_INT << A2S_PLAYER_REPLY << cnt;
uint8_t i = 0; uint8_t i = 0;
for (auto clientId : clientIds) { for (auto& pair : clientIds) {
m_playersResponse << i++ << m_universe->clientNick(clientId) << kills << timeConnected; auto timeConnected = float(now - pair.second) / 1000.f;
m_playersResponse << i++ << m_universe->clientNick(pair.first) << kills << timeConnected;
} }
m_lastPlayersResponse = now; m_lastPlayersResponse = now;

View File

@ -42,11 +42,12 @@ private:
static const uint8_t A2S_EDF_TAGS = 0x20; static const uint8_t A2S_EDF_TAGS = 0x20;
static const uint8_t A2S_EDF_STV = 0x40; static const uint8_t A2S_EDF_STV = 0x40;
static const uint8_t A2S_EDF_PORT = 0x80; static const uint8_t A2S_EDF_PORT = 0x80;
static const uint8_t A2S_ENV_WINDOWS = 'W'; static const uint8_t A2S_ENV_WINDOWS = 'w';
static const uint8_t A2S_ENV_LINUX = 'L'; static const uint8_t A2S_ENV_LINUX = 'l';
static const uint8_t A2S_TYPE_DEDICATED = 'D'; static const uint8_t A2S_ENV_MAC = 'm';
static const uint8_t A2S_TYPE_LISTEN = 'L'; static const uint8_t A2S_TYPE_DEDICATED = 'd';
static const uint8_t A2S_TYPE_TV = 'P'; static const uint8_t A2S_TYPE_LISTEN = 'l';
static const uint8_t A2S_TYPE_TV = 'p';
static const uint8_t A2S_VAC_OFF = 0x00; static const uint8_t A2S_VAC_OFF = 0x00;
static const uint8_t A2S_VAC_ON = 0x01; static const uint8_t A2S_VAC_ON = 0x01;
static constexpr const char* A2S_INFO_REQUEST_STRING = "Source Engine Query"; static constexpr const char* A2S_INFO_REQUEST_STRING = "Source Engine Query";