Модуль:Entity Tags

Материал из Space Station 14 Вики

Для документации этого модуля может быть создана страница Модуль:Entity Tags/doc

local p = {}
-- Функция для удаления пробелов в начале и конце строки
local function trim(s)
    return (s:gsub("^%s*(.-)%s*$", "%1"))
end

local tagsData = mw.loadData("Модуль:IanComradeBot/prototypes/entity tags.json/data") or {}

function p.search(frame)
    local args = frame.args
    local mode = trim(args[1] or "")
    local searchParam = trim(args[2] or "")
    
    if searchParam == "" then
        return "Ошибка: не указан параметр поиска."
    end
    
    if mode == "id" then
        -- Поиск по id
        for _, entry in ipairs(tagsData) do
            if trim(entry.id or "") == searchParam then
                if entry.Tag 
                   and type(entry.Tag.tags) == "table" 
                   and #entry.Tag.tags > 0 then
                    return table.concat(entry.Tag.tags, ", ")
                else
                    return "Теги не найдены для id «" .. searchParam .. "»."
                end
            end
        end
        return "Запись с id «" .. searchParam .. "» не найдена."
        
    elseif mode == "tag" then
        -- Поиск по тегу
        local foundIds = {}
        for _, entry in ipairs(tagsData) do
            if entry.Tag and type(entry.Tag.tags) == "table" then
                for _, tag in ipairs(entry.Tag.tags) do
                    if trim(tag or "") == searchParam then
                        table.insert(foundIds, entry.id)
                        break
                    end
                end
            end
        end
        
        if #foundIds > 0 then
            return table.concat(foundIds, ", ")
        else
            return "Нет записей с тегом «" .. searchParam .. "»."
        end
        
    else
        return "Ошибка: неизвестный режим поиска («" .. mode .. "»). Используйте «id» или «tag»."
    end
end

return p