Модуль:Покемоны
Материал из Chatmon Wiki
Для документации этого модуля может быть создана страница Модуль:Покемоны/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, mode, label, withNumber)
local title = pokemon.name
local attrs = mode and 'link=|alt=' or ('alt=' .. title .. '|link=' .. title)
label = label or title
if withNumber then label = '№' .. pokemon.number .. ' ' .. label end
local name = mode == 'self' and label or
('[[' .. title .. (label ~= title and ('|' .. label) or '') .. ']]')
return '[[Файл:' .. pokemon.sprite .. '|{{Размер спрайта покемона}}|middle|' .. attrs .. ']] ' .. name
end
local function reference(pokemon, label, selfReference)
return picture(pokemon, selfReference and 'self' or nil, label, true)
end
function export.reference(id, selfReference)
return '<span class="chatmon-pokemon-reference" style="white-space:nowrap;">' ..
reference(assert(data.pokemon[id], 'Неизвестный покемон'), nil, selfReference) .. '</span>'
end
export['упоминание'] = function(frame)
local pokemon = assert(data.pokemon[frame.args[1]], 'Неизвестный покемон')
local part = frame.args[3]
if part == 'номер' then return pokemon.number end
local label = mw.text.trim(frame.args[2] or '')
if label == '' then label = nil end
local selfReference = mw.title.getCurrentTitle().prefixedText == pokemon.name
local content = part == 'имя' and picture(pokemon, selfReference and 'self' or nil, label) or
reference(pokemon, label, selfReference)
return frame:preprocess('<span class="chatmon-pokemon-reference" style="white-space:nowrap;">' .. content .. '</span>')
end
function export.numberAndSprite(id)
local pokemon = assert(data.pokemon[id], 'Неизвестный покемон')
return pokemon.number, picture(pokemon)
end
export['вКоманде'] = function(frame)
local pokemon = assert(data.pokemon[frame.args[1]], 'Неизвестный покемон')
local level = assert(tonumber(frame.args[2]), 'Не указан уровень покемона')
return frame:preprocess('<span style="white-space:nowrap;">' ..
reference(pokemon) .. ' (' .. level .. ' ур.)</span>')
end
local function startTable(lines, columns, style, sticky, className)
local classes = 'wikitable sortable' .. (className and ' ' .. className or '')
lines[#lines + 1] = '{| class="' .. classes .. '"' .. (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[2] .. '}}'
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) .. '</span>',
table.concat(typeBadges, '<br />'),
'style="text-align:left;" | ' .. table.concat(abilities, '<br />'),
table.concat(values, ' || '),
}
end
local function hasType(pokemon, kind)
for _, typeId in ipairs(pokemon.types) do
if typeId == kind then return true end
end
return false
end
local function renderPokedex(frame, region, kind)
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, kind and 'chatmon-type-pokemon')
for _, id in ipairs(order) do
local pokemon = data.pokemon[id]
if not kind or hasType(pokemon, kind) then
appendRow(lines, statRow(pokemon))
end
end
lines[#lines + 1] = '|}'
return frame:preprocess(table.concat(lines, '\n'))
end
export['покедекс'] = function(frame)
return renderPokedex(frame, frame.args[1])
end
function export.typeDex(frame, kind)
assert(data.types[kind], 'Неизвестный тип')
return renderPokedex(frame, nil, kind)
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, '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), 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 = {{number}, 'Покемон', 'Уровни', 'Редкость', 'Время (МСК)'}
if section.showAreas then
columns[#columns + 1] = 'Где встречается'
end
startTable(lines, columns)
for _, row in ipairs(section.rows) do
local dexNumber, label = export.numberAndSprite(row.pokemon)
local cells = {dexNumber, 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
