Editing Module:Test
Jump to navigation
Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
− | + | --------------- | |
− | + | -- load data -- | |
+ | --------------- | ||
− | local | + | -- wiki -- |
− | + | local Biomes = mw.loadData('Module:Test/data/biomes') | |
− | + | --local Buildings = mw.loadData('Module:Test/data/buildings') | |
+ | local Races = mw.loadData('Module:Test/data/races') | ||
− | ------ | + | -- dev -- |
− | -- | + | --local Biomes = loadfile("./data/BiomeDefs.lua")() |
− | -- | + | --local Buildings = loadfile("./data/ThingDefs_Buildings.lua")() |
+ | --local Races = loadfile("./data/ThingDefs_Races.lua")() | ||
− | + | local data = {} | |
+ | data["Biomes"] = Biomes | ||
+ | --data["Buildings"] = Buildings | ||
+ | data["Races"] = Races | ||
− | + | ----------------------------- | |
− | + | -- small utility functions -- | |
+ | ----------------------------- | ||
− | + | -- returns the value (or nothing) found by first occurrence of a key within a table | |
− | + | local function search_table_recursive(key, table) | |
− | + | for k, v in pairs(table) do | |
− | + | if k == key then return v | |
− | + | elseif type(v) == "table" then | |
− | + | local found = search_table_recursive(key, v) | |
− | + | if found then return found end | |
− | + | end | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
+ | end | ||
− | + | -- returns the immediate parent table (or nothing) of an element specified by a key and its value | |
− | if | + | local function find_parent_table(key, value, table) |
− | + | for k, v in pairs(table) do | |
− | + | if k == key and v == value then return table | |
− | + | elseif type(v) == "table" then | |
− | + | local found = find_parent_table(key, value, v) | |
+ | if found then return found end | ||
end | end | ||
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | + | local function getParentName(defTable) | |
− | + | return defTable["ParentName"] | |
− | |||
− | |||
− | function | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | function | + | -- '#' operator seems to work for numerically indexed tables so it can be used instead |
− | if | + | -- this count function counts all the keys (of any type) |
− | local | + | local function count(table) |
− | + | if type(table) ~= "table" then | |
− | + | return "count(table): argument #1 is not a table" | |
− | + | else | |
+ | local length = 0; | ||
+ | for i, v in pairs(table) do | ||
+ | length = length + 1 | ||
end | end | ||
+ | return length | ||
end | end | ||
end | end | ||
− | function | + | -- "delimiter" must be a single character or the removal of the final delimiter won't work |
− | local | + | -- it's simpler this way but it could be reworked to enable multi-byte delimiters |
− | + | local function format_csv_string(simple_table, delimiter) | |
− | + | local list = "" | |
− | + | for k,v in pairs(simple_table) do | |
− | + | list = list .. v .. delimiter | |
− | + | end | |
− | + | if string.sub(list, -1) == delimiter then | |
− | + | list = string.sub(list, 1, -2) | |
− | + | end | |
− | + | return list | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | function | + | local function vardefine(var_name, var_value) |
− | + | local fName = debug.getinfo(1,"n").name | |
− | + | assert(var_name, string.format("bad argument #1 to '%s' (argument missing, name of variable to define)", fName)) | |
− | + | assert(var_name == "string", string.format("bad argument #1 to '%s' (string expected, got %s)", fName, type(var_name))) | |
− | + | assert(var_value, string.format("bad argument #2 to '%s' (argument missing, value to assign to variable)", fName)) | |
− | + | assert(var_value == "string" or var_value == "number", string.format("bad argument #2 to '%s' (string or number expected, got %s)", fName, type(var_value))) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | frame:callParserFunction('#vardefine', var_name, var_value) | |
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | + | ------------------ | |
− | + | -- search logic -- | |
+ | ------------------ | ||
− | for | + | local function find_defTable(def) |
− | + | for k1,v1 in pairs(data) do | |
− | if type( | + | if type(v1) == "table" then |
− | + | for k2,v2 in pairs(v1) do | |
− | + | if k2 == def then return v2 end | |
− | + | end | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
end | end | ||
end | end | ||
− | + | local function searchParentDef(key, defTable) | |
− | + | local ParentName = getParentName(defTable) | |
− | + | if not ParentName then return nil end | |
+ | local parentDefTable = search_table_recursive(ParentName, data) | ||
+ | if not parentDefTable then return nil end | ||
− | + | local found = search_table_recursive(key, parentDefTable) | |
− | local | + | if found then return found |
− | if | + | else |
− | return | + | found = searchParentDef(key, parentDefTable) |
+ | if found then return found end | ||
end | end | ||
end | end | ||
− | + | -------------------------------- | |
+ | -- publicly exposed functions -- | ||
+ | -------------------------------- | ||
− | + | local p = {} | |
− | + | function p.query(frame) | |
− | + | local fName = debug.getinfo(1,"n").name | |
− | + | assert(frame.args[1], string.format("bad argument #1 to '%s' (argument missing, def)", fName)) | |
− | + | assert(frame.args[2], string.format("bad argument #2 to '%s' (argument missing)", fName)) | |
− | local | + | local defTable = find_defTable(frame.args[1], data) |
− | + | if not defTable then | |
− | if not | + | return "'" .. frame.args[1] .. "' not found" |
− | |||
− | |||
end | end | ||
− | if | + | local found = search_table_recursive(frame.args[2], defTable) |
− | + | if not found then | |
− | + | found = searchParentDef(frame.args[2], defTable) | |
+ | if not found then | ||
+ | return "'" .. frame.args[2] .. "'" .. " not found in '" .. frame.args[1] .. "'" | ||
+ | end | ||
end | end | ||
− | + | -- multi-step query | |
− | + | for i,v in ipairs(frame.args) do | |
− | for i, | + | if i > 2 then |
− | + | found = search_table_recursive(v, found) | |
− | |||
− | if i | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | + | return found | |
− | return | ||
end | end | ||
− | + | ------------- | |
− | -- | + | -- logging -- |
− | + | ------------- | |
− | + | mw.log("Module:DefInfo:os.clock() " .. os.clock()*1000 .. " ms") | |
− | + | --print("Module:DefInfo:os.clock() " .. os.clock()*1000 .. " ms") | |
− | |||
− | -- | ||
− | |||
− | |||
− | |||
− | + | return p | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |