Module:AmmoTable
Jump to navigation
Jump to search
Documentation for this module may be created at Module:AmmoTable/doc
-- Module:AmmoTable creates a fancy-pants table of ammo -- declare p at the top, return p at the bottom. Functions of p are what's -- exposed to #invoke calls. local p = {} -- we use the getArgs function from the args module to get the args out -- of either the invoke call, or the template that called invoke. local getArgs = require('Module:Arguments').getArgs -- import the Gear module so that we can use its core.cargo.query function, -- which does the table joins for you. we'll only import 'core', which has -- the kind of library functions that we want. local gear = require('Module:Gear').core -- functions always take a frame as an argument. The frame describes the -- page that's calling the function function p.make_ammo(frame) -- just always start with this, trust me. local tpl_args = getArgs(frame, {parentFirst=true}) -- now, let's say the parameter on the template for this is just category, -- and the template gets a whole category of ammunition. local category = tpl_args['Category'] -- now we are going to query the ammo table. We want to replicate something -- similar to the ammo tables created by hand earlier, so we'll use the fields -- UIName, Tonnage, Capacity, and Effects. -- the database has both fields Name and UIName, and they're slightly different. -- check the database first and pick the one that fits best. local fields = 'Gear.UIName,Gear.Tonnage,Ammunition.Capacity,Gear.Bonuses,Ammunition.PerUnitCost' -- we only want the ammo types that match the category that the caller -- specified, so we'll set up a where argument. to make this easier, we -- will use string.format. local args = { where = string.format('Ammunition.Category="%s"', category) } local ammoRows = gear.cargo.query(gear.schema.ammunition, fields, args) -- now, we'll use scribunto's html library to build a pretty table -- start with, obviously, a table local ammoTable = mw.html.create('table') -- note here we use a colon : instead of a period. that's because this is -- a method call in Lua, not a function call. ammoTable:addClass('wikitable') -- now, we can set up the table headings local heading = ammoTable:tag('tr') heading:tag('th'):wikitext('Name') heading:tag('th'):wikitext('Tonnage') heading:tag('th'):wikitext('Capacity') heading:tag('th'):wikitext('Cost per shot') heading:tag('th'):wikitext('Effects') -- now we get to the meat of the code, where we're looping through the -- query results for _, ammo in ipairs(ammoRows) do local row = ammoTable:tag('tr') row:tag('td'):wikitext(ammo['Gear.UIName']) row:tag('td'):wikitext(ammo['Gear.Tonnage']) row:tag('td'):wikitext(ammo['Ammunition.Capacity']) row:tag('td'):wikitext(ammo['Ammunition.PerUnitCost']) -- now here comes a bit of a weird part. In blatant disregard for -- the First Normal Form, the Bonuses field is a comma-separated list. -- we want to make it pretty though, so what we'll do is put the bonuses -- in a ul tag local bonusList = row:tag('td'):tag('ul') for _, bonus in ipairs(mw.text.split(ammo['Gear.Bonuses'], ',')) do -- skip including CostPerShot in the bonuses list, because it's covered -- in its own column in the table. if string.match(bonus, 'Caseless') then bonusList:tag('li'):wikitext([[Caseless Ammo increases recoil but fits more ammo per bin.]]) elseif string.match(bonus, 'AlwaysStartsFire') then bonusList:tag('li'):wikitext([[Always ignites forests.]]) elseif string.match(bonus, 'OHDamage: 25%%') then bonusList:tag('li'):wikitext([[25%% bonus Damage against overheating Targets.]]) elseif string.match(bonus, 'OHDamage: 10%%') then bonusList:tag('li'):wikitext([[10%% bonus Damage against overheating Targets.]]) elseif string.match(bonus, 'ImpSight: -75%%') then bonusList:tag('li'):wikitext([[Target has 75%% reduced Sight for 2 Turns]]) elseif string.match(bonus, 'ImpSensors: -30%%') then bonusList:tag('li'):wikitext([[Target has 30%% reduced Sensors for 2 Turns]]) elseif string.match(bonus, 'Thunder') then bonusList:tag('li'):wikitext([[FASCAM munitions will create a minefield wherever a missile hit the ground.]]) elseif string.match(bonus, 'ThunderCripple') then bonusList:tag('li'):wikitext([[FASCAM munitions automatically damage the triggering unit's legs and will stop movement through them if they damage leg structure directly.]]) elseif not string.match(bonus, 'CostPerShot') then bonusList:tag('li'):wikitext(bonus) end end end -- finally, at the end, return the table we made return ammoTable end -- return p at the bottom, declare p at the top return p