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

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


--- Функция main принимает два параметра:
function p.main(frame)
--- @param mode string  "melee" или "attackRate"
    local args = getArgs(frame, { removeBlanks = false })
--- @param id string    идентификатор оружия
     local name = args[1] or ""
function p.main(mode, id)
    local attributes = args[2] or ""
     -- Проверка на наличие id
     if name == "" then
     if id == nil then
         return "<span class=\"error\">Ошибка: не указано имя файла.</span>"
         return "Ошибка: не задан параметр id."
     end
     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 entry = nil
     local found = {}
     -- Поиск записи по заданному id (предполагается, что weaponData — массив записей)
 
    for _, weapon in ipairs(weaponData) do
     if include_base then
         if weapon.id == id then
        local t = mw.title.new("Файл:" .. name .. "." .. ext)
             entry = weapon
         if t and t.exists then
            break
             table.insert(found, "")
         end
         end
     end
     end


     if not entry then
     for i = 1, max do
         return "Оружие с id '" .. tostring(id) .. "' не найдено."
         local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext)
        if t and t.exists then
            table.insert(found, "-" .. i)
        end
     end
     end


     if mode == "melee" then
     if #found == 0 then
         local melee = entry.MeleeWeapon
         return ""
        if not melee then
    end
            return "Нет данных о MeleeWeapon для оружия '" .. tostring(id) .. "'."
        end


        local oneHandDamage = melee.damage and melee.damage.types
    local before = "[[" .. namespace .. ":" .. name
        if not oneHandDamage then
    local after = "." .. ext .. "|" .. attributes .. "]]"
            return "Нет данных о повреждениях (MeleeWeapon.damage.types) для оружия '" .. tostring(id) .. "'."
        end


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


        if twoHandDamage then
    return frame:preprocess(table.concat(parts, "\n"))
            -- Секция одноручного хвата
            table.insert(lines, "В одноручном хвате:")
            for dmgType, value in pairs(oneHandDamage) do
                table.insert(lines, value .. " " .. dmgType)
            end
 
            -- Секция двуручного хвата
            table.insert(lines, "В двуручном хвате:")
            for dmgType, value in pairs(twoHandDamage) do
                -- Если тип присутствует в одноручном хвате, выводим так же
                if oneHandDamage[dmgType] then
                    table.insert(lines, value .. " " .. dmgType)
                else
                    table.insert(lines, value .. " " .. dmgType)
                end
            end
        else
            -- Если нет данных для двуручного хвата, выводим только одноручные повреждения
            for dmgType, value in pairs(oneHandDamage) do
                table.insert(lines, value .. " " .. dmgType)
            end
        end
 
        return table.concat(lines, "<br />")
 
    elseif mode == "attackRate" then
        local melee = entry.MeleeWeapon
        if not melee then
            return "Нет данных о MeleeWeapon для оружия '" .. tostring(id) .. "'."
        end
 
        -- Если attackRate отсутствует, возвращаем "1"
        if melee.attackRate then
            return tostring(melee.attackRate)
        else
            return "1"
        end
 
    else
        return "Неверный режим: " .. tostring(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