Модуль:Покемоны

Материал из Chatmon Wiki
Версия от 19:47, 30 ноября 2026; ChatmonWikiEditor (обсуждение | вклад) (Общий каталог покемонов для справочных таблиц)
(разн.) ← Предыдущая версия | Текущая версия (разн.) | Следующая версия → (разн.)

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

local data = mw.loadData('Module:Покемоны/данные')
local export = {}

local number = '<abbr title="Номер в национальном покедексе">№</abbr>'
local hp = '<abbr title="Очки здоровья">HP</abbr>'
local sumLabel = '<abbr title="Сумма базовых характеристик">Сумма</abbr>'
local stats = {hp, 'Атака', 'Защита', 'Спец. атака', 'Спец. защита', 'Скорость'}

local function picture(pokemon, size, mode)
    local attrs = 'alt=' .. pokemon.name .. '|link=' .. pokemon.name
    if mode == 'decorative' then
        attrs = 'link=|alt='
    elseif mode == 'encounter' then
        attrs = 'alt=' .. pokemon.alt .. '|link='
    end
    return '[[Файл:' .. pokemon.sprite .. '|' .. size .. 'px|middle|' .. attrs .. ']] [[' .. pokemon.name .. ']]'
end

function export.numberAndSprite(id, size)
    local pokemon = assert(data.pokemon[id], 'Неизвестный покемон')
    return pokemon.number, picture(pokemon, size)
end

local function startTable(lines, columns, style, sticky)
    lines[#lines + 1] = '{| class="wikitable sortable"' .. (style and ' style="' .. style .. '"' or '')
    for _, column in ipairs(columns) do
        local numeric = type(column) == 'table'
        local title = numeric and column[1] or column
        local sort = numeric and ' data-sort-type="number"' or ''
        local pin = sticky and ' style="position:sticky;top:0;z-index:1;"' or ''
        lines[#lines + 1] = '! scope="col"' .. sort .. pin .. ' | ' .. title
    end
end

local function appendRow(lines, cells)
    lines[#lines + 1] = '|-\n| ' .. table.concat(cells, '\n| ')
end

local function statRow(pokemon)
    local typeBadges, abilities = {}, {}
    for _, typeId in ipairs(pokemon.types) do
        local badge = data.types[typeId]
        typeBadges[#typeBadges + 1] = '{{Метка покедекса|' .. badge[1] .. '|' .. badge[2] .. '|' ..
            badge[3] .. '|' .. badge[4] .. '|' .. badge[5] .. '}}'
    end
    for _, slot in ipairs(pokemon.abilities) do
        abilities[#abilities + 1] = '[[' .. data.abilityNames[slot[1]] .. ']]' ..
            (slot[2] and ' <small>(скрытая)</small>' or '')
    end
    local total, values = 0, {}
    for _, value in ipairs(pokemon.stats) do
        total = total + value
        values[#values + 1] = value
    end
    values[#values + 1] = total
    return {
        pokemon.number,
        'style="text-align:left;" | <span style="white-space:nowrap;">' .. picture(pokemon, 48) .. '</span>',
        table.concat(typeBadges, '<br />'),
        'style="text-align:left;" | ' .. table.concat(abilities, '<br />'),
        table.concat(values, ' || '),
    }
end

export['покедекс'] = function(frame)
    local region = frame.args[1]
    local regional = region and region ~= ''
    local order = regional and assert(data.starters[region], 'Неизвестный регион') or data.order
    local columns = {{number}, 'Покемон', 'Тип', 'Способности'}
    for _, stat in ipairs(stats) do
        columns[#columns + 1] = {stat}
    end
    columns[#columns + 1] = {sumLabel}
    local lines = {}
    local style = regional and 'text-align:center;margin:0;font-size:93%;' or 'text-align:center;'
    startTable(lines, columns, style, not regional)
    for _, id in ipairs(order) do
        appendRow(lines, statRow(data.pokemon[id]))
    end
    lines[#lines + 1] = '|}'
    return frame:preprocess(table.concat(lines, '\n'))
end

export['приростEV'] = function(frame)
    local columns = {{number}, 'Покемон', {'HP'}}
    for index = 2, #stats do
        columns[#columns + 1] = {stats[index]}
    end
    local lines = {}
    startTable(lines, columns, 'text-align:center;')
    for _, id in ipairs(data.evOrder) do
        local pokemon = data.pokemon[id]
        local cells = {
            pokemon.number,
            'data-sort-value="' .. pokemon.name .. '" style="text-align:left;white-space:nowrap;" | ' ..
                picture(pokemon, 48, 'decorative'),
        }
        for _, value in ipairs(pokemon.evs) do
            cells[#cells + 1] = value == 0 and 'data-sort-value="0" |' or value
        end
        appendRow(lines, cells)
    end
    lines[#lines + 1] = '|}'
    return frame:preprocess(table.concat(lines, '\n'))
end

export['соСпособностью'] = function(frame)
    local ability = frame.args[1]
    assert(data.abilityNames[ability], 'Неизвестная способность')
    local lines = {}
    startTable(lines, {'№', 'Покемон', 'Слот способности'})
    local found = false
    for _, id in ipairs(data.order) do
        local pokemon = data.pokemon[id]
        for _, slot in ipairs(pokemon.abilities) do
            if slot[1] == ability then
                found = true
                appendRow(lines, {pokemon.number, picture(pokemon, 32), slot[2] and 'Скрытая' or 'Обычная'})
            end
        end
    end
    if not found then
        return 'В текущем покедексе нет покемонов с этой природной способностью.'
    end
    lines[#lines + 1] = '|}'
    return frame:preprocess(table.concat(lines, '\n'))
end

export['встречи'] = function(frame)
    local allEncounters = mw.loadData('Module:Покемоны/встречи')
    local region = assert(allEncounters[frame.args[1]], 'Неизвестный регион')
    local place = assert(region[frame.args[2]], 'Нет встреч для этой локации')
    local lines = {}
    for _, section in ipairs(place.sections) do
        local heading = section.headingLink and ('[[' .. section.heading .. ']]') or section.heading
        lines[#lines + 1] = '=== ' .. heading .. ' ==='
        local columns = {'Покемон', 'Уровни', 'Редкость', 'Время (МСК)'}
        if section.showAreas then
            columns[#columns + 1] = 'Где встречается'
        end
        startTable(lines, columns)
        for _, row in ipairs(section.rows) do
            local pokemon = data.pokemon[row.pokemon]
            local label = picture(pokemon, 36, 'encounter')
            local cells = {label, row.levels, row.rarity, row.time}
            if section.showAreas then
                local areas = {}
                for _, area in ipairs(row.areas) do
                    areas[#areas + 1] = area[3] and ('[[#loc-' .. area[1] .. '|' .. area[2] .. ']]') or area[2]
                end
                cells[#cells + 1] = row.everywhere and 'Все части' or table.concat(areas, ', ')
            end
            appendRow(lines, cells)
        end
        lines[#lines + 1] = '|}'
        lines[#lines + 1] = ''
    end
    return frame:preprocess(table.concat(lines, '\n'))
end

return export