From caafb216c96d1195e8b7b02ac4da163cb64bdecd Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Thu, 19 Sep 2024 07:49:59 +0200 Subject: [PATCH 1/3] Add MP.GetPlayerRole(player_id) --- include/TLuaEngine.h | 1 + src/TLuaEngine.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/include/TLuaEngine.h b/include/TLuaEngine.h index 07ede1b..060ce1f 100644 --- a/include/TLuaEngine.h +++ b/include/TLuaEngine.h @@ -263,6 +263,7 @@ private: sol::table Lua_TriggerGlobalEvent(const std::string& EventName, sol::variadic_args EventArgs); sol::table Lua_TriggerLocalEvent(const std::string& EventName, sol::variadic_args EventArgs); sol::table Lua_GetPlayerIdentifiers(int ID); + std::string Lua_GetPlayerRole(int ID); sol::table Lua_GetPlayers(); std::string Lua_GetPlayerName(int ID); sol::table Lua_GetPlayerVehicles(int ID); diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index 9810c99..a53b6b8 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -595,6 +595,15 @@ sol::table TLuaEngine::StateThreadData::Lua_GetPlayerIdentifiers(int ID) { } } +std::string TLuaEngine::StateThreadData::Lua_GetPlayerRole(int ID) { + auto MaybeClient = GetClient(mEngine->Server(), ID); + if (MaybeClient && !MaybeClient.value().expired()) + return MaybeClient.value().lock()->GetRoles(); + else + return ""; +} + + sol::table TLuaEngine::StateThreadData::Lua_GetPlayers() { sol::table Result = mStateView.create_table(); mEngine->Server().ForEachClient([&](std::weak_ptr Client) -> bool { @@ -883,6 +892,9 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI MPTable.set_function("GetPlayerIdentifiers", [&](int ID) -> sol::table { return Lua_GetPlayerIdentifiers(ID); }); + MPTable.set_function("GetPlayerRole", [&](int ID) -> std::string { + return Lua_GetPlayerRole(ID); + }); MPTable.set_function("Sleep", &LuaAPI::MP::Sleep); // const std::string& EventName, size_t IntervalMS, int strategy MPTable.set_function("CreateEventTimer", [&](sol::variadic_args Args) { From 623dfa17d50249b58dd1db62828b1f2a71e0f3f9 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Fri, 20 Sep 2024 14:45:41 +0200 Subject: [PATCH 2/3] Remove expiry check and add braces --- src/TLuaEngine.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index a53b6b8..5e88597 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -597,10 +597,11 @@ sol::table TLuaEngine::StateThreadData::Lua_GetPlayerIdentifiers(int ID) { std::string TLuaEngine::StateThreadData::Lua_GetPlayerRole(int ID) { auto MaybeClient = GetClient(mEngine->Server(), ID); - if (MaybeClient && !MaybeClient.value().expired()) + if (MaybeClient) { return MaybeClient.value().lock()->GetRoles(); - else + } else { return ""; + } } From 9a0270cb0980683e4f697a74ee844787b9ad16f1 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Sat, 28 Sep 2024 21:05:04 +0200 Subject: [PATCH 3/3] Return nil instead of "" when there's no client --- include/TLuaEngine.h | 2 +- src/TLuaEngine.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/TLuaEngine.h b/include/TLuaEngine.h index 060ce1f..e020850 100644 --- a/include/TLuaEngine.h +++ b/include/TLuaEngine.h @@ -263,7 +263,7 @@ private: sol::table Lua_TriggerGlobalEvent(const std::string& EventName, sol::variadic_args EventArgs); sol::table Lua_TriggerLocalEvent(const std::string& EventName, sol::variadic_args EventArgs); sol::table Lua_GetPlayerIdentifiers(int ID); - std::string Lua_GetPlayerRole(int ID); + std::variant Lua_GetPlayerRole(int ID); sol::table Lua_GetPlayers(); std::string Lua_GetPlayerName(int ID); sol::table Lua_GetPlayerVehicles(int ID); diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index 5e88597..5708fe7 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -595,12 +595,12 @@ sol::table TLuaEngine::StateThreadData::Lua_GetPlayerIdentifiers(int ID) { } } -std::string TLuaEngine::StateThreadData::Lua_GetPlayerRole(int ID) { +std::variant TLuaEngine::StateThreadData::Lua_GetPlayerRole(int ID) { auto MaybeClient = GetClient(mEngine->Server(), ID); if (MaybeClient) { return MaybeClient.value().lock()->GetRoles(); } else { - return ""; + return sol::nil; } } @@ -893,7 +893,7 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI MPTable.set_function("GetPlayerIdentifiers", [&](int ID) -> sol::table { return Lua_GetPlayerIdentifiers(ID); }); - MPTable.set_function("GetPlayerRole", [&](int ID) -> std::string { + MPTable.set_function("GetPlayerRole", [&](int ID) -> std::variant { return Lua_GetPlayerRole(ID); }); MPTable.set_function("Sleep", &LuaAPI::MP::Sleep);