Module:Learnsets: Difference between revisions

From Pokemon Revolution Online Wiki
Jump to navigation Jump to search
Cae (talk | contribs)
No edit summary
Cae (talk | contribs)
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


local function getArgs(frame)
local DATA_SOURCE = 'data'
    local args = {}
local DATA_FILE = 'PokemonRawList.csv'
    local parent = frame:getParent()
local DATA_FORMAT = 'CSV with header'
   
    if parent then
        for k, v in pairs(parent.args) do
            if v ~= '' then
                args[k] = v
            end
        end
    end
   
    for k, v in pairs(frame.args) do
        if v ~= '' then
            args[k] = v
        end
    end
   
    return args
end


local moveDataCache = nil
local COLUMNS = {
number = 'Pokedex Number',
name = 'Name',
type1 = 'Type1',
type2 = 'Type2',
ability1 = 'Ability 1',
ability2 = 'Ability 2',
ha = 'Hidden Ability',
generation = 'Generation',
hp = 'HP',
atk = 'Attack',
def = 'Defense',
spatk = 'Special Attack',
spdef = 'Special Defense',
spd = 'Speed',
catch = 'Catch Rate',
evhp = 'EVHP',
evatk = 'EVATK',
evdef = 'EVDEF',
evspa = 'EVSPA',
evspd = 'EVSPD',
evsp = 'EVSP',
male = 'Male Ratio',
height = 'Height',
weight = 'Weight',
npc = 'NPC',
exp = 'Base Experience',
rarity = 'Tier',
}


