Модуль:Entity Tags

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

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

local p = {}

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

function p.main(frame)
    local args = frame.args
    local mode = mw.text.trim(args[1] or "")
    local searchParam = mw.text.trim(args[2] or "")

    if searchParam == "" then
        return "Ошибка: не указан параметр поиска."
    end

    if mode == "id" then
        for _, entry in ipairs(tagsData) do
            if entry.id and mw.text.trim(entry.id) == searchParam then
                if entry.Tag and type(entry.Tag.tags) == "table" then
                    if #entry.Tag.tags > 0 then
                        return table.concat(entry.Tag.tags, ", ")
                    else
                        return "Теги не найдены для id «" .. searchParam .. "»."
                    end
                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 tag == 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