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

Материал из Space Station 14 Вики
Нет описания правки
Нет описания правки
 
(не показана 261 промежуточная версия этого же участника)
Строка 1: Строка 1:
-- Загрузка данных
local itemData = mw.loadData("Модуль:IanComradeBot/prototypes/fills/Item.json/data")
local itemSlotsData = mw.loadData("Модуль:IanComradeBot/prototypes/ItemSlots.json/data")
local p = {}
local p = {}
local getArgs = require('Module:Arguments').getArgs


-- Функция для поиска первого startingItem в slots
function p.main(frame)
local function getFirstStartingItem(data)
    local args = getArgs(frame, { removeBlanks = false })
     if not data or not data.ItemSlots or not data.ItemSlots.slots then  
     local name = args[1] or ""
         return nil
    local attributes = args[2] or ""
    if name == "" then
         return "<span class=\"error\">Ошибка: не указано имя файла.</span>"
     end
     end
      
     local ext = (args["ext"] or "png"):gsub("^%.", "")
     for _, slot in pairs(data.ItemSlots.slots) do
    local namespace = args["namespace"] or "Файл"
         if slot.startingItem and slot.startingItem ~= "" then
    local max = tonumber(args["max"]) or 50
             return slot.startingItem
    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
     end
     end
   
    return nil
end


-- Поиск данных по ID
     for i = 1, max do
local function findDataById(itemDataIndex, id)
        local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext)
    if not itemDataIndex then return nil end
         if t and t.exists then
     for _, item in ipairs(itemDataIndex) do
             table.insert(found, "-" .. i)
         if item.id == id then
             return item
         end
         end
     end
     end
end


-- Форматирование содержимого
     if #found == 0 then
local function formatContent(id)
        return ""
     if not id then return "Ошибка: отсутствует id у элемента." end
    end
    local name = string.format("{{#invoke:Entity Lookup|getname|%s}}", id)
    local image = string.format("%s.png", id)
    return string.format("{{LinkСard|SideStyle=1|background-color=#cbcbff0a|image=%s|name=%s}}", image, name)
end


-- Основная функция модуля
     local before = "[[" .. namespace .. ":" .. name
function p.main(frame)
     local after = "." .. ext .. "|" .. attributes .. "]]"
     local mode = frame.args[1]
     local id = frame.args[2]


     if not id then return "Не указан ID." end
     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>")


     if mode == "slot" then
     return frame:preprocess(table.concat(parts, "\n"))
        local itemDataEntry = findDataById(itemSlotsData, id)
        if not itemDataEntry then return "ID не найден в данных ItemSlots." end
 
        local startingItem = getFirstStartingItem(itemDataEntry)
        if not startingItem then return "Не найден startingItem в slots." end
 
        return frame:preprocess(formatContent(startingItem))
    else
        return "Неизвестный режим: " .. mode
    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