Модуль:Meta Sprite: различия между версиями

мНет описания правки
мНет описания правки
Строка 3: Строка 3:


local p = {}
local p = {}
-- Функция для разделения строки по разделителю
local function split(str, delimiter)
    local result = {}
    for token in string.gmatch(str, "([^" .. delimiter .. "]+)") do
        table.insert(result, token)
    end
    return result
end


function p.main(frame)
function p.main(frame)
     local mode = frame.args[1] -- Режим
     local mode = frame.args[1] -- Режим
     local path = frame.args[2] -- Путь
     local path = frame.args[2] -- Путь
   
 
     if not mode or not path then
     if not mode or not path then
         return "Ошибка: необходимо указать режим и путь."
         return "Ошибка: необходимо указать режим и путь."
     end
     end
      
 
     -- Поиск точного совпадение
     local entry = data[path]
     local entry = data[path]
    -- Если не найдено, ищем по последним трём сегментам пути
     if not entry then
     if not entry then
         return ""
         local segments = split(path, "/")
        local count = #segments
        local suffix
        if count >= 3 then
            suffix = table.concat({ segments[count-2], segments[count-1], segments[count] }, "/")
        else
            suffix = path
        end
 
        -- Ищем среди всех ключей, заканчивающихся на полученный суффикс
        for key, value in pairs(data) do
            if key:sub(-#suffix) == suffix then
                entry = value
                break
            end
        end
 
        if not entry then
            return ""
        end
     end
     end
   
 
     local result = entry[mode]
     local result = entry[mode]
     if not result then
     if not result then
         return "Ошибка: указанный режим не найден в данных."
         return "Ошибка: указанный режим не найден в данных."
     end
     end
   
 
     return result
     return result
end
end


return p
return p