Module:Pokemon/Wild

local getArgs = require('Module:Arguments').getArgs

local p = {}

local function getArgNums(prefix) -- Returns a table containing the numbers of the arguments that exist -- for the specified prefix. For example, if the prefix was 'data', and -- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}. local nums = {} for k, v in pairs(args) do local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$') if num then table.insert(nums, tonumber(num)) end end table.sort(nums) return nums end

function splitArgs(input) if not input or input == '' then return end local result = {} for val in mw.text.gsplit(input, ';') do		result[#result+1] = val end return result end

local function processRow( mode, numGames, gameTables, data ) local frame = mw.getCurrentFrame local j = 1 local monName = data[j] local encounters = {}

for _, i in ipairs( numGames ) do		local limit = 1 if mode == "Base" then limit = limit + i * 3 elseif mode == "Time" then limit = limit + i * 5 end local row = gameTables[i]:tag( 'tr' ) row :tag( 'th' ) :wikitext( frame:expandTemplate{ title = 'PokePic', args = { monName } } .. frame:expandTemplate{ title = 'bp', args = { monName } } ) j = j + 1 local gameName = data[j] while j < limit do			j = j + 1 row:tag( 'td' ) :addClass( 'pkmn-wild-' .. string.lower( gameName ) ) :wikitext( data[j] ) end end end

local function _wild local frame = mw.getCurrentFrame local mode = args[1] or 'Base' local numGames = getArgNums( 'game' ) local games = {} local mons = {} local gameTables = {} for k, num in ipairs( numGames ) do games[k] = args['game' .. k]		gameTables[k] = mw.html.create('table') :addClass('wikitable') :addClass('pkmn-wild') local row = gameTables[k]:tag( 'tr' ) row:tag( 'th' ):wikitext( 'Pokémon' ) row:tag( 'th' ):wikitext( 'Level' ) row:tag( 'th' ) :addClass( 'pkmn-wild-' .. string.lower( games[k] ) ) :wikitext( frame:expandTemplate{ title = 'Pokemon/Short', args = { games[k] } } ) end local ts = frame:extensionTag{ name = 'templatestyles', args = { src = "Template:Pokemon/Wild/styles.css" } }

local numMons = getArgNums( 'mon' ) for _, i in ipairs( numMons ) do mons[i] = args['mon' .. i]		processRow( mode, numGames, gameTables, splitArgs( mons[i] ) ) end output = '' for _, k in ipairs( numGames ) do output = output .. tostring(gameTables[k]) end return ts .. output end

local function createEncounterTable( encounters ) local frame = mw.getCurrentFrame local gameTable = mw.html.create( 'table' ) :addClass('wikitable') :addClass('pkmn-wild') local row = gameTable:tag( 'tr' ) row:tag( 'th' ):wikitext( 'Pokémon' ) row:tag( 'th' ):wikitext( 'Level' ) row:tag( 'th' ) :addClass( 'pkmn-wild-' .. string.lower( encounters.game ) ) :wikitext( frame:expandTemplate{ title = 'Pokemon/Short', args = { encounters.game } } ) for _, mon in ipairs(encounters) do		local rowData = gameTable:tag( 'tr' ) rowData:tag( 'td' ):wikitext( frame:expandTemplate{ title = 'PokePic', args = { mon.name } } .. frame:expandTemplate{ title = 'bp', args = { mon.name } } ) rowData:tag( 'td' ):wikitext( mon.level ) rowData:tag( 'td' ) :addClass( "pkmn-wild-" .. string.lower( encounters.game ) ) :wikitext( mon.rate .. "%" ) end return gameTable end

function p.wild( frame ) args = getArgs( frame, { wrappers = 'User:Prod/Wild' } ) return _wild end

return p