Difference between revisions of "Module:Test"
m |
m |
||
Line 2: | Line 2: | ||
local data = mw.loadData('Module:Test/data') | local data = mw.loadData('Module:Test/data') | ||
− | -- local data = loadfile("Hare.lua") | + | --[[ |
− | + | local data = loadfile("Hare.lua") | |
− | + | data = data() | |
+ | local frame = {} | ||
+ | frame.args = {} | ||
+ | frame.args[1] = "description" | ||
+ | --]] | ||
function traverse(table) | function traverse(table) | ||
for key, val in pairs(table) do | for key, val in pairs(table) do | ||
Line 15: | Line 19: | ||
end | end | ||
− | function find( | + | function find(in_table, string_key) |
− | for key, val in pairs( | + | for key, val in pairs(in_table) do |
if type(val) == "string" or type(val) == "number" then | if type(val) == "string" or type(val) == "number" then | ||
− | if key == | + | if key == string_key then |
+ | local result = val | ||
return val | return val | ||
end | end | ||
elseif type(val) == "table" then | elseif type(val) == "table" then | ||
− | find(val, | + | local var = find(val, string_key) |
+ | if var then return var end | ||
end | end | ||
end | end | ||
end | end | ||
− | |||
function p.hello(frame) return "Hello, world!\n" end | function p.hello(frame) return "Hello, world!\n" end | ||
Line 32: | Line 37: | ||
function p.get(frame) | function p.get(frame) | ||
if type(frame.args[1]) == "string" then | if type(frame.args[1]) == "string" then | ||
− | + | return find(data, frame.args[1]) | |
− | |||
else | else | ||
− | return " | + | return 'Error: argument to invoked "get" function must be a string.\n' |
end | end | ||
end | end | ||
return p | return p |
Revision as of 22:39, 4 March 2021
This module is used for development.
Purpose
This module is used to query information from the uploaded and parsed game files.
Its main purpose is to populate the infoboxes.
Usage
A note on the order of named parameters. All of the parameters that look like ...=...
are called named parameters and their order is not important (this is true for all templates).
query
{{#invoke:Test|query|<def ID>[|...|][|tag|][|sibling=...]}}
The work-horse. Output varies based on use:
- If only the
<def ID>
parameter is set, it will show the whole Def in the log. - If simple values are queried it will return them.
- If lists are queried it will return nothing but call
{{#vardefine}}
on all the simple values within it. What got defined can be seen in the page's log.
Named parameters:
<def ID>
- This parameter identifies the Def so it is mandatory. It can take two forms, if both are defined then
defName
takes preference.
- This parameter identifies the Def so it is mandatory. It can take two forms, if both are defined then
defName=<defName>
<defName>
(case sensitive) should be replaced with the actual defName of a Def.
label=<label>
<label>
(case insensitive) should be replaced with the actual label of a Def.
[sibling=...]
(optional) (case sensitive)- Allows querying for something if we know its sibling's value (works only for values at the moment).
Anonymous parameters:
[|...|]
(optional) (case sensitive)- Anonymous paramaters before the last one (
[tag]
) are here to help uniquely identify it. If the[tag]
is already unique within a Def tree, then these additional parameters are not needed.
- Anonymous paramaters before the last one (
[|tag|]
(optional) (case sensitive)- The final anonymous parameter defines what is to be queried.
count
{{#invoke:Test|count|<def ID>[|...|][|tag|][|sibling=...]}}
Parameters are the same as for query
. It's basically a wrapped up query that behaves a bit differently.
The difference is in how it handles lists. If a list is queried, unlike query
, it will return the length of the list.
How-to
Take a look at a Def
{{#invoke:Test|query|label=desert}}
Script error: The function "query" does not exist.
Data is in the log.
Retrieve a simple value
{{#invoke:Test|query|defName=Caribou|description}}
Script error: The function "query" does not exist.
Dealing with lists
{{#invoke:Test|query|defName=Mech_Scyther|tools}}
Script error: The function "query" does not exist.
When a list is retrieved there will be no output but the log will contain a list of defined variables.
For convenience the list is reprinted here:
tools_1_linkedBodyPartsGroup = LeftBlade tools_1_cooldownTime = 2 tools_1_label = left blade tools_1_DPS = 10 tools_1_power = 20 tools_1_capacities_1 = Cut tools_1_capacities_2 = Stab tools_2_linkedBodyPartsGroup = RightBlade tools_2_cooldownTime = 2 tools_2_label = right blade tools_2_DPS = 10 tools_2_power = 20 tools_2_capacities_1 = Cut tools_2_capacities_2 = Stab tools_3_linkedBodyPartsGroup = HeadAttackTool tools_3_capacities_1 = Blunt tools_3_label = head tools_3_DPS = 4.5 tools_3_chanceFactor = 0.2 tools_3_power = 9 tools_3_cooldownTime = 2
All of the above can be accessed with the use of {{#var:...}}
.
{{#var:tools_1_DPS}}
DPS is not a normal member of this table but has been added with Lua. Let's call it a virtual field.
Retrieve something if a sibling is known
{{#invoke:Test|query|label=guinea pig|minAge|sibling=AnimalAdult}}
Script error: The function "query" does not exist.
local p = {} local data = mw.loadData('Module:Test/data') --[[ local data = loadfile("Hare.lua") data = data() local frame = {} frame.args = {} frame.args[1] = "description" --]] function traverse(table) for key, val in pairs(table) do if type(val) == "string" or type(val) == "number" then print(key .. " = " .. val) elseif type(val) == "table" then traverse(val) end end end function find(in_table, string_key) for key, val in pairs(in_table) do if type(val) == "string" or type(val) == "number" then if key == string_key then local result = val return val end elseif type(val) == "table" then local var = find(val, string_key) if var then return var end end end end function p.hello(frame) return "Hello, world!\n" end function p.get(frame) if type(frame.args[1]) == "string" then return find(data, frame.args[1]) else return 'Error: argument to invoked "get" function must be a string.\n' end end return p