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

мНет описания правки
Нет описания правки
 
(не показано 7 промежуточных версий этого же участника)
Строка 44: Строка 44:
         return tostring(v)
         return tostring(v)
     elseif t == "table" then
     elseif t == "table" then
         local max = 0
         local ok, json = pcall(mw.text.jsonEncode, v)
        local hasNonNumber = false
         if ok and json then
        for k in pairs(v) do
             return json
            if type(k) ~= "number" then
                hasNonNumber = true
                break
            else
                if k > max then max = k end
            end
        end
         if not hasNonNumber and max > 0 then
            local out = {}
            for i = 1, max do
                out[#out + 1] = format_value(v[i])
            end
             return table.concat(out, ", ")
        else
            local out = {}
            for k, val in pairs(v) do
                out[#out + 1] = tostring(k) .. ": " .. format_value(val)
            end
            return table.concat(out, ", ")
         end
         end
        return ""
     else
     else
         return tostring(v)
         return tostring(v)
Строка 147: Строка 129:
end
end


function p.findInProto(frame)
function p.findInGenerator(frame)
     local args = frame.args or {}
     local args = frame.args or {}
     local protoKey = args[1] or ""
     local searchId = args[1] or ""
     local fieldId = args[2] or ""
    local kind = (args[2] or ""):lower()
     local fieldId = args[3] or ""


     if protoKey == "" or fieldId == "" then
     if searchId == "" or fieldId == "" then
        return ""
    end
    if kind ~= "prototype" and kind ~= "component" then
         return ""
         return ""
     end
     end


     local moduleName = "Module:IanComradeBot/prototype.json/data"
     local baseUser = "IanComradeBot/"
    local storeName
    if kind == "prototype" then
        storeName = "prototype_store.json"
    else
        storeName = "component_store.json"
    end
    local moduleName = "Module:" .. baseUser .. storeName .. "/data"


     local data = cache[moduleName]
     local data = cache[moduleName]
Строка 168: Строка 161:
     end
     end


     local proto = data[protoKey]
     local entry = data[searchId]
     if type(proto) ~= "table" then
     if type(entry) ~= "table" then
         return ""
         return ""
     end
     end


     local value = proto[fieldId]
     local value = entry[fieldId]
     if value == nil then
     if value == nil then
         return ""
         return ""
Строка 278: Строка 271:
     local value = get_by_path(entry, keyPath)
     local value = get_by_path(entry, keyPath)
     return format_value(value)
     return format_value(value)
end
function p.getId(frame)
    local args = frame.args or {}
    local searchValue = args[1] or ""
    local pagePath = args[2] or ""
    local keyPath = args[3] or ""
    if searchValue == "" or pagePath == "" or keyPath == "" then
        return ""
    end
    local baseUser = "IanComradeBot/"
    local moduleName = "Module:" .. baseUser .. pagePath .. "/data"
    local data = cache[moduleName]
    if not data then
        local ok, loaded = pcall(mw.loadData, moduleName)
        if not ok or not loaded then return "" end
        data = loaded
        cache[moduleName] = data
    end
    local idsTable = data.id
    if type(idsTable) ~= "table" then
        return ""
    end
    local target = tostring(searchValue)
    for idKey, entry in pairs(idsTable) do
        if type(entry) == "table" then
            local v = get_by_path(entry, keyPath)
            if v ~= nil then
                if type(v) == "table" then
                    if is_array(v) then
                        for _, item in ipairs(v) do
                            if tostring(item) == target then
                                return idKey
                            end
                        end
                    else
                        for _, item in pairs(v) do
                            if tostring(item) == target then
                                return idKey
                            end
                        end
                    end
                else
                    if tostring(v) == target then
                        return idKey
                    end
                end
            end
        end
    end
    return ""
end
function p.getTplId(frame)
    local args = frame.args or {}
    local searchValue = args[1] or ""
    local pagePath = args[2] or ""
    local keyPath = args[3] or ""
    local tplPath = mw.text.unstripNoWiki(args[4] or "")
    if searchValue == "" or pagePath == "" or keyPath == "" or tplPath == "" then
        return ""
    end
    local baseUser = "IanComradeBot/"
    local moduleName = "Module:" .. baseUser .. pagePath .. "/data"
    local data = cache[moduleName]
    if not data then
        local ok, loaded = pcall(mw.loadData, moduleName)
        if not ok or not loaded then return "" end
        data = loaded
        cache[moduleName] = data
    end
    local idsTable = data.id
    if type(idsTable) ~= "table" then
        return ""
    end
    local target = tostring(searchValue)
    local matches = {}
    for idKey, entry in pairs(idsTable) do
        if type(entry) == "table" then
            local v = get_by_path(entry, keyPath)
            if v ~= nil then
                if type(v) == "table" then
                    if is_array(v) then
                        for _, item in ipairs(v) do
                            if tostring(item) == target then
                                matches[#matches + 1] = idKey
                                break
                            end
                        end
                    else
                        for _, item in pairs(v) do
                            if tostring(item) == target then
                                matches[#matches + 1] = idKey
                                break
                            end
                        end
                    end
                else
                    if tostring(v) == target then
                        matches[#matches + 1] = idKey
                    end
                end
            end
        end
    end
    if #matches == 0 then
        return ""
    end
    local out = {}
    for _, idKey in ipairs(matches) do
        local tpl = p.getTpl({ args = { idKey, pagePath, tplPath } })
        if tpl ~= "" then
            out[#out + 1] = tpl
        end
    end
    if #out == 0 then
        return ""
    end
    local result = table.concat(out, "\n")
    if type(frame.preprocess) == "function" then
        return frame:preprocess(result)
    end
    return result
end
end


Строка 304: Строка 438:
end
end


function p.getTplProto(frame)
function p.getTplGenerator(frame)
     local args = frame.args or {}
     local args = frame.args or {}
     local searchId = args[1] or ""
     local searchId = args[1] or ""
     local protoId = args[2] or ""
     local kind = (args[2] or ""):lower()
     local tplPath = args[3] or ""
     local generatorId = args[3] or ""
     local pagePath = "prototype/" .. protoId .. ".json"
     local tplPath = args[4] or ""


     if searchId == "" or protoId == "" or tplPath == "" then
     if searchId == "" or generatorId == "" or tplPath == "" then
         return ""
         return ""
     end
     end
    if kind ~= "prototype" and kind ~= "component" then
        return ""
    end
    local dir = (kind == "prototype") and "prototype/" or "component/"
    local pagePath = dir .. generatorId .. ".json"


     local idsJson = p.findInProto({ args = { searchId, protoId } })
     local idsJson = p.findInGenerator({ args = { searchId, kind, generatorId } })
     local ok, ids = pcall(mw.text.jsonDecode, idsJson or "")
     local ok, ids = pcall(mw.text.jsonDecode, idsJson or "")
     if not ok or type(ids) ~= "table" or #ids == 0 then
     if not ok or type(ids) ~= "table" or #ids == 0 then