Модуль:Эффекты

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

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

local effects = mw.loadData('Module:Эффекты/данные')
local export = {}

local function effect(frame, entries)
    local record = assert(entries[frame.args[1]], 'Неизвестный эффект')
    return frame:preprocess(record.text)
end

function export.moveEffect(frame)
    return effect(frame, effects.moves)
end

function export.abilityEffect(frame)
    return effect(frame, effects.abilities)
end

local function moves()
    return mw.loadData('Module:Приёмы/данные').moves
end

local function numeric(value)
    local sortValue = tonumber((mw.ustring.gsub(value, '[^%d%.%-]', '')))
    if sortValue then
        return 'data-sort-value="' .. sortValue .. '" | ' .. value
    end
    return value
end

function export.moveHeadings()
    return {'Атака', 'Тип', 'Категория', {'Сила'}, {'PP'}, {'Точность'}, 'Эффект'}
end

function export.moveRow(id)
    local move = assert(moves()[id], 'Неизвестная атака')
    local record = assert(effects.moves[id], 'Неизвестный эффект')
    return {move[1], move[2], move[3], numeric(move[4]), numeric(move[5]), numeric(move[6]), record.text}
end

function export.moveCard(frame)
    local id = frame.args[1]
    local record = assert(effects.moves[id], 'Неизвестная атака')
    local move = assert(moves()[id], 'Неизвестная атака')
    local headings = {'Тип', 'Категория', 'Сила', 'Точность', 'PP', 'Приоритет', 'Цель'}
    local values = {move[2], move[3], move[4], move[6], move[5], record.priority, record.target}
    local lines = {'{| class="wikitable" style="margin-top:0;"'}
    for index, label in ipairs(headings) do
        lines[#lines + 1] = '|-\n! scope="row" | ' .. label .. '\n| ' .. values[index]
    end
    lines[#lines + 1] = '|}'
    return frame:preprocess(table.concat(lines, '\n'))
end

local function ordered(entries)
    local ids, labels = {}, {}
    for id, record in pairs(entries) do
        ids[#ids + 1] = id
        labels[id] = record.title:gsub('ё', 'е'):gsub('Ё', 'Е')
    end
    table.sort(ids, function(a, b)
        if labels[a] == labels[b] then return entries[a].title < entries[b].title end
        return labels[a] < labels[b]
    end)
    return ids
end

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

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

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

function export.moveList(frame)
    local lines = tableStart(export.moveHeadings())
    for _, id in ipairs(ordered(effects.moves)) do
        addRow(lines, export.moveRow(id))
    end
    return finish(frame, lines)
end

function export.abilityList(frame)
    local lines = tableStart({'Способность', 'Эффект'})
    for _, id in ipairs(ordered(effects.abilities)) do
        local record = effects.abilities[id]
        addRow(lines, {'[[' .. record.title .. ']]', record.text})
    end
    return finish(frame, lines)
end

return export