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

Новая страница: «local p = {} -- Загрузка данных local function loadData(filePath) local page = mw.title.new(filePath) local content = page and page:getContent() return content and mw.text.jsonDecode(content) or nil end -- Проверка равенства двух таблиц local function deepEqual(t1, t2) if t1 == t2 then return true end if type(t1) ~= "table" or type(t2) ~= "table" then return false end -- Если это ма...»
 
Нет описания правки
Строка 105: Строка 105:
end
end


-- Обновляем основную функцию
-- Функция генерации шаблона по записи
local function generateTemplate(entry, param, secondaryParam, data)
local function generateTemplate(entry, param, data)
     local spritePath = getSpritePath(entry)
     local spritePath = getSpritePath(entry)
     if not entry.id or not spritePath then
     if not entry.id or not spritePath then
Строка 113: Строка 113:


     if param == "image" then
     if param == "image" then
         if secondaryParam then
         return mw.getCurrentFrame():preprocess("{{Entity Sprite/Image|" .. entry.id .. "|" .. spritePath .. "}}")
            if tostring(entry.id) == tostring(secondaryParam) then
                return spritePath
            end
            return nil
        else
            return mw.getCurrentFrame():preprocess("{{Entity Sprite/Image|" .. entry.id .. "|" .. spritePath .. "}}")
        end
     elseif param == "path" then
     elseif param == "path" then
        if secondaryParam then
            for _, e in pairs(data) do
                if getSpritePath(e) == secondaryParam then
                    return e.id
                end
            end
            return nil
        end
         return mw.getCurrentFrame():preprocess("{{Entity Sprite/Path|" .. entry.id .. "|" .. spritePath .. "}}")
         return mw.getCurrentFrame():preprocess("{{Entity Sprite/Path|" .. entry.id .. "|" .. spritePath .. "}}")
     end
     end


     return nil
     return nil
end
-- Генерация шаблона по умолчанию
local function generateDefaultTemplate(data, params)
    local id = params.Id
    local description = params.Description or ""
    local servers = params.Servers or ""
    local source = params.Source or ""
    local tags = params.Tags or ""
    local spritePath = nil
    local path = params.Path
    local entry = nil
    if id and id ~= "" then
        for _, item in pairs(data) do
            if tostring(item.id) == tostring(id) then
                entry = item
                break
            end
        end
    end
    if entry then
        spritePath = getSpritePath(entry)
        if not spritePath then
            return ""
        end
    end
   
    if not path or path == "" then
        path = "Resources/Textures/" .. (spritePath or "")
    end
    return mw.getCurrentFrame():preprocess(
        "{{Файл\n" ..
        "|Описание = " .. description .. "\n" ..
        "|Id      = " .. id .. "\n" ..
        "|Сервера  = " .. servers .. "\n" ..
        "|Источник = " .. source .. "\n" ..
        "|Путь    = " .. path .. "\n" ..
        "|Теги    = " .. tags .. "\n" ..
        "}}\n"
    )
end
end


function p.main(frame)
function p.main(frame)
     local param = frame.args[1]
     local param = frame.args[1]
    local secondaryParam = frame.args[2]


     local data = loadData('User:IanComradeBot/prototypes/entity sprite.json')
     local data = loadData('User:IanComradeBot/prototypes/entity sprite.json')
Строка 191: Строка 131:
     if param == "repeat" then
     if param == "repeat" then
         return generateRepeatTemplate(data)
         return generateRepeatTemplate(data)
    elseif param == "path" and secondaryParam then
        for _, entry in ipairs(data) do
            local template = generateTemplate(entry, param, secondaryParam, data)
            if template then
                return template
            end
        end
        return nil
     elseif param == "image" or param == "path" then
     elseif param == "image" or param == "path" then
         local result = {}
         local result = {}
         for _, entry in pairs(data) do
         for _, entry in pairs(data) do
             local template = generateTemplate(entry, param, secondaryParam, data)
             local template = generateTemplate(entry, param, data)
             if template then
             if template then
                 table.insert(result, template)
                 table.insert(result, template)
Строка 209: Строка 141:
         return table.concat(result, "\n")
         return table.concat(result, "\n")
     else
     else
         return generateDefaultTemplate(data, frame.args)
         return nil
     end
     end
end
end


return p
return p