Module:AutomaticInfoboxMonster

local p = {} local cargo = mw.ext.cargo

-- Perform a SQL query to "monster" table in the Cargo database (see Special:CargoTables/monster). -- @param {string} monsterId -- @return {table} Database row. local function queryMonster( monsterId ) local tables = 'monster' local fields = 'name,description,capturable,health,protection,damage,physical,radioactive,poison,electric,fire,ice,cosmic,shadow,id' local queryOpt = { where = 'id="' .. monsterId .. '"', limit = 1 }	return ( cargo.query( tables, fields, queryOpt ) or {} )[1] end

-- Print the automatic infobox of monster. (based on Special:CargoTables/monster) -- Usage: -- First parameter: monster ID, e.g. "adultpoptopfire". -- Optional parameter: nocat=1 - if present, this infobox won't add any categories to the current article. (can used in examples, help pages, etc.) -- Optional parameter: image=Something.png - if present, will be used as infobox image (replaces the default image, which is image of the monster's body). -- Optional parameter: image_size=150px - if present, sets the width of image. function p.Main( frame ) local args = frame.args if not args[1] then args = frame:getParent.args end

local id = args[1] or 'adultpoptopfire' local nocat = args['nocat'] or false local image = args['image']

-- Perform a SQL query to the Cargo database (see Special:CargoTables/monster). local row = queryMonster( id ) if not row then -- Monster not found in the database. if nocat then return '' end return '' end

local ret = '' if not nocat then ret = ret .. '\n' end

-- Format "row" (information about monster: row.name, row.description, etc.) as wikitext. ret = ret .. '{| class="infobox"\n'

-- Find the image, if any. local imageTitle = mw.title.new( image or 'Monster body ' .. id, 6 ) if imageTitle.fileExists then local imageParams = { image } if args['image_size'] then imageParams['width'] = args['image_size'] end ret = ret .. frame:expandTemplate{ title = 'infobox/image', args = imageParams } end

ret = ret .. frame:expandTemplate{ title = 'infobox/title', args = { row.name } } ret = ret .. frame:expandTemplate{ title = 'infobox/line', args = { row.description } } ret = ret .. frame:expandTemplate{ title = 'infobox/field', args = { ' Capturable?', row.capturable and 'Yes' or 'No' } }

ret = ret .. frame:expandTemplate{ title = 'infobox/field', args = { ' Health', row.health } }	ret = ret .. frame:expandTemplate{ title = 'infobox/field', args = { ' Protection', row.protection } }	ret = ret .. frame:expandTemplate{ title = 'infobox/field', args = { ' Touch damage', row.damage } }

-- TODO: move inline CSS into TemplateStyles or something local resistBegin = '' local resistEnd = ' '

ret = ret .. frame:expandTemplate{ title = 'infobox/field', args = { 'Resistances', resistBegin .. ' ' .. row.physical .. resistEnd .. resistBegin .. ' ' .. row.ice .. resistEnd .. resistBegin .. ' ' .. row.fire .. resistEnd .. resistBegin .. ' ' .. row.electric .. resistEnd .. resistBegin .. ' ' .. row.poison .. resistEnd .. resistBegin .. ' ' .. row.radioactivel .. resistEnd .. resistBegin .. ' ' .. row.cosmic .. resistEnd .. resistBegin .. ' ' .. row.shadow .. resistEnd } }

return ret end

return p