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

Материал из Space Station 14 Вики
мНет описания правки
Нет описания правки
 
(не показано 176 промежуточных версий этого же участника)
Строка 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 ""
     local entry = nil
     if name == "" then
     -- Ищем оружие с заданным id
        return "<span class=\"error\">Ошибка: не указано имя файла.</span>"
     for _, weapon in ipairs(weaponData) do
        if weapon.id == id then
            entry = weapon
            break
        end
     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")


     if not entry then
     local found = {}
        return "Оружие с id '" .. id .. "' не найдено."
    end


     if mode == "melee" then
     if include_base then
         local melee = entry.MeleeWeapon
         local t = mw.title.new("Файл:" .. name .. "." .. ext)
         if not melee then
         if t and t.exists then
             return "Нет данных о MeleeWeapon для оружия '" .. id .. "'."
             table.insert(found, "")
         end
         end
    end


        -- Получаем таблицу повреждений для одноручного хвата
    for i = 1, max do
         local oneHandDamage = melee.damage and melee.damage.types
         local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext)
         if not oneHandDamage then
         if t and t.exists then
             return "Нет данных о повреждениях (MeleeWeapon.damage.types) для оружия '" .. id .. "'."
             table.insert(found, "-" .. i)
         end
         end
    end


         -- Пытаемся получить данные DamageOtherOnHit для двуручного хвата (если они есть)
    if #found == 0 then
        local twoHandDamage = entry.DamageOtherOnHit and entry.DamageOtherOnHit.damage and entry.DamageOtherOnHit.damage.types
         return ""
    end


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


        if twoHandDamage then
    local parts = {}
            -- Секция для одноручного хвата
    table.insert(parts, "<choose before=\"" .. before .. "\" after=\"" .. after .. "\">")
            table.insert(lines, "В одноручном хвате:")
    for _, suf in ipairs(found) do
            for dmgType, value in pairs(oneHandDamage) do
        table.insert(parts, "<option>" .. suf .. "</option>")
                table.insert(lines, value .. " " .. dmgType)
    end
            end
    table.insert(parts, "</choose>")
 
            -- Секция для двуручного хвата
            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
            -- Если нет DamageOtherOnHit, выводим просто одноручный урон
            for dmgType, value in pairs(oneHandDamage) do
                table.insert(lines, value .. " " .. dmgType)
            end
        end
 
        return table.concat(lines, "<br />")


     elseif mode == "attackRate" then
     return frame:preprocess(table.concat(parts, "\n"))
        local melee = entry.MeleeWeapon
        if not melee then
            return "Нет данных о MeleeWeapon для оружия '" .. id .. "'."
        end
 
        if melee.attackRate then
            return tostring(melee.attackRate)
        else
            return "Нет данных об attackRate для оружия '" .. id .. "'."
        end
    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