Difference between revisions of "Module:UnitData"

From Zero-K
Jump to navigation Jump to search
(Compatibility with new WeaponData module)
(Start working on ability infoboxes; try removing the preprocess calls)
Line 5: Line 5:
 
function unit.getData(frame)
 
function unit.getData(frame)
 
if not frame then return '' end
 
if not frame then return '' end
    local unitDefName = frame.args[1]
+
local unitDefName = frame.args[1]
 
local ud = unitData[unitDefName]
 
local ud = unitData[unitDefName]
    if not (ud) then return 'unitdef ' .. unitDefName .. ' not found' end
+
if not (ud) then return 'unitdef ' .. unitDefName .. ' not found' end
    local property = frame.args[2]
+
local property = frame.args[2]
    local result = ud[property]
+
local result = ud[property]
    if not result then result = '' end
+
return result or ''
    return frame:preprocess(result) or ''
+
end
 +
 
 +
function unit.getAbilityInfoboxTemplate(abType, args)
 +
local tbl = {title = "Infobox zkability " .. abType, args}
 +
return tbl
 +
end
 +
 
 +
function unit.printAbilityInfoboxes(frame)
 +
-- TODO
 +
if not frame then return '' end
 +
local unitDefName = frame.args[1]
 +
local ud = unitData[unitDefName]
 +
if not (ud) then return 'unitdef ' .. unitDefName .. ' not found' end
 +
 +
local result = ''
 +
if ud.buildSpeed then
 +
result = result .. frame:expandTemplate(unit.getAbilityInfoboxTemplate("construction", {buildpower = ud.buildSpeed}))
 +
end
 
end
 
end
  
 
function unit.printWeaponInfoboxes(frame)
 
function unit.printWeaponInfoboxes(frame)
 
if not frame then return '' end
 
if not frame then return '' end
    local unitDefName = frame.args[1]
+
local unitDefName = frame.args[1]
 
local ud = unitData[unitDefName]
 
local ud = unitData[unitDefName]
    if not (ud) then return 'unitdef ' .. unitDefName .. ' not found' end
+
if not (ud) then return 'unitdef ' .. unitDefName .. ' not found' end
   
+
    local result = ''
+
local result = ''
 
local weaponModule = require('Module:WeaponData')
 
local weaponModule = require('Module:WeaponData')
 
 
Line 27: Line 44:
 
result = result .. weaponModule.printInfobox(tempFrame)
 
result = result .. weaponModule.printInfobox(tempFrame)
 
end
 
end
   
+
    if not result then result = '' end
+
return result or ''
    return frame:preprocess(result) or ''
 
 
end
 
end
  
 
return unit
 
return unit

Revision as of 07:24, 11 May 2024

Documentation

This module is used to autogenerate unit infoboxes by drawing data from Module:UnitData/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.

This module is currently used with Template:Autoinfobox zkunit as follows:

{{Autoinfobox zkunit
|defname=striderdetriment
|name=Name override
}}

The module can also be invoked directly to generate an infobox (note that output may not be identical in some cases; observed with automatic detection of buildpic on Reef):

{{#invoke:UnitData|printInfobox|defname=striderdetriment|name=Name override}}

When invoking from module, defname may be passed as an anonymous parameter instead.

See also Module:WeaponData, which is used to generate the weapon infoboxes within the unit infoboxes.

Data page

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

The page is a Lua table written to the local file temp/unitStats.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 unit = {}
local unitData = mw.loadData('Module:UnitData/data')

function unit.getData(frame)
	if not frame then return '' end
	local unitDefName = frame.args[1]
	local ud = unitData[unitDefName]
	if not (ud) then return 'unitdef ' .. unitDefName .. ' not found' end
	local property = frame.args[2]
	local result = ud[property]
	return result or ''
end

function unit.getAbilityInfoboxTemplate(abType, args)
	local tbl = {title = "Infobox zkability " .. abType, args}
	return tbl
end

function unit.printAbilityInfoboxes(frame)
	-- TODO
	if not frame then return '' end
	local unitDefName = frame.args[1]
	local ud = unitData[unitDefName]
	if not (ud) then return 'unitdef ' .. unitDefName .. ' not found' end
	
	local result = ''
	if ud.buildSpeed then 
		result = result .. frame:expandTemplate(unit.getAbilityInfoboxTemplate("construction", {buildpower = ud.buildSpeed}))
	end
end

function unit.printWeaponInfoboxes(frame)
	if not frame then return '' end
	local unitDefName = frame.args[1]
	local ud = unitData[unitDefName]
	if not (ud) then return 'unitdef ' .. unitDefName .. ' not found' end
	
	local result = ''
	local weaponModule = require('Module:WeaponData')
	
	for index,weaponID in ipairs(ud.weaponIDs or {}) do
		local tempFrame = mw.getCurrentFrame():newChild{title="Module:WeaponData",args={defname=weaponID}}
		result = result .. weaponModule.printInfobox(tempFrame)
	end
	
	return result or ''
end

return unit