From 9593b643642bc724ee24f3069bba8be424ae785d Mon Sep 17 00:00:00 2001 From: TotallyNotElite <1yourexperiment@protonmail.com> Date: Sat, 15 Sep 2018 15:11:25 +0200 Subject: [PATCH] cat_pl_set_state --- data/menu/player-list-window.xml | 4 +- src/playerlist.cpp | 125 ++++++++++++++++++++++++++++--- 2 files changed, 118 insertions(+), 11 deletions(-) diff --git a/data/menu/player-list-window.xml b/data/menu/player-list-window.xml index 4c07c6b4..e5a11f65 100755 --- a/data/menu/player-list-window.xml +++ b/data/menu/player-list-window.xml @@ -1,4 +1,4 @@ - + @@ -19,4 +19,4 @@ Kick
-
\ No newline at end of file +
diff --git a/src/playerlist.cpp b/src/playerlist.cpp index 928822ea..e76bdb2e 100644 --- a/src/playerlist.cpp +++ b/src/playerlist.cpp @@ -11,6 +11,7 @@ #include #include #include +#include namespace playerlist { @@ -175,21 +176,121 @@ CatCommand pl_load("pl_load", "Load playerlist", Load); CatCommand pl_set_state( "pl_set_state", - "pl_set_state uniqueid state\nfor example pl_set_state 306902159 0", + "cat_pl_set_state [playername] [state] (Tab to autocomplete)", [](const CCommand &args) { - if (args.ArgC() < 3) + if (args.ArgC() != 3) { logging::Info("Invalid call"); return; } - unsigned steamid = strtoul(args.Arg(1), nullptr, 10); - k_EState state = - static_cast(strtol(args.Arg(2), nullptr, 10)); - if (state < k_EState::DEFAULT || state > k_EState::STATE_LAST) - state = k_EState::DEFAULT; - AccessData(steamid).state = state; - logging::Info("Set %d to %d", steamid, state); + auto name = args.Arg(1); + int id = -1; + for (int i = 0; i < g_IEngine->GetMaxClients(); i++) + { + player_info_s info; + if (!g_IEngine->GetPlayerInfo(i, &info)) + continue; + std::string currname(info.name); + std::replace(currname.begin(), currname.end(), ' ', '-'); + if (currname.find(name) != 0) + continue; + id = i; + break; + } + if (id == -1) + { + logging::Info("Unknown Player Name. (Use tab for autocomplete)"); + return; + } + std::string state = args.Arg(2); + boost::to_upper(state); + player_info_s info; + g_IEngine->GetPlayerInfo(id, &info); + + if (k_Names[0] == state) + AccessData(info.friendsID).state = k_EState::DEFAULT; + else if (k_Names[1] == state) + AccessData(info.friendsID).state = k_EState::FRIEND; + else if (k_Names[2] == state) + AccessData(info.friendsID).state = k_EState::RAGE; + else if (k_Names[3] == state) + AccessData(info.friendsID).state = k_EState::IPC; + else if (k_Names[4] == state) + AccessData(info.friendsID).state = k_EState::DEVELOPER; + else + logging::Info("Unknown State. (Use tab for autocomplete)"); }); + +static int cat_pl_set_state_completionCallback( + const char *c_partial, + char commands[COMMAND_COMPLETION_MAXITEMS][COMMAND_COMPLETION_ITEM_LENGTH]) +{ + std::string partial = c_partial; + std::string parts[2]{}; + auto j = 0u; + auto f = false; + int count = 0; + + for (auto i = 0u; i < partial.size() && j < 3; ++i) + { + auto space = (bool) isspace(partial[i]); + if (!space) + { + if (j) + parts[j - 1].push_back(partial[i]); + f = true; + } + + if (i == partial.size() - 1 || (f && space)) + { + if (space) + ++j; + f = false; + } + } + + std::vector names; + + for (int i = 0; i < g_IEngine->GetMaxClients(); i++) + { + player_info_s info; + if (!g_IEngine->GetPlayerInfo(i, &info)) + continue; + std::string name(info.name); + std::replace(name.begin(), name.end(), ' ', '-'); + names.push_back(name); + } + std::sort(names.begin(), names.end()); + + if (parts[0].empty() || + parts[1].empty() && (!parts[0].empty() && partial.back() != ' ')) + { + boost::to_lower(parts[0]); + for (const auto &s : names) + { + // if (s.find(parts[0]) == 0) + if (boost::to_lower_copy(s).find(parts[0]) == 0) + { + snprintf(commands[count++], COMMAND_COMPLETION_ITEM_LENGTH - 1, + "cat_pl_set_state %s", s.c_str()); + } + } + return count; + } + boost::to_lower(parts[1]); + for (const auto &s : k_Names) + { + if (boost::to_lower_copy(s).find(parts[1]) == 0) + { + snprintf(commands[count++], COMMAND_COMPLETION_ITEM_LENGTH - 1, + "cat_pl_set_state %s %s", parts[0].c_str(), s.c_str()); + if (count == COMMAND_COMPLETION_MAXITEMS) + break; + } + } + return count; +} + #if ENABLE_VISUALS CatCommand pl_set_color("pl_set_color", "pl_set_color uniqueid r g b", [](const CCommand &args) { @@ -222,4 +323,10 @@ CatCommand pl_info("pl_info", "pl_info uniqueid", [](const CCommand &args) { ConColorMsg(*reinterpret_cast<::Color*>(&clr), "[CUSTOM COLOR]\n"); }*/ }); + +static InitRoutine init([]() { + pl_set_state.cmd->m_bHasCompletionCallback = true; + pl_set_state.cmd->m_fnCompletionCallback = + cat_pl_set_state_completionCallback; +}); } // namespace playerlist