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

refactor: improve readability and optimize toPosition function #2928

Merged
merged 6 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 0 additions & 18 deletions data-otservbr-global/lib/quests/soul_war.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1569,21 +1569,3 @@ function Creature:applyZoneEffect(var, combat, zoneName)

return true
end

function string.toPosition(str)
local patterns = {
-- table format
"{%s*x%s*=%s*(%d+)%s*,%s*y%s*=%s*(%d+)%s*,%s*z%s*=%s*(%d+)%s*}",
-- Position format
"Position%s*%((%d+)%s*,%s*(%d+)%s*,%s*(%d+)%s*%)",
-- x, y, z format
"(%d+)%s*,%s*(%d+)%s*,%s*(%d+)",
}

for _, pattern in ipairs(patterns) do
local x, y, z = string.match(str, pattern)
if x and y and z then
return Position(tonumber(x), tonumber(y), tonumber(z))
end
end
end
16 changes: 16 additions & 0 deletions data/libs/functions/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,19 @@ end
string.capitalize = function(str)
return str:gsub("%f[%a].", string.upper)
end

function string.toPosition(inputString)
local positionPatterns = {
"{%s*x%s*=%s*(%d+)%s*,%s*y%s*=%s*(%d+)%s*,%s*z%s*=%s*(%d+)%s*}",
"Position%s*%((%d+)%s*,%s*(%d+)%s*,%s*(%d+)%s*%)",
"(%d+)%s*,%s*(%d+)%s*,%s*(%d+)",
}

for _, pattern in ipairs(positionPatterns) do
local posX, posY, posZ = string.match(inputString, pattern)
if posX and posY and posZ then
return Position(tonumber(posX), tonumber(posY), tonumber(posZ))
end
end
return nil
end
Loading