Skip to content

Commit

Permalink
Add: isActiveID(...) and existsID(...) functions
Browse files Browse the repository at this point in the history
This PR is an alternative/replacement for Mudlet#6401 that provides the
functionality with a *new* function rather than overloading the existing
`isActive(name as string, type as string)`. This is at the request of
@Kebap who expressed a strong desire for this alternative approach to
establishing the status of a Mudlet item by ID number rather than by name.
As temporary items do not *have* names but they do possess a unique ID this
will also work for them whereas the unmodified `isActive(...)` function can
not. Unlike the `isActive(...)` function there can only be a single item
that matches the ID so the return value is a boolean value rather than an
integer count (of matching items that are active).

Whilst developing the `isActiveID(...)` function I spotted that there was
potentially a need for the related `existsID(...)` in the same way that there
is an `exists(name, type)` alongside the `active(name, type)` function.
Like the `isActiveID(...)`function compared to the `isActive(...)` the
`existsID(...)` too only relates to a single item so also returns a boolean
result.

Signed-off-by: Stephen Lyons <slysven@virginmedia.com>
  • Loading branch information
SlySven committed Nov 2, 2022
1 parent 203b795 commit c810ee4
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
111 changes: 111 additions & 0 deletions src/TLuaInterpreter.cpp
Expand Up @@ -7925,6 +7925,50 @@ int TLuaInterpreter::exists(lua_State* L)
return 1;
}

// Documentation: https://wiki.mudlet.org/w/Manual:Lua_Functions#existsID
int TLuaInterpreter::existsID(lua_State* L)
{
auto id = getVerifiedInt(L, __func__, 1, "ID");
if (id < 0) {
// Must be zero or more but doesn't seem to be:
return warnArgumentValue(L, __func__, "item ID as %d does not seem to be a positive integer", id);
}
auto type = getVerifiedString(L, __func__, 2, "type").toLower();
Host& host = getHostFromLua(L);

if (!type.compare(QLatin1String("timer"), Qt::CaseInsensitive)) {
auto pT = host.getTimerUnit()->getTimer(id);
lua_pushboolean(L, static_cast<bool>(pT));
return 1;
}
if (!type.compare(QLatin1String("trigger"), Qt::CaseInsensitive)) {
auto pT = host.getTriggerUnit()->getTrigger(id);
lua_pushboolean(L, static_cast<bool>(pT));
return 1;
}
if (!type.compare(QLatin1String("alias"), Qt::CaseInsensitive)) {
auto pT = host.getAliasUnit()->getAlias(id);
lua_pushboolean(L, static_cast<bool>(pT));
return 1;
}
if (!type.compare(QLatin1String("keybind"), Qt::CaseInsensitive)) {
auto pT = host.getKeyUnit()->getKey(id);
lua_pushboolean(L, static_cast<bool>(pT));
return 1;
}
if (!type.compare(QLatin1String("button"), Qt::CaseInsensitive)) {
auto pT = host.getActionUnit()->getAction(id);
lua_pushboolean(L, static_cast<bool>(pT));
return 1;
}
if (!type.compare(QLatin1String("script"), Qt::CaseInsensitive)) {
auto pT = host.getScriptUnit()->getScript(id);
lua_pushboolean(L, static_cast<bool>(pT));
return 1;
}
return warnArgumentValue(L, __func__, qsl("invalid item type '%1' given, it should be one (case insensitive) of: 'alias', 'button', 'script', 'keybind', 'timer' or 'trigger'").arg(type));
}

// Documentation: https://wiki.mudlet.org/w/Manual:Lua_Functions#isActive
int TLuaInterpreter::isActive(lua_State* L)
{
Expand Down Expand Up @@ -7989,6 +8033,71 @@ int TLuaInterpreter::isActive(lua_State* L)
return 1;
}

