Skip to content

Commit b1bf2d6

Browse files
committed
feat(scripting/utils): GetPlayerId
1 parent 656d2f6 commit b1bf2d6

File tree

1 file changed

+38
-0
lines changed
  • src/components/Plugins/src/language/lua

1 file changed

+38
-0
lines changed

src/components/Plugins/src/language/lua/core.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@
77
#include <luacpp/luacpp.h>
88
#include <luacpp/func_utils.h>
99

10+
int StringToInt(const char *str)
11+
{
12+
int retval;
13+
try
14+
{
15+
retval = std::stoi(str);
16+
}
17+
catch (std::invalid_argument &ex)
18+
{
19+
retval = -1;
20+
}
21+
catch (std::out_of_range &exr)
22+
{
23+
retval = -1;
24+
}
25+
return retval;
26+
}
27+
1028
void SetupLuaEnvironment(Plugin *plugin)
1129
{
1230
luacpp::LuaState *state = plugin->GetLuaState();
@@ -45,6 +63,26 @@ void SetupLuaEnvironment(Plugin *plugin)
4563
if(playerTable.Get(playerID).GetType() == LUA_TNIL) return state->CreateNil();
4664
else return playerTable.Get(playerID); },
4765
"GetPlayer");
66+
state->CreateFunction([playerTable, state](const char *content, bool matchbots) -> int
67+
{
68+
int slot = StringToInt(content);
69+
if(slot != -1) return slot;
70+
71+
std::string match = str_tolower(content);
72+
for (int i = 0; i < g_playerManager->GetPlayerCap(); i++)
73+
{
74+
Player *player = g_playerManager->GetPlayer(i);
75+
if (player == nullptr)
76+
continue;
77+
if (!matchbots && player->IsFakeClient())
78+
continue;
79+
80+
if (std::to_string(player->GetSteamID()) == match || std::string(str_tolower(player->GetName())).find(match) != std::string::npos)
81+
return i;
82+
}
83+
84+
return -1; },
85+
"GetPlayerId");
4886
state->CreateFunction([playerTable]() -> luacpp::LuaTable
4987
{ return playerTable; },
5088
"GetPlayers");

0 commit comments

Comments
 (0)