Module:Pokemon/Wild

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

local p = {}

local modeRows = { Base = 1, Time = 2 } local modeCols = { Base = 2, Time = 4 }

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 processHeaders( mode, games ) local frame = mw.getCurrentFrame local gameTables = {} gameTables[0] = mw.html.create('table') :addClass('wikitable') :addClass('pkmn-wild') :addClass('pkmn-wild-' .. string.lower( mode ) ) :addClass('pkmn-wild-desktop') gameTables[0]:tag( 'tr' ):tag( 'th' ):addClass('pkmn-wild-pokemon'):wikitext( 'Pokémon' ) for k, gameName in ipairs( games ) do		gameTables[k] = mw.html.create('table') :addClass('wikitable') :addClass('pkmn-wild') :addClass('pkmn-wild-' .. string.lower( mode ) ) local row = gameTables[k]:tag( 'tr' ) row:tag( 'th' ):attr( 'rowspan', modeRows[ mode ] ):addClass('pkmn-wild-pokemon'):wikitext( 'Pokémon' )

if mode == "Base" then row:tag( 'th' ) :addClass( 'pkmn-wild-' .. string.lower( games[k] ) ) :wikitext( 'Level' ) row:tag( 'th' ) :addClass( 'pkmn-wild-' .. string.lower( games[k] ) ) :wikitext( frame:expandTemplate{ title = 'Pokemon/Short', args = { games[k] } } ) else row:tag( 'th' ) :attr( "colspan", modeCols[ mode ] ) :addClass( 'pkmn-wild-' .. string.lower( games[k] ) ) :wikitext( frame:expandTemplate{ title = 'Pokemon/Short', args = { games[k] } } ) if mode == "Time" then local row = gameTables[k]:tag( 'tr' ) row:tag( 'th' ) :addClass( 'pkmn-wild-' .. string.lower( games[k] ) ) :wikitext( 'Level' ) row:tag( 'th' ) :addClass( 'pkmn-wild-' .. string.lower( games[k] ) ) :wikitext( 'Morn' ) row:tag( 'th' ) :addClass( 'pkmn-wild-' .. string.lower( games[k] ) ) :wikitext( 'Day' ) row:tag( 'th' ) :addClass( 'pkmn-wild-' .. string.lower( games[k] ) ) :wikitext( 'Night' ) end end end return gameTables end

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

gameTables[0] :tag( 'tr' ) :tag( 'th' ) :addClass('pkmn-wild-pokemon') :wikitext( 					frame:expandTemplate{ title = 'PokePic', args = { monName } } .. 					frame:expandTemplate{ title = 'bp', args = { monName } } ..					flag				)

for i, gameName in ipairs( games ) do		local limit = modeCols[ mode ] or 1

j = j + 1 local row = gameTables[i]:tag( 'tr' ) row :tag( 'th' ) :addClass('pkmn-wild-pokemon') :wikitext( 					frame:expandTemplate{ title = 'PokePic', args = { monName } } .. 					frame:expandTemplate{ title = 'bp', args = { monName } } ..					flag				)

if data[j] == "" then data[j] = "n/a" end row:tag( 'td' ) :addClass( 'pkmn-wild-' .. string.lower( gameName ) ) :wikitext( data[j] )

while j < 1 + limit * i do			j = j + 1 if data[j] == "" then data[j] = "n/a" else data[j] = data[j] .. '%'			end 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 games = {} local flags = {} local gameTables = {} local references = '' local ts = frame:extensionTag{ name = 'templatestyles', args = { src = "Template:Pokemon/Wild/styles.css" } }

local numGames = getArgNums( 'game' ) for k, game in ipairs( numGames ) do games[k] = args['game' .. k]	end

local numFlags = getArgNums( 'flags' ) if next(numFlags) then references = mw.html.create:wikitext( mw.getCurrentFrame:preprocess( ' ' ) ) end for _, k in pairs( numFlags ) do flags[k] = args['flags' .. k]	end gameTables = processHeaders( mode, games )

local numMons = getArgNums( 'mon' ) for _, i in ipairs( numMons ) do processRow( mode, games, flags[i], splitArgs( args['mon' .. i] ), gameTables ) end local root = mw.html.create( 'div' ):addClass('pkmn-wild-container')

root:node(gameTables[0]) for k, game in ipairs( games ) do		root:node(gameTables[k]) end return ts .. tostring(root) .. tostring(references) 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