local function blankToNil(v)
local GEN_NUMERALS = { 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX' }
    if v == nil or v == '' then
        return nil
    end
    return v
end


local function loadMoveData()
local STATS = {
    if moveDataCache then
{ key = 'hp', label = 'HP', template = 'HP' },
        return moveDataCache
{ key = 'atk', label = 'Atk', template = 'Attack' },
    end
{ key = 'def', label = 'Def', template = 'Defense' },
   
{ key = 'spatk', label = 'Sp.Atk', template = 'Special Attack' },
    local csvData = mw.ext.externalData.getExternalData({
{ key = 'spdef', label = 'Sp.Def', template = 'Special Defense' },
        url = 'https://wiki.pokemonrevolution.net/index.php?title=Special:GetData/RawMovesList',
{ key = 'spd', label = 'Spd', template = 'Speed' },
        format = 'CSV with header',
}
        data = {
            Name = 'Name',
            Type = 'Type',
            Category = 'Category',
            Power = 'Power',
            Accuracy = 'Accuracy',
            PP = 'PP',
            Broken = 'Broken',
            Typeless = 'Typeless',
            Priority = 'Priority',
            TM = 'TM',
            ['Move Tutor'] = 'Move Tutor',
            Egg = 'Egg',
        },
    })


    moveDataCache = {}
local EV_KEYS = { 'evhp', 'evatk', 'evdef', 'evspa', 'evspd', 'evsp' }
    if csvData then
        for _, row in ipairs(csvData) do
            moveDataCache[row.Name] = {
                type = blankToNil(row.Type) or 'Normal',
                category = blankToNil(row.Category) or 'Status',
                power = blankToNil(row.Power),
                accuracy = blankToNil(row.Accuracy),
                pp = blankToNil(row.PP),
                broken = row.Broken or 'N',
                typeless = row.Typeless or 'N',
                priority = blankToNil(row.Priority) or '0',
                tm = blankToNil(row.TM),
                move_tutor = blankToNil(row['Move Tutor']),
                egg = blankToNil(row.Egg)
            }
        end
    end


    return moveDataCache
local DATA_SPEC
do
local parts = {}
for key, header in pairs(COLUMNS) do
parts[#parts + 1] = key .. '=' .. header
end
DATA_SPEC = table.concat(parts, ',')
end
end


local function loadLearnset(pokemonName)
local function nonEmpty(s)
    local jsonPath = '$[' .. mw.text.jsonEncode(pokemonName) .. ']'
if s == nil then return nil end
    local ok, result, errors = pcall(function()
s = mw.text.trim(tostring(s))
        return mw.ext.externalData.getFileData({
if s == '' then return nil end
            source = 'data',
return s
            ['file name'] = 'Learnsets.json',
            format = 'JSON',
            ['use jsonpath'] = true,
            data = { poke = jsonPath },
        })
    end)
    if not ok then
        return nil, 'Lua error: ' .. tostring(result)
    end
    if errors and #errors > 0 then
        return nil, 'ExternalData error: ' .. table.concat(errors, ' | ')
    end
    local raw = result and result.poke
    if type(raw) ~= 'table' then
        return nil, 'No learnset data found for ' .. pokemonName
    end
    return raw, nil
end
end


local learnsetCache = {}
local function templateArgs(frame)
local learnsetErrorCache = {}
local parent = frame:getParent()
return (parent and parent.args) or frame.args
end


local function getCachedLearnset(pokemonName)
local function padNumber(n)
    if learnsetCache[pokemonName] == nil and learnsetErrorCache[pokemonName] == nil then
n = tonumber(n) or 0
        learnsetCache[pokemonName], learnsetErrorCache[pokemonName] = loadLearnset(pokemonName)
return string.format('%03d', n)
    end
    return learnsetCache[pokemonName], learnsetErrorCache[pokemonName]
end
end


local pokemonTypeCache = nil
local function fetchRow(filterColumn, filterValue)
 
local ok, values, errors = pcall(function()
local function loadPokemonTypes()
return mw.ext.externalData.getFileData{
    if pokemonTypeCache then return pokemonTypeCache end
source = DATA_SOURCE,
 
['file name'] = DATA_FILE,
    local csvData = mw.ext.externalData.getExternalData({
format = DATA_FORMAT,
        url    = 'https://wiki.pokemonrevolution.net/index.php?title=Special:GetData/PokemonRawList',
filters = filterColumn .. '=' .. filterValue,
        format = 'CSV with header',
data = DATA_SPEC,
        data = {
}
            Name = 'Name',
end)
            Type1 = 'Type1',
if not ok then
            Type2 = 'Type2',
error('Module:Pokemon: External Data call failed (' .. tostring(values) ..
        },
'). Is Legacy mode enabled (mw.ext.externalData.getFileData)?')
    })
end
 
if not values or values.name == nil then
    pokemonTypeCache = {}
return nil
    if csvData then
end
        for _, row in ipairs(csvData) do
return values
            pokemonTypeCache[row.Name] = {
                type1 = row.Type1 or '',
                type2 = row.Type2 or '',
            }
        end
    end
    return pokemonTypeCache
end
end


local typeColorCache = {}
local function byName(name)
local function typeColor(frame, pokeType)
return fetchRow('Name', name)
    pokeType = (pokeType and pokeType ~= '') and pokeType or 'Normal'
    if not typeColorCache[pokeType] then
        typeColorCache[pokeType] = {
            light = frame:expandTemplate{ title = pokeType .. ' color light' },
            base = frame:expandTemplate{ title = pokeType .. ' color' },
            dark = frame:expandTemplate{ title = pokeType .. ' color dark' },
        }
    end
    return typeColorCache[pokeType]
end
end


local categoryColorCache = {}
local function byNumber(number)
local function categoryColor(frame, category)
return fetchRow('Pokedex Number', number)
    category = (category and category ~= '') and category or 'Status'
    if not categoryColorCache[category] then
        categoryColorCache[category] = {
            base = frame:expandTemplate{ title = category .. ' color' },
            dark = frame:expandTemplate{ title = category .. ' color dark' },
        }
    end
    return categoryColorCache[category]
end
end


local function isSTAB(moveType, primaryType, secondaryType)
local function typeColors(frame, pokeType)
    return moveType == primaryType or moveType == secondaryType
return {
light = frame:expandTemplate{ title = pokeType .. ' color light' },
base = frame:expandTemplate{ title = pokeType .. ' color' },
dark = frame:expandTemplate{ title = pokeType .. ' color dark' },
}
end
end


local function formatItemNumber(label, num)
local function statColors(frame, statTemplate)
    num = tonumber(num)
return {
    if not num then
base = frame:expandTemplate{ title = statTemplate .. ' color' },
        return ''
dark = frame:expandTemplate{ title = statTemplate .. ' color dark' },
    end
}
    return label .. string.format('%02d', num)
end
end


function p.renderLevelUp(frame)
local function navTableHtml(frame, data, colors, pageName)
    local args = getArgs(frame)
local number = tonumber(data.number)
    local pokemonName = args[1] or args.pokemon
    local primaryType = args.type1 or ""
    local secondaryType = args.type2 or ""
   
    local pokemon, lsErr = getCachedLearnset(pokemonName)
    local moveData = loadMoveData()


    if not pokemon then
local prevCell = ''
        return "<span style='color:red'>Data load error: " .. (lsErr or 'unknown') .. "</span>"
if number and number > 1 then
    end
local prev = byNumber(number - 1)
if prev then
prevCell = '&nbsp;[[File:' .. (number - 1) .. 'Icon.png|' .. prev.name .. '|link=' .. prev.name ..
']]&nbsp;&nbsp;&nbsp;#' .. padNumber(prev.number)
end
end


    local moves = pokemon.level_up or {}
local nextCell = ''
    local colors = typeColor(frame, primaryType)
if number then
local nxt = byNumber(number + 1)
if nxt then
nextCell = '#' .. padNumber(nxt.number) .. '&nbsp;&nbsp;&nbsp;[[File:' .. (number + 1) ..
'Icon.png|' .. nxt.name .. '|link=' .. nxt.name .. ']]'
end
end


    local html = mw.html.create('div')
return '<table style="margin-left: auto; margin-right: auto; margin-bottom: 15px; height: 60px; ' ..
        :css('width', '77%')
'border: 4px #' .. colors.dark .. ' solid; font-weight: bold; width: 65%; color: #' .. colors.dark ..
        :css('margin', 'auto')
'; border-radius: 20px; padding: 3px; background: #' .. colors.base .. ';">' ..
    local table = html:tag('table')
'<tr>' ..
        :addClass('sortable')
'<td style="text-align: left; width: 36%; background: #' .. colors.light ..
        :css('width', '100%')
'; border-radius: 15px 1px 1px 15px; border: 1px solid #' .. colors.dark .. ';">' .. prevCell .. '</td>' ..
        :css('text-align', 'center')
'<td style="text-align: center; width: 28%; background: #' .. colors.light ..
        :css('border-radius', '15px 15px 0 0')
'; border: 1px solid #' .. colors.dark .. ';">#' .. padNumber(number) .. '&nbsp;&nbsp;-&nbsp;&nbsp;' ..
        :css('border', '3px solid #' .. colors.dark)
pageName .. '</td>' ..
        :css('border-bottom', 'none')
'<td style="text-align: right; width: 36%; padding-right: 10px; background: #' .. colors.light ..
        :css('background-color', '#' .. colors.base)
'; border: 1px solid #' .. colors.dark .. '; border-radius: 1px 15px 15px 1px;">' .. nextCell .. '</td>' ..
        :css('padding', '7px')
'</tr></table>'
end


        local header = table:tag('tr')
function p.navTable(frame)
        :css('background-color', '#' .. colors.light)
local args = templateArgs(frame)
        :css('color', '#' .. colors.dark)
local pageName = mw.title.getCurrentTitle().text
    header:tag('th'):wikitext('Level'):css('width', '10%'):css('border-radius', '15px 1px 1px 1px')
local name = nonEmpty(args.Name) or pageName
    header:tag('th'):wikitext('Move'):css('width', '15%')
    header:tag('th'):wikitext('Type'):css('width', '17%')
    header:tag('th'):wikitext('Category'):css('width', '16%')
    header:tag('th'):wikitext('Base Power'):css('width', '17%')
    header:tag('th'):wikitext('Accuracy'):css('width', '15%')
    header:tag('th'):wikitext('PP'):css('width', '10%'):css('border-radius', '1px 15px 1px 1px')


        for _, move in ipairs(moves) do
local data = byName(name)
        local moveName = move.move
if not data then
        local level = move.level
return '<strong class="error">PokeNavTable: no data found for "' .. name .. '"</strong>'
        local moveInfo = moveData[moveName] or {}
end


        local row = table:tag('tr'):css('background-color', '#FFFFFF')
return navTableHtml(frame, data, typeColors(frame, data.type1), pageName)
        row:tag('td'):wikitext(tostring(level))
end


        local moveLink = '[[' .. moveName .. ']]'
local function infoCell(colors, labelHtml, valueHtml)
        if isSTAB(moveInfo.type, primaryType, secondaryType) and moveInfo.category ~= "Status" then
return '<td style="border: 3px solid #' .. colors.dark .. '; background-color: #' .. colors.light ..
            moveLink = "'''" .. moveLink .. "'''"
'; border-radius: 15px; padding: 5px;">' ..
        end
'<p style="margin-bottom: 7px; font-weight: bold; color: #' .. colors.dark .. ';">' .. labelHtml .. '</p>' ..
        if moveInfo.broken == "Y" then
'<span style="border-radius: 20px; padding: 6px; border: 4px solid #' .. colors.dark ..
            moveLink = "''" .. moveLink .. "''"
'; background-color: #FFFFFF;">&nbsp;&nbsp;' .. valueHtml .. '&nbsp;&nbsp;</span></td>'
        end
end
        row:tag('td'):wikitext(moveLink)


        local moveTypeColors = typeColor(frame, moveInfo.type)
local function abilityCell(colors, label, ability, widthPercent)
        row:tag('td')
if not ability then return '' end
            :css('background-color', '#' .. moveTypeColors.base)
local style = 'border-radius: 20px; padding: 6px; border: 4px solid #' .. colors.dark ..
            :css('border', '2px solid #' .. moveTypeColors.dark)
'; background-color: #FFFFFF;'
            :wikitext('[[' .. (moveInfo.type or 'Normal') .. '_(type)|<span style="color:#FFFFFF;">' .. (moveInfo.type or 'Normal') .. '</span>]]')
if widthPercent then
style = 'width: ' .. widthPercent .. '%; ' .. style
end
return '<td style="' .. style .. '"><b>' .. label .. '</b><br>[[' .. ability .. '_(ability)|' .. ability ..
']]</td>'
end


        local moveCatColors = categoryColor(frame, moveInfo.category)
local function abilitiesRow(colors, data)
        row:tag('td')
local entries = {
            :css('background-color', '#' .. moveCatColors.base)
{ 'Ability 1', nonEmpty(data.ability1) },
            :css('border', '2px solid #' .. moveCatColors.dark)
{ 'Ability 2', nonEmpty(data.ability2) },
            :wikitext('[[' .. (moveInfo.category or 'Status') .. ' moves|<span style="color:#FFFFFF;">' .. (moveInfo.category or 'Status') .. '</span>]]')
{ 'Hidden Ability', nonEmpty(data.ha) },
}
local present = {}
for _, entry in ipairs(entries) do
if entry[2] then present[#present + 1] = entry end
end
local widthPercent = (#present == 2) and 50 or nil
local cells = {}
for _, entry in ipairs(present) do
cells[#cells + 1] = abilityCell(colors, entry[1], entry[2], widthPercent)
end
return table.concat(cells)
end


        row:tag('td'):wikitext(moveInfo.power or '—')
local function statValueCell(frame, stat, value)
        row:tag('td'):wikitext(moveInfo.accuracy or '—')
local colors = statColors(frame, stat.template)
        row:tag('td'):wikitext(moveInfo.pp or '—')
return '<td style="border: 4px solid #' .. colors.dark .. '; background-color: #' .. colors.base ..
    end
'; border-radius: 15px; padding: 5px; font-size: 11px;">\'\'\'' .. stat.label .. '\'\'\'<br>' ..
 
value .. '</td>'
    html:tag('div')
        :css('text-align', 'left')
        :css('background-color', '#' .. colors.light)
        :css('padding', '10px')
        :css('border', '3px solid #' .. colors.dark)
        :css('border-top', 'none')
        :css('border-radius', '0 0 15px 15px')
        :wikitext([=[
* '''Emboldened''' moves denote this Pokémon avails from a STAB bonus for it.
* ''Italicized'' moves are [[Broken_Moves_%26_Abilities|broken]]; see their individual pages for more information.
* All moves subsumed on this list are relearnable via the [[Move Relearner]]s if lost.
]=])
   
    return tostring(html) .. '<br clear="all">'
end
end


function p.renderTMsHMs(frame)
local function genderRatioHtml(male)
    local args = getArgs(frame)
if male == 'Genderless' then
    local pokemonName = args[1] or args.pokemon
return '<span style="color:#000000; background-color: #FFFFFF; border-radius: 15px; ' ..
    local primaryType = args.type1 or ""
'border: 4px solid #000000; padding: 5px;">&nbsp;&nbsp;Genderless&nbsp;&nbsp;</span>'
    local secondaryType = args.type2 or ""
end
   
local m = tonumber(male) or 100
    local pokemon = getCachedLearnset(pokemonName)
if m >= 100 then
    local moveData = loadMoveData()
return '<span style="color:#0000FF; border-radius: 15px; background-color: #FFFFFF; padding: 5px; ' ..
 
'border: 4px solid #0000FF;">&nbsp;&nbsp;100%&nbsp;&nbsp;</span>'
    if not pokemon then return '' end
elseif m <= 0 then
 
return '<span style="color:#FF6060; background-color: #FFFFFF; border-radius: 15px; ' ..
    local tms = pokemon.tms or {}
'border: 4px solid #FF6060; padding: 5px;">&nbsp;&nbsp;100%&nbsp;&nbsp;</span>'
    local hms = pokemon.hms or {}
else
    local colors = typeColor(frame, primaryType)
return '<span style="color:#0000FF; border-radius: 15px; background-color: #FFFFFF; padding: 5px; ' ..
 
'border: 4px solid #0000FF;">&nbsp;&nbsp;' .. m .. '%&nbsp;&nbsp;</span>' ..
    local html = mw.html.create('div')
'<span style="color:#FF6060; background-color: #FFFFFF; border-radius: 15px; ' ..
        :css('width', '77%')
'border: 4px solid #FF6060; padding: 5px;">&nbsp;&nbsp;' .. (100 - m) .. '%&nbsp;&nbsp;</span>'
        :css('margin', 'auto')
end
    local table = html:tag('table')
        :addClass('sortable')
        :css('width', '100%')
        :css('text-align', 'center')
        :css('border-radius', '25px 25px 0 0')
        :css('border', '3px solid #' .. colors.dark)
        :css('border-bottom', 'none')
        :css('background-color', '#' .. colors.base)
        :css('padding', '7px')
 
    local header = table:tag('tr')
        :css('background-color', '#' .. colors.light)
        :css('color', '#' .. colors.dark)
    header:tag('th'):attr('colspan', '2'):wikitext('TM'):css('width', '10%'):css('border-top-left-radius', '25px')
    header:tag('th'):wikitext('Move'):css('width', '15%')
    header:tag('th'):wikitext('Type'):css('width', '17%')
    header:tag('th'):wikitext('Category'):css('width', '16%')
    header:tag('th'):wikitext('Base Power'):css('width', '17%')
    header:tag('th'):wikitext('Accuracy'):css('width', '15%')
    header:tag('th'):wikitext('PP'):css('width', '10%'):css('border-top-right-radius', '25px')
 
    for _, moveName in ipairs(tms) do
        local moveInfo = moveData[moveName] or {}
        local row = table:tag('tr'):css('background-color', '#FFFFFF')
 
        row:tag('td'):attr('colspan', '2'):wikitext(formatItemNumber('TM', moveInfo.tm))
 
        local moveLink = '[[' .. moveName .. ']]'
        if isSTAB(moveInfo.type, primaryType, secondaryType) and moveInfo.category ~= "Status" then
            moveLink = "'''" .. moveLink .. "'''"
        end
        if moveInfo.broken == "Y" then
            moveLink = "''" .. moveLink .. "''"
        end
        row:tag('td'):wikitext(moveLink)
 
        local moveTypeColors = typeColor(frame, moveInfo.type)
        row:tag('td')
            :css('background-color', '#' .. moveTypeColors.base)
            :css('border', '2px solid #' .. moveTypeColors.dark)
            :wikitext('[[' .. (moveInfo.type or 'Normal') .. '_(type)|<span style="color:#FFFFFF;">' .. (moveInfo.type or 'Normal') .. '</span>]]')
 
        local moveCatColors = categoryColor(frame, moveInfo.category)
        row:tag('td')
            :css('background-color', '#' .. moveCatColors.base)
            :css('border', '2px solid #' .. moveCatColors.dark)
            :wikitext('[[' .. (moveInfo.category or 'Status') .. ' moves|<span style="color:#FFFFFF;">' .. (moveInfo.category or 'Status') .. '</span>]]')
 
        row:tag('td'):wikitext(moveInfo.power or '—')
        row:tag('td'):wikitext(moveInfo.accuracy or '—')
        row:tag('td'):wikitext(moveInfo.pp or '—')
    end
 
    for _, moveName in ipairs(hms) do
        local moveInfo = moveData[moveName] or {}
        local row = table:tag('tr'):css('background-color', '#FFFFCC')
 
        row:tag('td'):attr('colspan', '2'):wikitext(formatItemNumber('HM', moveInfo.tm)):css('font-weight', 'bold')
 
        local moveLink = '[[' .. moveName .. ']]'
        if isSTAB(moveInfo.type, primaryType, secondaryType) and moveInfo.category ~= "Status" then
            moveLink = "'''" .. moveLink .. "'''"
        end
        if moveInfo.broken == "Y" then
            moveLink = "''" .. moveLink .. "''"
        end
        row:tag('td'):wikitext(moveLink)
 
        local moveTypeColors = typeColor(frame, moveInfo.type)
        row:tag('td')
            :css('background-color', '#' .. moveTypeColors.base)
            :css('border', '2px solid #' .. moveTypeColors.dark)
            :wikitext('[[' .. (moveInfo.type or 'Normal') .. '_(type)|<span style="color:#FFFFFF;">' .. (moveInfo.type or 'Normal') .. '</span>]]')
 
        local moveCatColors = categoryColor(frame, moveInfo.category)
        row:tag('td')
            :css('background-color', '#' .. moveCatColors.base)
            :css('border', '2px solid #' .. moveCatColors.dark)
            :wikitext('[[' .. (moveInfo.category or 'Status') .. ' moves|<span style="color:#FFFFFF;">' .. (moveInfo.category or 'Status') .. '</span>]]')
 
        row:tag('td'):wikitext(moveInfo.power or '—')
        row:tag('td'):wikitext(moveInfo.accuracy or '—')
        row:tag('td'):wikitext(moveInfo.pp or '—')
    end
 
    html:tag('div')
        :css('text-align', 'left')
        :css('background-color', '#' .. colors.light)
        :css('padding', '10px')
        :css('border', '3px solid #' .. colors.dark)
        :css('border-top', 'none')
        :css('border-radius', '0 0 25px 25px')
        :wikitext([=[
* '''Emboldened''' moves denote that this Pokémon avails from a STAB bonus for it.
* ''Italicized'' moves are [[Broken_Moves_%26_Abilities|broken]]; see their individual pages for more information.
* Refer to [[TMs and HMs]] for information on each move's obtainability.
]=])
   
    return tostring(html) .. '<br clear="all">'
end
end


function p.renderMoveTutors(frame)
local function categories(data, gen, bstCategory, args)
    local args = getArgs(frame)
local cats = {
    local pokemonName = args[1] or args.pokemon
'[[Category:Pokémon species]]',
    local primaryType = args.type1 or ""
'[[Category:Generation ' .. gen .. ' Pokémon]]',
    local secondaryType = args.type2 or ""
'[[Category:' .. data.type1 .. '-type Pokémon]]',
   
}
    local pokemon = getCachedLearnset(pokemonName)
if nonEmpty(data.type2) then
    local moveData = loadMoveData()
cats[#cats + 1] = '[[Category:' .. data.type2 .. '-type Pokémon]]'
 
end
    if not pokemon then return '' end
cats[#cats + 1] = '[[Category:' .. data.rarity .. ' Pokémon]]'
 
if nonEmpty(data.npc) then
    local moves = pokemon.move_tutors or {}
cats[#cats + 1] = '[[Category:NPC-obtainable Pokémon]]'
   
end
    if #moves == 0 then
if not nonEmpty(args.EvolvesFrom) then
        return ""
cats[#cats + 1] = '[[Category:Base-stage Pokémon]]'
    end
end
   
cats[#cats + 1] = '[[Category:' .. bstCategory .. '-plus-BST Pokémon]]'
    local colors = typeColor(frame, primaryType)
return table.concat(cats, ' ')
 
    local html = mw.html.create('div')
        :css('width', '77%')
        :css('margin', 'auto')
    local table = html:tag('table')
        :addClass('sortable')
        :css('width', '100%')
        :css('text-align', 'center')
        :css('border-radius', '25px 25px 0 0')
        :css('border', '3px solid #' .. colors.dark)
        :css('border-bottom', 'none')
        :css('background-color', '#' .. colors.base)
        :css('padding', '7px')
 
    local header = table:tag('tr')
        :css('background-color', '#' .. colors.light)
        :css('color', '#' .. colors.dark)
    header:tag('th'):wikitext('Move'):css('width', '20%'):css('border-top-left-radius', '25px')
    header:tag('th'):wikitext('Type'):css('width', '17%')
    header:tag('th'):wikitext('Category'):css('width', '16%')
    header:tag('th'):wikitext('Base Power'):css('width', '17%')
    header:tag('th'):wikitext('Accuracy'):css('width', '15%')
    header:tag('th'):wikitext('PP'):css('width', '15%'):css('border-top-right-radius', '25px')
 
    for _, moveName in ipairs(moves) do
        local moveInfo = moveData[moveName] or {}
        local row = table:tag('tr'):css('background-color', '#FFFFFF')
 
        local moveLink = '[[' .. moveName .. ']]'
        if isSTAB(moveInfo.type, primaryType, secondaryType) and moveInfo.category ~= "Status" then
            moveLink = "'''" .. moveLink .. "'''"
        end
        if moveInfo.broken == "Y" then
            moveLink = "''" .. moveLink .. "''"
        end
        row:tag('td'):wikitext(moveLink)
 
        local moveTypeColors = typeColor(frame, moveInfo.type)
        row:tag('td')
            :css('background-color', '#' .. moveTypeColors.base)
            :css('border', '2px solid #' .. moveTypeColors.dark)
            :wikitext('[[' .. (moveInfo.type or 'Normal') .. '_(type)|<span style="color:#FFFFFF;">' .. (moveInfo.type or 'Normal') .. '</span>]]')
 
        local moveCatColors = categoryColor(frame, moveInfo.category)
        row:tag('td')
            :css('background-color', '#' .. moveCatColors.base)
            :css('border', '2px solid #' .. moveCatColors.dark)
            :wikitext('[[' .. (moveInfo.category or 'Status') .. ' moves|<span style="color:#FFFFFF;">' .. (moveInfo.category or 'Status') .. '</span>]]')
 
        row:tag('td'):wikitext(moveInfo.power or '—')
        row:tag('td'):wikitext(moveInfo.accuracy or '—')
        row:tag('td'):wikitext(moveInfo.pp or '—')
    end
 
    html:tag('div')
        :css('text-align', 'left')
        :css('background-color', '#' .. colors.light)
        :css('padding', '10px')
        :css('border', '3px solid #' .. colors.dark)
        :css('border-top', 'none')
        :css('border-radius', '0 0 25px 25px')
        :wikitext([=[
* '''Emboldened''' moves denote that this Pokémon avails from a STAB bonus for it.
* ''Italicized'' moves are [[Broken_Moves_%26_Abilities|broken]]; see their individual pages for more information.
* Refer to the [[Move Tutors]] article for tutorship information for all moves hereon.
]=])
 
    return tostring(html) .. '<br clear="all">'
end
end


function p.renderEggMoves(frame)
function p.infobox(frame)
    local args = getArgs(frame)
local args = templateArgs(frame)
    local pokemonName = args[1] or args.pokemon
local pageName = mw.title.getCurrentTitle().text
    local primaryType = args.type1 or ""
local name = nonEmpty(args.Name) or pageName
    local secondaryType = args.type2 or ""
local displayName = nonEmpty(args.Name) or pageName
   
local width = nonEmpty(args.Width) or '45'
    local pokemon = getCachedLearnset(pokemonName)
    local moveData = loadMoveData()


    if not pokemon then return '' end
local data = byName(name)
if not data then
return '<strong class="error">PokemonInfo: no data found for "' .. name .. '"</strong>'
end


    local moves = pokemon.egg_moves or {}
local colors = typeColors(frame, data.type1)
   
local gen = GEN_NUMERALS[tonumber(data.generation)]
    if #moves == 0 then
        return ""
    end
   
    local colors = typeColor(frame, primaryType)


    local html = mw.html.create('div')
-- Types pill(s).
        :css('width', '77%')
local typesHtml
        :css('margin', 'auto')
if nonEmpty(data.type2) then
    local table = html:tag('table')
local type2Colors = typeColors(frame, data.type2)
        :addClass('sortable')
typesHtml =
        :css('width', '100%')
'<td style="width: 50%; background-color: #' .. colors.dark .. '; border-radius: 25px 1px 1px 25px;">' ..
        :css('text-align', 'center')
'[[' .. data.type1 .. '_(type)|<span style="color: #FFFFFF;">' .. data.type1 .. '</span>]]</td>' ..
        :css('border-radius', '25px 25px 0 0')
'<td style="width: 50%; background-color: #' .. type2Colors.dark .. '; border-radius: 1px 25px 25px 1px;">' ..
        :css('border', '3px solid #' .. colors.dark)
'[[' .. data.type2 .. '_(type)|<span style="color: #FFFFFF;">' .. data.type2 .. '</span>]]</td>'
        :css('border-bottom', 'none')
else
        :css('background-color', '#' .. colors.base)
typesHtml =
        :css('padding', '7px')
'<td style="width:100%; background-color: #' .. colors.dark .. '; border-radius: 25px;">' ..
'[[' .. data.type1 .. '_(type)|<span style="color: #FFFFFF;">' .. data.type1 .. '</span>]]</td>'
end


    local header = table:tag('tr')
-- Base stats
        :css('background-color', '#' .. colors.light)
local statCells, bst = {}, 0
        :css('color', '#' .. colors.dark)
for _, stat in ipairs(STATS) do
    header:tag('th'):wikitext('Move'):css('width', '20%'):css('border-top-left-radius', '25px')
local value = tonumber(data[stat.key]) or 0
    header:tag('th'):wikitext('Type'):css('width', '17%')
bst = bst + value
    header:tag('th'):wikitext('Category'):css('width', '16%')
statCells[#statCells + 1] = statValueCell(frame, stat, value)
    header:tag('th'):wikitext('Base Power'):css('width', '17%')
end
    header:tag('th'):wikitext('Accuracy'):css('width', '15%')
local bstCategory = math.floor(bst / 100 + 0.5) * 100
    header:tag('th'):wikitext('PP'):css('width', '15%'):css('border-top-right-radius', '25px')


    for _, moveName in ipairs(moves) do
-- EV yield.
        local moveInfo = moveData[moveName] or {}
local evCells, evTotal = {}, 0
        local row = table:tag('tr'):css('background-color', '#FFFFFF')
for i, stat in ipairs(STATS) do
 
local ev = tonumber(data[EV_KEYS[i]]) or 0
        local moveLink = '[[' .. moveName .. ']]'
evTotal = evTotal + ev
        if isSTAB(moveInfo.type, primaryType, secondaryType) and moveInfo.category ~= "Status" then
evCells[#evCells + 1] = statValueCell(frame, stat, ev)
            moveLink = "'''" .. moveLink .. "'''"
end
        end
        if moveInfo.broken == "Y" then
            moveLink = "''" .. moveLink .. "''"
        end
        row:tag('td'):wikitext(moveLink)
 
        local moveTypeColors = typeColor(frame, moveInfo.type)
        row:tag('td')
            :css('background-color', '#' .. moveTypeColors.base)
            :css('border', '2px solid #' .. moveTypeColors.dark)
            :wikitext('[[' .. (moveInfo.type or 'Normal') .. '_(type)|<span style="color:#FFFFFF;">' .. (moveInfo.type or 'Normal') .. '</span>]]')
 
        local moveCatColors = categoryColor(frame, moveInfo.category)
        row:tag('td')
            :css('background-color', '#' .. moveCatColors.base)
            :css('border', '2px solid #' .. moveCatColors.dark)
            :wikitext('[[' .. (moveInfo.category or 'Status') .. ' moves|<span style="color:#FFFFFF;">' .. (moveInfo.category or 'Status') .. '</span>]]')
 
        row:tag('td'):wikitext(moveInfo.power or '—')
        row:tag('td'):wikitext(moveInfo.accuracy or '—')
        row:tag('td'):wikitext(moveInfo.pp or '—')
    end
 
    html:tag('div')
        :css('text-align', 'left')
        :css('background-color', '#' .. colors.light)
        :css('padding', '10px')
        :css('border', '3px solid #' .. colors.dark)
        :css('border-top', 'none')
        :css('border-radius', '0 0 25px 25px')
        :wikitext([=[
* '''Emboldened''' moves denote that this Pokémon avails from a STAB bonus for it.
* ''Italicized'' moves are [[Broken_Moves_%26_Abilities|broken]]; see their individual pages for more information.
* For more information on egg-move-tutors, refer to: [[Egg Moves]].
]=])
   
    return tostring(html) .. '<br clear="all">'
end


function p.main(frame)
-- Catch rate.
    local args        = getArgs(frame)
local catchRaw = tonumber(data.catch) or 0
    local pokemonName = args[1] or args.pokemon or ''
local catchPercent = string.format('%.2f', catchRaw / 255 / 3 * 100)


    if pokemonName == '' then
local buf = {}
        return "<span style='color:red'>Usage: {{Learnsets|PokemonName}}</span>"
buf[#buf + 1] = navTableHtml(frame, data, colors, pageName)
    end
buf[#buf + 1] = '<table align="right" class="informational-box" cellpadding="4" cellspacing="4" ' ..
'style="width: ' .. width .. '%; border: 4px solid #' .. colors.dark .. '; background-color: #' ..
colors.base .. '; padding: 4px; border-radius: 15px;">'


    local primaryType  = args.type1 or ''
-- Header: dex number + icon, name.
    local secondaryType = args.type2 or ''
buf[#buf + 1] = '<tr><td style="border: 3px solid #' .. colors.dark .. '; background-color: #' ..
colors.light .. '; border-radius: 15px;"><span style="margin-bottom: 7px; font-weight: bold; color:#' ..
colors.dark .. ';">#' .. padNumber(data.number) .. '</span>&nbsp;&nbsp;&nbsp;[[File:' .. data.number ..
'Icon.png]]</td><td style="border: 3px solid #' .. colors.dark .. '; background-color: #' ..
colors.light .. '; border-radius: 15px;"><span style="margin-bottom: 7px; font-weight: bold; color:#' ..
colors.dark .. ';">' .. displayName .. '</span></td></tr>'


    if primaryType == '' then
-- Artwork.
        local typeData = loadPokemonTypes()
buf[#buf + 1] = '<tr><td colspan="3" style="border: 3px solid #' .. colors.dark .. '; background-color: #' ..
        local pt = typeData[pokemonName]
colors.light .. '; border-radius: 15px; padding: 5px;"><div style="border-radius: 15px; ' ..
        if pt then
'text-align: center; padding: 5px; background-color: #FFFFFF;">[[File:' .. data.number .. data.name ..
            primaryType  = pt.type1 or ''
'.png|150px]]</div></td></tr>'
            secondaryType = pt.type2 or ''
        end
    end


    local function callRender(fn)
-- Types + Generation.
        return fn(frame:newChild({
buf[#buf + 1] = '<tr><td style="border: 3px solid #' .. colors.dark .. '; background-color: #' ..
            title = frame:getTitle(),
colors.light .. '; padding: 11px; border-radius: 15px;"><p style="margin-bottom: 7px; font-weight: bold; ' ..
            args  = { pokemonName, type1 = primaryType, type2 = secondaryType },
'color: #' .. colors.dark .. ';">Types</p><table align="center" style="width: 75%; font-weight: bold; ' ..
        }))
'padding: 3px;"><tr>' .. typesHtml .. '</tr></table></td>' ..
    end
'<td style="border: 3px solid #' .. colors.dark .. '; background-color: #' .. colors.light ..
'; border-radius: 15px;"><p style="margin-bottom: 7px; font-weight: bold; color: #' .. colors.dark ..
';">Generation</p><span style="border-radius: 20px; padding: 6px; border: 4px solid #' .. colors.dark ..
'; background-color: #FFFFFF;">&nbsp;&nbsp;[[:Category:Generation ' .. gen .. ' Pokémon|<span style="color:#' ..
colors.dark .. ';">' .. gen .. '</span>]]&nbsp;&nbsp;</span></td></tr>'


    local poke, lsErr = getCachedLearnset(pokemonName)
-- Abilities.
    if not poke then
buf[#buf + 1] = '<tr><td colspan="6" style="border: 3px solid #' .. colors.dark .. '; background-color: #' ..
        return "<span style='color:red'>Data load error: " .. (lsErr or 'unknown') .. "</span>"
colors.light .. '; padding: 11px; border-radius: 15px; width: 100%;"><table width="100%">' ..
    end
'<tr><th colspan="6"><p style="margin-bottom: 7px; font-weight: bold; color:#' .. colors.dark ..
';">[[Abilities|<span style="color:#' .. colors.dark .. ';">Abilities</span>]]</p></th></tr>' ..
'<tr>' .. abilitiesRow(colors, data) .. '</tr></table></td></tr>'


    local sections = {}
-- Rarity + Catch rate.
buf[#buf + 1] = '<tr>' ..
infoCell(colors, '[[List of Pokémon by Rarity Tier|<span style="color:#' .. colors.dark ..
';">Rarity Tier</span>]]', '[[:Category:' .. data.rarity .. ' Pokémon|<span style="color:#' ..
colors.dark .. ';">' .. data.rarity .. '</span>]]') ..
infoCell(colors, '[[Catch Rates|<span style="color:#' .. colors.dark .. ';">Catch rate</span>]]',
data.catch .. '/255 (' .. catchPercent .. '%)') ..
'</tr>'


    if #(poke.level_up or {}) > 0 then
-- Base stats.
        table.insert(sections, "===Level-up===")
buf[#buf + 1] = '<tr><td colspan="6" style="border: 3px solid #' .. colors.dark .. '; background-color: #' ..
        table.insert(sections, callRender(p.renderLevelUp))
colors.light .. '; border-radius: 15px; padding: 5px; width: 100%"><table width="100%">' ..
    end
'<tr><th colspan="6"><p style="margin-bottom: 7px; font-weight: bold; color: #' .. colors.dark ..
';">[[Base stats|<span style="margin-bottom: 7px; font-weight: bold; color: #' .. colors.dark ..
';">Base stats</span>]]</p></th></tr><tr>' .. table.concat(statCells) .. '</tr>' ..
'<tr><td colspan="6"><div style="margin: auto; background-color:#FFFFFF; border: 3px solid #' ..
colors.dark .. '; padding: 6px; border-radius: 25px; margin-bottom: 5px; width: 50%;">' ..
'Base-stat total: ' .. bst .. '</div></td></tr></table></td></tr>'


    if #(poke.tms or {}) > 0 or #(poke.hms or {}) > 0 then
-- EV yield.
        table.insert(sections, "===[[TMs and HMs]]===")
buf[#buf + 1] = '<tr><td colspan="6" style="border: 3px solid #' .. colors.dark .. '; background-color: #' ..
        table.insert(sections, callRender(p.renderTMsHMs))
colors.light .. '; border-radius: 15px; padding: 5px; width: 100%"><table width="100%">' ..
    end
'<tr><th colspan="6">[[EVs|<span style="margin-bottom: 7px; font-weight: bold; color: #' .. colors.dark ..
';">EV Yield</span>]]</th></tr><tr>' .. table.concat(evCells) .. '</tr>' ..
'<tr><td colspan="6"><div style="margin: auto; background-color:#FFFFFF; border: 3px solid #' ..
colors.dark .. '; padding: 6px; border-radius: 25px; margin-bottom: 5px; width: 50%;">' ..
'EV-yield total: ' .. evTotal .. '</div></td></tr></table></td></tr>'


    if #(poke.move_tutors or {}) > 0 then
-- Base experience + Gender ratio.
        table.insert(sections, "===[[Move Tutors]]===")
buf[#buf + 1] = '<tr>' ..
        table.insert(sections, callRender(p.renderMoveTutors))
infoCell(colors, '[[Experience System#Base Experience|<span style="color:#' .. colors.dark ..
    end
';">Base Experience</span>]]', data.exp) ..
'<td style="border: 3px solid #' .. colors.dark .. '; background-color: #' .. colors.light ..
'; border-radius: 15px; padding: 5px;"><p style="margin-bottom: 7px; font-weight: bold; color: #' ..
colors.dark .. ';">Gender Ratio</p>&nbsp;&nbsp;' .. genderRatioHtml(data.male) .. '&nbsp;&nbsp;</td>' ..
'</tr>'


    if #(poke.egg_moves or {}) > 0 then
-- Height + Weight.
        table.insert(sections, "===[[Egg Moves]]===")
buf[#buf + 1] = '<tr>' ..
        table.insert(sections, callRender(p.renderEggMoves))
infoCell(colors, 'Height', data.height .. 'm') ..
    end
infoCell(colors, 'Weight', data.weight .. 'kg') ..
'</tr>'


    if #sections == 0 then
buf[#buf + 1] = '</table>'
        return ''
buf[#buf + 1] = categories(data, gen, bstCategory, args)
    end


    return "==Moves==\n" .. table.concat(sections, '\n')
return table.concat(buf)
end
end


return p
return p

Revision as of 19:23, 9 July 2026

Documentation for this module may be created at Module:Learnsets/doc

local p = {}

local DATA_SOURCE = 'data'
local DATA_FILE = 'PokemonRawList.csv'
local DATA_FORMAT = 'CSV with header'

local COLUMNS = {
	number = 'Pokedex Number',
	name = 'Name',
	type1 = 'Type1',
	type2 = 'Type2',
	ability1 = 'Ability 1',
	ability2 = 'Ability 2',
	ha = 'Hidden Ability',
	generation = 'Generation',
	hp = 'HP',
	atk = 'Attack',
	def = 'Defense',
	spatk = 'Special Attack',
	spdef = 'Special Defense',
	spd = 'Speed',
	catch = 'Catch Rate',
	evhp = 'EVHP',
	evatk = 'EVATK',
	evdef = 'EVDEF',
	evspa = 'EVSPA',
	evspd = 'EVSPD',
	evsp = 'EVSP',
	male = 'Male Ratio',
	height = 'Height',
	weight = 'Weight',
	npc = 'NPC',
	exp = 'Base Experience',
	rarity = 'Tier',
}

local GEN_NUMERALS = { 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX' }

local STATS = {
	{ key = 'hp', label = 'HP', template = 'HP' },
	{ key = 'atk', label = 'Atk', template = 'Attack' },
	{ key = 'def', label = 'Def', template = 'Defense' },
	{ key = 'spatk', label = 'Sp.Atk', template = 'Special Attack' },
	{ key = 'spdef', label = 'Sp.Def', template = 'Special Defense' },
	{ key = 'spd', label = 'Spd', template = 'Speed' },
}

local EV_KEYS = { 'evhp', 'evatk', 'evdef', 'evspa', 'evspd', 'evsp' }

local DATA_SPEC
do
	local parts = {}
	for key, header in pairs(COLUMNS) do
		parts[#parts + 1] = key .. '=' .. header
	end
	DATA_SPEC = table.concat(parts, ',')
end

local function nonEmpty(s)
	if s == nil then return nil end
	s = mw.text.trim(tostring(s))
	if s == '' then return nil end
	return s
end

local function templateArgs(frame)
	local parent = frame:getParent()
	return (parent and parent.args) or frame.args
end

local function padNumber(n)
	n = tonumber(n) or 0
	return string.format('%03d', n)
end

local function fetchRow(filterColumn, filterValue)
	local ok, values, errors = pcall(function()
		return mw.ext.externalData.getFileData{
			source = DATA_SOURCE,
			['file name'] = DATA_FILE,
			format = DATA_FORMAT,
			filters = filterColumn .. '=' .. filterValue,
			data = DATA_SPEC,
		}
	end)
	if not ok then
		error('Module:Pokemon: External Data call failed (' .. tostring(values) ..
			'). Is Legacy mode enabled (mw.ext.externalData.getFileData)?')
	end
	if not values or values.name == nil then
		return nil
	end
	return values
end

local function byName(name)
	return fetchRow('Name', name)
end

local function byNumber(number)
	return fetchRow('Pokedex Number', number)
end

local function typeColors(frame, pokeType)
	return {
		light = frame:expandTemplate{ title = pokeType .. ' color light' },
		base = frame:expandTemplate{ title = pokeType .. ' color' },
		dark = frame:expandTemplate{ title = pokeType .. ' color dark' },
	}
end

local function statColors(frame, statTemplate)
	return {
		base = frame:expandTemplate{ title = statTemplate .. ' color' },
		dark = frame:expandTemplate{ title = statTemplate .. ' color dark' },
	}
end

local function navTableHtml(frame, data, colors, pageName)
	local number = tonumber(data.number)

	local prevCell = ''
	if number and number > 1 then
		local prev = byNumber(number - 1)
		if prev then
			prevCell = '&nbsp;[[File:' .. (number - 1) .. 'Icon.png|' .. prev.name .. '|link=' .. prev.name ..
				']]&nbsp;&nbsp;&nbsp;#' .. padNumber(prev.number)
		end
	end

	local nextCell = ''
	if number then
		local nxt = byNumber(number + 1)
		if nxt then
			nextCell = '#' .. padNumber(nxt.number) .. '&nbsp;&nbsp;&nbsp;[[File:' .. (number + 1) ..
				'Icon.png|' .. nxt.name .. '|link=' .. nxt.name .. ']]'
		end
	end

	return '<table style="margin-left: auto; margin-right: auto; margin-bottom: 15px; height: 60px; ' ..
		'border: 4px #' .. colors.dark .. ' solid; font-weight: bold; width: 65%; color: #' .. colors.dark ..
		'; border-radius: 20px; padding: 3px; background: #' .. colors.base .. ';">' ..
		'<tr>' ..
		'<td style="text-align: left; width: 36%; background: #' .. colors.light ..
		'; border-radius: 15px 1px 1px 15px; border: 1px solid #' .. colors.dark .. ';">' .. prevCell .. '</td>' ..
		'<td style="text-align: center; width: 28%; background: #' .. colors.light ..
		'; border: 1px solid #' .. colors.dark .. ';">#' .. padNumber(number) .. '&nbsp;&nbsp;-&nbsp;&nbsp;' ..
		pageName .. '</td>' ..
		'<td style="text-align: right; width: 36%; padding-right: 10px; background: #' .. colors.light ..
		'; border: 1px solid #' .. colors.dark .. '; border-radius: 1px 15px 15px 1px;">' .. nextCell .. '</td>' ..
		'</tr></table>'
end

function p.navTable(frame)
	local args = templateArgs(frame)
	local pageName = mw.title.getCurrentTitle().text
	local name = nonEmpty(args.Name) or pageName

	local data = byName(name)
	if not data then
		return '<strong class="error">PokeNavTable: no data found for "' .. name .. '"</strong>'
	end

	return navTableHtml(frame, data, typeColors(frame, data.type1), pageName)
end

local function infoCell(colors, labelHtml, valueHtml)
	return '<td style="border: 3px solid #' .. colors.dark .. '; background-color: #' .. colors.light ..
		'; border-radius: 15px; padding: 5px;">' ..
		'<p style="margin-bottom: 7px; font-weight: bold; color: #' .. colors.dark .. ';">' .. labelHtml .. '</p>' ..
		'<span style="border-radius: 20px; padding: 6px; border: 4px solid #' .. colors.dark ..
		'; background-color: #FFFFFF;">&nbsp;&nbsp;' .. valueHtml .. '&nbsp;&nbsp;</span></td>'
end

local function abilityCell(colors, label, ability, widthPercent)
	if not ability then return '' end
	local style = 'border-radius: 20px; padding: 6px; border: 4px solid #' .. colors.dark ..
		'; background-color: #FFFFFF;'
	if widthPercent then
		style = 'width: ' .. widthPercent .. '%; ' .. style
	end
	return '<td style="' .. style .. '"><b>' .. label .. '</b><br>[[' .. ability .. '_(ability)|' .. ability ..
		']]</td>'
end

local function abilitiesRow(colors, data)
	local entries = {
		{ 'Ability 1', nonEmpty(data.ability1) },
		{ 'Ability 2', nonEmpty(data.ability2) },
		{ 'Hidden Ability', nonEmpty(data.ha) },
	}
	local present = {}
	for _, entry in ipairs(entries) do
		if entry[2] then present[#present + 1] = entry end
	end
	local widthPercent = (#present == 2) and 50 or nil
	local cells = {}
	for _, entry in ipairs(present) do
		cells[#cells + 1] = abilityCell(colors, entry[1], entry[2], widthPercent)
	end
	return table.concat(cells)
end

local function statValueCell(frame, stat, value)
	local colors = statColors(frame, stat.template)
	return '<td style="border: 4px solid #' .. colors.dark .. '; background-color: #' .. colors.base ..
		'; border-radius: 15px; padding: 5px; font-size: 11px;">\'\'\'' .. stat.label .. '\'\'\'<br>' ..
		value .. '</td>'
end

local function genderRatioHtml(male)
	if male == 'Genderless' then
		return '<span style="color:#000000; background-color: #FFFFFF; border-radius: 15px; ' ..
			'border: 4px solid #000000; padding: 5px;">&nbsp;&nbsp;Genderless&nbsp;&nbsp;</span>'
	end
	local m = tonumber(male) or 100
	if m >= 100 then
		return '<span style="color:#0000FF; border-radius: 15px; background-color: #FFFFFF; padding: 5px; ' ..
			'border: 4px solid #0000FF;">&nbsp;&nbsp;100%&nbsp;&nbsp;</span>'
	elseif m <= 0 then
		return '<span style="color:#FF6060; background-color: #FFFFFF; border-radius: 15px; ' ..
			'border: 4px solid #FF6060; padding: 5px;">&nbsp;&nbsp;100%&nbsp;&nbsp;</span>'
	else
		return '<span style="color:#0000FF; border-radius: 15px; background-color: #FFFFFF; padding: 5px; ' ..
			'border: 4px solid #0000FF;">&nbsp;&nbsp;' .. m .. '%&nbsp;&nbsp;</span>' ..
			'<span style="color:#FF6060; background-color: #FFFFFF; border-radius: 15px; ' ..
			'border: 4px solid #FF6060; padding: 5px;">&nbsp;&nbsp;' .. (100 - m) .. '%&nbsp;&nbsp;</span>'
	end
end

local function categories(data, gen, bstCategory, args)
	local cats = {
		'[[Category:Pokémon species]]',
		'[[Category:Generation ' .. gen .. ' Pokémon]]',
		'[[Category:' .. data.type1 .. '-type Pokémon]]',
	}
	if nonEmpty(data.type2) then
		cats[#cats + 1] = '[[Category:' .. data.type2 .. '-type Pokémon]]'
	end
	cats[#cats + 1] = '[[Category:' .. data.rarity .. ' Pokémon]]'
	if nonEmpty(data.npc) then
		cats[#cats + 1] = '[[Category:NPC-obtainable Pokémon]]'
	end
	if not nonEmpty(args.EvolvesFrom) then
		cats[#cats + 1] = '[[Category:Base-stage Pokémon]]'
	end
	cats[#cats + 1] = '[[Category:' .. bstCategory .. '-plus-BST Pokémon]]'
	return table.concat(cats, ' ')
end

function p.infobox(frame)
	local args = templateArgs(frame)
	local pageName = mw.title.getCurrentTitle().text
	local name = nonEmpty(args.Name) or pageName
	local displayName = nonEmpty(args.Name) or pageName
	local width = nonEmpty(args.Width) or '45'

	local data = byName(name)
	if not data then
		return '<strong class="error">PokemonInfo: no data found for "' .. name .. '"</strong>'
	end

	local colors = typeColors(frame, data.type1)
	local gen = GEN_NUMERALS[tonumber(data.generation)]

	-- Types pill(s).
	local typesHtml
	if nonEmpty(data.type2) then
		local type2Colors = typeColors(frame, data.type2)
		typesHtml =
			'<td style="width: 50%; background-color: #' .. colors.dark .. '; border-radius: 25px 1px 1px 25px;">' ..
			'[[' .. data.type1 .. '_(type)|<span style="color: #FFFFFF;">' .. data.type1 .. '</span>]]</td>' ..
			'<td style="width: 50%; background-color: #' .. type2Colors.dark .. '; border-radius: 1px 25px 25px 1px;">' ..
			'[[' .. data.type2 .. '_(type)|<span style="color: #FFFFFF;">' .. data.type2 .. '</span>]]</td>'
	else
		typesHtml =
			'<td style="width:100%; background-color: #' .. colors.dark .. '; border-radius: 25px;">' ..
			'[[' .. data.type1 .. '_(type)|<span style="color: #FFFFFF;">' .. data.type1 .. '</span>]]</td>'
	end

	-- Base stats
	local statCells, bst = {}, 0
	for _, stat in ipairs(STATS) do
		local value = tonumber(data[stat.key]) or 0
		bst = bst + value
		statCells[#statCells + 1] = statValueCell(frame, stat, value)
	end
	local bstCategory = math.floor(bst / 100 + 0.5) * 100

	-- EV yield.
	local evCells, evTotal = {}, 0
	for i, stat in ipairs(STATS) do
		local ev = tonumber(data[EV_KEYS[i]]) or 0
		evTotal = evTotal + ev
		evCells[#evCells + 1] = statValueCell(frame, stat, ev)
	end

	-- Catch rate.
	local catchRaw = tonumber(data.catch) or 0
	local catchPercent = string.format('%.2f', catchRaw / 255 / 3 * 100)

	local buf = {}
	buf[#buf + 1] = navTableHtml(frame, data, colors, pageName)
	buf[#buf + 1] = '<table align="right" class="informational-box" cellpadding="4" cellspacing="4" ' ..
		'style="width: ' .. width .. '%; border: 4px solid #' .. colors.dark .. '; background-color: #' ..
		colors.base .. '; padding: 4px; border-radius: 15px;">'

	-- Header: dex number + icon, name.
	buf[#buf + 1] = '<tr><td style="border: 3px solid #' .. colors.dark .. '; background-color: #' ..
		colors.light .. '; border-radius: 15px;"><span style="margin-bottom: 7px; font-weight: bold; color:#' ..
		colors.dark .. ';">#' .. padNumber(data.number) .. '</span>&nbsp;&nbsp;&nbsp;[[File:' .. data.number ..
		'Icon.png]]</td><td style="border: 3px solid #' .. colors.dark .. '; background-color: #' ..
		colors.light .. '; border-radius: 15px;"><span style="margin-bottom: 7px; font-weight: bold; color:#' ..
		colors.dark .. ';">' .. displayName .. '</span></td></tr>'

	-- Artwork.
	buf[#buf + 1] = '<tr><td colspan="3" style="border: 3px solid #' .. colors.dark .. '; background-color: #' ..
		colors.light .. '; border-radius: 15px; padding: 5px;"><div style="border-radius: 15px; ' ..
		'text-align: center; padding: 5px; background-color: #FFFFFF;">[[File:' .. data.number .. data.name ..
		'.png|150px]]</div></td></tr>'

	-- Types + Generation.
	buf[#buf + 1] = '<tr><td style="border: 3px solid #' .. colors.dark .. '; background-color: #' ..
		colors.light .. '; padding: 11px; border-radius: 15px;"><p style="margin-bottom: 7px; font-weight: bold; ' ..
		'color: #' .. colors.dark .. ';">Types</p><table align="center" style="width: 75%; font-weight: bold; ' ..
		'padding: 3px;"><tr>' .. typesHtml .. '</tr></table></td>' ..
		'<td style="border: 3px solid #' .. colors.dark .. '; background-color: #' .. colors.light ..
		'; border-radius: 15px;"><p style="margin-bottom: 7px; font-weight: bold; color: #' .. colors.dark ..
		';">Generation</p><span style="border-radius: 20px; padding: 6px; border: 4px solid #' .. colors.dark ..
		'; background-color: #FFFFFF;">&nbsp;&nbsp;[[:Category:Generation ' .. gen .. ' Pokémon|<span style="color:#' ..
		colors.dark .. ';">' .. gen .. '</span>]]&nbsp;&nbsp;</span></td></tr>'

	-- Abilities.
	buf[#buf + 1] = '<tr><td colspan="6" style="border: 3px solid #' .. colors.dark .. '; background-color: #' ..
		colors.light .. '; padding: 11px; border-radius: 15px; width: 100%;"><table width="100%">' ..
		'<tr><th colspan="6"><p style="margin-bottom: 7px; font-weight: bold; color:#' .. colors.dark ..
		';">[[Abilities|<span style="color:#' .. colors.dark .. ';">Abilities</span>]]</p></th></tr>' ..
		'<tr>' .. abilitiesRow(colors, data) .. '</tr></table></td></tr>'

	-- Rarity + Catch rate.
	buf[#buf + 1] = '<tr>' ..
		infoCell(colors, '[[List of Pokémon by Rarity Tier|<span style="color:#' .. colors.dark ..
			';">Rarity Tier</span>]]', '[[:Category:' .. data.rarity .. ' Pokémon|<span style="color:#' ..
			colors.dark .. ';">' .. data.rarity .. '</span>]]') ..
		infoCell(colors, '[[Catch Rates|<span style="color:#' .. colors.dark .. ';">Catch rate</span>]]',
			data.catch .. '/255 (' .. catchPercent .. '%)') ..
		'</tr>'

	-- Base stats.
	buf[#buf + 1] = '<tr><td colspan="6" style="border: 3px solid #' .. colors.dark .. '; background-color: #' ..
		colors.light .. '; border-radius: 15px; padding: 5px; width: 100%"><table width="100%">' ..
		'<tr><th colspan="6"><p style="margin-bottom: 7px; font-weight: bold; color: #' .. colors.dark ..
		';">[[Base stats|<span style="margin-bottom: 7px; font-weight: bold; color: #' .. colors.dark ..
		';">Base stats</span>]]</p></th></tr><tr>' .. table.concat(statCells) .. '</tr>' ..
		'<tr><td colspan="6"><div style="margin: auto; background-color:#FFFFFF; border: 3px solid #' ..
		colors.dark .. '; padding: 6px; border-radius: 25px; margin-bottom: 5px; width: 50%;">' ..
		'Base-stat total: ' .. bst .. '</div></td></tr></table></td></tr>'

	-- EV yield.
	buf[#buf + 1] = '<tr><td colspan="6" style="border: 3px solid #' .. colors.dark .. '; background-color: #' ..
		colors.light .. '; border-radius: 15px; padding: 5px; width: 100%"><table width="100%">' ..
		'<tr><th colspan="6">[[EVs|<span style="margin-bottom: 7px; font-weight: bold; color: #' .. colors.dark ..
		';">EV Yield</span>]]</th></tr><tr>' .. table.concat(evCells) .. '</tr>' ..
		'<tr><td colspan="6"><div style="margin: auto; background-color:#FFFFFF; border: 3px solid #' ..
		colors.dark .. '; padding: 6px; border-radius: 25px; margin-bottom: 5px; width: 50%;">' ..
		'EV-yield total: ' .. evTotal .. '</div></td></tr></table></td></tr>'

	-- Base experience + Gender ratio.
	buf[#buf + 1] = '<tr>' ..
		infoCell(colors, '[[Experience System#Base Experience|<span style="color:#' .. colors.dark ..
			';">Base Experience</span>]]', data.exp) ..
		'<td style="border: 3px solid #' .. colors.dark .. '; background-color: #' .. colors.light ..
		'; border-radius: 15px; padding: 5px;"><p style="margin-bottom: 7px; font-weight: bold; color: #' ..
		colors.dark .. ';">Gender Ratio</p>&nbsp;&nbsp;' .. genderRatioHtml(data.male) .. '&nbsp;&nbsp;</td>' ..
		'</tr>'

	-- Height + Weight.
	buf[#buf + 1] = '<tr>' ..
		infoCell(colors, 'Height', data.height .. 'm') ..
		infoCell(colors, 'Weight', data.weight .. 'kg') ..
		'</tr>'

	buf[#buf + 1] = '</table>'
	buf[#buf + 1] = categories(data, gen, bstCategory, args)

	return table.concat(buf)
end

return p