Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server/impl): add GetPlayerTimeOnline native #2061

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion code/components/citizen-server-gui/src/PlayerList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::chrono::seconds>(client->GetLastSeen() - client->GetFirstSeen()).count();
unsigned int secondsTotalOnline = client->GetSecondsOnline();
entry.secondsOnline = (secondsTotalOnline % 60);
entry.minutesOnline = ((secondsTotalOnline / 60) % 60);
entry.hoursOnline = (secondsTotalOnline / 3600);
Expand Down
5 changes: 5 additions & 0 deletions code/components/citizen-server-impl/include/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ namespace fx
return m_lastSeen;
}

inline unsigned int GetSecondsOnline()
{
return std::chrono::duration_cast<std::chrono::seconds>(m_lastSeen - m_firstSeen).count();
}

inline const std::vector<std::string>& GetIdentifiers()
{
return m_identifiers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}));
}
35 changes: 35 additions & 0 deletions ext/native-decls/GetPlayerTimeOnline.md
Original file line number Diff line number Diff line change
@@ -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 : %f H %f min %f"):format(
(secondsTotalOnline / 3600),
((secondsTotalOnline / 60) % 60),
(secondsTotalOnline % 60)
))
end

RegisterNetEvent("myTimeOnline", ShowTimeOnline)
```
Loading