// Documentation: https://wiki.mudlet.org/w/Manual:Lua_Functions#isActiveID
int TLuaInterpreter::isActiveID(lua_State* L)
{
auto id = getVerifiedInt(L, __func__, 1, "item ID");
if (id < 0) {
// Must be zero or more but doesn't seem to be:
return warnArgumentValue(L, __func__, "item ID as %d does not seem to be a positive integer", id);
}

// Although we only use 4 ASCII strings the user may not enter a purely
// ASCII value which we might have to report...
QString type = getVerifiedString(L, __func__, 2, "item type");

Host& host = getHostFromLua(L);
if (!type.compare(QLatin1String("timer"), Qt::CaseInsensitive)) {
auto pT = host.getTimerUnit()->getTimer(id);
if (!pT) {
return warnArgumentValue(L, __func__, "timer ID %d does not exist", id);
}
lua_pushboolean(L, pT->isActive());
return 1;
}
if (!type.compare(QLatin1String("trigger"), Qt::CaseInsensitive)) {
auto pT = host.getTriggerUnit()->getTrigger(id);
if (!pT) {
return warnArgumentValue(L, __func__, "trigger ID %d does not exist", id);
}
lua_pushboolean(L, pT->isActive());
return 1;
}
if (!type.compare(QLatin1String("alias"), Qt::CaseInsensitive)) {
auto pT = host.getAliasUnit()->getAlias(id);
if (!pT) {
return warnArgumentValue(L, __func__, "alias ID %d does not exist", id);
}
lua_pushboolean(L, pT->isActive());
return 1;
}
if (!type.compare(QLatin1String("keybind"), Qt::CaseInsensitive)) {
auto pT = host.getKeyUnit()->getKey(id);
if (!pT) {
return warnArgumentValue(L, __func__, "key-binding ID %d does not exist", id);
}
lua_pushboolean(L, pT->isActive());
return 1;
}
if (!type.compare(QLatin1String("button"), Qt::CaseInsensitive)) {
auto pT = host.getActionUnit()->getAction(id);
if (!pT) {
return warnArgumentValue(L, __func__, "button/menu/toolbar ID %d does not exist", id);
}
lua_pushboolean(L, pT->isActive());
return 1;
}
if (!type.compare(QLatin1String("script"), Qt::CaseInsensitive)) {
auto pT = host.getScriptUnit()->getScript(id);
if (!pT) {
return warnArgumentValue(L, __func__, "script ID %d does not exist", id);
}
lua_pushboolean(L, pT->isActive());
return 1;
}
return warnArgumentValue(L, __func__, qsl("invalid item type '%1' given, it should be one (case insensitive) of: 'alias', 'button', 'script', 'keybind', 'timer' or 'trigger'").arg(type));
}

// Documentation: https://wiki.mudlet.org/w/Manual:Lua_Functions#permAlias
int TLuaInterpreter::permAlias(lua_State* L)
{
Expand Down Expand Up @@ -15100,7 +15209,9 @@ void TLuaInterpreter::initLuaGlobals()
lua_register(pGlobalLua, "permKey", TLuaInterpreter::permKey);
lua_register(pGlobalLua, "tempKey", TLuaInterpreter::tempKey);
lua_register(pGlobalLua, "exists", TLuaInterpreter::exists);
lua_register(pGlobalLua, "existsID", TLuaInterpreter::existsID);
lua_register(pGlobalLua, "isActive", TLuaInterpreter::isActive);
lua_register(pGlobalLua, "isActiveID", TLuaInterpreter::isActiveID);
lua_register(pGlobalLua, "enableAlias", TLuaInterpreter::enableAlias);
lua_register(pGlobalLua, "tempAlias", TLuaInterpreter::tempAlias);
lua_register(pGlobalLua, "disableAlias", TLuaInterpreter::disableAlias);
Expand Down
2 changes: 2 additions & 0 deletions src/TLuaInterpreter.h
Expand Up @@ -483,7 +483,9 @@ class TLuaInterpreter : public QThread
static int disableScript(lua_State*);
static int permAlias(lua_State*);
static int exists(lua_State*);
static int existsID(lua_State*);
static int isActive(lua_State*);
static int isActiveID(lua_State*);
static int tempAlias(lua_State*);
static int enableAlias(lua_State*);
static int disableAlias(lua_State*);
Expand Down

0 comments on commit c810ee4

Please sign in to comment.