Module:Stats

-- NB: Depends on, , and. -- Use instead of invoking this module directly. -- local p = {}

local data = mw.loadData('Module:Stats/data') local ranks = { ['∞' ] = '6',   ['S+'] = '6', ['S' ] = '6', ['S-'] = '6', ['A+'] = '5', ['A' ] = '5', ['A-'] = '5', ['B+'] = '4', ['B' ] = '4', ['B-'] = '4', ['C+'] = '3', ['C' ] = '3', ['C-'] = '3', ['D+'] = '2', ['D' ] = '2', ['D-'] = '2', ['E+'] = '1', ['E' ] = '1', ['E-'] = '1', ['?' ] = '?' }

local function build_ultra_archive_stats(stats, short_name) local length = 0 for i, v in ipairs(stats) do       if i <= 5 then assert(mw.ustring.match(stats[i], '^[SABCDE?]$')) end length = length + 1 end assert(length == 5) return {'Ultra Archive', ''} end

local function build_ultra_analysis_stats(stats, short_name) local length = 0 for i, v in ipairs(stats) do       if i <= 5 then assert(mw.ustring.match(stats[i], '^[SABCDE][+-]?$') or stats[i] == '?') end length = length + 1 end assert(length == 6) assert(mw.ustring.match(stats[6], '^%u.*%a$')) return {'Ultra Analysis', ''} end

local function build_hawks_villain_report_stats(stats, short_name) local length = 0 for i, v in ipairs(stats) do       if i <= 5 then assert(mw.ustring.match(stats[i], '^[SABCDE][+-]?$') or stats[i] == '∞') end length = length + 1 end assert(length == 6) assert(mw.ustring.match(stats[6], '^%u.*%a$')) return {'Hawks\' Villain Report', ''} end

function p.render(frame) -- NB: Ideally we'd use the frontier pattern (like `^%f[%u].*%a$`), --    but `mw.ustring` is missing the [backport](https://github.com/Wikia/app/pull/15769). local full_name = assert(mw.ustring.match(frame.args[1], '^%u.*%a$')) local stats = assert(data[full_name]) assert(type(stats[1]) == 'boolean')

local short_name = stats[1] and assert(mw.ustring.match(full_name, '^[^%s]+')) or full_name local fragments = {}

if type(stats[2]) == 'table' then table.insert(fragments, build_ultra_archive_stats(stats[2], short_name)) end

if type(stats[3]) == 'table' then table.insert(fragments, build_ultra_analysis_stats(stats[3], short_name)) end

if type(stats[4]) == 'table' then table.insert(fragments, build_hawks_villain_report_stats(stats[4], short_name)) end

local wikitext if #fragments == 0 then return '' elseif #fragments == 1 then wikitext = fragments[1][2] else wikitext = ' \n' for i, v in ipairs(fragments) do           wikitext = wikitext .. (i > 1 and '|-|\n' or '') .. v[1] .. '=' .. v[2] .. '\n' end wikitext = wikitext .. ' '    end return frame:preprocess(wikitext) end

return p --