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

Add More Conditions to runif #320

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
64 changes: 62 additions & 2 deletions Cmdr/BuiltInCommands/Utility/runif.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,66 @@ local conditions = {
return text:sub(#arg + 1)
end
end,

contains = function(text, arg)
if text:find(arg, 1, true) then
return text
end
end,

endsWith = function(text, arg)
if text:sub(-#arg) == arg then
return text:sub(1, -#arg - 1)
end
end,

pattern = function(text, arg)
if text:match(arg) then
return text
end
end,

equals = function(text, arg)
if text == arg then
return text
end
end,

notEquals = function(text, arg)
if text ~= arg then
return text
end
end,

greaterThan = function(text, arg)
if tonumber(text) > tonumber(arg) then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment you'd have to do runIf greaterThan 1 2 command and it would return true as 2 is greater than 1 but I think tonumber(text) > tonumber(arg) should be switched to tonumber(arg) > tonumber(text) to be a little more intuitive as you're comparing the argument against the text

return text
end
end,

lessThan = function(text, arg)
if tonumber(text) < tonumber(arg) then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as line 39 comment, switch them

return text
end
end,

greaterThanOrEqual = function(text, arg)
if tonumber(text) >= tonumber(arg) then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as line 39 comment, switch them

return text
end
end,

lessThanOrEqual = function(text, arg)
if tonumber(text) <= tonumber(arg) then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as line 39 comment, switch them

return text
end
end,

length = function(text, arg)
if #text == tonumber(arg) then
return text
end
end,
}

return {
Expand Down Expand Up @@ -42,9 +102,9 @@ return {
return ("Condition %q is not valid."):format(condition)
end

local text = conditionFunc(testAgainst, arg)
local success, text = pcall(conditionFunc, testAgainst, arg)

if text then
if success and text then
autonordev marked this conversation as resolved.
Show resolved Hide resolved
return context.Dispatcher:EvaluateAndRun(
context.Cmdr.Util.RunEmbeddedCommands(context.Dispatcher, command or text)
)
Expand Down
17 changes: 16 additions & 1 deletion Cmdr/BuiltInTypes/ConditionFunction.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
return function(registry)
registry:RegisterType("conditionFunction", registry.Cmdr.Util.MakeEnumType("ConditionFunction", { "startsWith" }))
registry:RegisterType(
"conditionFunction",
registry.Cmdr.Util.MakeEnumType("ConditionFunction", {
"startsWith",
"contains",
"endsWith",
"pattern",
"equals",
"notEquals",
"greaterThan",
"lessThan",
"greaterThanOrEqual",
"lessThanOrEqual",
"length",
})
)
end