Module:Pokemon/Fishing

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

local p = {}

local modeRows = { Rod = 1, multiRods = 2 } local modeCols = { Rod = 2, multiRods = 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 == "Rod" 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 == "multiRods" 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( 'Old Rod' ) row:tag( 'th' ) :addClass( 'pkmn-wild-' .. string.lower( games[k] ) ) :wikitext( 'Good Rod' ) row:tag( 'th' ) :addClass( 'pkmn-wild-' .. string.lower( games[k] ) ) :wikitext( 'Super Rod' ) end end end return gameTables end

local function processRow( mode, games, data, gameTables ) local frame = mw.getCurrentFrame local j = 1 local monName = data[1] 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 } }				)

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 } }				)

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 _fishing(p) local frame = mw.getCurrentFrame local mode = args[1] or 'Rod' local games = {} local gameTables = {} local ts = frame:extensionTag{ name = 'templatestyles', args = { src = "User:Prod/Wild/styles.css" } }

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

local numMons = getArgNums( 'mon' ) for _, i in ipairs( numMons ) do processRow( mode, games, 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) 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.fishing( frame ) args = getArgs( frame, { wrappers = 'User:Ewasser33/Fishing' } ) return _fishing end

return p