Module:WeaponData

From Zero-K
Revision as of 22:23, 10 May 2024 by Histidine (talk | contribs)
Jump to navigation Jump to search
Documentation

This module is used to autogenerate the weapon infoboxes contained in unit infoboxes, by drawing data from Module:WeaponData/data. The intent is to ease updating of unit infoboxes on the wiki; ideally, only the one data page needing to be changed every release. The concept is taken from e.g. the Combat Card Data module on the Library of Ruina Wiki.

Unlike Module:UnitData, there is no template for this module. If it is desired to generate a weapon infobox outside its unit infobox, the module should be invoked directly, e.g.:

{{#invoke:WeaponData|printInfobox|defname=striderdetriment_gauss|name=Name override}}

Data page

Module:WeaponData/data is a central store of data used by Module:WeaponData to automatically populate weapon infoboxes.

The page is a Lua table written to the local file temp/weaponStats.lua by the Wiki Data Export (dbg_wiki_export.lua) widget in Zero-K. This widget should be run once each update and the data page on the wiki replaced accordingly. It should not be necessary to edit the data manually.


local getArgs = require('Module:Arguments').getArgs
local weapon = {}
local weaponData = mw.loadData('Module:WeaponData/data')

local function getMiscStatsTable(weaponDefName)
	local wd = weaponData[weaponDefName]
	if not wd then return nil end
	if not wd.miscstats then return nil end
	
	local tbl = {}
	for index, tag in ipairs(wd.miscstats) do
		tbl[tag] = true
	end
	return tbl
end

function weapon.getData(frame)
	if not frame then return '' end
    local weaponDefName = frame.args[1]
    if not (weaponData[weaponDefName]) then return 'weapondef ' .. weaponDefName .. ' not found' end
    local property = frame.args[2]
    local result = weaponData[weaponDefName][property]
    if not result then result = '' end
    return frame:preprocess(result) or ''
end

function weapon.printInfobox(frame)
	local weaponDefName = frame.args[1]
    if not (weaponData[weaponDefName]) then return 'weapondef ' .. weaponDefName .. ' not found' end
	return [[{{User:Histidine/Autoinfobox zkweapon
|defname=]]..weaponDefName..[[}}]]
end

function weapon.hasTag(frame)
	if not frame then return '' end
    local weaponDefName = frame.args[1]
    if not (weaponData[weaponDefName]) then return 'weapondef ' .. weaponDefName .. ' not found' end
    local tag = frame.args[2]
	
	local miscStats = getMiscStatsTable(weaponDefName)
	if not miscStats then return false end
	return miscStats[tag]
end

return weapon