From 92cabbb0014d9b52064870d8601eda016c907a6a Mon Sep 17 00:00:00 2001 From: gaspard Date: Mon, 19 Jun 2023 15:39:15 +0200 Subject: [PATCH 1/5] feat(server/impl): add GetPlayerTimeOnline native --- .../citizen-server-impl/include/Client.h | 5 +++ .../src/PlayerScriptFunctions.cpp | 5 +++ ext/native-decls/GetPlayerTimeOnline.md | 35 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 ext/native-decls/GetPlayerTimeOnline.md diff --git a/code/components/citizen-server-impl/include/Client.h b/code/components/citizen-server-impl/include/Client.h index 81a0b0a8fc..b42013c60f 100644 --- a/code/components/citizen-server-impl/include/Client.h +++ b/code/components/citizen-server-impl/include/Client.h @@ -213,6 +213,11 @@ namespace fx return m_lastSeen; } + inline int GetSecondsOnline() + { + return std::chrono::duration_cast(m_lastSeen - m_firstSeen).count(); + } + inline const std::vector& GetIdentifiers() { return m_identifiers; diff --git a/code/components/citizen-server-impl/src/PlayerScriptFunctions.cpp b/code/components/citizen-server-impl/src/PlayerScriptFunctions.cpp index 042c436895..606798d522 100644 --- a/code/components/citizen-server-impl/src/PlayerScriptFunctions.cpp +++ b/code/components/citizen-server-impl/src/PlayerScriptFunctions.cpp @@ -233,4 +233,9 @@ static void CreatePlayerCommands() return 0; } })); + + fx::ScriptEngine::RegisterNativeHandler("GET_PLAYER_TIME_ONLINE", MakeClientFunction([](fx::ScriptContext& context, const fx::ClientSharedPtr& client) + { + return client->GetSecondsOnline(); + })); } diff --git a/ext/native-decls/GetPlayerTimeOnline.md b/ext/native-decls/GetPlayerTimeOnline.md new file mode 100644 index 0000000000..2a3abef914 --- /dev/null +++ b/ext/native-decls/GetPlayerTimeOnline.md @@ -0,0 +1,35 @@ +--- +ns: CFX +apiset: server +--- +## GET_PLAYER_TIME_ONLINE + +```c +int GET_PLAYER_TIME_ONLINE(char* playerSrc); +``` + +Gets the current time online for a specified player. + +## Parameters +* **playerSrc**: A player. + +## Return value + +The current time online in seconds. + +## Examples + +```lua +local function ShowTimeOnline() + local player = source + local secondsTotalOnline = GetPlayerTimeOnline(player) + + print("Time online : %s H %s min %s s":format( + (secondsTotalOnline / 3600), + ((secondsTotalOnline / 60) % 60), + (secondsTotalOnline % 60), + )) +end + +RegisterNetEvent("myTimeOnline", ShowTimeOnline) +```v \ No newline at end of file From d5a97afaab0fbd7e54f702edf7d7eb6f4324b181 Mon Sep 17 00:00:00 2001 From: gaspard Date: Mon, 19 Jun 2023 15:41:00 +0200 Subject: [PATCH 2/5] feat(server/gui): add method GetSecondsOnline --- code/components/citizen-server-gui/src/PlayerList.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/components/citizen-server-gui/src/PlayerList.cpp b/code/components/citizen-server-gui/src/PlayerList.cpp index f59d79a309..d1f65ba2d4 100644 --- a/code/components/citizen-server-gui/src/PlayerList.cpp +++ b/code/components/citizen-server-gui/src/PlayerList.cpp @@ -116,7 +116,7 @@ static const auto& CollectPlayers(fx::ServerInstanceBase* instance) std::unique_lock lock(g_playerListDataMutex); auto& entry = g_playerListData[client->GetGuid()]; entry.name = fmt::sprintf("[%d] %s", client->GetNetId(), client->GetName()); - unsigned int secondsTotalOnline = std::chrono::duration_cast(client->GetLastSeen() - client->GetFirstSeen()).count(); + unsigned int secondsTotalOnline = client->GetSecondsOnline(); entry.secondsOnline = (secondsTotalOnline % 60); entry.minutesOnline = ((secondsTotalOnline / 60) % 60); entry.hoursOnline = (secondsTotalOnline / 3600); From 5ffe66e6359dda94b3602525db0637b19dfc0f53 Mon Sep 17 00:00:00 2001 From: utopi_160 <93077305+utopi160@users.noreply.github.com> Date: Mon, 19 Jun 2023 15:43:03 +0200 Subject: [PATCH 3/5] Update GetPlayerTimeOnline.md --- ext/native-decls/GetPlayerTimeOnline.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/native-decls/GetPlayerTimeOnline.md b/ext/native-decls/GetPlayerTimeOnline.md index 2a3abef914..309e1b178a 100644 --- a/ext/native-decls/GetPlayerTimeOnline.md +++ b/ext/native-decls/GetPlayerTimeOnline.md @@ -32,4 +32,4 @@ local function ShowTimeOnline() end RegisterNetEvent("myTimeOnline", ShowTimeOnline) -```v \ No newline at end of file +``` From 894aa0f3c2dd16a2c34c5ff4ab536f756e753fc8 Mon Sep 17 00:00:00 2001 From: utopi_160 <93077305+utopi160@users.noreply.github.com> Date: Mon, 19 Jun 2023 15:58:07 +0200 Subject: [PATCH 4/5] Update GetPlayerTimeOnline.md --- ext/native-decls/GetPlayerTimeOnline.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/native-decls/GetPlayerTimeOnline.md b/ext/native-decls/GetPlayerTimeOnline.md index 309e1b178a..2be15a02a6 100644 --- a/ext/native-decls/GetPlayerTimeOnline.md +++ b/ext/native-decls/GetPlayerTimeOnline.md @@ -24,10 +24,10 @@ local function ShowTimeOnline() local player = source local secondsTotalOnline = GetPlayerTimeOnline(player) - print("Time online : %s H %s min %s s":format( + print(("Time online : %f H %f min %f"):format( (secondsTotalOnline / 3600), ((secondsTotalOnline / 60) % 60), - (secondsTotalOnline % 60), + (secondsTotalOnline % 60) )) end From 549e0f505ec170aa1270cd73bd745de52b500136 Mon Sep 17 00:00:00 2001 From: utopi_160 <93077305+utopi160@users.noreply.github.com> Date: Thu, 13 Jul 2023 16:38:10 +0200 Subject: [PATCH 5/5] tweak(server/impl): int to unsigned int Inattention error, we do not expect negative numbers. --- code/components/citizen-server-impl/include/Client.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/components/citizen-server-impl/include/Client.h b/code/components/citizen-server-impl/include/Client.h index b42013c60f..5c6afb4bc5 100644 --- a/code/components/citizen-server-impl/include/Client.h +++ b/code/components/citizen-server-impl/include/Client.h @@ -213,7 +213,7 @@ namespace fx return m_lastSeen; } - inline int GetSecondsOnline() + inline unsigned int GetSecondsOnline() { return std::chrono::duration_cast(m_lastSeen - m_firstSeen).count(); }