Модуль:Типы

Материал из Chatmon Wiki

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

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

local function badge(id)
    local kind = dexData.types[id]
    return '{{Метка покедекса||' .. kind[2] .. '|' .. kind[3] .. '}}'
end

local function tableStart(class, headings)
    local lines = {'{| class="wikitable sortable ' .. class .. '"'}
    for _, heading in ipairs(headings) do
        local numeric = type(heading) == 'table'
        local label = numeric and heading[1] or heading
        local attr = numeric and ' data-sort-type="number"' or ''
        lines[#lines + 1] = '! scope="col"' .. attr .. ' | ' .. label
    end
    return lines
end

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

local function render(frame, lines)
    lines[#lines + 1] = '|}'
    return frame:preprocess(table.concat(lines, '\n'))
end

local function hasType(types, kind)
    for _, id in ipairs(types) do
        if id == kind then return true end
    end
    return false
end

export['покемоны'] = function(frame)
    local kind = frame.args[1]
    assert(dexData.types[kind], 'Неизвестный тип')
    local lines = tableStart('chatmon-type-pokemon', {{'№'}, 'Покемон', 'Тип'})
    for _, id in ipairs(dexData.order) do
        local types = dexData.pokemon[id].types
        if hasType(types, kind) then
            local number, label = dex.numberAndSprite(id)
            local badges = {}
            for _, typeId in ipairs(types) do
                local markup = badge(typeId)
                if typeId ~= kind then
                    markup = '[[' .. dexData.types[typeId][2] .. ' тип|' .. markup .. ']]'
                end
                badges[#badges + 1] = markup
            end
            addRow(lines, {number, label, table.concat(badges, ' ')})
        end
    end
    return render(frame, lines)
end

export['атаки'] = function(frame)
    local kind = assert(dexData.types[frame.args[1]], 'Неизвестный тип')
    local effects = require('Module:Эффекты')
    local metadata = mw.loadData('Module:Приёмы/данные').moves
    local details = mw.loadData('Module:Эффекты/данные').moves
    local headings = effects.moveHeadings()
    table.remove(headings, 2)
    local lines = tableStart('chatmon-type-moves', headings)
    local ids, names = {}, {}
    for id, description in pairs(details) do
        local label = metadata[id][2]:match('{{Метка покедекса|[^|]*|([^|]+)|')
        if label == kind[2] then
            ids[#ids + 1] = id
            names[id] = description.title:gsub('ё', 'е'):gsub('Ё', 'Е')
        end
    end
    table.sort(ids, function(a, b)
        if names[a] == names[b] then return details[a].title < details[b].title end
        return names[a] < names[b]
    end)
    for _, id in ipairs(ids) do
        local row = effects.moveRow(id)
        table.remove(row, 2)
        addRow(lines, row)
    end
    return render(frame, lines)
end

return export