Tooth and Tail Wiki
Advertisement

Notes[]

  • Named and numbered args have whitespace trimmed, whereas anonymous args do not. Named args are not supported.
  • Array results output comma separated values.

Example usage[]

{{#invoke: TNTGame | get_unit_attr |fox|desc}}
She spent her summers sitting on the roof, picking off rustlers and interlopers. She hates the city, and is happy to be back at home in the countryside.


{{#invoke: TNTGame | get_unit_attr |fox|traits}}
sniperfox,swift


{{#invoke: TNTGame | get_trait_attr |swift|desc}}
Moves 1.75x as fast.


{{#invoke: TNTGame | get_wpn_attr |sniperfox|AtkRange}}
6


{{#invoke: TNTGame | get_unit_wpn_attr |fox|AtkRange}}
6


{{#invoke: TNTGame | get_game_version}}
1.4.0.1


local TNTGame = {
	WPN_NAME_PREFIX = 'weapon_',
	game_data = mw.loadData( 'Module:TNTGame/data' )
}

function TNTGame:getUnitAttr(uname, attr)
    local unit = self.game_data.units[uname]
    if (unit == nil) then
    	return nil
    end
    return unit[attr]
end

function TNTGame:getTraitAttr(tname, attr)
    local trait = self.game_data.filters.traits[tname]
    if (trait == nil) then
    	return nil
    end
    return trait[attr]
end

function TNTGame:getWeaponAttr(wname, attr)
    local wpn = self.game_data.weapons[wname]
    if (wpn == nil) then
    	return nil
    end
    return wpn[attr]
end

function TNTGame:getUnitWeaponAttr(uname, attr)
    local wpn = self:getUnitWeapon(uname)
    if (wpn == nil) then
    	return nil
    end
    return wpn[attr]
end

function TNTGame:getUnitWeapon(uname)
    local unit = self.game_data.units[uname]
    if (unit == nil) then
    	return nil
    end
    
    local traits = unit.traits
    if (traits == nil) then
    	return nil
    end
    
    for _,tname in ipairs(traits) do
    	local wpn_name = self.game_data.filters.traits[tname].wpn
    	if (wpn_name ~= nil) then
    		return self.game_data.weapons[string.sub(wpn_name, 
    			string.len(TNTGame.WPN_NAME_PREFIX)+1)]
    	end
    end
    
    return nil
end

function TNTGame:getVersion()
    return self.game_data.version
end

-----------------------------------------------------------------------
-- Utils
-----------------------------------------------------------------------

-- standart lua unpack doesn't work on frame's arguments table (f.args)
local function unpacklike(args)
	return args[1], args[2], args[3], args[4], args[5], args[6], args[7]
end

local function copy_table_values(t)
	local tc = {}
	for _,v in ipairs(t) do
		table.insert(tc, v)
	end
	return tc
end

local function convert2string(v)
	if (type(v) == 'table') then
		-- table.concat doesn't work on a table returned from wm.loadData()
		v = table.concat(copy_table_values(v), ',')
	end
	
	return v
end

-----------------------------------------------------------------------
-- Export
-----------------------------------------------------------------------
--[[
The argument values are always strings. Like templates, named and numbered args 
have whitespace trimmed, whereas anonymous args do not; and arguments that are 
specified but with no value will be empty strings, rather than nil.
--]]
local export = {}

function export.get_unit_attr(f)
	return convert2string(TNTGame:getUnitAttr(unpacklike(f.args)))
end

function export.get_trait_attr(f)
	return convert2string(TNTGame:getTraitAttr(unpacklike(f.args)))
end

function export.get_wpn_attr(f)
	return convert2string(TNTGame:getWeaponAttr(unpacklike(f.args)))
end

function export.get_unit_wpn_attr(f)
	return convert2string(TNTGame:getUnitWeaponAttr(unpacklike(f.args)))
end

function export.get_game_version(f)
	return TNTGame:getVersion()
end

return export
Advertisement