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

Нет описания правки
Нет описания правки
Строка 129: Строка 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]
Строка 150: Строка 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 ""
Строка 286: Строка 297:
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