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

Материал из Space Station 14 Вики
мНет описания правки
Нет описания правки
 
(не показано 359 промежуточных версий этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}
local getArgs = require('Module:Arguments').getArgs


local cachedData = nil
function p.main(frame)
local loadCount = 0
    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 cache = mw.ext.LuaCache and mw.ext.LuaCache.get("entity_data_cache")
    local found = {}


local function loadData(filePath)
    if include_base then
    if cachedData then
        local t = mw.title.new("Файл:" .. name .. "." .. ext)
         return cachedData
        if t and t.exists then
            table.insert(found, "")
         end
     end
     end


     if cache then
     for i = 1, max do
         local cachedContent = cache:get(filePath)
         local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext)
         if cachedContent then
         if t and t.exists then
             cachedData = mw.text.jsonDecode(cachedContent)
             table.insert(found, "-" .. i)
            return cachedData
         end
         end
     end
     end


     loadCount = loadCount + 1
     if #found == 0 then
    local page = mw.title.new(filePath)
         return ""
    local content = page and page:getContent()
 
    cachedData = content and mw.text.jsonDecode(content) or nil
 
    if cachedData and cache then
         cache:set(filePath, content, 3600)
     end
     end


     return cachedData
     local before = "[[" .. namespace .. ":" .. name
end
    local after = "." .. ext .. "|" .. attributes .. "]]"


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


     if param == "debug" then
     return frame:preprocess(table.concat(parts, "\n"))
        return "Количество загрузок JSON: " .. loadCount
    end
 
    if param == "image" and secondaryParam then
        for _, entry in ipairs(data) do
            if entry.id == secondaryParam then
                return entry.Sprite and entry.Sprite.sprite or (entry.Icon and entry.Icon.sprite) or "Ошибка: Спрайт не найден."
            end
        end
        return "Ошибка: ID не найден."
    end
end
end


return p
return p

Текущая версия от 02:52, 17 марта 2026

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

local p = {}
local getArgs = require('Module:Arguments').getArgs

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 found = {}

    if include_base then
        local t = mw.title.new("Файл:" .. name .. "." .. ext)
        if t and t.exists then
            table.insert(found, "")
        end
    end

    for i = 1, max do
        local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext)
        if t and t.exists then
            table.insert(found, "-" .. i)
        end
    end

    if #found == 0 then
        return ""
    end

    local before = "[[" .. namespace .. ":" .. name
    local after = "." .. ext .. "|" .. attributes .. "]]"

    local parts = {}
    table.insert(parts, "<choose before=\"" .. before .. "\" after=\"" .. after .. "\">")
    for _, suf in ipairs(found) do
        table.insert(parts, "<option>" .. suf .. "</option>")
    end
    table.insert(parts, "</choose>")

    return frame:preprocess(table.concat(parts, "\n"))
end

return p