Модуль:Песочница/Pok: различия между версиями

Нет описания правки
Нет описания правки
 
(не показаны 363 промежуточные версии этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}
local getArgs = require('Module:Arguments').getArgs


local cachedData = nil
function p.main(frame)
    local args = getArgs(frame, { removeBlanks = false })
    local name = args[1] or ""
    local attributes = args[2] or ""
    if name == "" then
        return "<span class=\"error\">Ошибка: не указано имя файла.</span>"
    end
    local ext = (args["ext"] or "png"):gsub("^%.", "")
    local namespace = args["namespace"] or "Файл"
    local max = tonumber(args["max"]) or 50
    local include_base = (args["base"] ~= "no")


local function loadData(filePath)
    local found = {}
    if cachedData then
 
         return cachedData
    if include_base then
        local t = mw.title.new("Файл:" .. name .. "." .. ext)
        if t and t.exists then
            table.insert(found, "")
         end
     end
     end
    local page = mw.title.new(filePath)
    local content = page and page:getContent()
    cachedData = content and mw.text.jsonDecode(content) or nil
    return cachedData
end


local function getSpritePath(entry)
     for i = 1, max do
     if entry.Sprite and entry.Sprite.sprite then
         local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext)
         return entry.Sprite.sprite
        if t and t.exists then
    elseif entry.Icon and entry.Icon.sprite then
            table.insert(found, "-" .. i)
        return entry.Icon.sprite
    elseif entry.Sprite and entry.Sprite.layers then
        for _, layer in ipairs(entry.Sprite.layers) do
            if layer.sprite then
                return layer.sprite
            end
         end
         end
     end
     end
    return nil
end


function p.main(frame)
    if #found == 0 then
     local param = frame.args[1]
        return ""
     local secondaryParam = frame.args[2]
    end
 
     local before = "[[" .. namespace .. ":" .. name
     local after = "." .. ext .. "|" .. attributes .. "]]"


     local data = loadData('User:IanComradeBot/prototypes/entity sprite.json')
     local parts = {}
     if not data or type(data) ~= 'table' then
    table.insert(parts, "<choose before=\"" .. before .. "\" after=\"" .. after .. "\">")
         return 'Ошибка: Невозможно загрузить данные из JSON.'
     for _, suf in ipairs(found) do
         table.insert(parts, "<option>" .. suf .. "</option>")
     end
     end
    table.insert(parts, "</choose>")


     if param == "image" and secondaryParam then
     return frame:preprocess(table.concat(parts, "\n"))
        for _, entry in ipairs(data) do
            if entry.id == secondaryParam then
                return getSpritePath(entry) or "Ошибка: Спрайт не найден."
            end
        end
        return "Ошибка: ID не найден."
    end
end
end


return p
return p