Module:Arcana list/sandbox

local lang = mw.getLanguage('en') -- this is a constant available to every function, and will be used to lowercase a variable below. local args = require('Module:ProcessArgs').merge(true) local p = {} function p.main(frame) -- this is the function that's called from the page itself local args = require('Module:ProcessArgs').merge(true)

local arcana = args[1] or mw.title.getCurrentTitle.text local result = p.doQuery(arcana) -- first thing is to do the cargo query, which is done in its own function; see comments on that function for more info if not result then -- if there was no result then return return 'There are no Arcana which match the given query.' end -- now we format all of the data that we got from our result to prepare it for output local formatted = p.formatResult(result) -- now we return a table that prints all the formatted data return frame:preprocess(p.makeTable(formatted)) end

--- -- cargo --- -- the code to do the query is broken up into a bunch of functions -- just so that it's easier to see what each piece does -- this is the outer function, which actually does the query -- and returns the result to the main function, or returns nil if there is no result -- the query itself will return {} (an empty table) if there is no result to the query function p.doQuery(arcana) -- the query takes 3 arguments:	1) a list of tables (it expects a comma-separated list as a string)	2) a list of fields to return (again, a comma separated list)	3) a table with a bunch of optional values. in this case we give a "where" condition and an "orderBy."	  Mostly these parameters are the same as the ones available in a normal mediawiki Cargo query,	   but there are some differences in what they're called so that they are just 1 single word in Lua	   and can be written more clearly as the keys in a table	local result = mw.ext.cargo.query('Arcana', p.cargoFields, -- the fields that we want in this example is actually kind of long and annoying to construct, -- so it's done in its own function. -- all you need to know is that a string is returned that contains a list of fields {			where = args[1], -- again, constructing the 'where' is kind of annoying so it's done in its own function. all you need to know is that it returns a string. groupBy="name", orderBy="element,name", limit="200" }	)	if not next(result) then		-- this just checks if the table is non-empty		-- the following code: 		--  if result == {}		-- will NOT work because Lua == when used on two tables only returns true if you are comparing		-- one table to literally itself, and NOT another table that has the same structure & values as it		-- however consider the following code:		-- a = {}		-- b = a		-- in this case, a does equal b		return nil	else		return result	end end

-- don't worry too much about the specifics of this function, -- just know it returns a string with a list of fields we want to query -- if we wanted to alias a field name, -- we could do for example "result=result_alias" instead of just "result" -- and then that would be returned to us as row.result_alias instead of row.result -- when we do our for k, row in ipairs.... loop later on function p.cargoFields -- we'll first make a table with all of the fields in it	local fields = { 'name','description','element','type','damage','cooldown','knockback','duration','gemCost','goldCost','pool' }	return table.concat(fields,',') -- then concat the table into a comma-separated string and return it to the main function end

-- -- process data for output -- -- in the end we want to print a table with three columns. -- the function that does the actual printing should only need to print data to the page, -- and not need to do any kind of processing on it anymore, -- so we are doing the processing / formatting here, -- saving it into a table called "formatted", -- and then returning it to the main function where it will then be printed. function p.formatResult(result) local formatted = {} -- this loop structure is pretty much always how you will process cargo data. -- you will loop over the ipairs iterator of the results table and apply a bunch of processing to the row, and save it. -- once it's been processed you will then print it. for k, row in ipairs(result) do		formatted[k] = p.formatRow(row.name, row.description, row.element, row.type, row.damage, row.cooldown, row.knockback, row.duration, row.gemCost, row.goldCost, row.pool) end return formatted end

--MARK

function p.formatRow(name, description, element, type, damage, cooldown, knockback, duration, gemCost, goldCost, pool) return string.format("",       name,        description,        element,        type,        damage,        cooldown,        knockback,        duration,        gemCost,        goldCost,        pool    ) end

-- make table for output

-- since we did all our processing above, literally all that's left to do is to make the table for output. -- this is done using the mw.html library. function p.makeTable(formatted) local tbl = "\n"

for _, row in ipairs(formatted) do       tbl = tbl .. row .. '\n' end

tbl = tbl .. " "

return tbl -- done! end

return p